diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index e503af28a..ebbeac6cd 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -7544,6 +7544,31 @@ bool ContactsManager::speculative_add_count(int32 &count, int32 new_count) { return true; } +void ContactsManager::speculative_add_channel_participants(ChannelId channel_id, const vector &added_user_ids, + bool by_me) { + int32 new_participant_count = 0; + for (auto &user_id : added_user_ids) { + if (!user_id.is_valid()) { + continue; + } + + new_participant_count++; + } + if (new_participant_count == 0) { + return; + } + + speculative_add_channel_participants(channel_id, new_participant_count, by_me); +} + +void ContactsManager::speculative_delete_channel_participant(ChannelId channel_id, UserId deleted_user_id, bool by_me) { + if (!deleted_user_id.is_valid()) { + return; + } + + speculative_add_channel_participants(channel_id, -1, by_me); +} + void ContactsManager::speculative_add_channel_participants(ChannelId channel_id, int32 new_participant_count, bool by_me) { if (by_me) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 967b74e08..fa235aa49 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -172,7 +172,9 @@ class ContactsManager : public Actor { void on_update_dialog_administrators(DialogId dialog_id, vector administrator_user_ids, bool have_access); - void speculative_add_channel_participants(ChannelId channel_id, int32 new_participant_count, bool by_me); + void speculative_add_channel_participants(ChannelId channel_id, const vector &added_user_ids, bool by_me); + + void speculative_delete_channel_participant(ChannelId channel_id, UserId deleted_user_id, bool by_me); void invalidate_channel_full(ChannelId channel_id, bool drop_invite_link); @@ -891,6 +893,8 @@ class ContactsManager : public Actor { static bool speculative_add_count(int32 &count, int32 new_count); + void speculative_add_channel_participants(ChannelId channel_id, int32 new_participant_count, bool by_me); + void speculative_add_channel_user(ChannelId channel_id, UserId user_id, DialogParticipantStatus status, DialogParticipantStatus old_status); diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 98963098e..40527e39a 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -2618,19 +2618,6 @@ int32 get_message_content_index_mask(const MessageContent *content, const Td *td return 0; } -int32 get_message_content_new_participant_count(const MessageContent *content) { - switch (content->get_type()) { - case MessageContentType::ChatAddUsers: - return narrow_cast(static_cast(content)->user_ids.size()); - case MessageContentType::ChatJoinedByLink: - return 1; - case MessageContentType::ChatDeleteUser: - return -1; - default: - return 0; - } -} - MessageId get_message_content_pinned_message_id(const MessageContent *content) { switch (content->get_type()) { case MessageContentType::PinMessage: @@ -2653,6 +2640,11 @@ MessageId get_message_content_replied_message_id(const MessageContent *content) } } +vector get_message_content_added_user_ids(const MessageContent *content) { + CHECK(content->get_type() == MessageContentType::ChatAddUsers); + return static_cast(content)->user_ids; +} + UserId get_message_content_deleted_user_id(const MessageContent *content) { switch (content->get_type()) { case MessageContentType::ChatDeleteUser: diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index db769e588..6b6e2630f 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -167,12 +167,12 @@ bool update_opened_message_content(MessageContent *content); int32 get_message_content_index_mask(const MessageContent *content, const Td *td, bool is_secret, bool is_outgoing); -int32 get_message_content_new_participant_count(const MessageContent *content); - MessageId get_message_content_pinned_message_id(const MessageContent *content); MessageId get_message_content_replied_message_id(const MessageContent *content); +vector get_message_content_added_user_ids(const MessageContent *content); + UserId get_message_content_deleted_user_id(const MessageContent *content); int32 get_message_content_live_location_period(const MessageContent *content); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 60c40f86c..dbfc6269f 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -22105,10 +22105,23 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq register_message_content(td_, m->content.get(), {dialog_id, message_id}); if (from_update && message_id.is_server() && dialog_id.get_type() == DialogType::Channel) { - int32 new_participant_count = get_message_content_new_participant_count(m->content.get()); - if (new_participant_count != 0) { - td_->contacts_manager_->speculative_add_channel_participants(dialog_id.get_channel_id(), new_participant_count, - m->sender_user_id == my_user_id); + switch (message_content_type) { + case MessageContentType::ChatAddUsers: + td_->contacts_manager_->speculative_add_channel_participants( + dialog_id.get_channel_id(), get_message_content_added_user_ids(m->content.get()), + m->sender_user_id == my_user_id); + break; + case MessageContentType::ChatJoinedByLink: + td_->contacts_manager_->speculative_add_channel_participants(dialog_id.get_channel_id(), {m->sender_user_id}, + m->sender_user_id == my_user_id); + break; + case MessageContentType::ChatDeleteUser: + td_->contacts_manager_->speculative_delete_channel_participant( + dialog_id.get_channel_id(), get_message_content_deleted_user_id(m->content.get()), + m->sender_user_id == my_user_id); + break; + default: + break; } } if (from_update && message_id.is_server() && message_content_type == MessageContentType::PinMessage) {