From 9922b02a0ad956272db894b3a84cd8d31524f20c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 29 Feb 2024 22:01:13 +0300 Subject: fixes #4252 (Telegram: forum support) --- protocols/Telegram/src/groupchat.cpp | 25 +++++++++++++++++++- protocols/Telegram/src/proto.h | 3 ++- protocols/Telegram/src/server.cpp | 44 +++++++++++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 8 deletions(-) (limited to 'protocols/Telegram') diff --git a/protocols/Telegram/src/groupchat.cpp b/protocols/Telegram/src/groupchat.cpp index 2b73a2dba1..e38a182abb 100644 --- a/protocols/Telegram/src/groupchat.cpp +++ b/protocols/Telegram/src/groupchat.cpp @@ -416,6 +416,30 @@ void CTelegramProto::ProcessBasicGroupInfo(TD::updateBasicGroupFullInfo *pObj) } } +void CTelegramProto::ProcessForum(TD::updateForumTopicInfo *pForum) +{ + auto *pUser = FindChat(pForum->chat_id_); + if (!pUser) { + debugLogA("Uknown chat id %lld, skipping", pForum->chat_id_); + return; + } + + if (pUser->m_si == nullptr) { + debugLogA("No parent chat for id %lld, skipping", pForum->chat_id_); + return; + } + + wchar_t wszId[100]; + mir_snwprintf(wszId, L"%lld_%lld", pForum->chat_id_, pForum->info_->message_thread_id_); + + auto *si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszId, Utf2T(pForum->info_->name_.c_str()), pUser); + si->pParent = pUser->m_si; + Clist_SetGroup(si->hContact, ptrW(Clist_GetGroup(pUser->hContact))); + + Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE); + Chat_Control(si, SESSION_ONLINE); +} + void CTelegramProto::ProcessSuperGroup(TD::updateSupergroup *pObj) { auto iStatusId = pObj->supergroup_->status_->get_id(); @@ -425,7 +449,6 @@ void CTelegramProto::ProcessSuperGroup(TD::updateSupergroup *pObj) } TG_SUPER_GROUP tmp(pObj->supergroup_->id_, 0); - auto *pGroup = m_arSuperGroups.find(&tmp); if (pGroup == nullptr) { pGroup = new TG_SUPER_GROUP(tmp.id, std::move(pObj->supergroup_)); diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index b179b7d6f2..439a6b9697 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -98,7 +98,7 @@ struct TG_USER : public MZeroedObject int64_t id, chatId = -1; MCONTACT hContact; - bool isGroupChat, isBot, bLoadMembers, bStartChat, bInited; + bool isGroupChat, isBot, isForum, bLoadMembers, bStartChat, bInited; CMStringA szAvatarHash; CMStringW wszNick, wszFirstName, wszLastName; time_t m_timer1 = 0, m_timer2 = 0; @@ -264,6 +264,7 @@ class CTelegramProto : public PROTO void ProcessDeleteMessage(TD::updateDeleteMessages *pObj); void ProcessFile(TD::updateFile *pObj); void ProcessFileMessage(TG_FILE_REQUEST *ft, const TD::message *pMsg, bool); + void ProcessForum(TD::updateForumTopicInfo *pForum); void ProcessGroups(TD::updateChatFolders *pObj); void ProcessMarkRead(TD::updateChatReadInbox *pObj); void ProcessMessage(const TD::message *pMsg); diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index 2d7cc79258..b80bef0988 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -246,6 +246,10 @@ void CTelegramProto::ProcessResponse(td::ClientManager::Response response) ProcessFile((TD::updateFile *)response.object.get()); break; + case TD::updateForumTopicInfo::ID: + ProcessForum((TD::updateForumTopicInfo*)response.object.get()); + break; + case TD::updateMessageContent::ID: ProcessMessageContent((TD::updateMessageContent *)response.object.get()); break; @@ -460,6 +464,7 @@ void CTelegramProto::ProcessChat(TD::updateNewChat *pObj) int64_t userId; auto *pChat = pObj->chat_.get(); std::string szTitle; + bool isForum = false; switch (pChat->type_->get_id()) { case TD::chatTypePrivate::ID: @@ -475,6 +480,14 @@ void CTelegramProto::ProcessChat(TD::updateNewChat *pObj) case TD::chatTypeSupergroup::ID: userId = ((TD::chatTypeSupergroup *)pChat->type_.get())->supergroup_id_; szTitle = pChat->title_; + { + TG_SUPER_GROUP tmp(userId, 0); + if (auto *pGroup = m_arSuperGroups.find(&tmp)) + isForum = pGroup->group->is_forum_; + + if (isForum) + SendQuery(new TD::getForumTopics(pChat->id_, "", 0, 0, 0, 100)); + } break; default: @@ -489,14 +502,17 @@ void CTelegramProto::ProcessChat(TD::updateNewChat *pObj) } pUser->chatId = pChat->id_; + pUser->isForum = isForum; MCONTACT hContact = (pUser->id == m_iOwnId) ? 0 : pUser->hContact; if (!m_arChats.find(pUser)) m_arChats.insert(pUser); if (!szTitle.empty()) { - if (hContact != INVALID_CONTACT_ID) + if (hContact != INVALID_CONTACT_ID) { setUString(hContact, "Nick", szTitle.c_str()); + pUser->wszNick = Utf2T(szTitle.c_str()); + } else if (pUser->wszNick.IsEmpty()) pUser->wszFirstName = Utf2T(szTitle.c_str()); } @@ -517,7 +533,7 @@ void CTelegramProto::ProcessChat(TD::updateNewChat *pObj) Contact::Readonly(hContact, !pChat->permissions_->can_send_basic_messages_); if (pUser->isGroupChat && pUser->m_si == nullptr) - InitGroupChat(pUser, Utf2T(pChat->title_.c_str())); + InitGroupChat(pUser, (pUser->isForum) ? TranslateT("General") : Utf2T(pChat->title_.c_str())); } } @@ -608,6 +624,10 @@ void CTelegramProto::ProcessChatPosition(TD::updateChatPosition *pObj) wszGroup = TranslateT("Archive"); break; + case TD::chatListMain::ID: + wszGroup = TranslateT("Main"); + break; + case TD::chatListFolder::ID: { int iGroupId = ((TD::chatListFolder *)pList)->chat_folder_id_; @@ -625,7 +645,8 @@ void CTelegramProto::ProcessChatPosition(TD::updateChatPosition *pObj) return; } - ptrW pwszExistingGroup(Clist_GetGroup(pUser->hContact)); + MCONTACT hContact = GetRealContact(pUser); + ptrW pwszExistingGroup(Clist_GetGroup(hContact)); debugLogW(L"Existing contact group %s, calculated %s", pwszExistingGroup.get(), wszGroup.c_str()); if (!pwszExistingGroup @@ -633,9 +654,12 @@ void CTelegramProto::ProcessChatPosition(TD::updateChatPosition *pObj) || (pUser->isGroupChat && !mir_wstrcmp(pwszExistingGroup, ptrW(Chat_GetGroup())))) { CMStringW wszNewGroup(FORMAT, L"%s\\%s", (wchar_t *)m_wszDefaultGroup, wszGroup.c_str()); - debugLogW(L"Setting group for %d to %s", pUser->hContact, wszNewGroup.c_str()); + if (pUser->isForum) + wszNewGroup.AppendFormat(L"\\%s", pUser->wszNick.c_str()); + + debugLogW(L"Setting group for %d to %s", hContact, wszNewGroup.c_str()); Clist_GroupCreate(0, wszNewGroup); - Clist_SetGroup(pUser->hContact, wszNewGroup); + Clist_SetGroup(hContact, wszNewGroup); } } } @@ -816,6 +840,14 @@ void CTelegramProto::ProcessMessage(const TD::message *pMessage) Contact::RemoveFromList(pUser->hContact); } + MCONTACT hContact = GetRealContact(pUser); + if (pMessage->message_thread_id_) { + wchar_t buf[100]; + mir_snwprintf(buf, L"%lld_%lld", pMessage->chat_id_, pMessage->message_thread_id_); + if (auto *si = Chat_Find(buf, m_szModuleName)) + hContact = si->hContact; + } + DB::EventInfo dbei(hOldEvent); dbei.szId = szMsgId; dbei.cbBlob = szText.GetLength(); @@ -837,7 +869,7 @@ void CTelegramProto::ProcessMessage(const TD::message *pMessage) } else { dbei.pBlob = szText.GetBuffer(); - ProtoChainRecvMsg(GetRealContact(pUser), dbei); + ProtoChainRecvMsg(hContact, dbei); } } -- cgit v1.2.3