Sort message reactions by the number of choosers.

This commit is contained in:
levlam 2022-01-26 12:01:14 +03:00
parent 9eb3a9a403
commit bad018bc32
4 changed files with 32 additions and 1 deletions

View File

@ -276,6 +276,23 @@ void MessageReactions::update_from(const MessageReactions &old_reactions) {
} }
} }
void MessageReactions::sort(const std::unordered_map<string, size_t> &active_reaction_pos) {
std::sort(reactions_.begin(), reactions_.end(), [&active_reaction_pos](const MessageReaction &lhs, const MessageReaction &rhs) {
if (lhs.get_choose_count() != rhs.get_choose_count()) {
return lhs.get_choose_count() > rhs.get_choose_count();
}
auto lhs_it = active_reaction_pos.find(lhs.get_reaction());
auto lhs_pos = lhs_it != active_reaction_pos.end() ? lhs_it->second : active_reaction_pos.size();
auto rhs_it = active_reaction_pos.find(rhs.get_reaction());
auto rhs_pos = rhs_it != active_reaction_pos.end() ? rhs_it->second : active_reaction_pos.size();
if (lhs_pos != rhs_pos) {
return lhs_pos < rhs_pos;
}
return lhs.get_reaction() < rhs.get_reaction();
});
}
bool MessageReactions::need_update_message_reactions(const MessageReactions *old_reactions, bool MessageReactions::need_update_message_reactions(const MessageReactions *old_reactions,
const MessageReactions *new_reactions) { const MessageReactions *new_reactions) {
if (old_reactions == nullptr) { if (old_reactions == nullptr) {

View File

@ -18,6 +18,7 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/StringBuilder.h" #include "td/utils/StringBuilder.h"
#include <unordered_map>
#include <utility> #include <utility>
namespace td { namespace td {
@ -64,6 +65,10 @@ class MessageReaction {
void set_is_chosen(bool is_chosen, DialogId chooser_dialog_id = DialogId()); void set_is_chosen(bool is_chosen, DialogId chooser_dialog_id = DialogId());
int32 get_choose_count() const {
return choose_count_;
}
const vector<DialogId> &get_recent_chooser_dialog_ids() const { const vector<DialogId> &get_recent_chooser_dialog_ids() const {
return recent_chooser_dialog_ids_; return recent_chooser_dialog_ids_;
} }
@ -104,6 +109,8 @@ struct MessageReactions {
void update_from(const MessageReactions &old_reactions); void update_from(const MessageReactions &old_reactions);
void sort(const std::unordered_map<string, size_t> &active_reaction_pos);
static bool need_update_message_reactions(const MessageReactions *old_reactions, static bool need_update_message_reactions(const MessageReactions *old_reactions,
const MessageReactions *new_reactions); const MessageReactions *new_reactions);

View File

@ -7012,6 +7012,9 @@ bool MessagesManager::update_message_interaction_info(DialogId dialog_id, Messag
if (has_reactions && reactions != nullptr && m->reactions != nullptr) { if (has_reactions && reactions != nullptr && m->reactions != nullptr) {
reactions->update_from(*m->reactions); reactions->update_from(*m->reactions);
} }
if (has_reactions && reactions != nullptr) {
reactions->sort(active_reaction_pos_);
}
bool need_update_reactions = bool need_update_reactions =
has_reactions && MessageReactions::need_update_message_reactions(m->reactions.get(), reactions.get()); has_reactions && MessageReactions::need_update_message_reactions(m->reactions.get(), reactions.get());
if (view_count > m->view_count || forward_count > m->forward_count || need_update_reply_info || if (view_count > m->view_count || forward_count > m->forward_count || need_update_reply_info ||
@ -8263,6 +8266,9 @@ void MessagesManager::set_active_reactions(vector<string> active_reactions) {
auto old_active_reactions = std::move(active_reactions_); auto old_active_reactions = std::move(active_reactions_);
active_reactions_ = std::move(active_reactions); active_reactions_ = std::move(active_reactions);
for (size_t i = 0; i < active_reactions_.size(); i++) {
active_reaction_pos_[active_reactions_[i]] = i;
}
for (const auto &dialog : dialogs_) { for (const auto &dialog : dialogs_) {
const Dialog *d = dialog.second.get(); const Dialog *d = dialog.second.get();
@ -23774,13 +23780,13 @@ void MessagesManager::set_message_reaction(FullMessageId full_message_id, string
} }
// m->reactions->has_pending_reaction_ = true; // m->reactions->has_pending_reaction_ = true;
if (!is_found && !reaction.empty()) { if (!is_found && !reaction.empty()) {
// TODO place to the correct position
vector<DialogId> recent_chooser_dialog_ids; vector<DialogId> recent_chooser_dialog_ids;
if (!is_broadcast_channel(dialog_id)) { if (!is_broadcast_channel(dialog_id)) {
recent_chooser_dialog_ids.push_back(get_my_dialog_id()); recent_chooser_dialog_ids.push_back(get_my_dialog_id());
} }
m->reactions->reactions_.emplace_back(reaction, 1, true, std::move(recent_chooser_dialog_ids), Auto()); m->reactions->reactions_.emplace_back(reaction, 1, true, std::move(recent_chooser_dialog_ids), Auto());
} }
m->reactions->sort(active_reaction_pos_);
send_update_message_interaction_info(dialog_id, m); send_update_message_interaction_info(dialog_id, m);
on_message_changed(d, m, true, "set_message_reaction"); on_message_changed(d, m, true, "set_message_reaction");

View File

@ -3603,6 +3603,7 @@ class MessagesManager final : public Actor {
std::unordered_map<DialogId, MessageId, DialogIdHash> previous_repaired_read_inbox_max_message_id_; std::unordered_map<DialogId, MessageId, DialogIdHash> previous_repaired_read_inbox_max_message_id_;
vector<string> active_reactions_; vector<string> active_reactions_;
std::unordered_map<string, size_t> active_reaction_pos_;
uint32 scheduled_messages_sync_generation_ = 1; uint32 scheduled_messages_sync_generation_ = 1;