Save chosen reaction order.

This commit is contained in:
levlam 2022-09-11 13:35:25 +03:00
parent ce9175a00b
commit e07ab8e71a
4 changed files with 31 additions and 3 deletions

View File

@ -491,6 +491,7 @@ unique_ptr<MessageReactions> MessageReactions::get_message_reactions(
result->is_min_ = reactions->min_;
FlatHashSet<string> reaction_strings;
vector<std::pair<int32, string>> chosen_reaction_order;
for (auto &reaction_count : reactions->results_) {
auto reaction_str = get_message_reaction_string(reaction_count->reaction_);
if (reaction_count->count_ <= 0 || reaction_count->count_ >= MessageReaction::MAX_CHOOSE_COUNT ||
@ -552,9 +553,17 @@ unique_ptr<MessageReactions> MessageReactions::get_message_reactions(
}
bool is_chosen = (reaction_count->flags_ & telegram_api::reactionCount::CHOSEN_ORDER_MASK) != 0;
if (is_chosen) {
chosen_reaction_order.emplace_back(reaction_count->chosen_order_, reaction_str);
}
result->reactions_.emplace_back(std::move(reaction_str), reaction_count->count_, is_chosen,
std::move(recent_chooser_dialog_ids), std::move(recent_chooser_min_channels));
}
if (chosen_reaction_order.size() > 1) {
std::sort(chosen_reaction_order.begin(), chosen_reaction_order.end());
result->chosen_reaction_order_ =
transform(chosen_reaction_order, [](const std::pair<int32, string> &order) { return order.second; });
}
return result;
}
@ -589,6 +598,7 @@ void MessageReactions::update_from(const MessageReactions &old_reactions) {
}
}
unread_reactions_ = old_reactions.unread_reactions_;
chosen_reaction_order_ = old_reactions.chosen_reaction_order_;
}
for (const auto &old_reaction : old_reactions.reactions_) {
if (old_reaction.is_chosen() &&
@ -700,7 +710,7 @@ bool MessageReactions::need_update_message_reactions(const MessageReactions *old
return true;
}
// unread_reactions_ are updated independently; compare all other fields
// unread_reactions_ and chosen_reaction_order_ are updated independently; compare all other fields
return old_reactions->reactions_ != new_reactions->reactions_ || old_reactions->is_min_ != new_reactions->is_min_ ||
old_reactions->can_get_added_reactions_ != new_reactions->can_get_added_reactions_ ||
old_reactions->need_polling_ != new_reactions->need_polling_;
@ -716,7 +726,8 @@ bool MessageReactions::need_update_unread_reactions(const MessageReactions *old_
StringBuilder &operator<<(StringBuilder &string_builder, const MessageReactions &reactions) {
return string_builder << (reactions.is_min_ ? "Min" : "") << "MessageReactions{" << reactions.reactions_
<< " with unread " << reactions.unread_reactions_
<< " with unread " << reactions.unread_reactions_ << ", reaction order "
<< reactions.chosen_reaction_order_
<< " and can_get_added_reactions = " << reactions.can_get_added_reactions_;
}

View File

@ -136,6 +136,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const UnreadMessageReac
struct MessageReactions {
vector<MessageReaction> reactions_;
vector<UnreadMessageReaction> unread_reactions_;
vector<string> chosen_reaction_order_;
bool is_min_ = false;
bool need_polling_ = true;
bool can_get_added_reactions_ = false;

View File

@ -77,12 +77,14 @@ template <class StorerT>
void MessageReactions::store(StorerT &storer) const {
bool has_reactions = !reactions_.empty();
bool has_unread_reactions = !unread_reactions_.empty();
bool has_chosen_reaction_order = !chosen_reaction_order_.empty();
BEGIN_STORE_FLAGS();
STORE_FLAG(is_min_);
STORE_FLAG(need_polling_);
STORE_FLAG(can_get_added_reactions_);
STORE_FLAG(has_unread_reactions);
STORE_FLAG(has_reactions);
STORE_FLAG(has_chosen_reaction_order);
END_STORE_FLAGS();
if (has_reactions) {
td::store(reactions_, storer);
@ -90,18 +92,23 @@ void MessageReactions::store(StorerT &storer) const {
if (has_unread_reactions) {
td::store(unread_reactions_, storer);
}
if (has_chosen_reaction_order) {
td::store(chosen_reaction_order_, storer);
}
}
template <class ParserT>
void MessageReactions::parse(ParserT &parser) {
bool has_reactions;
bool has_unread_reactions;
bool has_chosen_reaction_order;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(is_min_);
PARSE_FLAG(need_polling_);
PARSE_FLAG(can_get_added_reactions_);
PARSE_FLAG(has_unread_reactions);
PARSE_FLAG(has_reactions);
PARSE_FLAG(has_chosen_reaction_order);
END_PARSE_FLAGS();
if (has_reactions) {
td::parse(reactions_, parser);
@ -109,6 +116,9 @@ void MessageReactions::parse(ParserT &parser) {
if (has_unread_reactions) {
td::parse(unread_reactions_, parser);
}
if (has_chosen_reaction_order) {
td::parse(chosen_reaction_order_, parser);
}
}
} // namespace td

View File

@ -7032,8 +7032,10 @@ bool MessagesManager::update_message_interaction_info(Dialog *d, Message *m, int
has_reactions && MessageReactions::need_update_message_reactions(m->reactions.get(), reactions.get());
bool need_update_unread_reactions =
has_reactions && MessageReactions::need_update_unread_reactions(m->reactions.get(), reactions.get());
bool need_update_chosen_reaction_order = has_reactions && reactions != nullptr && m->reactions != nullptr &&
m->reactions->chosen_reaction_order_ != reactions->chosen_reaction_order_;
if (view_count > m->view_count || forward_count > m->forward_count || need_update_reply_info ||
need_update_reactions || need_update_unread_reactions) {
need_update_reactions || need_update_unread_reactions || need_update_chosen_reaction_order) {
LOG(DEBUG) << "Update interaction info of " << FullMessageId{dialog_id, m->message_id} << " from " << m->view_count
<< '/' << m->forward_count << '/' << m->reply_info << '/' << m->reactions << " to " << view_count << '/'
<< forward_count << '/' << reply_info << '/' << reactions;
@ -7099,6 +7101,10 @@ bool MessagesManager::update_message_interaction_info(Dialog *d, Message *m, int
m->available_reactions_generation = d->available_reactions_generation;
is_changed = true;
}
if (need_update_chosen_reaction_order) {
m->reactions->chosen_reaction_order_ = std::move(reactions->chosen_reaction_order_);
is_changed = true;
}
if (is_changed) {
on_message_changed(d, m, false, "update_message_interaction_info");
}