diff --git a/td/telegram/MessageReaction.cpp b/td/telegram/MessageReaction.cpp index 2678bfc52..e54e0f115 100644 --- a/td/telegram/MessageReaction.cpp +++ b/td/telegram/MessageReaction.cpp @@ -762,6 +762,41 @@ vector MessageReactions::get_chosen_reactions() const { return reaction_order; } +bool MessageReactions::are_consistent_with_list(const string &reaction, FlatHashMap> reactions, + int32 total_count) const { + auto are_consistent = [](const vector &lhs, const vector &rhs) { + size_t i = 0; + size_t max_i = td::min(lhs.size(), rhs.size()); + while (i < max_i && lhs[i] == rhs[i]) { + i++; + } + return i == max_i; + }; + + if (reaction.empty()) { + // received list and total_count for all reactions + int32 old_total_count = 0; + for (const auto &message_reaction : reactions_) { + if (!are_consistent(reactions[message_reaction.get_reaction()], + message_reaction.get_recent_chooser_dialog_ids())) { + return false; + } + old_total_count += message_reaction.get_choose_count(); + reactions.erase(message_reaction.get_reaction()); + } + return old_total_count == total_count && reactions.empty(); + } + + // received list and total_count for a single reaction + const auto *message_reaction = get_reaction(reaction); + if (message_reaction == nullptr) { + return reactions.count(reaction) == 0 && total_count == 0; + } else { + return are_consistent(reactions[reaction], message_reaction->get_recent_chooser_dialog_ids()) && + message_reaction->get_choose_count() == total_count; + } +} + bool MessageReactions::need_update_message_reactions(const MessageReactions *old_reactions, const MessageReactions *new_reactions) { if (old_reactions == nullptr) { diff --git a/td/telegram/MessageReaction.h b/td/telegram/MessageReaction.h index 9051a8a99..868b0a570 100644 --- a/td/telegram/MessageReaction.h +++ b/td/telegram/MessageReaction.h @@ -166,6 +166,9 @@ struct MessageReactions { vector get_chosen_reactions() const; + bool are_consistent_with_list(const string &reaction, FlatHashMap> reactions, + int32 total_count) const; + static bool need_update_message_reactions(const MessageReactions *old_reactions, const MessageReactions *new_reactions); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 21d2886c9..1d290d5df 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -6726,40 +6726,9 @@ void MessagesManager::on_get_message_reaction_list(FullMessageId full_message_id return; } - auto are_consistent = [](const vector &lhs, const vector &rhs) { - size_t i = 0; - size_t max_i = td::min(lhs.size(), rhs.size()); - while (i < max_i && lhs[i] == rhs[i]) { - i++; - } - return i == max_i; - }; - // it's impossible to use received reactions to update message reactions, because there is no way to find, // which reactions are chosen by the current user, so just reload message reactions for consistency - bool need_reload = false; - if (reaction.empty()) { - // received list and total_count for all reactions - int32 old_total_count = 0; - for (const auto &message_reaction : m->reactions->reactions_) { - need_reload |= - !are_consistent(reactions[message_reaction.get_reaction()], message_reaction.get_recent_chooser_dialog_ids()); - old_total_count += message_reaction.get_choose_count(); - reactions.erase(message_reaction.get_reaction()); - } - need_reload |= old_total_count != total_count || !reactions.empty(); - } else { - // received list and total_count for a single reaction - const auto *message_reaction = m->reactions->get_reaction(reaction); - if (message_reaction == nullptr) { - need_reload = reactions.count(reaction) != 0 || total_count > 0; - } else { - need_reload = !are_consistent(reactions[reaction], message_reaction->get_recent_chooser_dialog_ids()) || - message_reaction->get_choose_count() != total_count; - } - } - - if (!need_reload) { + if (m->reactions->are_consistent_with_list(reaction, std::move(reactions), total_count)) { return; }