diff options
author | George Hazan <george.hazan@gmail.com> | 2023-11-25 21:05:25 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2023-11-25 21:05:25 +0300 |
commit | 7f73433ab8374b7456b29d3a607591029fe97c81 (patch) | |
tree | 21221513cc01079f793d8f499ce22e38e61ebc4b /protocols/Telegram/src/proto.cpp | |
parent | e9b8599fe66f6cdc2a3b691b1ae3fc5d9fbdb615 (diff) |
fixes #3954 (Telegram+NewStory: отправка нескольких файлов)
Diffstat (limited to 'protocols/Telegram/src/proto.cpp')
-rw-r--r-- | protocols/Telegram/src/proto.cpp | 221 |
1 files changed, 153 insertions, 68 deletions
diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp index d275c046d5..763db665e9 100644 --- a/protocols/Telegram/src/proto.cpp +++ b/protocols/Telegram/src/proto.cpp @@ -345,17 +345,9 @@ INT_PTR CTelegramProto::GetCaps(int type, MCONTACT) ///////////////////////////////////////////////////////////////////////////////////////// -MEVENT CTelegramProto::RecvFile(MCONTACT hContact, PROTORECVFILE *pre) +MEVENT CTelegramProto::RecvFile(MCONTACT, PROTORECVFILE *) { - MEVENT hEvent = CSuper::RecvFile(hContact, pre); - if (hEvent) - if (auto *ft = (TG_FILE_REQUEST *)pre->lParam) { - DBVARIANT dbv = { DBVT_DWORD }; - dbv.dVal = ft->m_type; - db_event_setJson(hEvent, "t", &dbv); - } - - return hEvent; + return 0; } ///////////////////////////////////////////////////////////////////////////////////////// @@ -403,6 +395,91 @@ HANDLE CTelegramProto::SearchByName(const wchar_t *nick, const wchar_t *firstNam ///////////////////////////////////////////////////////////////////////////////////////// +void CTelegramProto::ProcessFileMessage(TG_FILE_REQUEST *ft, const TD::message *pMsg, bool bCreateEvent) +{ + if (auto *pUser = FindChat(pMsg->chat_id_)) { + const TD::MessageContent *pBody = pMsg->content_.get(); + + TD::file *pFile; + switch (pBody->get_id()) { + case TD::messagePhoto::ID: + pFile = ((TD::messagePhoto *)pBody)->photo_->sizes_[0]->photo_.get(); + break; + + case TD::messageAudio::ID: + pFile = ((TD::messageAudio *)pBody)->audio_->audio_.get(); + break; + + case TD::messageVideo::ID: + pFile = ((TD::messageVideo *)pBody)->video_->video_.get(); + break; + + case TD::messageDocument::ID: + pFile = ((TD::messageDocument *)pBody)->document_->document_.get(); + break; + + default: + return; + } + + auto *pOwnMsg = new TG_OWN_MESSAGE(pUser->hContact, 0, pMsg->id_); + pOwnMsg->tmpFileId = pFile->id_; + m_arOwnMsg.insert(pOwnMsg); + + char szMsgId[40], szUserId[100]; + _i64toa(pMsg->id_, szMsgId, 10); + + if (!GetGcUserId(pUser, pMsg, szUserId)) + szUserId[0] = 0; + + if (bCreateEvent) { + auto *pFileName = pFile->local_->path_.c_str(); + CMStringA szDescr = GetMessageText(pUser, pMsg); + + DB::EventInfo dbei; + dbei.szModule = Proto_GetBaseAccountName(ft->m_hContact); + dbei.eventType = EVENTTYPE_FILE; + dbei.flags = DBEF_SENT | DBEF_UTF; + dbei.timestamp = time(0); + + TG_FILE_REQUEST localft(TG_FILE_REQUEST::FILE, 0, 0); + localft.m_fileName = Utf2T(pFileName); + localft.m_fileSize = pFile->size_; + localft.m_uniqueId = szMsgId; + localft.m_szUserId = szUserId; + + DB::FILE_BLOB blob(localft.m_fileName, ft->m_wszDescr); + OnSendOfflineFile(dbei, blob, &localft); + blob.write(dbei); + + db_event_add(ft->m_hContact, &dbei); + } + else { + ft->m_szUserId = szUserId; + ft->m_uniqueId = szMsgId; + } + } +} + +void CTelegramProto::OnSendFile(td::ClientManager::Response &response, void *pUserInfo) +{ + auto *ft = (TG_FILE_REQUEST *)pUserInfo; + + if (response.object->get_id() == TD::message::ID) + ProcessFileMessage(ft, (TD::message *)response.object.get(), false); + else if (response.object->get_id() == TD::messages::ID) { + int i = 0; + auto *pMessages = (TD::messages *)response.object.get(); + for (auto &it : pMessages->messages_) { + ProcessFileMessage(ft, it.get(), i != 0); + i++; + } + } + + ProtoBroadcastAck(ft->m_hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft); + delete ft; +} + HANDLE CTelegramProto::SendFile(MCONTACT hContact, const wchar_t *szDescription, wchar_t **ppszFiles) { auto *pUser = FindUser(GetId(hContact)); @@ -411,7 +488,17 @@ HANDLE CTelegramProto::SendFile(MCONTACT hContact, const wchar_t *szDescription, return nullptr; } - TG_FILE_REQUEST *pTransfer = nullptr; + struct FileItem + { + wchar_t *pwszName; + long iSize; + + FileItem(wchar_t *_1, long _2) : + pwszName(_1), iSize(_2) + {} + }; + + OBJLIST<FileItem> arFiles(1); for (int i = 0; ppszFiles[i] != 0; i++) { struct _stat statbuf; @@ -420,92 +507,90 @@ HANDLE CTelegramProto::SendFile(MCONTACT hContact, const wchar_t *szDescription, continue; } - pTransfer = new TG_FILE_REQUEST(TG_FILE_REQUEST::FILE, 0, ""); - pTransfer->m_fileName = ppszFiles[i]; - if (m_bCompressFiles) - pTransfer->AutoDetectType(); - - pTransfer->m_hContact = hContact; - pTransfer->m_fileSize = statbuf.st_size; - if (mir_wstrlen(szDescription)) - pTransfer->m_wszDescr = szDescription; - - // create a message with embedded file - auto *pMessage = new TD::sendMessage(); + arFiles.insert(new FileItem(ppszFiles[i], statbuf.st_size)); + } + + if (!arFiles.getCount()) { + debugLogA("No files to be sent"); + return nullptr; + } + + if (arFiles.getCount() > 10) { + debugLogA("Too many files to be sent"); + return nullptr; + } + + // create a message with embedded file + TD::sendMessage *pMessage = nullptr; + TD::sendMessageAlbum *pAlbum = nullptr; + + if (arFiles.getCount() == 1) { + pMessage = new TD::sendMessage(); pMessage->chat_id_ = pUser->chatId; + } + else { + pAlbum = new TD::sendMessageAlbum(); + pAlbum->chat_id_ = pUser->chatId; + } + + auto *ft = new TG_FILE_REQUEST(TG_FILE_REQUEST::FILE, 0, 0); + ft->m_hContact = hContact; + ft->m_fileName = arFiles[0].pwszName; + ft->m_fileSize = arFiles[0].iSize; + ft->m_wszDescr = szDescription; + + for (auto &it: arFiles) { + auto iFileType = (m_bCompressFiles) ? AutoDetectType(it->pwszName) : TG_FILE_REQUEST::FILE; auto caption = formatBbcodes(T2Utf(szDescription)); + TD::object_ptr<TD::InputMessageContent> pPart; - if (pTransfer->m_type == TG_FILE_REQUEST::FILE) { + if (iFileType == TG_FILE_REQUEST::FILE) { auto pContent = TD::make_object<TD::inputMessageDocument>(); - pContent->document_= makeFile(pTransfer->m_fileName); + pContent->document_= makeFile(it->pwszName); pContent->caption_ = std::move(caption); pContent->thumbnail_ = 0; - pMessage->input_message_content_ = std::move(pContent); + pPart = std::move(pContent); } - else if (pTransfer->m_type == TG_FILE_REQUEST::PICTURE) { + else if (iFileType == TG_FILE_REQUEST::PICTURE) { auto pContent = TD::make_object<TD::inputMessagePhoto>(); - pContent->photo_ = makeFile(pTransfer->m_fileName); + pContent->photo_ = makeFile(it->pwszName); pContent->thumbnail_ = 0; pContent->caption_ = std::move(caption); pContent->height_ = 0; pContent->width_ = 0; - pMessage->input_message_content_ = std::move(pContent); + pPart = std::move(pContent); } - else if (pTransfer->m_type == TG_FILE_REQUEST::VOICE) { + else if (iFileType == TG_FILE_REQUEST::VOICE) { auto pContent = TD::make_object<TD::inputMessageVoiceNote>(); - pContent->voice_note_ = makeFile(pTransfer->m_fileName); + pContent->voice_note_ = makeFile(it->pwszName); pContent->caption_ = std::move(caption); pContent->duration_ = 0; - pMessage->input_message_content_ = std::move(pContent); + pPart = std::move(pContent); } - else if (pTransfer->m_type == TG_FILE_REQUEST::VIDEO) { + else if (iFileType == TG_FILE_REQUEST::VIDEO) { auto pContent = TD::make_object<TD::inputMessageVideo>(); - pContent->video_ = makeFile(pTransfer->m_fileName); + pContent->video_ = makeFile(it->pwszName); pContent->caption_ = std::move(caption); pContent->duration_ = 0; pContent->height_ = 0; pContent->width_ = 0; - pMessage->input_message_content_ = std::move(pContent); + pPart = std::move(pContent); } else return nullptr; - SendQuery(pMessage, &CTelegramProto::OnSendFile, pTransfer); + if (pMessage) + pMessage->input_message_content_ = std::move(pPart); + else + pAlbum->input_message_contents_.push_back(std::move(pPart)); } - return pTransfer; -} - -void CTelegramProto::OnSendFile(td::ClientManager::Response &response, void *pUserInfo) -{ - auto *ft = (TG_FILE_REQUEST *)pUserInfo; - - if (response.object->get_id() == TD::message::ID) { - auto *pMsg = (TD::message *)response.object.get(); - ft->m_uniqueId.Format("%lld", pMsg->id_); - - if (auto *pUser = FindChat(pMsg->chat_id_)) { - char szUserId[100]; - if (this->GetGcUserId(pUser, pMsg, szUserId)) - ft->m_szUserId = szUserId; - - auto *pOwnMsg = new TG_OWN_MESSAGE(pUser->hContact, 0, pMsg->id_); - const TD::MessageContent *pBody = pMsg->content_.get(); - switch (pBody->get_id()) { - case TD::messagePhoto::ID: - pOwnMsg->tmpFileId = ((TD::messagePhoto*)pBody)->photo_->sizes_[0]->photo_->id_; - break; - - case TD::messageDocument::ID: - pOwnMsg->tmpFileId = ((TD::messageDocument *)pBody)->document_->document_->id_; - break; - } - m_arOwnMsg.insert(pOwnMsg); - } - } + if (pMessage) + SendQuery(pMessage, &CTelegramProto::OnSendFile, ft); + else + SendQuery(pAlbum, &CTelegramProto::OnSendFile, ft); - ProtoBroadcastAck(ft->m_hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ft); - delete ft; + return ft; } ///////////////////////////////////////////////////////////////////////////////////////// |