Add class QuickReplyShortcutId.

This commit is contained in:
levlam 2024-02-24 03:16:56 +03:00
parent 274720f395
commit 817a64ce0e
4 changed files with 92 additions and 26 deletions

View File

@ -791,6 +791,7 @@ set(TDLIB_SOURCE
td/telegram/QueryCombiner.h
td/telegram/QueryMerger.h
td/telegram/QuickReplyManager.h
td/telegram/QuickReplyShortcutId.h
td/telegram/ReactionListType.h
td/telegram/ReactionManager.h
td/telegram/ReactionType.h

View File

@ -25,7 +25,6 @@
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/logging.h"
namespace td {
@ -65,8 +64,8 @@ class DeleteQuickReplyShortcutQuery final : public Td::ResultHandler {
explicit DeleteQuickReplyShortcutQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(int32 shortcut_id) {
send_query(G()->net_query_creator().create(telegram_api::messages_deleteQuickReplyShortcut(shortcut_id)));
void send(QuickReplyShortcutId shortcut_id) {
send_query(G()->net_query_creator().create(telegram_api::messages_deleteQuickReplyShortcut(shortcut_id.get())));
}
void on_result(BufferSlice packet) final {
@ -383,7 +382,7 @@ unique_ptr<QuickReplyManager::QuickReplyMessage> QuickReplyManager::create_messa
}
auto result = make_unique<QuickReplyMessage>();
result->shortcut_id = message->quick_reply_shortcut_id_;
result->shortcut_id = QuickReplyShortcutId(message->quick_reply_shortcut_id_);
result->message_id = message_id;
result->edit_date = max(message->edit_date_, 0);
result->disable_web_page_preview = disable_web_page_preview;
@ -554,35 +553,36 @@ void QuickReplyManager::on_reload_quick_reply_shortcuts(
message_id_to_message[message_id] = std::move(message);
}
FlatHashSet<int32> old_shortcut_ids;
FlatHashSet<QuickReplyShortcutId, QuickReplyShortcutIdHash> old_shortcut_ids;
for (auto &shortcut : shortcuts_.shortcuts_) {
old_shortcut_ids.insert(shortcut->shortcut_id_);
}
FlatHashSet<int32> added_shortcut_ids;
FlatHashSet<QuickReplyShortcutId, QuickReplyShortcutIdHash> added_shortcut_ids;
FlatHashSet<string> added_shortcut_names;
vector<unique_ptr<Shortcut>> new_shortcuts;
vector<int32> changed_shortcut_ids;
vector<int32> deleted_shortcut_ids;
vector<QuickReplyShortcutId> changed_shortcut_ids;
vector<QuickReplyShortcutId> deleted_shortcut_ids;
for (auto &quick_reply : shortcuts->quick_replies_) {
if (quick_reply->shortcut_id_ <= 0 || quick_reply->shortcut_.empty() || quick_reply->count_ <= 0 ||
auto shortcut_id = QuickReplyShortcutId(quick_reply->shortcut_id_);
if (!shortcut_id.is_valid() || quick_reply->shortcut_.empty() || quick_reply->count_ <= 0 ||
quick_reply->top_message_ <= 0) {
LOG(ERROR) << "Receive " << to_string(quick_reply);
continue;
}
if (added_shortcut_ids.count(quick_reply->shortcut_id_) || added_shortcut_names.count(quick_reply->shortcut_)) {
if (added_shortcut_ids.count(shortcut_id) || added_shortcut_names.count(quick_reply->shortcut_)) {
LOG(ERROR) << "Receive duplicate " << to_string(quick_reply);
continue;
}
if (deleted_shortcut_ids_.count(quick_reply->shortcut_id_)) {
if (deleted_shortcut_ids_.count(shortcut_id)) {
continue;
}
added_shortcut_ids.insert(quick_reply->shortcut_id_);
added_shortcut_ids.insert(shortcut_id);
added_shortcut_names.insert(quick_reply->shortcut_);
MessageId first_message_id(ServerMessageId(quick_reply->top_message_));
auto it = message_id_to_message.find(first_message_id);
if (it == message_id_to_message.end()) {
LOG(ERROR) << "Can't find last " << first_message_id << " in shortcut " << quick_reply->shortcut_;
LOG(ERROR) << "Can't find last " << first_message_id << " in " << shortcut_id;
continue;
}
auto message = create_message(std::move(it->second), "on_reload_quick_reply_shortcuts");
@ -590,26 +590,25 @@ void QuickReplyManager::on_reload_quick_reply_shortcuts(
if (message == nullptr) {
continue;
}
if (message->shortcut_id != quick_reply->shortcut_id_) {
LOG(ERROR) << "Receive message from shortcut " << message->shortcut_id << " instead of "
<< quick_reply->shortcut_id_;
if (message->shortcut_id != shortcut_id) {
LOG(ERROR) << "Receive message from " << message->shortcut_id << " instead of " << shortcut_id;
continue;
}
auto shortcut = td::make_unique<Shortcut>();
shortcut->name_ = std::move(quick_reply->shortcut_);
shortcut->shortcut_id_ = quick_reply->shortcut_id_;
shortcut->shortcut_id_ = shortcut_id;
shortcut->server_total_count_ = quick_reply->count_;
shortcut->messages_.push_back(std::move(message));
auto old_shortcut = get_shortcut(shortcut->shortcut_id_);
auto old_shortcut = get_shortcut(shortcut_id);
auto is_object_changed = false;
if (old_shortcut == nullptr || update_shortcut_from(shortcut.get(), old_shortcut, true, &is_object_changed)) {
if (old_shortcut == nullptr || is_object_changed) {
changed_shortcut_ids.push_back(shortcut->shortcut_id_);
changed_shortcut_ids.push_back(shortcut_id);
}
}
old_shortcut_ids.erase(shortcut->shortcut_id_);
old_shortcut_ids.erase(shortcut_id);
new_shortcuts.push_back(std::move(shortcut));
}
@ -673,7 +672,7 @@ int64 QuickReplyManager::get_shortcuts_hash() const {
for (auto &shortcut : shortcuts_.shortcuts_) {
for (auto &message : shortcut->messages_) {
if (message->message_id.is_server()) {
numbers.push_back(shortcut->shortcut_id_);
numbers.push_back(shortcut->shortcut_id_.get());
numbers.push_back(get_md5_string_hash(shortcut->name_));
numbers.push_back(message->message_id.get_server_message_id().get());
numbers.push_back(message->edit_date);
@ -697,7 +696,7 @@ void QuickReplyManager::delete_quick_reply_shortcut(const string &name, Promise<
td_->create_handler<DeleteQuickReplyShortcutQuery>(std::move(promise))->send(shortcut_id);
}
QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(int32 shortcut_id) {
QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(QuickReplyShortcutId shortcut_id) {
if (!shortcuts_.are_inited_) {
return nullptr;
}

View File

@ -8,6 +8,7 @@
#include "td/telegram/DialogId.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/QuickReplyShortcutId.h"
#include "td/telegram/td_api.h"
#include "td/telegram/UserId.h"
@ -48,7 +49,7 @@ class QuickReplyManager final : public Actor {
~QuickReplyMessage();
MessageId message_id;
int32 shortcut_id = 0;
QuickReplyShortcutId shortcut_id;
int32 sending_id = 0; // for yet unsent messages
int32 edit_date = 0;
@ -103,7 +104,7 @@ class QuickReplyManager final : public Actor {
~Shortcut();
string name_;
int32 shortcut_id_ = 0;
QuickReplyShortcutId shortcut_id_;
int32 server_total_count_ = 0;
int32 local_total_count_ = 0;
vector<unique_ptr<QuickReplyMessage>> messages_;
@ -163,7 +164,7 @@ class QuickReplyManager final : public Actor {
int64 get_shortcuts_hash() const;
Shortcut *get_shortcut(int32 shortcut_id);
Shortcut *get_shortcut(QuickReplyShortcutId shortcut_id);
Shortcut *get_shortcut(const string &name);
@ -198,7 +199,7 @@ class QuickReplyManager final : public Actor {
Shortcuts shortcuts_;
FlatHashSet<int32> deleted_shortcut_ids_;
FlatHashSet<QuickReplyShortcutId, QuickReplyShortcutIdHash> deleted_shortcut_ids_;
Td *td_;
ActorShared<> parent_;

View File

@ -0,0 +1,65 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"
#include <type_traits>
namespace td {
class QuickReplyShortcutId {
int32 id = 0;
public:
QuickReplyShortcutId() = default;
explicit constexpr QuickReplyShortcutId(int32 quick_reply_shortcut_id) : id(quick_reply_shortcut_id) {
}
template <class T, typename = std::enable_if_t<std::is_convertible<T, int32>::value>>
QuickReplyShortcutId(T quick_reply_shortcut_id) = delete;
int32 get() const {
return id;
}
bool operator==(const QuickReplyShortcutId &other) const {
return id == other.id;
}
bool operator!=(const QuickReplyShortcutId &other) const {
return id != other.id;
}
bool is_valid() const {
return id > 0;
}
template <class StorerT>
void store(StorerT &storer) const {
storer.store_int(id);
}
template <class ParserT>
void parse(ParserT &parser) {
id = parser.fetch_int();
}
};
struct QuickReplyShortcutIdHash {
uint32 operator()(QuickReplyShortcutId quick_reply_shortcut_id) const {
return Hash<int32>()(quick_reply_shortcut_id.get());
}
};
inline StringBuilder &operator<<(StringBuilder &string_builder, QuickReplyShortcutId quick_reply_shortcut_id) {
return string_builder << "shortcut " << quick_reply_shortcut_id.get();
}
} // namespace td