From c38733b3bcdfdf4af461f1d6a2cd9350b575249f Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 6 Dec 2023 18:19:48 +0300 Subject: fixes #4019 completely --- protocols/Telegram/src/groupchat.cpp | 83 ++++++++++++++++++++---------------- protocols/Telegram/src/proto.cpp | 1 + protocols/Telegram/src/proto.h | 5 ++- protocols/Telegram/src/server.cpp | 4 +- protocols/Telegram/src/utils.cpp | 1 + 5 files changed, 54 insertions(+), 40 deletions(-) (limited to 'protocols/Telegram') diff --git a/protocols/Telegram/src/groupchat.cpp b/protocols/Telegram/src/groupchat.cpp index ddd51c036f..99198d452e 100644 --- a/protocols/Telegram/src/groupchat.cpp +++ b/protocols/Telegram/src/groupchat.cpp @@ -32,7 +32,7 @@ static const wchar_t* getRoleById(uint32_t ID) } } -void CTelegramProto::InitGroupChat(TG_USER *pUser, const TD::chat *pChat) +void CTelegramProto::InitGroupChat(TG_USER *pUser, const wchar_t *pwszTitle) { if (pUser->m_si) return; @@ -41,10 +41,11 @@ void CTelegramProto::InitGroupChat(TG_USER *pUser, const TD::chat *pChat) _i64tow(pUser->id, wszId, 10); SESSION_INFO *si; - Utf2T wszNick(pChat->title_.c_str()); + if (pwszTitle == nullptr) + pwszTitle = pUser->wszNick; if (pUser->bLoadMembers) { - si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszId, wszNick, pUser); + pUser->m_si = si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszId, pwszTitle, pUser); if (!si->pStatuses) { Chat_AddGroup(si, TranslateT("Creator")); Chat_AddGroup(si, TranslateT("Admin")); @@ -56,39 +57,39 @@ void CTelegramProto::InitGroupChat(TG_USER *pUser, const TD::chat *pChat) else SendQuery(new TD::getSupergroupMembers(pUser->id, 0, 0, 100), &CTelegramProto::StartGroupChat, pUser); } - else { - Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE); - Chat_Control(si, SESSION_ONLINE); - } + else GcRun(pUser); } else { - if (si = Chat_NewSession(GCW_CHANNEL, m_szModuleName, wszId, wszNick, pUser)) { - if (!si->pStatuses) { - Chat_AddGroup(si, TranslateT("SuperAdmin")); - Chat_AddGroup(si, TranslateT("Visitor")); - - ptrW wszMyId(getWStringA(DBKEY_ID)), wszMyNick(Contact::GetInfo(CNF_DISPLAY, 0, m_szModuleName)); - - GCEVENT gce = { si, GC_EVENT_JOIN }; - gce.pszUID.w = wszMyId; - gce.pszNick.w = wszMyNick; - gce.bIsMe = true; - gce.pszStatus.w = TranslateT("Visitor"); - Chat_Event(&gce); - - gce.bIsMe = false; - gce.pszUID.w = wszId; - gce.pszNick.w = wszNick; - gce.pszStatus.w = TranslateT("SuperAdmin"); - Chat_Event(&gce); - } + pUser->m_si = si = Chat_NewSession(GCW_CHANNEL, m_szModuleName, wszId, pwszTitle, pUser); + if (!si->pStatuses) { + Chat_AddGroup(si, TranslateT("SuperAdmin")); + Chat_AddGroup(si, TranslateT("Visitor")); - Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE); - Chat_Control(si, SESSION_ONLINE); + ptrW wszMyId(getWStringA(DBKEY_ID)), wszMyNick(Contact::GetInfo(CNF_DISPLAY, 0, m_szModuleName)); + + GCEVENT gce = { si, GC_EVENT_JOIN }; + gce.pszUID.w = wszMyId; + gce.pszNick.w = wszMyNick; + gce.bIsMe = true; + gce.pszStatus.w = TranslateT("Visitor"); + Chat_Event(&gce); + + gce.bIsMe = false; + gce.pszUID.w = wszId; + gce.pszNick.w = pwszTitle; + gce.pszStatus.w = TranslateT("SuperAdmin"); + Chat_Event(&gce); } + + GcRun(pUser); } - - pUser->m_si = si; +} + +void CTelegramProto::GcRun(TG_USER *pChat) +{ + pChat->bStartChat = false; + Chat_Control(pChat->m_si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE); + Chat_Control(pChat->m_si, SESSION_ONLINE); } void CTelegramProto::StartGroupChat(td::ClientManager::Response &response, void *pUserData) @@ -111,6 +112,9 @@ void CTelegramProto::StartGroupChat(td::ClientManager::Response &response, void debugLogA("Gotten class ID %d instead of %d, exiting", response.object->get_id(), TD::basicGroupFullInfo::ID); return; } + + if (pChat->bStartChat) + GcRun(pChat); } void CTelegramProto::GcAddMembers(TG_USER *pChat, const TD::array> &pMembers, bool bSilent) @@ -424,15 +428,22 @@ void CTelegramProto::ProcessSuperGroup(TD::updateSupergroup *pObj) if (iStatusId == TD::chatMemberStatusLeft::ID) { auto *pUser = AddFakeUser(tmp.id, true); - pUser->wszNick = Utf2T(getName(pGroup->group->usernames_.get())); - pUser->wszLastName.Format(TranslateT("%d member(s)"), pGroup->group->member_count_); + if (pUser->hContact == INVALID_CONTACT_ID) { + // cache some information for the search + if (pUser->wszNick.IsEmpty()) + pUser->wszNick = Utf2T(getName(pGroup->group->usernames_.get())); + pUser->wszLastName.Format(TranslateT("%d member(s)"), pGroup->group->member_count_); + } } else { auto *pChat = AddUser(tmp.id, true); - if (auto *si = pChat->m_si) { - CMStringW wszUserId(FORMAT, L"%lld", m_iOwnId); + if (pChat->bStartChat) + InitGroupChat(pChat, pChat->wszNick); + + if (pChat->m_si) { + CMStringW wszUserId(FORMAT, L"%lld", m_iOwnId); - GCEVENT gce = { si, GC_EVENT_SETSTATUS }; + GCEVENT gce = { pChat->m_si, GC_EVENT_SETSTATUS }; gce.pszUID.w = wszUserId; gce.time = time(0); gce.bIsMe = true; diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp index d7c72e8062..d23574b4e3 100644 --- a/protocols/Telegram/src/proto.cpp +++ b/protocols/Telegram/src/proto.cpp @@ -128,6 +128,7 @@ void CTelegramProto::OnContactDeleted(MCONTACT hContact) if (auto *pUser = FindUser(id)) { pUser->hContact = INVALID_CONTACT_ID; + pUser->wszNick = getMStringW(hContact, "Nick"); pUser->wszFirstName = getMStringW(hContact, "FirstName"); pUser->wszLastName = getMStringW(hContact, "LastName"); } diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index 14b617fe1f..0d3b759740 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, bLoadMembers; + bool isGroupChat, bLoadMembers, bStartChat; CMStringA szAvatarHash; CMStringW wszNick, wszFirstName, wszLastName; time_t m_timer1 = 0, m_timer2 = 0; @@ -287,7 +287,7 @@ class CTelegramProto : public PROTO INT_PTR __cdecl SvcLeaveChat(WPARAM, LPARAM); - void InitGroupChat(TG_USER *pUser, const TD::chat *pChat); + void InitGroupChat(TG_USER *pUser, const wchar_t *pwszTitle); void StartGroupChat(td::ClientManager::Response &response, void *pUserData); void Chat_SendPrivateMessage(GCHOOK *gch); @@ -297,6 +297,7 @@ class CTelegramProto : public PROTO void GcAddMembers(TG_USER *pChat, const TD::array> &pMembers, bool bSilent); void GcChangeMember(TG_USER *pChat, const char *adminId, TD::int53 userId, bool bJoined); void GcChangeTopic(TG_USER *pChat, const wchar_t *pwszNewTopic); + void GcRun(TG_USER *pChat); // Search volatile unsigned m_iSearchCount; diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index 5cc78c2439..6066f55d46 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -491,8 +491,8 @@ void CTelegramProto::ProcessChat(TD::updateNewChat *pObj) if (pChat->permissions_) Contact::Readonly(hContact, !pChat->permissions_->can_send_basic_messages_); - if (pUser->isGroupChat) - InitGroupChat(pUser, pChat); + if (pUser->isGroupChat && pUser->m_si == nullptr) + InitGroupChat(pUser, Utf2T(pChat->title_.c_str())); } } else debugLogA("Unknown user id %lld, ignoring", userId); diff --git a/protocols/Telegram/src/utils.cpp b/protocols/Telegram/src/utils.cpp index 2db0a384f7..8fff0f9d2b 100644 --- a/protocols/Telegram/src/utils.cpp +++ b/protocols/Telegram/src/utils.cpp @@ -305,6 +305,7 @@ TG_USER* CTelegramProto::AddUser(int64_t id, bool bIsChat) setWString(hContact, "FirstName", pUser->wszFirstName); setWString(hContact, "LastName", pUser->wszLastName); } + else pUser->bStartChat = true; } return pUser; -- cgit v1.2.3