From 220ad5352b478ffb55fbbf471996d8b45d7b0b32 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 9 Sep 2020 03:40:17 +0300 Subject: [PATCH 01/10] Allow write access to linked supergroups. GitOrigin-RevId: 05008f9e07dc1d0759235431d999d59f50d7e820 --- td/telegram/ContactsManager.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 1e3f0b054..94fe60545 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -4161,6 +4161,14 @@ bool ContactsManager::have_input_peer_channel(const Channel *c, ChannelId channe if (dialog_access_by_invite_link_.count(DialogId(channel_id))) { return true; } + } else { + if (!from_linked && c->is_megagroup) { + auto linked_channel_id = get_linked_channel_id(channel_id); + if (linked_channel_id.is_valid()) { + return !c->username.empty() || c->has_location || + have_input_peer_channel(get_channel(linked_channel_id), linked_channel_id, AccessRights::Read, true); + } + } } if (!c->status.is_member()) { return false; From aa6c746e8d82ffd7f71c843b0b36e45a19418e8f Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 10 Sep 2020 14:54:34 +0300 Subject: [PATCH 02/10] Add MessagesManager::fix_server_reply_to_message_id. GitOrigin-RevId: cafb49b6885fc992ff4867a254771a1fa9a6084b --- td/telegram/MessagesManager.cpp | 47 ++++++++++++++++++++++----------- td/telegram/MessagesManager.h | 5 ++++ td/telegram/UpdatesManager.cpp | 6 ++--- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 8301e83ff..5986da624 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -12151,6 +12151,7 @@ std::pair> MessagesManager::creat MessageId reply_to_message_id = message_info.reply_to_message_id; DialogId reply_in_dialog_id; + MessageId top_reply_message_id; if (message_info.reply_header != nullptr) { reply_to_message_id = MessageId(ServerMessageId(message_info.reply_header->reply_to_msg_id_)); auto reply_to_peer_id = std::move(message_info.reply_header->reply_to_peer_id_); @@ -12159,23 +12160,19 @@ std::pair> MessagesManager::creat if (!reply_in_dialog_id.is_valid()) { LOG(ERROR) << " Receive reply in invalid " << to_string(reply_to_peer_id); reply_to_message_id = MessageId(); + reply_in_dialog_id = DialogId(); + } + } + if (reply_to_message_id.is_valid()) { + if ((message_info.reply_header->flags_ & telegram_api::messageReplyHeader::REPLY_TO_TOP_ID_MASK) != 0) { + top_reply_message_id = MessageId(ServerMessageId(message_info.reply_header->reply_to_top_id_)); + } else if (message_info.reply_info != nullptr && !is_broadcast_channel(dialog_id)) { + top_reply_message_id = reply_to_message_id; } } } - CHECK(!reply_to_message_id.is_scheduled()); - if (reply_to_message_id != MessageId()) { - if (!reply_to_message_id.is_valid()) { - LOG(ERROR) << "Receive reply to " << reply_to_message_id << " for " << message_id << " in " << dialog_id; - reply_to_message_id = MessageId(); - } else { - if (!message_id.is_scheduled() && !reply_in_dialog_id.is_valid() && reply_to_message_id >= message_id) { - if (reply_to_message_id.get() - message_id.get() <= MessageId(ServerMessageId(2000000000)).get()) { - LOG(ERROR) << "Receive reply to wrong " << reply_to_message_id << " in " << message_id; - } - reply_to_message_id = MessageId(); - } - } - } + fix_server_reply_to_message_id(dialog_id, message_id, reply_in_dialog_id, reply_to_message_id); + fix_server_reply_to_message_id(dialog_id, message_id, reply_in_dialog_id, top_reply_message_id); UserId via_bot_user_id = message_info.via_bot_user_id; if (!via_bot_user_id.is_valid()) { @@ -12259,7 +12256,7 @@ std::pair> MessagesManager::creat message->legacy_layer = (is_legacy ? MTPROTO_LAYER : 0); message->content = std::move(message_info.content); message->reply_markup = get_reply_markup(std::move(message_info.reply_markup), td_->auth_manager_->is_bot(), false, - message->contains_mention || dialog_id.get_type() == DialogType::User); + message->contains_mention || dialog_type == DialogType::User); if (content_type == MessageContentType::ExpiredPhoto || content_type == MessageContentType::ExpiredVideo) { CHECK(message->ttl == 0); // ttl is ignored/set to 0 if the message has already been expired @@ -20745,6 +20742,26 @@ MessageId MessagesManager::get_reply_to_message_id(Dialog *d, MessageId message_ return m->message_id; } +void MessagesManager::fix_server_reply_to_message_id(DialogId dialog_id, MessageId message_id, + DialogId reply_in_dialog_id, MessageId &reply_to_message_id) { + CHECK(!reply_to_message_id.is_scheduled()); + if (!reply_to_message_id.is_valid()) { + if (reply_to_message_id != MessageId()) { + LOG(ERROR) << "Receive reply to " << reply_to_message_id << " for " << message_id << " in " << dialog_id; + reply_to_message_id = MessageId(); + } + return; + } + + if (!message_id.is_scheduled() && !reply_in_dialog_id.is_valid() && reply_to_message_id >= message_id) { + if (reply_to_message_id.get() - message_id.get() <= MessageId(ServerMessageId(2000000000)).get() || + dialog_id.get_type() == DialogType::Channel) { + LOG(ERROR) << "Receive reply to wrong " << reply_to_message_id << " in " << message_id << " in " << dialog_id; + } + reply_to_message_id = MessageId(); + } +} + vector MessagesManager::get_message_file_ids(const Message *m) const { CHECK(m != nullptr); return get_message_content_file_ids(m->content.get(), td_); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 64fb40bf6..3b8afda6c 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -990,6 +990,8 @@ class MessagesManager : public Actor { MessageId reply_to_message_id; int64 reply_to_random_id = 0; // for send_message + DialogId reply_in_dialog_id; + MessageId top_reply_message_id; UserId via_bot_user_id; @@ -1720,6 +1722,9 @@ class MessagesManager : public Actor { MessageId get_reply_to_message_id(Dialog *d, MessageId message_id); + static void fix_server_reply_to_message_id(DialogId dialog_id, MessageId message_id, DialogId reply_in_dialog_id, + MessageId &reply_to_message_id); + bool can_set_game_score(DialogId dialog_id, const Message *m) const; bool check_update_dialog_id(const tl_object_ptr &update, DialogId dialog_id); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index a59c56c5c..e10909ff6 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -501,10 +501,10 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ } /* - // the users are always min, so no need to check + // the dialogs are always min, so no need to check if (message->replies_ != nullptr) { - for (auto &user_id : message->replies_->recent_repliers_) { - if (!is_acceptable_user(UserId(user_id))) { + for (auto &peer : message->replies_->recent_repliers_) { + if (!is_acceptable_dialog(DialogId(peer))) { return false; } } From 643371aa2fec599af05e9081612daa29fdb606eb Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 10 Sep 2020 16:28:25 +0300 Subject: [PATCH 03/10] Check message reply header in updates. GitOrigin-RevId: bb71f4cf73f104b6d2f9da18305007336b9397bf --- td/telegram/UpdatesManager.cpp | 29 ++++++++++++++++++++++------- td/telegram/UpdatesManager.h | 3 +++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index e10909ff6..884689b47 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -393,20 +393,34 @@ bool UpdatesManager::is_acceptable_message_entities( return true; } +bool UpdatesManager::is_acceptable_message_reply_header( + const telegram_api::object_ptr &header) const { + if (header == nullptr) { + return true; + } + + if (header->reply_to_peer_id_ != nullptr) { + DialogId dialog_id(header->reply_to_peer_id_); + if (!is_acceptable_dialog(dialog_id)) { + return false; + } + } + return true; +} + bool UpdatesManager::is_acceptable_message_forward_header( const telegram_api::object_ptr &header) const { if (header == nullptr) { return true; } - auto flags = header->flags_; - if (flags & telegram_api::messageFwdHeader::FROM_ID_MASK) { + if (header->from_id_ != nullptr) { DialogId dialog_id(header->from_id_); if (!is_acceptable_dialog(dialog_id)) { return false; } } - if (flags & telegram_api::messageFwdHeader::SAVED_FROM_PEER_MASK) { + if (header->saved_from_peer_ != nullptr) { DialogId dialog_id(header->saved_from_peer_); if (!is_acceptable_dialog(dialog_id)) { return false; @@ -428,12 +442,13 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ if (!is_acceptable_dialog(DialogId(message->peer_id_))) { return false; } - if (message->flags_ & MessagesManager::MESSAGE_FLAG_HAS_FROM_ID) { - if (!is_acceptable_dialog(DialogId(message->from_id_))) { - return false; - } + if (message->from_id_ != nullptr && !is_acceptable_dialog(DialogId(message->from_id_))) { + return false; } + if (!is_acceptable_message_reply_header(message->reply_to_)) { + return false; + } if (!is_acceptable_message_forward_header(message->fwd_from_)) { return false; } diff --git a/td/telegram/UpdatesManager.h b/td/telegram/UpdatesManager.h index 45c4978e3..8dab6c4e9 100644 --- a/td/telegram/UpdatesManager.h +++ b/td/telegram/UpdatesManager.h @@ -190,6 +190,9 @@ class UpdatesManager : public Actor { bool is_acceptable_message_entities(const vector> &message_entities) const; + bool is_acceptable_message_reply_header( + const telegram_api::object_ptr &header) const; + bool is_acceptable_message_forward_header( const telegram_api::object_ptr &header) const; From eb72b64c8004153020c8b25e555a29f65ef1549e Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 10 Sep 2020 16:29:10 +0300 Subject: [PATCH 04/10] Add reply_in_dialog_id and message_thread_id support. GitOrigin-RevId: 4b8a21566fbf6d31a6f4ef2b19f787361e2d1a1a --- td/generate/scheme/td_api.tl | 6 +- td/generate/scheme/td_api.tlo | Bin 181232 -> 181320 bytes td/telegram/MessagesManager.cpp | 128 +++++++++++++++++++++++--------- td/telegram/MessagesManager.h | 4 +- td/telegram/Td.cpp | 4 +- 5 files changed, 102 insertions(+), 40 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 7e0e62e21..f4d637468 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -676,7 +676,9 @@ messageSendingStateFailed error_code:int32 error_message:string can_retry:Bool r //@edit_date Point in time (Unix timestamp) when the message was last edited //@forward_info Information about the initial message sender; may be null //@interaction_info Information about interactions with the message; may be null +//@reply_in_chat_id If non-zero, the identifier of the chat to which the replied message belongs; Currently, only messages in the Replies chat can have different reply_in_chat_id and chat_id //@reply_to_message_id If non-zero, the identifier of the message this message is replying to; can be the identifier of a deleted message +//@message_thread_id If non-zero, the identifier of the message thread the message belongs to //@ttl For self-destructing messages, the message's TTL (Time To Live), in seconds; 0 if none. TDLib will send updateDeleteMessages or updateMessageContent once the TTL expires //@ttl_expires_in Time left before the message expires, in seconds //@via_bot_user_id If non-zero, the user identifier of the bot through which this message was sent @@ -685,7 +687,7 @@ messageSendingStateFailed error_code:int32 error_message:string can_retry:Bool r //@restriction_reason If non-empty, contains a human-readable description of the reason why access to this message must be restricted //@content Content of the message //@reply_markup Reply markup for the message; may be null -message id:int53 sender_user_id:int32 sender_chat_id:int53 chat_id:int53 sending_state:MessageSendingState scheduling_state:MessageSchedulingState is_outgoing:Bool can_be_edited:Bool can_be_forwarded:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_get_statistics:Bool can_get_replies:Bool is_channel_post:Bool contains_unread_mention:Bool date:int32 edit_date:int32 forward_info:messageForwardInfo interaction_info:messageInteractionInfo reply_to_message_id:int53 ttl:int32 ttl_expires_in:double via_bot_user_id:int32 author_signature:string media_album_id:int64 restriction_reason:string content:MessageContent reply_markup:ReplyMarkup = Message; +message id:int53 sender_user_id:int32 sender_chat_id:int53 chat_id:int53 sending_state:MessageSendingState scheduling_state:MessageSchedulingState is_outgoing:Bool can_be_edited:Bool can_be_forwarded:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_get_statistics:Bool can_get_replies:Bool is_channel_post:Bool contains_unread_mention:Bool date:int32 edit_date:int32 forward_info:messageForwardInfo interaction_info:messageInteractionInfo reply_in_chat_id:int53 reply_to_message_id:int53 message_thread_id:int53 ttl:int32 ttl_expires_in:double via_bot_user_id:int32 author_signature:string media_album_id:int64 restriction_reason:string content:MessageContent reply_markup:ReplyMarkup = Message; //@description Contains a list of messages @total_count Approximate total count of messages found @messages List of messages; messages may be null messages total_count:int32 messages:vector = Messages; @@ -2537,7 +2539,7 @@ notificationTypeNewSecretChat = NotificationType; notificationTypeNewCall call_id:int32 = NotificationType; //@description New message was received through a push notification -//@message_id The message identifier. The message will not be available in the chat history, but the ID can be used in viewMessages and as reply_to_message_id +//@message_id The message identifier. The message will not be available in the chat history, but the ID can be used in viewMessages, or as reply_to_message_id //@sender_user_id Sender of the message; 0 if unknown. Corresponding user may be inaccessible //@sender_chat_id Sender chat of the message; 0 if none //@sender_name Name of the sender; can be different from the name of the sender user diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 56c6c485a530576015ec7e8d73879e65ac3660a8..3125b2ebe648cadc77c9208b1839c148b87d8fb7 100644 GIT binary patch delta 714 zcmey+&V8bTdqajK%X7K!GdAZ)&aj2BIKn-+!K}$4NeZkWNyf>Isq&L&B-tSGexUFo zl9A*#?@0dO2{CKU+KMPqkP+;;sl~;K>8T+0&0|77a+4Fc2`~5IU}WYLC`v8Jsf^Fe zo6f7us673`R3@G2FWQ+jCSQnPna(qjQD*xTZblhKuz}mR@G#0qftk}|5}8b<+e9!4 zY?n}HtYC%+UT|en*uF)Jk%I%G{(uooXT(?9NIl%JlnkMRsdsm*@I7C6gb`ho+D ztlNL?XKZH!3r^+;cbNWTFQdfvJI5G%oFNKxxR{=RS=&RnnU1JJ1V2PD1x$Zn%%n1X zf+3RzL~IY7g^*pK$D{+1?J;6fLDM_=z;O$RP>M0kkO{_2H4rfY6Q(P;G;E(?%JhX7 zB0IsEDFDpc{=}In1rlV_H@GqxKm;3HnI;HAR2rx;g+N$2;Y>f!#I_%ZU}}M+f$4CC z+ZUuT$v{1DU;o0B{|g8;?fWV1ue#JUW0~~ zsKG!G`WmE!B!U_Y8VVX+0tbTzp@#IkDz%++&-u>1-@Om}N^Vby`8%u59={Xujn2rZ zMD%_SdVFfZtVn9iuUM;#4ls6ny+&lk{fFiMvBlLtTKsx7H_g$}%fNDbF`{Efrl5n~Z^6xRg<7J9Vd1Bladw^_qei)Uyk;e;V5*-L-Hrj$ zhp~@on;i!$!YQMSy{zg^jB+jEDErb$n4@6961G5TZ2t(pltRi6M9zs{7Fjf!wl(9# zs~Wn_e&L?LHC_zO^`K3<#OovmnT3z`JCiNCKjPX%%Y4bjzX~zS|t-NA?z#7@pFV(?wJjv%1AEJMR>vP3}LLX(xrx)9q6Ht NbL3lzzy+qee*x2 0; bool has_reply_info = !reply_info.is_empty(); bool has_sender_dialog_id = sender_dialog_id.is_valid(); + bool has_reply_in_dialog_id = is_reply && reply_in_dialog_id.is_valid(); + bool has_top_reply_message_id = is_reply && top_reply_message_id.is_valid(); BEGIN_STORE_FLAGS(); STORE_FLAG(is_channel_post); STORE_FLAG(is_outgoing); @@ -4244,6 +4246,8 @@ void MessagesManager::Message::store(StorerT &storer) const { STORE_FLAG(has_forward_count); STORE_FLAG(has_reply_info); STORE_FLAG(has_sender_dialog_id); + STORE_FLAG(has_reply_in_dialog_id); + STORE_FLAG(has_top_reply_message_id); END_STORE_FLAGS(); } @@ -4331,6 +4335,12 @@ void MessagesManager::Message::store(StorerT &storer) const { if (has_sender_dialog_id) { store(sender_dialog_id, storer); } + if (has_reply_in_dialog_id) { + store(reply_in_dialog_id, storer); + } + if (has_top_reply_message_id) { + store(top_reply_message_id, storer); + } store_message_content(content.get(), storer); if (has_reply_markup) { store(reply_markup, storer); @@ -4367,6 +4377,8 @@ void MessagesManager::Message::parse(ParserT &parser) { bool has_forward_count = false; bool has_reply_info = false; bool has_sender_dialog_id = false; + bool has_reply_in_dialog_id = false; + bool has_top_reply_message_id = false; BEGIN_PARSE_FLAGS(); PARSE_FLAG(is_channel_post); PARSE_FLAG(is_outgoing); @@ -4418,6 +4430,8 @@ void MessagesManager::Message::parse(ParserT &parser) { PARSE_FLAG(has_forward_count); PARSE_FLAG(has_reply_info); PARSE_FLAG(has_sender_dialog_id); + PARSE_FLAG(has_reply_in_dialog_id); + PARSE_FLAG(has_top_reply_message_id); END_PARSE_FLAGS(); } @@ -4511,6 +4525,12 @@ void MessagesManager::Message::parse(ParserT &parser) { if (has_sender_dialog_id) { parse(sender_dialog_id, parser); } + if (has_reply_in_dialog_id) { + parse(reply_in_dialog_id, parser); + } + if (has_top_reply_message_id) { + parse(top_reply_message_id, parser); + } parse_message_content(content, parser); if (has_reply_markup) { parse(reply_markup, parser); @@ -11017,6 +11037,8 @@ void MessagesManager::on_message_ttl_expired_impl(Dialog *d, Message *m) { update_message_contains_unread_mention(d, m, false, "on_message_ttl_expired_impl"); m->contains_mention = false; m->reply_to_message_id = MessageId(); + m->reply_in_dialog_id = DialogId(); + m->top_reply_message_id = MessageId(); m->is_content_secret = false; } @@ -12234,6 +12256,8 @@ std::pair> MessagesManager::creat message->random_id = message_info.random_id; message->forward_info = get_message_forward_info(std::move(message_info.forward_header)); message->reply_to_message_id = reply_to_message_id; + message->reply_in_dialog_id = reply_in_dialog_id; + message->top_reply_message_id = top_reply_message_id; message->via_bot_user_id = via_bot_user_id; message->restriction_reasons = std::move(message_info.restriction_reasons); message->author_signature = std::move(message_info.author_signature); @@ -12267,6 +12291,8 @@ std::pair> MessagesManager::creat message->reply_markup = nullptr; } message->reply_to_message_id = MessageId(); + message->reply_in_dialog_id = DialogId(); + message->top_reply_message_id = MessageId(); } if (message_info.media_album_id != 0) { @@ -15206,13 +15232,16 @@ MessagesManager::Message *MessagesManager::get_message_force(FullMessageId full_ return get_message_force(d, full_message_id.get_message_id(), source); } -MessageId MessagesManager::get_replied_message_id(const Message *m) { +FullMessageId MessagesManager::get_replied_message_id(DialogId dialog_id, const Message *m) { auto message_id = get_message_content_replied_message_id(m->content.get()); if (message_id.is_valid()) { CHECK(!m->reply_to_message_id.is_valid()); - return message_id; + return {dialog_id, message_id}; } - return m->reply_to_message_id; + if (!m->reply_to_message_id.is_valid()) { + return {}; + } + return {m->reply_in_dialog_id.is_valid() ? m->reply_in_dialog_id : dialog_id, m->reply_to_message_id}; } void MessagesManager::get_message_force_from_server(Dialog *d, MessageId message_id, Promise &&promise, @@ -15266,13 +15295,13 @@ void MessagesManager::get_message(FullMessageId full_message_id, Promise & get_message_force_from_server(d, full_message_id.get_message_id(), std::move(promise)); } -MessageId MessagesManager::get_replied_message(DialogId dialog_id, MessageId message_id, bool force, - Promise &&promise) { +FullMessageId MessagesManager::get_replied_message(DialogId dialog_id, MessageId message_id, bool force, + Promise &&promise) { LOG(INFO) << "Get replied message to " << message_id << " in " << dialog_id; Dialog *d = get_dialog_force(dialog_id); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); - return MessageId(); + return FullMessageId(); } auto m = get_message_force(d, message_id, "get_replied_message"); @@ -15282,15 +15311,22 @@ MessageId MessagesManager::get_replied_message(DialogId dialog_id, MessageId mes } else { get_message_force_from_server(d, message_id, std::move(promise)); } - return MessageId(); + return FullMessageId(); } tl_object_ptr input_message; if (m->message_id.is_valid() && m->message_id.is_server()) { input_message = make_tl_object(m->message_id.get_server_message_id().get()); } - auto replied_message_id = get_replied_message_id(m); - get_message_force_from_server(d, replied_message_id, std::move(promise), std::move(input_message)); + auto replied_message_id = get_replied_message_id(dialog_id, m); + if (replied_message_id.get_dialog_id() != dialog_id) { + d = get_dialog_force(replied_message_id.get_dialog_id()); + if (d == nullptr) { + promise.set_error(Status::Error(6, "Chat with replied message not found")); + return FullMessageId(); + } + } + get_message_force_from_server(d, replied_message_id.get_message_id(), std::move(promise), std::move(input_message)); return replied_message_id; } @@ -20334,7 +20370,9 @@ tl_object_ptr MessagesManager::get_message_object(DialogId dial bool can_get_replies = for_event_log || is_scheduled ? false : !m->reply_info.is_empty() && m->message_id.is_server(); auto via_bot_user_id = td_->contacts_manager_->get_user_id_object(m->via_bot_user_id, "via_bot_user_id"); auto media_album_id = for_event_log ? static_cast(0) : m->media_album_id; + auto reply_in_dialog_id = m->reply_in_dialog_id.is_valid() ? m->reply_in_dialog_id : dialog_id; auto reply_to_message_id = for_event_log ? static_cast(0) : m->reply_to_message_id.get(); + auto top_reply_message_id = for_event_log || is_scheduled ? static_cast(0) : m->top_reply_message_id.get(); bool contains_unread_mention = for_event_log ? false : m->contains_unread_mention; auto live_location_date = m->is_failed_to_send ? 0 : m->date; auto date = is_scheduled ? 0 : m->date; @@ -20345,8 +20383,8 @@ tl_object_ptr MessagesManager::get_message_object(DialogId dial can_be_edited, can_be_forwarded, can_delete_for_self, can_delete_for_all_users, can_get_statistics, can_get_replies, m->is_channel_post, contains_unread_mention, date, edit_date, get_message_forward_info_object(m->forward_info), get_message_interaction_info_object(dialog_id, m), - reply_to_message_id, ttl, ttl_expires_in, via_bot_user_id, m->author_signature, media_album_id, - get_restriction_reason_description(m->restriction_reasons), + reply_in_dialog_id.get(), reply_to_message_id, top_reply_message_id, ttl, ttl_expires_in, via_bot_user_id, + m->author_signature, media_album_id, get_restriction_reason_description(m->restriction_reasons), get_message_content_object(m->content.get(), td_, live_location_date, m->is_content_secret), get_reply_markup_object(m->reply_markup)); } @@ -20886,6 +20924,13 @@ void MessagesManager::add_message_dependencies(Dependencies &dependencies, Dialo if (m->sender_dialog_id.is_valid() && dependencies.dialog_ids.insert(m->sender_dialog_id).second) { add_dialog_dependencies(dependencies, m->sender_dialog_id); } + if (m->reply_in_dialog_id.is_valid() && dependencies.dialog_ids.insert(m->reply_in_dialog_id).second) { + add_dialog_dependencies(dependencies, m->reply_in_dialog_id); + } + if (m->real_forward_from_dialog_id.is_valid() && + dependencies.dialog_ids.insert(m->real_forward_from_dialog_id).second) { + add_dialog_dependencies(dependencies, m->real_forward_from_dialog_id); + } dependencies.user_ids.insert(m->via_bot_user_id); if (m->forward_info != nullptr) { dependencies.user_ids.insert(m->forward_info->sender_user_id); @@ -28872,7 +28917,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } if (d->deleted_message_ids.count(message->reply_to_message_id)) { // LOG(INFO) << "Remove reply to deleted " << message->reply_to_message_id << " in " << message_id << " from " << dialog_id << " from " << source; - // we don't want to lose information that the message was a reply for now + // we don't want to lose information that the message was a reply // message->reply_to_message_id = MessageId(); } @@ -30158,7 +30203,30 @@ bool MessagesManager::update_message(Dialog *d, Message *old_message, unique_ptr << old_message->reply_to_message_id << " to " << new_message->reply_to_message_id << ", message content type is " << old_message->content->get_type() << '/' << new_message->content->get_type(); - dump_debug_message_op(d); + } + } + if (old_message->reply_in_dialog_id != new_message->reply_in_dialog_id) { + if (new_message->reply_in_dialog_id == DialogId() || replace_legacy) { + LOG(DEBUG) << "Drop message reply_in_dialog_id"; + old_message->reply_in_dialog_id = DialogId(); + need_send_update = true; + } else if (is_new_available && old_message->reply_in_dialog_id.is_valid()) { + LOG(ERROR) << message_id << " in " << dialog_id << " has changed dialog it is reply in from " + << old_message->reply_in_dialog_id << " to " << new_message->reply_in_dialog_id + << ", message content type is " << old_message->content->get_type() << '/' + << new_message->content->get_type(); + } + } + if (old_message->top_reply_message_id != new_message->top_reply_message_id) { + if (new_message->top_reply_message_id == MessageId()) { + LOG(DEBUG) << "Drop message top_reply_message_id"; + old_message->reply_to_message_id = MessageId(); + need_send_update = true; + } else if (is_new_available && old_message->top_reply_message_id.is_valid()) { + LOG(ERROR) << message_id << " in " << dialog_id << " has changed top message it is reply to from " + << old_message->top_reply_message_id << " to " << new_message->top_reply_message_id + << ", message content type is " << old_message->content->get_type() << '/' + << new_message->content->get_type(); } } if (old_message->via_bot_user_id != new_message->via_bot_user_id) { @@ -30882,29 +30950,21 @@ void MessagesManager::fix_new_dialog(Dialog *d, unique_ptr &&last_datab if (last_database_message != nullptr) { int32 dependent_dialog_count = 0; + auto depend_on_dialog = [&](DialogId other_dialog_id) { + if (other_dialog_id.is_valid() && !have_dialog(other_dialog_id)) { + LOG(INFO) << "Postpone adding of last message in " << dialog_id << " because of cyclic dependency with " + << other_dialog_id; + pending_add_dialog_last_database_message_dependent_dialogs_[other_dialog_id].push_back(dialog_id); + dependent_dialog_count++; + } + }; if (last_database_message->forward_info != nullptr) { - auto other_dialog_id = last_database_message->forward_info->sender_dialog_id; - if (other_dialog_id.is_valid() && !have_dialog(other_dialog_id)) { - LOG(INFO) << "Postpone adding of last message in " << dialog_id << " because of cyclic dependency with " - << other_dialog_id; - pending_add_dialog_last_database_message_dependent_dialogs_[other_dialog_id].push_back(dialog_id); - dependent_dialog_count++; - } - other_dialog_id = last_database_message->forward_info->from_dialog_id; - if (other_dialog_id.is_valid() && !have_dialog(other_dialog_id)) { - LOG(INFO) << "Postpone adding of last message in " << dialog_id << " because of cyclic dependency with " - << other_dialog_id; - pending_add_dialog_last_database_message_dependent_dialogs_[other_dialog_id].push_back(dialog_id); - dependent_dialog_count++; - } - other_dialog_id = last_database_message->sender_dialog_id; - if (other_dialog_id.is_valid() && !have_dialog(other_dialog_id)) { - LOG(INFO) << "Postpone adding of last message in " << dialog_id << " because of cyclic dependency with " - << other_dialog_id; - pending_add_dialog_last_database_message_dependent_dialogs_[other_dialog_id].push_back(dialog_id); - dependent_dialog_count++; - } + depend_on_dialog(last_database_message->forward_info->sender_dialog_id); + depend_on_dialog(last_database_message->forward_info->from_dialog_id); } + depend_on_dialog(last_database_message->sender_dialog_id); + depend_on_dialog(last_database_message->reply_in_dialog_id); + depend_on_dialog(last_database_message->real_forward_from_dialog_id); if (dependent_dialog_count == 0) { add_dialog_last_database_message(d, std::move(last_database_message)); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 3b8afda6c..10e85d40e 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -549,7 +549,7 @@ class MessagesManager : public Actor { void get_message(FullMessageId full_message_id, Promise &&promise); - MessageId get_replied_message(DialogId dialog_id, MessageId message_id, bool force, Promise &&promise); + FullMessageId get_replied_message(DialogId dialog_id, MessageId message_id, bool force, Promise &&promise); MessageId get_dialog_pinned_message(DialogId dialog_id, Promise &&promise); @@ -1718,7 +1718,7 @@ class MessagesManager : public Actor { MessageId get_persistent_message_id(const Dialog *d, MessageId message_id) const; - static MessageId get_replied_message_id(const Message *m); + static FullMessageId get_replied_message_id(DialogId dialog_id, const Message *m); MessageId get_reply_to_message_id(Dialog *d, MessageId message_id); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index a8a59d7d2..c74e8b607 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -1032,7 +1032,7 @@ class GetRepliedMessageRequest : public RequestOnceActor { DialogId dialog_id_; MessageId message_id_; - MessageId replied_message_id_; + FullMessageId replied_message_id_; void do_run(Promise &&promise) override { replied_message_id_ = @@ -1040,7 +1040,7 @@ class GetRepliedMessageRequest : public RequestOnceActor { } void do_send_result() override { - send_result(td->messages_manager_->get_message_object({dialog_id_, replied_message_id_})); + send_result(td->messages_manager_->get_message_object(replied_message_id_)); } public: From 2c0a9367ac078aecc2472f6adad4e5d865bd15fd Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 10 Sep 2020 23:24:33 +0300 Subject: [PATCH 05/10] Simplify adding dialog dependencies. GitOrigin-RevId: 27c9fcc1221e60960f0183e9d792121ca5e8fb0a --- td/telegram/ContactsManager.cpp | 2 +- td/telegram/Dependencies.cpp | 27 ++++++++++++++++++++ td/telegram/Dependencies.h | 4 +++ td/telegram/MessagesManager.cpp | 44 ++++----------------------------- td/telegram/MessagesManager.h | 2 -- td/telegram/PollManager.cpp | 4 +-- 6 files changed, 39 insertions(+), 44 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 94fe60545..a477b00c5 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -8764,7 +8764,7 @@ void ContactsManager::on_load_channel_full_from_database(ChannelId channel_id, s Dependencies dependencies; dependencies.channel_ids.insert(channel_id); - MessagesManager::add_dialog_dependencies(dependencies, DialogId(channel_full->linked_channel_id)); + add_dialog_and_dependencies(dependencies, DialogId(channel_full->linked_channel_id)); dependencies.chat_ids.insert(channel_full->migrated_from_chat_id); dependencies.user_ids.insert(channel_full->bot_user_ids.begin(), channel_full->bot_user_ids.end()); resolve_dependencies_force(td_, dependencies); diff --git a/td/telegram/Dependencies.cpp b/td/telegram/Dependencies.cpp index d4fc94660..f2be0288d 100644 --- a/td/telegram/Dependencies.cpp +++ b/td/telegram/Dependencies.cpp @@ -15,6 +15,33 @@ namespace td { +void add_dialog_and_dependencies(Dependencies &dependencies, DialogId dialog_id) { + if (dialog_id.is_valid() && dependencies.dialog_ids.insert(dialog_id).second) { + add_dialog_dependencies(dependencies, dialog_id); + } +} + +void add_dialog_dependencies(Dependencies &dependencies, DialogId dialog_id) { + switch (dialog_id.get_type()) { + case DialogType::User: + dependencies.user_ids.insert(dialog_id.get_user_id()); + break; + case DialogType::Chat: + dependencies.chat_ids.insert(dialog_id.get_chat_id()); + break; + case DialogType::Channel: + dependencies.channel_ids.insert(dialog_id.get_channel_id()); + break; + case DialogType::SecretChat: + dependencies.secret_chat_ids.insert(dialog_id.get_secret_chat_id()); + break; + case DialogType::None: + break; + default: + UNREACHABLE(); + } +} + void resolve_dependencies_force(Td *td, const Dependencies &dependencies) { for (auto user_id : dependencies.user_ids) { if (user_id.is_valid() && !td->contacts_manager_->have_user_force(user_id)) { diff --git a/td/telegram/Dependencies.h b/td/telegram/Dependencies.h index 50760d314..5394572a3 100644 --- a/td/telegram/Dependencies.h +++ b/td/telegram/Dependencies.h @@ -28,6 +28,10 @@ struct Dependencies { std::unordered_set web_page_ids; }; +void add_dialog_and_dependencies(Dependencies &dependencies, DialogId dialog_id); + +void add_dialog_dependencies(Dependencies &dependencies, DialogId dialog_id); + void resolve_dependencies_force(Td *td, const Dependencies &dependencies); } // namespace td diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 91d00c33a..690d46426 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -20921,27 +20921,14 @@ bool MessagesManager::is_message_auto_read(DialogId dialog_id, bool is_outgoing) void MessagesManager::add_message_dependencies(Dependencies &dependencies, DialogId dialog_id, const Message *m) { dependencies.user_ids.insert(m->sender_user_id); - if (m->sender_dialog_id.is_valid() && dependencies.dialog_ids.insert(m->sender_dialog_id).second) { - add_dialog_dependencies(dependencies, m->sender_dialog_id); - } - if (m->reply_in_dialog_id.is_valid() && dependencies.dialog_ids.insert(m->reply_in_dialog_id).second) { - add_dialog_dependencies(dependencies, m->reply_in_dialog_id); - } - if (m->real_forward_from_dialog_id.is_valid() && - dependencies.dialog_ids.insert(m->real_forward_from_dialog_id).second) { - add_dialog_dependencies(dependencies, m->real_forward_from_dialog_id); - } + add_dialog_and_dependencies(dependencies, m->sender_dialog_id); + add_dialog_and_dependencies(dependencies, m->reply_in_dialog_id); + add_dialog_and_dependencies(dependencies, m->real_forward_from_dialog_id); dependencies.user_ids.insert(m->via_bot_user_id); if (m->forward_info != nullptr) { dependencies.user_ids.insert(m->forward_info->sender_user_id); - if (m->forward_info->sender_dialog_id.is_valid() && - dependencies.dialog_ids.insert(m->forward_info->sender_dialog_id).second) { - add_dialog_dependencies(dependencies, m->forward_info->sender_dialog_id); - } - if (m->forward_info->from_dialog_id.is_valid() && - dependencies.dialog_ids.insert(m->forward_info->from_dialog_id).second) { - add_dialog_dependencies(dependencies, m->forward_info->from_dialog_id); - } + add_dialog_and_dependencies(dependencies, m->forward_info->sender_dialog_id); + add_dialog_and_dependencies(dependencies, m->forward_info->from_dialog_id); } for (auto recent_replier_dialog_id : m->reply_info.recent_replier_dialog_ids) { if (dialog_id.get_type() == DialogType::User) { @@ -20951,27 +20938,6 @@ void MessagesManager::add_message_dependencies(Dependencies &dependencies, Dialo add_message_content_dependencies(dependencies, m->content.get()); } -void MessagesManager::add_dialog_dependencies(Dependencies &dependencies, DialogId dialog_id) { - switch (dialog_id.get_type()) { - case DialogType::User: - dependencies.user_ids.insert(dialog_id.get_user_id()); - break; - case DialogType::Chat: - dependencies.chat_ids.insert(dialog_id.get_chat_id()); - break; - case DialogType::Channel: - dependencies.channel_ids.insert(dialog_id.get_channel_id()); - break; - case DialogType::SecretChat: - dependencies.secret_chat_ids.insert(dialog_id.get_secret_chat_id()); - break; - case DialogType::None: - break; - default: - UNREACHABLE(); - } -} - class MessagesManager::SendMessageLogEvent { public: DialogId dialog_id; diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 10e85d40e..14c67d5a3 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -885,8 +885,6 @@ class MessagesManager : public Actor { void get_current_state(vector> &updates) const; - static void add_dialog_dependencies(Dependencies &dependencies, DialogId dialog_id); - ActorOwn sequence_dispatcher_; private: diff --git a/td/telegram/PollManager.cpp b/td/telegram/PollManager.cpp index c1392d734..775ff8e3f 100644 --- a/td/telegram/PollManager.cpp +++ b/td/telegram/PollManager.cpp @@ -1652,7 +1652,7 @@ void PollManager::on_binlog_events(vector &&events) { auto dialog_id = log_event.full_message_id_.get_dialog_id(); Dependencies dependencies; - MessagesManager::add_dialog_dependencies(dependencies, dialog_id); + add_dialog_dependencies(dependencies, dialog_id); // do not load the dialog itself resolve_dependencies_force(td_, dependencies); do_set_poll_answer(log_event.poll_id_, log_event.full_message_id_, std::move(log_event.options_), event.id_, @@ -1671,7 +1671,7 @@ void PollManager::on_binlog_events(vector &&events) { auto dialog_id = log_event.full_message_id_.get_dialog_id(); Dependencies dependencies; - MessagesManager::add_dialog_dependencies(dependencies, dialog_id); + add_dialog_dependencies(dependencies, dialog_id); // do not load the dialog itself resolve_dependencies_force(td_, dependencies); do_stop_poll(log_event.poll_id_, log_event.full_message_id_, nullptr, event.id_, Auto()); From 01f3eecd85f9b88c57f400c8a46d7fcd1d7cb17b Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 11 Sep 2020 01:49:15 +0300 Subject: [PATCH 06/10] Add support for is_anonymous administrator right. GitOrigin-RevId: d0d46d961aebcc67275bb09ec883f6feaa465c63 --- td/generate/scheme/td_api.tl | 6 ++- td/generate/scheme/td_api.tlo | Bin 181320 -> 181400 bytes td/telegram/ContactsManager.cpp | 16 ++++---- td/telegram/DialogParticipant.cpp | 61 +++++++++++++++++++----------- td/telegram/DialogParticipant.h | 9 ++++- td/telegram/Td.cpp | 3 +- td/telegram/cli.cpp | 25 +++++++----- 7 files changed, 74 insertions(+), 46 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index f4d637468..a7a6147be 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -441,11 +441,13 @@ chatPermissions can_send_messages:Bool can_send_media_messages:Bool can_send_pol //@class ChatMemberStatus @description Provides information about the status of a member in a chat //@description The user is the owner of a chat and has all the administrator privileges +//@is_anonymous True, if the creator isn't shown in the chat member list and sends messages anonymously //@custom_title A custom title of the owner; 0-16 characters without emojis; applicable to supergroups only //@is_member True, if the user is a member of the chat -chatMemberStatusCreator custom_title:string is_member:Bool = ChatMemberStatus; +chatMemberStatusCreator is_anonymous:Bool custom_title:string is_member:Bool = ChatMemberStatus; //@description The user is a member of a chat and has some additional privileges. In basic groups, administrators can edit and delete messages sent by others, add new members, and ban unprivileged members. In supergroups and channels, there are more detailed options for administrator privileges +//@is_anonymous True, if the administrator isn't shown in the chat member list and sends messages anonymously //@custom_title A custom title of the administrator; 0-16 characters without emojis; applicable to supergroups only //@can_be_edited True, if the current user can edit the administrator privileges for the called user //@can_change_info True, if the administrator can change the chat title, photo, and other settings @@ -456,7 +458,7 @@ chatMemberStatusCreator custom_title:string is_member:Bool = ChatMemberStatus; //@can_restrict_members True, if the administrator can restrict, ban, or unban chat members //@can_pin_messages True, if the administrator can pin messages; applicable to groups only //@can_promote_members True, if the administrator can add new administrators with a subset of their own privileges or demote administrators that were directly or indirectly promoted by them -chatMemberStatusAdministrator custom_title:string can_be_edited:Bool can_change_info:Bool can_post_messages:Bool can_edit_messages:Bool can_delete_messages:Bool can_invite_users:Bool can_restrict_members:Bool can_pin_messages:Bool can_promote_members:Bool = ChatMemberStatus; +chatMemberStatusAdministrator is_anonymous:Bool custom_title:string can_be_edited:Bool can_change_info:Bool can_post_messages:Bool can_edit_messages:Bool can_delete_messages:Bool can_invite_users:Bool can_restrict_members:Bool can_pin_messages:Bool can_promote_members:Bool = ChatMemberStatus; //@description The user is a member of a chat, without any additional privileges or restrictions chatMemberStatusMember = ChatMemberStatus; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 3125b2ebe648cadc77c9208b1839c148b87d8fb7..72a96bc836e16a1a0c7b7073eae01376c76577a4 100644 GIT binary patch delta 336 zcmX@n!9Amsd&3P5mKXYcahvaOEU|{LT3iAYz^us)sS1;Sq$EH%F{yCT77P)E%|2-| z0ubSxG8a{|?b&7G$r*_yzNxuMsYSshi6y1Q&PAz-CHX}l>uw$s@?oA{nZc+(xg$Yi zdO|59%Vdsn6%mje0~B19uw$s@?o0Xn4mv>Ln$N6 zWP@@Q5s)wg6kL_#in*l`oIN?CT!9m&o(rUY^NaExMuiFNsCrOk17(participant_ptr); new_creator_user_id = UserId(participant->user_id_); dialog_participant = {new_creator_user_id, new_creator_user_id, c->date, - DialogParticipantStatus::Creator(true, string())}; + DialogParticipantStatus::Creator(true, false, string())}; break; } case telegram_api::chatParticipantAdmin::ID: { @@ -11567,7 +11567,7 @@ void ContactsManager::on_update_chat_add_user(ChatId chat_id, UserId inviter_use } chat_full->participants.push_back(DialogParticipant{user_id, inviter_user_id, date, user_id == chat_full->creator_user_id - ? DialogParticipantStatus::Creator(true, string()) + ? DialogParticipantStatus::Creator(true, false, string()) : DialogParticipantStatus::Member()}); update_chat_online_member_count(chat_full, chat_id, false); chat_full->is_changed = true; @@ -13703,7 +13703,7 @@ void ContactsManager::on_chat_update(telegram_api::chat &chat, const char *sourc } if (is_creator) { - return DialogParticipantStatus::Creator(!has_left, string()); + return DialogParticipantStatus::Creator(!has_left, false, string()); } else if (chat.admin_rights_ != nullptr) { return get_dialog_participant_status(false, std::move(chat.admin_rights_), string()); } else if (was_kicked) { @@ -13864,7 +13864,7 @@ void ContactsManager::on_chat_update(telegram_api::channel &channel, const char bool is_creator = (channel.flags_ & CHANNEL_FLAG_USER_IS_CREATOR) != 0; if (is_creator) { - return DialogParticipantStatus::Creator(!has_left, string()); + return DialogParticipantStatus::Creator(!has_left, false, string()); } else if (channel.admin_rights_ != nullptr) { return get_dialog_participant_status(false, std::move(channel.admin_rights_), string()); } else if (channel.banned_rights_ != nullptr) { diff --git a/td/telegram/DialogParticipant.cpp b/td/telegram/DialogParticipant.cpp index fc46bce06..9bc2d97ca 100644 --- a/td/telegram/DialogParticipant.cpp +++ b/td/telegram/DialogParticipant.cpp @@ -27,17 +27,18 @@ int32 DialogParticipantStatus::fix_until_date(int32 date) { return date; } -DialogParticipantStatus DialogParticipantStatus::Creator(bool is_member, string rank) { +DialogParticipantStatus DialogParticipantStatus::Creator(bool is_member, bool is_anonymous, string rank) { return DialogParticipantStatus(Type::Creator, - ALL_ADMINISTRATOR_RIGHTS | ALL_PERMISSION_RIGHTS | (is_member ? IS_MEMBER : 0), 0, - std::move(rank)); + ALL_ADMINISTRATOR_RIGHTS | ALL_PERMISSION_RIGHTS | (is_member ? IS_MEMBER : 0) | + (is_anonymous ? IS_ANONYMOUS : 0), + 0, std::move(rank)); } -DialogParticipantStatus DialogParticipantStatus::Administrator(string rank, bool can_be_edited, bool can_change_info, - bool can_post_messages, bool can_edit_messages, - bool can_delete_messages, bool can_invite_users, - bool can_restrict_members, bool can_pin_messages, - bool can_promote_members) { +DialogParticipantStatus DialogParticipantStatus::Administrator(bool is_anonymous, string rank, bool can_be_edited, + bool can_change_info, bool can_post_messages, + bool can_edit_messages, bool can_delete_messages, + bool can_invite_users, bool can_restrict_members, + bool can_pin_messages, bool can_promote_members) { uint32 flags = (static_cast(can_be_edited) * CAN_BE_EDITED) | (static_cast(can_change_info) * CAN_CHANGE_INFO_AND_SETTINGS_ADMIN) | (static_cast(can_post_messages) * CAN_POST_MESSAGES) | @@ -50,7 +51,9 @@ DialogParticipantStatus DialogParticipantStatus::Administrator(string rank, bool if (flags == 0 || flags == CAN_BE_EDITED) { return Member(); } - return DialogParticipantStatus(Type::Administrator, IS_MEMBER | ALL_RESTRICTED_RIGHTS | flags, 0, std::move(rank)); + return DialogParticipantStatus(Type::Administrator, + IS_MEMBER | ALL_RESTRICTED_RIGHTS | flags | (is_anonymous ? IS_ANONYMOUS : 0), 0, + std::move(rank)); } DialogParticipantStatus DialogParticipantStatus::Member() { @@ -88,14 +91,14 @@ DialogParticipantStatus DialogParticipantStatus::Banned(int32 banned_until_date) } DialogParticipantStatus DialogParticipantStatus::GroupAdministrator(bool is_creator) { - return Administrator(string(), is_creator, true, false, false, true, true, true, true, false); + return Administrator(false, string(), is_creator, true, false, false, true, true, true, true, false); } DialogParticipantStatus DialogParticipantStatus::ChannelAdministrator(bool is_creator, bool is_megagroup) { if (is_megagroup) { - return Administrator(string(), is_creator, true, false, false, true, true, true, true, false); + return Administrator(false, string(), is_creator, true, false, false, true, true, true, true, false); } else { - return Administrator(string(), is_creator, false, true, true, true, false, true, false, false); + return Administrator(false, string(), is_creator, false, true, true, true, false, true, false, false); } } @@ -108,11 +111,12 @@ RestrictedRights DialogParticipantStatus::get_restricted_rights() const { tl_object_ptr DialogParticipantStatus::get_chat_member_status_object() const { switch (type_) { case Type::Creator: - return td_api::make_object(rank_, is_member()); + return td_api::make_object(is_anonymous(), rank_, is_member()); case Type::Administrator: return td_api::make_object( - rank_, can_be_edited(), can_change_info_and_settings(), can_post_messages(), can_edit_messages(), - can_delete_messages(), can_invite_users(), can_restrict_members(), can_pin_messages(), can_promote_members()); + is_anonymous(), rank_, can_be_edited(), can_change_info_and_settings(), can_post_messages(), + can_edit_messages(), can_delete_messages(), can_invite_users(), can_restrict_members(), can_pin_messages(), + can_promote_members()); case Type::Member: return td_api::make_object(); case Type::Restricted: @@ -154,6 +158,9 @@ tl_object_ptr DialogParticipantStatus::get_chat_a if (can_promote_members()) { flags |= telegram_api::chatAdminRights::ADD_ADMINS_MASK; } + if (is_anonymous()) { + flags |= telegram_api::chatAdminRights::ANONYMOUS_MASK; + } LOG(INFO) << "Create chat admin rights " << flags; return make_tl_object(flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, @@ -278,6 +285,9 @@ StringBuilder &operator<<(StringBuilder &string_builder, const DialogParticipant if (!status.rank_.empty()) { string_builder << " [" << status.rank_ << "]"; } + if (status.is_anonymous()) { + string_builder << "-anonymous"; + } return string_builder; case DialogParticipantStatus::Type::Administrator: string_builder << "Administrator: "; @@ -308,6 +318,9 @@ StringBuilder &operator<<(StringBuilder &string_builder, const DialogParticipant if (!status.rank_.empty()) { string_builder << " [" << status.rank_ << "]"; } + if (status.is_anonymous()) { + string_builder << "-anonymous"; + } return string_builder; case DialogParticipantStatus::Type::Member: return string_builder << "Member"; @@ -377,14 +390,14 @@ DialogParticipantStatus get_dialog_participant_status(const tl_object_ptr(status.get()); - return DialogParticipantStatus::Creator(st->is_member_, st->custom_title_); + return DialogParticipantStatus::Creator(st->is_member_, st->is_anonymous_, st->custom_title_); } case td_api::chatMemberStatusAdministrator::ID: { auto st = static_cast(status.get()); return DialogParticipantStatus::Administrator( - st->custom_title_, st->can_be_edited_, st->can_change_info_, st->can_post_messages_, st->can_edit_messages_, - st->can_delete_messages_, st->can_invite_users_, st->can_restrict_members_, st->can_pin_messages_, - st->can_promote_members_); + st->is_anonymous_, st->custom_title_, st->can_be_edited_, st->can_change_info_, st->can_post_messages_, + st->can_edit_messages_, st->can_delete_messages_, st->can_invite_users_, st->can_restrict_members_, + st->can_pin_messages_, st->can_promote_members_); } case td_api::chatMemberStatusMember::ID: return DialogParticipantStatus::Member(); @@ -425,9 +438,10 @@ DialogParticipantStatus get_dialog_participant_status(bool can_be_edited, bool can_restrict_members = (admin_rights->flags_ & telegram_api::chatAdminRights::BAN_USERS_MASK) != 0; bool can_pin_messages = (admin_rights->flags_ & telegram_api::chatAdminRights::PIN_MESSAGES_MASK) != 0; bool can_promote_members = (admin_rights->flags_ & telegram_api::chatAdminRights::ADD_ADMINS_MASK) != 0; - return DialogParticipantStatus::Administrator(std::move(rank), can_be_edited, can_change_info, can_post_messages, - can_edit_messages, can_delete_messages, can_invite_users, - can_restrict_members, can_pin_messages, can_promote_members); + bool is_anonymous = (admin_rights->flags_ & telegram_api::chatAdminRights::ANONYMOUS_MASK) != 0; + return DialogParticipantStatus::Administrator( + is_anonymous, std::move(rank), can_be_edited, can_change_info, can_post_messages, can_edit_messages, + can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages, can_promote_members); } DialogParticipantStatus get_dialog_participant_status( @@ -635,8 +649,9 @@ DialogParticipant::DialogParticipant(tl_object_ptr(participant_ptr); + bool is_anonymous = (participant->admin_rights_->flags_ & telegram_api::chatAdminRights::ANONYMOUS_MASK) != 0; *this = {UserId(participant->user_id_), UserId(), 0, - DialogParticipantStatus::Creator(true, std::move(participant->rank_))}; + DialogParticipantStatus::Creator(true, is_anonymous, std::move(participant->rank_))}; break; } case telegram_api::channelParticipantAdmin::ID: { diff --git a/td/telegram/DialogParticipant.h b/td/telegram/DialogParticipant.h index 981cf447c..d98730d24 100644 --- a/td/telegram/DialogParticipant.h +++ b/td/telegram/DialogParticipant.h @@ -135,6 +135,7 @@ class DialogParticipantStatus { static constexpr uint32 IS_MEMBER = 1 << 27; + static constexpr uint32 IS_ANONYMOUS = 1 << 13; static constexpr uint32 HAS_RANK = 1u << 14; // bits 28-30 reserved for Type static constexpr int TYPE_SHIFT = 28; @@ -165,9 +166,9 @@ class DialogParticipantStatus { DialogParticipantStatus(Type type, uint32 flags, int32 until_date, string rank); public: - static DialogParticipantStatus Creator(bool is_member, string rank); + static DialogParticipantStatus Creator(bool is_member, bool is_anonymous, string rank); - static DialogParticipantStatus Administrator(string rank, bool can_be_edited, bool can_change_info, + static DialogParticipantStatus Administrator(bool is_anonymous, string rank, bool can_be_edited, bool can_change_info, bool can_post_messages, bool can_edit_messages, bool can_delete_messages, bool can_invite_users, bool can_restrict_members, bool can_pin_messages, bool can_promote_members); @@ -308,6 +309,10 @@ class DialogParticipantStatus { return until_date_; } + bool is_anonymous() const { + return (flags_ & IS_ANONYMOUS) != 0; + } + const string &get_rank() const { return rank_; } diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index c74e8b607..2b06a3332 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6082,7 +6082,8 @@ void Td::on_request(uint64 id, const td_api::leaveChat &request) { return promise.set_value(Unit()); } - new_status = td_api::make_object(status.get_rank(), false); + new_status = + td_api::make_object(status.is_anonymous(), status.get_rank(), false); } } messages_manager_->set_dialog_participant_status(dialog_id, contacts_manager_->get_my_id(), std::move(new_status), diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 94d67d5a4..72582c342 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3717,21 +3717,26 @@ class CliClient final : public Actor { } else if (status_str == "banned") { status = td_api::make_object(std::numeric_limits::max()); } else if (status_str == "creator") { - status = td_api::make_object("", true); + status = td_api::make_object(false, "", true); + } else if (status_str == "creatoranon") { + status = td_api::make_object(true, "", true); } else if (status_str == "uncreator") { - status = td_api::make_object("", false); + status = td_api::make_object(false, "", false); + } else if (status_str == "anon") { + status = td_api::make_object(true, "anon", true, true, true, true, true, + true, true, true, true); } else if (status_str == "admin") { - status = td_api::make_object("", true, true, true, true, true, true, - true, true, true); + status = td_api::make_object(false, "", true, true, true, true, true, + true, true, true, true); } else if (status_str == "adminq") { - status = td_api::make_object("title", true, true, true, true, true, true, - true, true, true); + status = td_api::make_object(false, "title", true, true, true, true, + true, true, true, true, true); } else if (status_str == "minadmin") { - status = td_api::make_object("", true, true, false, false, false, false, - false, false, false); + status = td_api::make_object(false, "", true, true, false, false, false, + false, false, false, false); } else if (status_str == "unadmin") { - status = td_api::make_object("", true, false, false, false, false, false, - false, false, false); + status = td_api::make_object(false, "", true, false, false, false, false, + false, false, false, false); } else if (status_str == "rest") { status = td_api::make_object( true, static_cast(120 + std::time(nullptr)), From cd3bf3618b528741f9da0a7c2cc6595d7d380c08 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 11 Sep 2020 14:51:13 +0300 Subject: [PATCH 07/10] Show comment button only if the linked channel is still the same. GitOrigin-RevId: cc8f31a56e0de7ace7f8b1a756dd34e14e4a5457 --- td/telegram/ContactsManager.cpp | 8 ++++++ td/telegram/ContactsManager.h | 1 + td/telegram/MessagesManager.cpp | 44 +++++++++++++++++++++++++++++---- td/telegram/MessagesManager.h | 2 ++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index cf7f98084..ba801852c 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -13029,6 +13029,14 @@ bool ContactsManager::get_channel_has_linked_channel(const Channel *c) { return c->has_linked_channel; } +ChannelId ContactsManager::get_channel_linked_channel_id(ChannelId channel_id) { + auto channel_full = get_channel_full_force(channel_id, "get_channel_linked_channel_id"); + if (channel_full == nullptr) { + return ChannelId(); + } + return channel_full->linked_channel_id; +} + int32 ContactsManager::get_channel_slow_mode_delay(ChannelId channel_id) { auto channel_full = get_channel_full_force(channel_id, "get_channel_slow_mode_delay"); if (channel_full == nullptr) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 3c649d881..38c169f1d 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -493,6 +493,7 @@ class ContactsManager : public Actor { int32 get_channel_participant_count(ChannelId channel_id) const; bool get_channel_sign_messages(ChannelId channel_id) const; bool get_channel_has_linked_channel(ChannelId channel_id) const; + ChannelId get_channel_linked_channel_id(ChannelId channel_id); int32 get_channel_slow_mode_delay(ChannelId channel_id); std::pair> search_among_users(const vector &user_ids, const string &query, int32 limit); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 690d46426..10384129f 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -6140,9 +6140,38 @@ void MessagesManager::update_message_interaction_info(FullMessageId full_message } } +bool MessagesManager::is_active_message_reply_info(DialogId dialog_id, const MessageReplyInfo &info) const { + if (info.is_empty()) { + return false; + } + if (dialog_id.get_type() != DialogType::Channel) { + return false; + } + if (!info.is_comment) { + return true; + } + if (!is_broadcast_channel(dialog_id)) { + return true; + } + + auto channel_id = dialog_id.get_channel_id(); + if (!td_->contacts_manager_->get_channel_has_linked_channel(channel_id)) { + return false; + } + auto linked_channel_id = td_->contacts_manager_->get_channel_linked_channel_id(channel_id); + if (!linked_channel_id.is_valid()) { + // keep the comment button while linked channel is unknown + td_->contacts_manager_->get_channel_full(channel_id, true, Auto()); + return true; + } + + return linked_channel_id == info.channel_id; +} + td_api::object_ptr MessagesManager::get_message_interaction_info_object( DialogId dialog_id, const Message *m) const { - if (m->view_count == 0 && m->forward_count == 0 && m->reply_info.is_empty()) { + bool is_active_reply_info = is_active_message_reply_info(dialog_id, m->reply_info); + if (m->view_count == 0 && m->forward_count == 0 && !is_active_reply_info) { return nullptr; } if (m->message_id.is_scheduled() && (m->forward_info == nullptr || is_broadcast_channel(dialog_id))) { @@ -6152,14 +6181,19 @@ td_api::object_ptr MessagesManager::get_message_ return nullptr; } + int32 reply_count = -1; vector recent_replier_user_ids; - for (auto recent_replier_dialog_id : m->reply_info.recent_replier_dialog_ids) { - if (dialog_id.get_type() == DialogType::User) { - recent_replier_user_ids.push_back(recent_replier_dialog_id.get_user_id()); + if (is_active_reply_info) { + reply_count = m->reply_info.reply_count; + for (auto recent_replier_dialog_id : m->reply_info.recent_replier_dialog_ids) { + if (dialog_id.get_type() == DialogType::User) { + recent_replier_user_ids.push_back(recent_replier_dialog_id.get_user_id()); + } } } + return td_api::make_object( - m->view_count, m->forward_count, m->reply_info.reply_count, + m->view_count, m->forward_count, reply_count, td_->contacts_manager_->get_user_ids_object(recent_replier_user_ids, "get_message_interaction_info_object")); } diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 14c67d5a3..b31df7c69 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1858,6 +1858,8 @@ class MessagesManager : public Actor { void update_message_interaction_info(FullMessageId full_message_id, int32 view_count, int32 forward_count, bool has_reply_info, tl_object_ptr &&reply_info); + bool is_active_message_reply_info(DialogId dialog_id, const MessageReplyInfo &info) const; + td_api::object_ptr get_message_interaction_info_object(DialogId dialog_id, const Message *m) const; From 7df0c5084a99f335dddbaa255c15ac2ec7109dfa Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 11 Sep 2020 17:51:01 +0300 Subject: [PATCH 08/10] Fix updateShortMessage handling. GitOrigin-RevId: 35c667af5646654ed9f8495774043adb10b12faf --- td/telegram/MessagesManager.cpp | 10 +++++++--- td/telegram/Td.cpp | 4 +++- td/telegram/UpdatesManager.cpp | 25 +++++++------------------ td/telegram/cli.cpp | 10 ++++++++++ 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 10384129f..aca06dca0 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -12048,8 +12048,10 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message( auto message = move_tl_object_as(message_ptr); message_info.dialog_id = DialogId(message->peer_id_); - if (message->flags_ & MESSAGE_FLAG_HAS_FROM_ID) { + if (message->from_id_ != nullptr) { message_info.sender_dialog_id = DialogId(message->from_id_); + } else { + message_info.sender_dialog_id = message_info.dialog_id; } message_info.date = message->date_; message_info.forward_header = std::move(message->fwd_from_); @@ -12101,8 +12103,10 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message( auto message = move_tl_object_as(message_ptr); message_info.dialog_id = DialogId(message->peer_id_); - if (message->flags_ & MESSAGE_FLAG_HAS_FROM_ID) { + if (message->from_id_ != nullptr) { message_info.sender_dialog_id = DialogId(message->from_id_); + } else { + message_info.sender_dialog_id = message_info.dialog_id; } message_info.date = message->date_; message_info.flags = message->flags_; @@ -23193,7 +23197,7 @@ unique_ptr MessagesManager::get_message_for DialogId from_dialog_id; MessageId from_message_id; string sender_name; - if ((flags & telegram_api::messageFwdHeader::FROM_ID_MASK) != 0) { + if (forward_header->from_id_ != nullptr) { sender_dialog_id = DialogId(forward_header->from_id_); if (!sender_dialog_id.is_valid()) { LOG(ERROR) << "Receive invalid sender id in message forward header: " << oneline(to_string(forward_header)); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 2b06a3332..22aa960a0 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3778,7 +3778,9 @@ void Td::inc_actor_refcnt() { void Td::dec_actor_refcnt() { actor_refcnt_--; - LOG(DEBUG) << "Decrease reference count to " << actor_refcnt_; + if (actor_refcnt_ < 3) { + LOG(DEBUG) << "Decrease reference count to " << actor_refcnt_; + } if (actor_refcnt_ == 0) { if (close_flag_ == 2) { create_reference(); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 884689b47..1bbe3cd06 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -414,17 +414,11 @@ bool UpdatesManager::is_acceptable_message_forward_header( return true; } - if (header->from_id_ != nullptr) { - DialogId dialog_id(header->from_id_); - if (!is_acceptable_dialog(dialog_id)) { - return false; - } + if (header->from_id_ != nullptr && !is_acceptable_dialog(DialogId(header->from_id_))) { + return false; } - if (header->saved_from_peer_ != nullptr) { - DialogId dialog_id(header->saved_from_peer_); - if (!is_acceptable_dialog(dialog_id)) { - return false; - } + if (header->saved_from_peer_ != nullptr && !is_acceptable_dialog(DialogId(header->saved_from_peer_))) { + return false; } return true; } @@ -534,10 +528,8 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ if (!is_acceptable_dialog(DialogId(message->peer_id_))) { return false; } - if (message->flags_ & MessagesManager::MESSAGE_FLAG_HAS_FROM_ID) { - if (!is_acceptable_dialog(DialogId(message->from_id_))) { - return false; - } + if (message->from_id_ != nullptr && !is_acceptable_dialog(DialogId(message->from_id_))) { + return false; } const telegram_api::MessageAction *action = message->action_.get(); @@ -707,16 +699,13 @@ void UpdatesManager::on_get_updates(tl_object_ptr &&updat auto from_id = update->flags_ & MessagesManager::MESSAGE_FLAG_IS_OUT ? td_->contacts_manager_->get_my_id().get() : update->user_id_; - auto peer_id = update->flags_ & MessagesManager::MESSAGE_FLAG_IS_OUT ? update->user_id_ - : td_->contacts_manager_->get_my_id().get(); - update->flags_ |= MessagesManager::MESSAGE_FLAG_HAS_FROM_ID; on_pending_update(make_tl_object( make_tl_object( update->flags_, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, update->id_, make_tl_object(from_id), - make_tl_object(peer_id), std::move(update->fwd_from_), + make_tl_object(update->user_id_), std::move(update->fwd_from_), update->via_bot_id_, std::move(update->reply_to_), update->date_, update->message_, nullptr, nullptr, std::move(update->entities_), 0, 0, nullptr, 0, string(), 0, Auto()), update->pts_, update->pts_count_), diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 72582c342..d299187c8 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -505,6 +505,10 @@ class CliClient final : public Actor { return transform(full_split(trim(message_ids), get_delimiter(message_ids)), as_message_id); } + static int64 as_message_thread_id(Slice str) { + return as_message_id(str); + } + static int32 as_button_id(Slice str) { return to_integer(trim(str)); } @@ -1860,6 +1864,12 @@ class CliClient final : public Actor { send_request(td_api::make_object(get_history_chat_id_, std::numeric_limits::max(), 0, 100, false)); + } else if (op == "replies") { + string chat_id; + string message_thread_id; + + send_request(td_api::make_object(as_chat_id(chat_id), "", 0, 0, 0, 100, nullptr, + as_message_thread_id(message_thread_id))); } else if (op == "spvf") { search_chat_id_ = as_chat_id(args); From f9a7917eff37f881f4e2313cba217efacca47f26 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 11 Sep 2020 17:56:17 +0300 Subject: [PATCH 09/10] Simplify is_acceptable_dialog usage. GitOrigin-RevId: 0914cd24bb8c1a2cc878692a0f157d17dab667dd --- td/telegram/UpdatesManager.cpp | 28 +++++++++++++++------------- td/telegram/UpdatesManager.h | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 1bbe3cd06..7fc647191 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -352,7 +352,12 @@ bool UpdatesManager::is_acceptable_channel(ChannelId channel_id) const { return td_->contacts_manager_->have_channel_force(channel_id); } -bool UpdatesManager::is_acceptable_dialog(DialogId dialog_id) const { +bool UpdatesManager::is_acceptable_peer(const tl_object_ptr &peer) const { + if (peer == nullptr) { + return true; + } + + DialogId dialog_id(peer); switch (dialog_id.get_type()) { case DialogType::User: if (!is_acceptable_user(dialog_id.get_user_id())) { @@ -399,11 +404,8 @@ bool UpdatesManager::is_acceptable_message_reply_header( return true; } - if (header->reply_to_peer_id_ != nullptr) { - DialogId dialog_id(header->reply_to_peer_id_); - if (!is_acceptable_dialog(dialog_id)) { - return false; - } + if (is_acceptable_peer(header->reply_to_peer_id_)) { + return false; } return true; } @@ -414,10 +416,10 @@ bool UpdatesManager::is_acceptable_message_forward_header( return true; } - if (header->from_id_ != nullptr && !is_acceptable_dialog(DialogId(header->from_id_))) { + if (!is_acceptable_peer(header->from_id_)) { return false; } - if (header->saved_from_peer_ != nullptr && !is_acceptable_dialog(DialogId(header->saved_from_peer_))) { + if (!is_acceptable_peer(header->saved_from_peer_)) { return false; } return true; @@ -433,10 +435,10 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ case telegram_api::message::ID: { auto message = static_cast(message_ptr); - if (!is_acceptable_dialog(DialogId(message->peer_id_))) { + if (!is_acceptable_peer(message->peer_id_)) { return false; } - if (message->from_id_ != nullptr && !is_acceptable_dialog(DialogId(message->from_id_))) { + if (!is_acceptable_peer(message->from_id_)) { return false; } @@ -513,7 +515,7 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ // the dialogs are always min, so no need to check if (message->replies_ != nullptr) { for (auto &peer : message->replies_->recent_repliers_) { - if (!is_acceptable_dialog(DialogId(peer))) { + if (!is_acceptable_peer(peer)) { return false; } } @@ -525,10 +527,10 @@ bool UpdatesManager::is_acceptable_message(const telegram_api::Message *message_ case telegram_api::messageService::ID: { auto message = static_cast(message_ptr); - if (!is_acceptable_dialog(DialogId(message->peer_id_))) { + if (!is_acceptable_peer(message->peer_id_)) { return false; } - if (message->from_id_ != nullptr && !is_acceptable_dialog(DialogId(message->from_id_))) { + if (!is_acceptable_peer(message->from_id_)) { return false; } diff --git a/td/telegram/UpdatesManager.h b/td/telegram/UpdatesManager.h index 8dab6c4e9..fd7298fef 100644 --- a/td/telegram/UpdatesManager.h +++ b/td/telegram/UpdatesManager.h @@ -186,7 +186,7 @@ class UpdatesManager : public Actor { bool is_acceptable_channel(ChannelId channel_id) const; - bool is_acceptable_dialog(DialogId dialog_id) const; + bool is_acceptable_peer(const tl_object_ptr &peer) const; bool is_acceptable_message_entities(const vector> &message_entities) const; From 6d23ea4aa90d6a9815138efb832d45ee91cb984d Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 11 Sep 2020 18:57:49 +0300 Subject: [PATCH 10/10] Do not change download order after file merge. GitOrigin-RevId: cb1327b7b78cd42cd873040acca328ed00dddb2b --- td/telegram/files/FileManager.cpp | 20 +++++++++++--------- td/telegram/files/FileManager.h | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 64c124936..2c69466c4 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -1661,7 +1661,7 @@ Result FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy file_nodes_[node_ids[other_node_i]] = nullptr; run_generate(node); - run_download(node); + run_download(node, false); run_upload(node, {}); if (other_pmc_id.is_valid()) { @@ -2182,12 +2182,12 @@ void FileManager::download(FileId file_id, std::shared_ptr cal // TODO: send current progress? run_generate(node); - run_download(node); + run_download(node, true); try_flush_node(node, "download"); } -void FileManager::run_download(FileNodePtr node) { +void FileManager::run_download(FileNodePtr node, bool force_update_priority) { int8 priority = 0; for (auto id : node->file_ids_) { auto *info = get_file_id_info(id); @@ -2230,7 +2230,9 @@ void FileManager::run_download(FileNodePtr node) { if (old_priority != 0) { LOG(INFO) << "Update download offset and limits of file " << node->main_file_id_; CHECK(node->download_id_ != 0); - send_closure(file_load_manager_, &FileLoadManager::update_priority, node->download_id_, priority); + if (force_update_priority || priority != old_priority) { + send_closure(file_load_manager_, &FileLoadManager::update_priority, node->download_id_, priority); + } if (need_update_limit || need_update_offset) { auto download_offset = node->download_offset_; auto download_limit = node->download_limit_; @@ -3626,7 +3628,7 @@ void FileManager::on_error_impl(FileNodePtr node, Query::Type type, bool was_act if ((status.message() == "FILE_ID_INVALID" || status.message() == "LOCATION_INVALID") && FileView(node).may_reload_photo()) { node->need_reload_photo_ = true; - run_download(node); + run_download(node, true); return; } @@ -3646,7 +3648,7 @@ void FileManager::on_error_impl(FileNodePtr node, Query::Type type, bool was_act } CHECK(!node->file_ids_.empty()); delete_file_reference(node->file_ids_.back(), file_reference); - run_download(node); + run_download(node, true); return; } @@ -3660,16 +3662,16 @@ void FileManager::on_error_impl(FileNodePtr node, Query::Type type, bool was_act if (begins_with(status.message(), "FILE_DOWNLOAD_RESTART")) { if (ends_with(status.message(), "WITH_FILE_REFERENCE")) { node->download_was_update_file_reference_ = true; - run_download(node); + run_download(node, true); return; } else if (ends_with(status.message(), "INCREASE_PART_SIZE")) { if (try_fix_partial_local_location(node)) { - run_download(node); + run_download(node, true); return; } } else { node->can_search_locally_ = false; - run_download(node); + run_download(node, true); return; } } diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index 91d50ab03..127f4ba6d 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -629,7 +629,7 @@ class FileManager : public FileLoadManager::Callback { void do_cancel_upload(FileNodePtr node); void do_cancel_generate(FileNodePtr node); void run_upload(FileNodePtr node, std::vector bad_parts); - void run_download(FileNodePtr node); + void run_download(FileNodePtr node, bool force_update_priority); void run_generate(FileNodePtr node); void on_start_download(QueryId query_id) override;