Add and use get_reactions_hash.

This commit is contained in:
levlam 2022-09-12 21:55:30 +03:00
parent 8e5dccbdb5
commit 38fbd00e99
3 changed files with 34 additions and 1 deletions

View File

@ -12,6 +12,7 @@
#include "td/telegram/Global.h"
#include "td/telegram/MessageSender.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/misc.h"
#include "td/telegram/OptionManager.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/StickersManager.h"
@ -24,6 +25,8 @@
#include "td/utils/as.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/crypto.h"
#include "td/utils/emoji.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"
@ -881,4 +884,26 @@ void add_recent_reaction(Td *td, const string &reaction) {
td->stickers_manager_->add_recent_reaction(reaction);
}
int64 get_reactions_hash(const vector<string> &reactions) {
vector<uint64> numbers;
for (auto &reaction : reactions) {
if (is_custom_reaction(reaction)) {
auto custom_emoji_id = static_cast<uint64>(get_custom_emoji_id(reaction));
numbers.push_back(custom_emoji_id >> 32);
numbers.push_back(custom_emoji_id & 0xFFFFFFFF);
} else {
auto emoji = remove_emoji_selectors(reaction);
unsigned char hash[16];
md5(emoji, {hash, sizeof(hash)});
auto get = [hash](int num) {
return static_cast<uint32>(hash[num]);
};
numbers.push_back(0);
numbers.push_back(static_cast<int32>((get(0) << 24) + (get(1) << 16) + (get(2) << 8) + get(3)));
}
}
return get_vector_hash(numbers);
}
} // namespace td

View File

@ -214,4 +214,6 @@ void report_message_reactions(Td *td, FullMessageId full_message_id, DialogId ch
void add_recent_reaction(Td *td, const string &reaction);
int64 get_reactions_hash(const vector<string> &reactions);
} // namespace td

View File

@ -1592,7 +1592,7 @@ void StickersManager::add_recent_reaction(const string &reaction) {
}
std::rotate(reactions.begin(), it, it + 1);
recent_reactions_.hash_ = 0;
recent_reactions_.hash_ = get_reactions_hash(reactions);
}
void StickersManager::clear_recent_reactions(Promise<Unit> &&promise) {
@ -4025,6 +4025,12 @@ void StickersManager::on_get_recent_reactions(tl_object_ptr<telegram_api::messag
recent_reactions_.reactions_ = std::move(new_reactions);
recent_reactions_.hash_ = reactions->hash_;
auto expected_hash = get_reactions_hash(recent_reactions_.reactions_);
if (recent_reactions_.hash_ != expected_hash) {
LOG(ERROR) << "Receive hash " << recent_reactions_.hash_ << " instead of " << expected_hash << " for reactions "
<< recent_reactions_.reactions_;
}
save_recent_reactions();
}