Ensure that the current user is among recent choosers of the chosen reaction.

This commit is contained in:
levlam 2022-04-27 22:13:05 +03:00
parent ee157920e9
commit 36c2ef9e62
3 changed files with 42 additions and 5 deletions

View File

@ -217,6 +217,17 @@ class GetMessageReactionsListQuery final : public Td::ResultHandler {
}
};
void MessageReaction::add_recent_chooser_dialog_id(DialogId dialog_id) {
recent_chooser_dialog_ids_.insert(recent_chooser_dialog_ids_.begin(), dialog_id);
if (recent_chooser_dialog_ids_.size() > MAX_RECENT_CHOOSERS) {
recent_chooser_dialog_ids_.resize(MAX_RECENT_CHOOSERS);
}
}
bool MessageReaction::remove_recent_chooser_dialog_id(DialogId dialog_id) {
return td::remove(recent_chooser_dialog_ids_, dialog_id);
}
void MessageReaction::set_is_chosen(bool is_chosen, DialogId chooser_dialog_id, bool can_get_added_reactions) {
if (is_chosen_ == is_chosen) {
return;
@ -227,12 +238,9 @@ void MessageReaction::set_is_chosen(bool is_chosen, DialogId chooser_dialog_id,
if (chooser_dialog_id.is_valid()) {
choose_count_ += is_chosen_ ? 1 : -1;
if (can_get_added_reactions) {
td::remove(recent_chooser_dialog_ids_, chooser_dialog_id);
remove_recent_chooser_dialog_id(chooser_dialog_id);
if (is_chosen_) {
recent_chooser_dialog_ids_.insert(recent_chooser_dialog_ids_.begin(), chooser_dialog_id);
if (recent_chooser_dialog_ids_.size() > MAX_RECENT_CHOOSERS) {
recent_chooser_dialog_ids_.resize(MAX_RECENT_CHOOSERS);
}
add_recent_chooser_dialog_id(chooser_dialog_id);
}
}
}
@ -410,6 +418,24 @@ void MessageReactions::sort_reactions(const FlatHashMap<string, size_t> &active_
});
}
void MessageReactions::fix_chosen_reaction(DialogId my_dialog_id) {
bool need_fix = false;
for (auto &reaction : reactions_) {
if (!reaction.is_chosen() && reaction.remove_recent_chooser_dialog_id(my_dialog_id)) {
LOG(WARNING) << "Fix recent chosen reaction in " << *this;
need_fix = true;
}
}
if (!need_fix) {
return;
}
for (auto &reaction : reactions_) {
if (reaction.is_chosen() && !td::contains(reaction.get_recent_chooser_dialog_ids(), my_dialog_id)) {
reaction.add_recent_chooser_dialog_id(my_dialog_id);
}
}
}
bool MessageReactions::need_update_message_reactions(const MessageReactions *old_reactions,
const MessageReactions *new_reactions) {
if (old_reactions == nullptr) {

View File

@ -78,6 +78,10 @@ class MessageReaction {
return recent_chooser_min_channels_;
}
void add_recent_chooser_dialog_id(DialogId dialog_id);
bool remove_recent_chooser_dialog_id(DialogId dialog_id);
td_api::object_ptr<td_api::messageReaction> get_message_reaction_object(Td *td) const;
template <class StorerT>
@ -149,6 +153,8 @@ struct MessageReactions {
void sort_reactions(const FlatHashMap<string, size_t> &active_reaction_pos);
void fix_chosen_reaction(DialogId my_dialog_id);
static bool need_update_message_reactions(const MessageReactions *old_reactions,
const MessageReactions *new_reactions);

View File

@ -6979,6 +6979,11 @@ bool MessagesManager::update_message_interaction_info(Dialog *d, Message *m, int
reactions->update_from(*m->reactions);
}
reactions->sort_reactions(active_reaction_pos_);
reactions->fix_chosen_reaction(get_my_dialog_id());
if (d->default_send_message_as_dialog_id.is_valid()) {
// the reaction could be set by previous owner of the broadcast
// reactions->fix_chosen_reaction(d->default_send_message_as_dialog_id);
}
}
bool need_update_reactions =
has_reactions && MessageReactions::need_update_message_reactions(m->reactions.get(), reactions.get());