Send shortcut identifiers in updates and receive them in requests.

This commit is contained in:
levlam 2024-02-27 18:10:11 +03:00
parent 88363b1113
commit f13f409e5f
6 changed files with 71 additions and 38 deletions

View File

@ -6637,11 +6637,11 @@ updateSavedMessagesTopicCount topic_count:int32 = Update;
//@shortcut New data about the shortcut
updateQuickReplyShortcut shortcut:quickReplyShortcut = Update;
//@description A quick reply shortcut was deleted @name The name of the deleted shortcut
updateQuickReplyShortcutDeleted name:string = Update;
//@description A quick reply shortcut was deleted @shortcut_id The identifier of the deleted shortcut
updateQuickReplyShortcutDeleted shortcut_id:int32 = Update;
//@description The list of quick reply shortcuts has changed @shortcut_names The new list of names of quick reply shortcuts
updateQuickReplyShortcuts shortcut_names:vector<string> = Update;
//@description The list of quick reply shortcuts has changed @shortcut_ids The new list of identifiers of quick reply shortcuts
updateQuickReplyShortcuts shortcut_ids:vector<int32> = Update;
//@description Basic information about a topic in a forum chat was changed @chat_id Chat identifier @info New information about the topic
updateForumTopicInfo chat_id:int53 info:forumTopicInfo = Update;
@ -7773,11 +7773,11 @@ editMessageSchedulingState chat_id:int53 message_id:int53 scheduling_state:Messa
//@description Loads quick reply shortcuts created by the current user. The loaded topics will be sent through updateQuickReplyShortcuts
loadQuickReplyShortcuts = Ok;
//@description Deletes a quick reply shortcut @shortcut_name The name of the quick reply shortcut
deleteQuickReplyShortcut shortcut_name:string = Ok;
//@description Deletes a quick reply shortcut @shortcut_id Unique identifier of the quick reply shortcut
deleteQuickReplyShortcut shortcut_id:int32 = Ok;
//@description Changes the order of quick reply shortcuts @shortcut_names The new order of quick reply shortcuts
reorderQuickReplyShortcuts shortcut_names:vector<string> = Ok;
//@description Changes the order of quick reply shortcuts @shortcut_ids The new order of quick reply shortcuts
reorderQuickReplyShortcuts shortcut_ids:vector<int32> = Ok;
//@description Returns list of custom emojis, which can be used as forum topic icon by all users

View File

