From 1362e985f52a4501d3a7796fa63f290cffad0c88 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 29 Feb 2024 13:10:29 +0300 Subject: fixes #4248 (Telegram: add chat avatars support) --- protocols/Telegram/src/avatars.cpp | 21 +++++++++++ protocols/Telegram/src/proto.h | 3 +- protocols/Telegram/src/server.cpp | 73 +++++++++++++++++--------------------- 3 files changed, 56 insertions(+), 41 deletions(-) (limited to 'protocols/Telegram') diff --git a/protocols/Telegram/src/avatars.cpp b/protocols/Telegram/src/avatars.cpp index 4c9cca1d58..ed6efbd3de 100644 --- a/protocols/Telegram/src/avatars.cpp +++ b/protocols/Telegram/src/avatars.cpp @@ -190,6 +190,27 @@ TG_FILE_REQUEST* CTelegramProto::FindFile(const char *pszUniqueId) return nullptr; } +///////////////////////////////////////////////////////////////////////////////////////// +// Extracts a photo/avatar to a file + +void CTelegramProto::ProcessAvatar(const TD::file *pFile, TG_USER *pUser) +{ + if (pUser->hContact == INVALID_CONTACT_ID) + return; + + auto remoteId = pFile->remote_->unique_id_; + auto storedId = getMStringA(pUser->hContact, DBKEY_AVATAR_HASH); + auto wszFileName = GetAvatarFilename(pUser->hContact); + if (remoteId != storedId.c_str() || _waccess(wszFileName, 0)) { + if (!remoteId.empty()) { + pUser->szAvatarHash = remoteId.c_str(); + setString(pUser->hContact, DBKEY_AVATAR_HASH, remoteId.c_str()); + SendQuery(new TD::downloadFile(pFile->id_, 5, 0, 0, false)); + } + else delSetting(pUser->hContact, DBKEY_AVATAR_HASH); + } +} + ///////////////////////////////////////////////////////////////////////////////////////// // handles file info updates diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index acf288aeba..b179b7d6f2 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -246,8 +246,9 @@ class CTelegramProto : public PROTO void SendMarkRead(void); int SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr); int SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER_FULL pHandler, void *pUserInfo); - int SendTextMessage(TD::int53 chatId, TD::int53 replyId, const char *pszMessage); + int SendTextMessage(int64_t chatId, int64_t replyId, const char *pszMessage); + void ProcessAvatar(const TD::file *pFile, TG_USER *pUser); void ProcessAuth(TD::updateAuthorizationState *pObj); void ProcessBasicGroup(TD::updateBasicGroup *pObj); void ProcessBasicGroupInfo(TD::updateBasicGroupFullInfo *pObj); diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index f9b9b9f760..2b04167928 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -336,7 +336,7 @@ void CTelegramProto::OnSendMessage(td::ClientManager::Response &response) } } -int CTelegramProto::SendTextMessage(int64_t chatId, TD::int53 replyId, const char *pszMessage) +int CTelegramProto::SendTextMessage(int64_t chatId, int64_t replyId, const char *pszMessage) { auto pContent = TD::make_object(); pContent->text_ = formatBbcodes(pszMessage); @@ -482,37 +482,43 @@ void CTelegramProto::ProcessChat(TD::updateNewChat *pObj) return; } - if (auto *pUser = FindUser(userId)) { - pUser->chatId = pChat->id_; - MCONTACT hContact = (pUser->id == m_iOwnId) ? 0 : pUser->hContact; + auto *pUser = FindUser(userId); + if (pUser == nullptr) { + debugLogA("Unknown user id %lld for chat %lld, ignoring", userId, pChat->id_); + return; + } - if (!m_arChats.find(pUser)) - m_arChats.insert(pUser); + pUser->chatId = pChat->id_; + MCONTACT hContact = (pUser->id == m_iOwnId) ? 0 : pUser->hContact; - if (!szTitle.empty()) { - if (hContact != INVALID_CONTACT_ID) - setUString(hContact, "Nick", szTitle.c_str()); - else if (pUser->wszNick.IsEmpty()) - pUser->wszFirstName = Utf2T(szTitle.c_str()); - } + if (!m_arChats.find(pUser)) + m_arChats.insert(pUser); - if (CheckSearchUser(pUser)) - return; + if (!szTitle.empty()) { + if (hContact != INVALID_CONTACT_ID) + setUString(hContact, "Nick", szTitle.c_str()); + else if (pUser->wszNick.IsEmpty()) + pUser->wszFirstName = Utf2T(szTitle.c_str()); + } - if (pUser->hContact != INVALID_CONTACT_ID) { - if (pChat->has_protected_content_) - setByte(pUser->hContact, "Protected", 1); - else - delSetting(pUser->hContact, "Protected"); + if (auto *pPhoto = pChat->photo_.get()) + ProcessAvatar(pPhoto->small_.get(), pUser); - if (pChat->permissions_) - Contact::Readonly(hContact, !pChat->permissions_->can_send_basic_messages_); + if (CheckSearchUser(pUser)) + return; - if (pUser->isGroupChat && pUser->m_si == nullptr) - InitGroupChat(pUser, Utf2T(pChat->title_.c_str())); - } + if (pUser->hContact != INVALID_CONTACT_ID) { + if (pChat->has_protected_content_) + setByte(pUser->hContact, "Protected", 1); + else + delSetting(pUser->hContact, "Protected"); + + if (pChat->permissions_) + Contact::Readonly(hContact, !pChat->permissions_->can_send_basic_messages_); + + if (pUser->isGroupChat && pUser->m_si == nullptr) + InitGroupChat(pUser, Utf2T(pChat->title_.c_str())); } - else debugLogA("Unknown user id %lld, ignoring", userId); } void CTelegramProto::ProcessChatAction(TD::updateChatAction *pObj) @@ -1056,21 +1062,8 @@ void CTelegramProto::ProcessUser(TD::updateUser *pObj) } else ExtraIcon_SetIconByName(g_plugin.m_hIcon, pu->hContact, nullptr); - if (auto *pPhoto = pUser->profile_photo_.get()) { - if (auto *pSmall = pPhoto->small_.get()) { - auto remoteId = pSmall->remote_->unique_id_; - auto storedId = getMStringA(pu->hContact, DBKEY_AVATAR_HASH); - auto wszFileName = GetAvatarFilename(pu->hContact); - if (remoteId != storedId.c_str() || _waccess(wszFileName, 0)) { - if (!remoteId.empty()) { - pu->szAvatarHash = remoteId.c_str(); - setString(pu->hContact, DBKEY_AVATAR_HASH, remoteId.c_str()); - SendQuery(new TD::downloadFile(pSmall->id_, 5, 0, 0, false)); - } - else delSetting(pu->hContact, DBKEY_AVATAR_HASH); - } - } - } + if (auto *pPhoto = pUser->profile_photo_.get()) + ProcessAvatar(pPhoto->small_.get(), pu); if (pUser->status_) { if (pUser->status_->get_id() == TD::userStatusOffline::ID) { -- cgit v1.2.3