From 758a391e5567c5986d40ed850d4a117d9f6ad37a Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 22 Dec 2021 16:09:34 +0300 Subject: [PATCH] Store title and photo of min-channels. --- CMakeLists.txt | 1 + td/telegram/ContactsManager.cpp | 28 +++++++++++++---- td/telegram/ContactsManager.h | 8 +++-- td/telegram/MinChannel.h | 55 +++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 td/telegram/MinChannel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 661b2bc60..a3ed1d6bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -567,6 +567,7 @@ set(TDLIB_SOURCE td/telegram/MessageSender.h td/telegram/MessagesManager.h td/telegram/MessageTtl.h + td/telegram/MinChannel.h td/telegram/misc.h td/telegram/net/AuthDataShared.h td/telegram/net/ConnectionCreator.h diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 3e621fea4..c0fd1d872 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -25,6 +25,7 @@ #include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/MessageTtl.h" +#include "td/telegram/MinChannel.h" #include "td/telegram/misc.h" #include "td/telegram/net/NetQuery.h" #include "td/telegram/NotificationManager.h" @@ -14552,6 +14553,14 @@ bool ContactsManager::have_min_channel(ChannelId channel_id) const { return min_channels_.count(channel_id) > 0; } +const MinChannel *ContactsManager::get_min_channel(ChannelId channel_id) const { + auto it = min_channels_.find(channel_id); + if (it == min_channels_.end()) { + return nullptr; + } + return it->second.get(); +} + const ContactsManager::Channel *ContactsManager::get_channel(ChannelId channel_id) const { auto p = channels_.find(channel_id); if (p == channels_.end()) { @@ -15585,8 +15594,8 @@ void ContactsManager::on_chat_update(telegram_api::channel &channel, const char Channel *c = get_channel_force(channel_id); LOG(ERROR) << "Receive empty " << to_string(channel) << " from " << source << ", have " << to_string(get_supergroup_object(channel_id, c)); - if (c == nullptr) { - min_channels_.insert(channel_id); + if (c == nullptr && !have_min_channel(channel_id)) { + min_channels_[channel_id] = td::make_unique(); } return; } @@ -15654,7 +15663,6 @@ void ContactsManager::on_chat_update(telegram_api::channel &channel, const char }(); if (is_min) { - // TODO there can be better support for min channels Channel *c = get_channel_force(channel_id); if (c != nullptr) { LOG(DEBUG) << "Receive known min " << channel_id; @@ -15689,7 +15697,15 @@ void ContactsManager::on_chat_update(telegram_api::channel &channel, const char update_channel(c, channel_id); } else { - min_channels_.insert(channel_id); + auto min_channel = td::make_unique(); + min_channel->photo_ = + get_dialog_photo(td_->file_manager_.get(), DialogId(channel_id), access_hash, std::move(channel.photo_)); + if (td_->auth_manager_->is_bot()) { + min_channel->photo_.minithumbnail.clear(); + } + min_channel->title_ = std::move(channel.title_); + + min_channels_[channel_id] = std::move(min_channel); } return; } @@ -15789,8 +15805,8 @@ void ContactsManager::on_chat_update(telegram_api::channelForbidden &channel, co Channel *c = get_channel_force(channel_id); LOG(ERROR) << "Receive empty " << to_string(channel) << " from " << source << ", have " << to_string(get_supergroup_object(channel_id, c)); - if (c == nullptr) { - min_channels_.insert(channel_id); + if (c == nullptr && !have_min_channel(channel_id)) { + min_channels_[channel_id] = td::make_unique(); } return; } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 02674d418..321457657 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -55,6 +55,8 @@ namespace td { struct BinlogEvent; +struct MinChannel; + class Td; class ContactsManager final : public Actor { @@ -490,8 +492,10 @@ class ContactsManager final : public Actor { DialogParticipantStatus get_chat_permissions(ChatId chat_id) const; bool is_appointed_chat_administrator(ChatId chat_id) const; - bool have_channel(ChannelId channel_id) const; bool have_min_channel(ChannelId channel_id) const; + const MinChannel *get_min_channel(ChannelId channel_id) const; + + bool have_channel(ChannelId channel_id) const; bool have_channel_force(ChannelId channel_id); bool get_channel(ChannelId channel_id, int left_tries, Promise &&promise); void reload_channel(ChannelId channel_id, Promise &&promise); @@ -1645,7 +1649,7 @@ class ContactsManager final : public Actor { mutable std::unordered_set unknown_chats_; std::unordered_map chat_full_file_source_ids_; - std::unordered_set min_channels_; + std::unordered_map, ChannelIdHash> min_channels_; std::unordered_map, ChannelIdHash> channels_; std::unordered_map, ChannelIdHash> channels_full_; mutable std::unordered_set unknown_channels_; diff --git a/td/telegram/MinChannel.h b/td/telegram/MinChannel.h new file mode 100644 index 000000000..8dc84f67d --- /dev/null +++ b/td/telegram/MinChannel.h @@ -0,0 +1,55 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/Photo.h" + +#include "td/utils/common.h" +#include "td/utils/tl_helpers.h" + +namespace td { + +struct MinChannel { + string title_; + DialogPhoto photo_; + + template + void store(StorerT &storer) const { + using td::store; + bool has_title = !title_.empty(); + bool has_photo = photo_.small_file_id.is_valid(); + BEGIN_STORE_FLAGS(); + STORE_FLAG(has_title); + STORE_FLAG(has_photo); + END_STORE_FLAGS(); + if (has_title) { + store(title_, storer); + } + if (has_photo) { + store(photo_, storer); + } + } + + template + void parse(ParserT &parser) { + using td::parse; + bool has_title; + bool has_photo; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_title); + PARSE_FLAG(has_photo); + END_PARSE_FLAGS(); + if (has_title) { + parse(title_, parser); + } + if (has_photo) { + parse(photo_, parser); + } + } +}; + +} // namespace td