@ -705,7 +705,7 @@ bool QuickReplyManager::is_shortcut_list_changed(const vector<unique_ptr<Shortcu
return true;
}
for (size_t i = 0; i < new_shortcuts.size(); i++) {
if (shortcuts_.shortcuts_[i]->name_ != new_shortcuts[i]->name_) {
if (shortcuts_.shortcuts_[i]->shortcut_id_ != new_shortcuts[i]->shortcut_id_) {
return true;
}
}
@ -736,12 +736,11 @@ int64 QuickReplyManager::get_shortcuts_hash() const {
return get_vector_hash(numbers);
}
void QuickReplyManager::delete_quick_reply_shortcut(const string &name, Promise<Unit> &&promise) {
auto it = get_shortcut_it(name);
void QuickReplyManager::delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise) {
auto it = get_shortcut_it(shortcut_id);
if (it == shortcuts_.shortcuts_.end()) {
return promise.set_error(Status::Error(400, "Shortcut not found"));
}
auto shortcut_id = (*it)->shortcut_id_;
shortcuts_.shortcuts_.erase(it);
if (!shortcut_id.is_server()) {
@ -759,34 +758,36 @@ void QuickReplyManager::delete_quick_reply_shortcut_from_server(QuickReplyShortc
td_->create_handler<DeleteQuickReplyShortcutQuery>(std::move(promise))->send(shortcut_id);
}
void QuickReplyManager::reorder_quick_reply_shortcuts(const vector<string> &names, Promise<Unit> &&promise) {
FlatHashSet<string> unique_names;
for (const auto &name : names) {
if (get_shortcut(name) == nullptr) {
void QuickReplyManager::reorder_quick_reply_shortcuts(const vector<QuickReplyShortcutId> &shortcut_ids,
Promise<Unit> &&promise) {
FlatHashSet<QuickReplyShortcutId, QuickReplyShortcutIdHash> unique_shortcut_ids;
for (const auto &shortcut_id : shortcut_ids) {
if (get_shortcut(shortcut_id) == nullptr) {
return promise.set_error(Status::Error(400, "Shortcut not found"));
}
unique_names.insert(name);
unique_shortcut_ids.insert(shortcut_id);
}
if (unique_names.size() != names.size()) {
return promise.set_error(Status::Error(400, "Duplicate shortcut names specified"));
if (unique_shortcut_ids.size() != shortcut_ids.size()) {
return promise.set_error(Status::Error(400, "Duplicate shortcut identifiers specified"));
}
if (!shortcuts_.are_inited_) {
return promise.set_value(Unit());
}
auto old_shortcut_ids = get_shortcut_ids();
auto old_server_shortcut_ids = get_server_shortcut_ids();
vector<unique_ptr<Shortcut>> shortcuts;
for (const auto &name : names) {
auto it = get_shortcut_it(name);
for (const auto &shortcut_id : shortcut_ids) {
auto it = get_shortcut_it(shortcut_id);
CHECK(it != shortcuts_.shortcuts_.end() && *it != nullptr);
shortcuts.push_back(std::move(*it));
}
for (auto &shortcut : shortcuts_.shortcuts_) {
if (shortcut != nullptr) {
CHECK(unique_names.count(shortcut->name_) == 0);
CHECK(unique_shortcut_ids.count(shortcut->shortcut_id_) == 0);
shortcuts.push_back(std::move(shortcut));
}
}
auto old_server_shortcut_ids = get_server_shortcut_ids();
bool is_list_changed = is_shortcut_list_changed(shortcuts);
bool is_list_changed = old_shortcut_ids != get_shortcut_ids();
shortcuts_.shortcuts_ = std::move(shortcuts);
if (!is_list_changed) {
return promise.set_value(Unit());
@ -794,7 +795,7 @@ void QuickReplyManager::reorder_quick_reply_shortcuts(const vector<string> &name
send_update_quick_reply_shortcuts();
auto new_server_shortcut_ids = get_server_shortcut_ids();
if (new_server_shortcut_ids == old_server_shortcut_ids || new_server_shortcut_ids.empty()) {
if (new_server_shortcut_ids == old_server_shortcut_ids) {
return promise.set_value(Unit());
}
@ -830,6 +831,16 @@ QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(const string &name)
return nullptr;
}
vector<unique_ptr<QuickReplyManager::Shortcut>>::iterator QuickReplyManager::get_shortcut_it(
QuickReplyShortcutId shortcut_id) {
for (auto it = shortcuts_.shortcuts_.begin(); it != shortcuts_.shortcuts_.end(); ++it) {
if ((*it)->shortcut_id_ == shortcut_id) {
return it;
}
}
return shortcuts_.shortcuts_.end();
}
vector<unique_ptr<QuickReplyManager::Shortcut>>::iterator QuickReplyManager::get_shortcut_it(const string &name) {
for (auto it = shortcuts_.shortcuts_.begin(); it != shortcuts_.shortcuts_.end(); ++it) {
if ((*it)->name_ == name) {
@ -839,6 +850,10 @@ vector<unique_ptr<QuickReplyManager::Shortcut>>::iterator QuickReplyManager::get
return shortcuts_.shortcuts_.end();
}
vector<QuickReplyShortcutId> QuickReplyManager::get_shortcut_ids() const {
return transform(shortcuts_.shortcuts_, [](const unique_ptr<Shortcut> &shortcut) { return shortcut->shortcut_id_; });
}
vector<QuickReplyShortcutId> QuickReplyManager::get_server_shortcut_ids() const {
vector<QuickReplyShortcutId> shortcut_ids;
for (auto &shortcut : shortcuts_.shortcuts_) {
@ -936,7 +951,7 @@ void QuickReplyManager::send_update_quick_reply_shortcut(const Shortcut *s, cons
td_api::object_ptr<td_api::updateQuickReplyShortcutDeleted>
QuickReplyManager::get_update_quick_reply_shortcut_deleted_object(const Shortcut *s) const {
CHECK(s != nullptr);
return td_api::make_object<td_api::updateQuickReplyShortcutDeleted>(s->name_);
return td_api::make_object<td_api::updateQuickReplyShortcutDeleted>(s->shortcut_id_.get());
}
void QuickReplyManager::send_update_quick_reply_shortcut_deleted(const Shortcut *s) {
@ -946,8 +961,8 @@ void QuickReplyManager::send_update_quick_reply_shortcut_deleted(const Shortcut
td_api::object_ptr<td_api::updateQuickReplyShortcuts> QuickReplyManager::get_update_quick_reply_shortcuts_object()
const {
CHECK(shortcuts_.are_inited_);
return td_api::make_object<td_api::updateQuickReplyShortcuts>(
transform(shortcuts_.shortcuts_, [](const unique_ptr<Shortcut> &shortcut) { return shortcut->name_; }));
return td_api::make_object<td_api::updateQuickReplyShortcuts>(transform(
shortcuts_.shortcuts_, [](const unique_ptr<Shortcut> &shortcut) { return shortcut->shortcut_id_.get(); }));
}
void QuickReplyManager::send_update_quick_reply_shortcuts() {

View File

@ -33,9 +33,9 @@ class QuickReplyManager final : public Actor {
void get_quick_reply_shortcuts(Promise<Unit> &&promise);
void delete_quick_reply_shortcut(const string &name, Promise<Unit> &&promise);
void delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
void reorder_quick_reply_shortcuts(const vector<string> &names, Promise<Unit> &&promise);
void reorder_quick_reply_shortcuts(const vector<QuickReplyShortcutId> &shortcut_ids, Promise<Unit> &&promise);
void reload_quick_reply_shortcuts();
@ -170,10 +170,14 @@ class QuickReplyManager final : public Actor {
Shortcut *get_shortcut(const string &name);
vector<unique_ptr<Shortcut>>::iterator get_shortcut_it(QuickReplyShortcutId shortcut_id);
vector<unique_ptr<Shortcut>>::iterator get_shortcut_it(const string &name);
bool is_shortcut_list_changed(const vector<unique_ptr<Shortcut>> &new_shortcuts) const;
vector<QuickReplyShortcutId> get_shortcut_ids() const;
vector<QuickReplyShortcutId> get_server_shortcut_ids() const;
static void sort_quick_reply_messages(vector<unique_ptr<QuickReplyMessage>> &messages);

View File

@ -41,6 +41,15 @@ class QuickReplyShortcutId {
return input_quick_reply_shortcut_ids;
}
static vector<QuickReplyShortcutId> get_quick_reply_shortcut_ids(const vector<int32> &shortcut_ids) {
vector<QuickReplyShortcutId> quick_reply_shortcut_ids;
quick_reply_shortcut_ids.reserve(shortcut_ids.size());
for (auto &shortcut_id : shortcut_ids) {
quick_reply_shortcut_ids.emplace_back(shortcut_id);
}
return quick_reply_shortcut_ids;
}
bool operator==(const QuickReplyShortcutId &other) const {
return id == other.id;
}

View File

@ -5770,13 +5770,14 @@ void Td::on_request(uint64 id, const td_api::loadQuickReplyShortcuts &request) {
void Td::on_request(uint64 id, const td_api::deleteQuickReplyShortcut &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
quick_reply_manager_->delete_quick_reply_shortcut(request.shortcut_name_, std::move(promise));
quick_reply_manager_->delete_quick_reply_shortcut(QuickReplyShortcutId(request.shortcut_id_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::reorderQuickReplyShortcuts &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
quick_reply_manager_->reorder_quick_reply_shortcuts(request.shortcut_names_, std::move(promise));
quick_reply_manager_->reorder_quick_reply_shortcuts(
QuickReplyShortcutId::get_quick_reply_shortcut_ids(request.shortcut_ids_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getStory &request) {

View File

@ -940,6 +940,10 @@ class CliClient final : public Actor {
return to_integer<int32>(trim(str));
}
vector<int32> as_shortcut_ids(Slice shortcut_ids) const {
return transform(autosplit(shortcut_ids), [](Slice str) { return as_shortcut_id(str); });
}
td_api::object_ptr<td_api::InputMessageReplyTo> get_input_message_reply_to() const {
if (reply_message_id_ != 0) {
td_api::object_ptr<td_api::inputTextQuote> quote;
@ -4844,13 +4848,13 @@ class CliClient final : public Actor {
} else if (op == "lqrs") {
send_request(td_api::make_object<td_api::loadQuickReplyShortcuts>());
} else if (op == "dqrs") {
string name;
get_args(args, name);
send_request(td_api::make_object<td_api::deleteQuickReplyShortcut>(name));
ShortcutId shortcut_id;
get_args(args, shortcut_id);
send_request(td_api::make_object<td_api::deleteQuickReplyShortcut>(shortcut_id));
} else if (op == "rqrs") {
string names;
get_args(args, names);
send_request(td_api::make_object<td_api::reorderQuickReplyShortcuts>(autosplit_str(names)));
string shortcut_ids;
get_args(args, shortcut_ids);
send_request(td_api::make_object<td_api::reorderQuickReplyShortcuts>(as_shortcut_ids(shortcut_ids)));
} else if (op == "gftdi") {
send_request(td_api::make_object<td_api::getForumTopicDefaultIcons>());
} else if (op == "cft") {