2024-02-21 18:43:55 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/QuickReplyManager.h"
|
|
|
|
|
2024-02-21 20:06:07 +01:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2024-02-21 20:43:29 +01:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2024-02-21 20:06:07 +01:00
|
|
|
#include "td/telegram/Dependencies.h"
|
|
|
|
#include "td/telegram/DialogManager.h"
|
2024-02-29 15:41:05 +01:00
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
2024-02-29 17:01:40 +01:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2024-02-22 15:32:12 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2024-02-23 14:17:37 +01:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
|
|
|
#include "td/telegram/logevent/LogEventHelper.h"
|
2024-02-21 20:06:07 +01:00
|
|
|
#include "td/telegram/MessageContent.h"
|
2024-02-23 18:22:33 +01:00
|
|
|
#include "td/telegram/MessageContentType.h"
|
2024-02-21 20:06:07 +01:00
|
|
|
#include "td/telegram/MessageReplyHeader.h"
|
2024-02-21 22:48:05 +01:00
|
|
|
#include "td/telegram/MessageSelfDestructType.h"
|
2024-02-23 12:49:29 +01:00
|
|
|
#include "td/telegram/misc.h"
|
2024-02-21 20:27:46 +01:00
|
|
|
#include "td/telegram/RepliedMessageInfo.h"
|
2024-02-21 20:06:07 +01:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
2024-02-22 15:32:12 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2024-02-21 18:43:55 +01:00
|
|
|
namespace td {
|
|
|
|
|
2024-02-22 15:32:12 +01:00
|
|
|
class GetQuickRepliesQuery final : public Td::ResultHandler {
|
|
|
|
Promise<telegram_api::object_ptr<telegram_api::messages_QuickReplies>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetQuickRepliesQuery(Promise<telegram_api::object_ptr<telegram_api::messages_QuickReplies>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(int64 hash) {
|
2024-02-24 11:42:34 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getQuickReplies(hash), {{"me"}}));
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getQuickReplies>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(DEBUG) << "Receive result for GetQuickRepliesQuery: " << to_string(ptr);
|
|
|
|
promise_.set_value(std::move(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-24 01:02:42 +01:00
|
|
|
class DeleteQuickReplyShortcutQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DeleteQuickReplyShortcutQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:16:56 +01:00
|
|
|
void send(QuickReplyShortcutId shortcut_id) {
|
2024-02-24 11:42:34 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_deleteQuickReplyShortcut(shortcut_id.get()), {{"me"}}));
|
2024-02-24 01:02:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_deleteQuickReplyShortcut>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
2024-02-24 12:40:25 +01:00
|
|
|
td_->quick_reply_manager_->reload_quick_reply_shortcuts();
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReorderQuickRepliesQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ReorderQuickRepliesQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(vector<QuickReplyShortcutId> shortcut_ids) {
|
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_reorderQuickReplies(
|
|
|
|
QuickReplyShortcutId::get_input_quick_reply_shortcut_ids(shortcut_ids)),
|
|
|
|
{{"me"}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_reorderQuickReplies>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
td_->quick_reply_manager_->reload_quick_reply_shortcuts();
|
2024-02-24 01:02:42 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-28 15:19:28 +01:00
|
|
|
class GetQuickReplyMessagesQuery final : public Td::ResultHandler {
|
|
|
|
Promise<telegram_api::object_ptr<telegram_api::messages_Messages>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetQuickReplyMessagesQuery(Promise<telegram_api::object_ptr<telegram_api::messages_Messages>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(QuickReplyShortcutId shortcut_id, const vector<MessageId> &message_ids, int64 hash) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (!message_ids.empty()) {
|
|
|
|
flags |= telegram_api::messages_getQuickReplyMessages::ID_MASK;
|
|
|
|
}
|
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_getQuickReplyMessages(flags, shortcut_id.get(),
|
|
|
|
MessageId::get_server_message_ids(message_ids), hash),
|
|
|
|
{{"me"}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getQuickReplyMessages>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(DEBUG) << "Receive result for GetQuickReplyMessagesQuery: " << to_string(ptr);
|
|
|
|
promise_.set_value(std::move(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-22 15:32:12 +01:00
|
|
|
QuickReplyManager::QuickReplyMessage::~QuickReplyMessage() = default;
|
|
|
|
|
2024-02-23 14:17:37 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void QuickReplyManager::QuickReplyMessage::store(StorerT &storer) const {
|
|
|
|
bool is_server = message_id.is_server();
|
|
|
|
bool has_edit_date = edit_date != 0;
|
|
|
|
bool has_random_id = !is_server && random_id != 0;
|
|
|
|
bool has_reply_to_message_id = reply_to_message_id != MessageId();
|
|
|
|
bool has_send_emoji = !is_server && !send_emoji.empty();
|
|
|
|
bool has_via_bot_user_id = via_bot_user_id != UserId();
|
|
|
|
bool has_legacy_layer = legacy_layer != 0;
|
|
|
|
bool has_send_error_code = !is_server && send_error_code != 0;
|
|
|
|
bool has_send_error_message = !is_server && !send_error_message.empty();
|
|
|
|
bool has_try_resend_at = !is_server && try_resend_at != 0;
|
|
|
|
bool has_media_album_id = media_album_id != 0;
|
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(has_edit_date);
|
|
|
|
STORE_FLAG(has_random_id);
|
|
|
|
STORE_FLAG(has_reply_to_message_id);
|
|
|
|
STORE_FLAG(has_send_emoji);
|
|
|
|
STORE_FLAG(has_via_bot_user_id);
|
|
|
|
STORE_FLAG(is_failed_to_send);
|
|
|
|
STORE_FLAG(disable_notification);
|
|
|
|
STORE_FLAG(noforwards);
|
|
|
|
STORE_FLAG(invert_media);
|
|
|
|
STORE_FLAG(from_background);
|
|
|
|
STORE_FLAG(disable_web_page_preview);
|
|
|
|
STORE_FLAG(hide_via_bot);
|
|
|
|
STORE_FLAG(has_legacy_layer);
|
|
|
|
STORE_FLAG(has_send_error_code);
|
|
|
|
STORE_FLAG(has_send_error_message);
|
|
|
|
STORE_FLAG(has_try_resend_at);
|
|
|
|
STORE_FLAG(has_media_album_id);
|
|
|
|
END_STORE_FLAGS();
|
|
|
|
td::store(message_id, storer);
|
|
|
|
td::store(shortcut_id, storer);
|
|
|
|
if (has_edit_date) {
|
|
|
|
td::store(edit_date, storer);
|
|
|
|
}
|
|
|
|
if (has_random_id) {
|
|
|
|
td::store(random_id, storer);
|
|
|
|
}
|
|
|
|
if (has_reply_to_message_id) {
|
|
|
|
td::store(reply_to_message_id, storer);
|
|
|
|
}
|
|
|
|
if (has_send_emoji) {
|
|
|
|
td::store(send_emoji, storer);
|
|
|
|
}
|
|
|
|
if (has_via_bot_user_id) {
|
|
|
|
td::store(via_bot_user_id, storer);
|
|
|
|
}
|
|
|
|
if (has_legacy_layer) {
|
|
|
|
td::store(legacy_layer, storer);
|
|
|
|
}
|
|
|
|
if (has_send_error_code) {
|
|
|
|
td::store(send_error_code, storer);
|
|
|
|
}
|
|
|
|
if (has_send_error_message) {
|
|
|
|
td::store(send_error_message, storer);
|
|
|
|
}
|
|
|
|
if (has_try_resend_at) {
|
|
|
|
td::store_time(try_resend_at, storer);
|
|
|
|
}
|
|
|
|
if (has_media_album_id) {
|
|
|
|
td::store(media_album_id, storer);
|
|
|
|
}
|
|
|
|
store_message_content(content.get(), storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void QuickReplyManager::QuickReplyMessage::parse(ParserT &parser) {
|
|
|
|
bool has_edit_date;
|
|
|
|
bool has_random_id;
|
|
|
|
bool has_reply_to_message_id;
|
|
|
|
bool has_send_emoji;
|
|
|
|
bool has_via_bot_user_id;
|
|
|
|
bool has_legacy_layer;
|
|
|
|
bool has_send_error_code;
|
|
|
|
bool has_send_error_message;
|
|
|
|
bool has_try_resend_at;
|
|
|
|
bool has_media_album_id;
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(has_edit_date);
|
|
|
|
PARSE_FLAG(has_random_id);
|
|
|
|
PARSE_FLAG(has_reply_to_message_id);
|
|
|
|
PARSE_FLAG(has_send_emoji);
|
|
|
|
PARSE_FLAG(has_via_bot_user_id);
|
|
|
|
PARSE_FLAG(is_failed_to_send);
|
|
|
|
PARSE_FLAG(disable_notification);
|
|
|
|
PARSE_FLAG(noforwards);
|
|
|
|
PARSE_FLAG(invert_media);
|
|
|
|
PARSE_FLAG(from_background);
|
|
|
|
PARSE_FLAG(disable_web_page_preview);
|
|
|
|
PARSE_FLAG(hide_via_bot);
|
|
|
|
PARSE_FLAG(has_legacy_layer);
|
|
|
|
PARSE_FLAG(has_send_error_code);
|
|
|
|
PARSE_FLAG(has_send_error_message);
|
|
|
|
PARSE_FLAG(has_try_resend_at);
|
|
|
|
PARSE_FLAG(has_media_album_id);
|
|
|
|
END_PARSE_FLAGS();
|
|
|
|
td::parse(message_id, parser);
|
|
|
|
td::parse(shortcut_id, parser);
|
|
|
|
if (has_edit_date) {
|
|
|
|
td::parse(edit_date, parser);
|
|
|
|
}
|
|
|
|
if (has_random_id) {
|
|
|
|
td::parse(random_id, parser);
|
|
|
|
}
|
|
|
|
if (has_reply_to_message_id) {
|
|
|
|
td::parse(reply_to_message_id, parser);
|
|
|
|
}
|
|
|
|
if (has_send_emoji) {
|
|
|
|
td::parse(send_emoji, parser);
|
|
|
|
}
|
|
|
|
if (has_via_bot_user_id) {
|
|
|
|
td::parse(via_bot_user_id, parser);
|
|
|
|
}
|
|
|
|
if (has_legacy_layer) {
|
|
|
|
td::parse(legacy_layer, parser);
|
|
|
|
}
|
|
|
|
if (has_send_error_code) {
|
|
|
|
td::parse(send_error_code, parser);
|
|
|
|
}
|
|
|
|
if (has_send_error_message) {
|
|
|
|
td::parse(send_error_message, parser);
|
|
|
|
}
|
|
|
|
if (has_try_resend_at) {
|
|
|
|
td::parse_time(try_resend_at, parser);
|
|
|
|
}
|
|
|
|
if (has_media_album_id) {
|
|
|
|
td::parse(media_album_id, parser);
|
|
|
|
}
|
|
|
|
parse_message_content(content, parser);
|
|
|
|
}
|
|
|
|
|
2024-02-22 15:32:12 +01:00
|
|
|
QuickReplyManager::Shortcut::~Shortcut() = default;
|
|
|
|
|
2024-02-23 14:17:37 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void QuickReplyManager::Shortcut::store(StorerT &storer) const {
|
|
|
|
bool has_server_total_count = server_total_count_ != 0;
|
|
|
|
bool has_local_total_count = local_total_count_ != 0;
|
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(has_server_total_count);
|
|
|
|
STORE_FLAG(has_local_total_count);
|
|
|
|
END_STORE_FLAGS();
|
|
|
|
td::store(name_, storer);
|
|
|
|
td::store(shortcut_id_, storer);
|
|
|
|
if (has_server_total_count) {
|
|
|
|
td::store(server_total_count_, storer);
|
|
|
|
}
|
|
|
|
if (has_local_total_count) {
|
|
|
|
td::store(local_total_count_, storer);
|
|
|
|
}
|
|
|
|
td::store(messages_, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void QuickReplyManager::Shortcut::parse(ParserT &parser) {
|
|
|
|
bool has_server_total_count;
|
|
|
|
bool has_local_total_count;
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(has_server_total_count);
|
|
|
|
PARSE_FLAG(has_local_total_count);
|
|
|
|
END_PARSE_FLAGS();
|
|
|
|
td::parse(name_, parser);
|
|
|
|
td::parse(shortcut_id_, parser);
|
|
|
|
if (has_server_total_count) {
|
|
|
|
td::parse(server_total_count_, parser);
|
|
|
|
}
|
|
|
|
if (has_local_total_count) {
|
|
|
|
td::parse(local_total_count_, parser);
|
|
|
|
}
|
|
|
|
td::parse(messages_, parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void QuickReplyManager::Shortcuts::store(StorerT &storer) const {
|
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
END_STORE_FLAGS();
|
|
|
|
td::store(shortcuts_, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void QuickReplyManager::Shortcuts::parse(ParserT &parser) {
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
END_PARSE_FLAGS();
|
|
|
|
td::parse(shortcuts_, parser);
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:43:55 +01:00
|
|
|
QuickReplyManager::QuickReplyManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
2024-02-21 20:06:07 +01:00
|
|
|
unique_ptr<QuickReplyManager::QuickReplyMessage> QuickReplyManager::create_message(
|
|
|
|
telegram_api::object_ptr<telegram_api::Message> message_ptr, const char *source) const {
|
|
|
|
LOG(DEBUG) << "Receive from " << source << " " << to_string(message_ptr);
|
|
|
|
CHECK(message_ptr != nullptr);
|
|
|
|
|
|
|
|
switch (message_ptr->get_id()) {
|
|
|
|
case telegram_api::messageEmpty::ID:
|
|
|
|
break;
|
|
|
|
case telegram_api::message::ID: {
|
2024-02-22 15:32:12 +01:00
|
|
|
auto message_id = MessageId::get_message_id(message_ptr, false);
|
2024-02-21 20:06:07 +01:00
|
|
|
auto message = move_tl_object_as<telegram_api::message>(message_ptr);
|
2024-02-29 14:39:38 +01:00
|
|
|
auto shortcut_id = QuickReplyShortcutId(message->quick_reply_shortcut_id_);
|
|
|
|
if (!shortcut_id.is_server()) {
|
|
|
|
LOG(ERROR) << "Receive invalid quick reply " << shortcut_id << " from " << source;
|
2024-02-21 20:06:07 +01:00
|
|
|
break;
|
|
|
|
}
|
2024-02-29 15:27:12 +01:00
|
|
|
if (deleted_message_full_ids_.count({shortcut_id, message_id})) {
|
|
|
|
// a previously deleted message
|
|
|
|
break;
|
|
|
|
}
|
2024-02-21 20:06:07 +01:00
|
|
|
|
|
|
|
auto my_dialog_id = td_->dialog_manager_->get_my_dialog_id();
|
2024-02-22 15:32:12 +01:00
|
|
|
if (DialogId(message->peer_id_) != my_dialog_id || message->from_id_ != nullptr ||
|
2024-02-27 18:52:59 +01:00
|
|
|
message->saved_peer_id_ != nullptr || message->fwd_from_ != nullptr || message->views_ != 0 ||
|
|
|
|
message->forwards_ != 0 || message->replies_ != nullptr || message->reactions_ != nullptr ||
|
|
|
|
message->ttl_period_ != 0 || !message->out_ || message->post_ || message->edit_hide_ ||
|
|
|
|
message->from_scheduled_ || message->pinned_ || message->noforwards_ || message->mentioned_ ||
|
|
|
|
message->media_unread_ || message->reply_markup_ != nullptr || !message->restriction_reason_.empty() ||
|
|
|
|
!message->post_author_.empty() || message->from_boosts_applied_ != 0) {
|
2024-02-21 20:06:07 +01:00
|
|
|
LOG(ERROR) << "Receive an invalid quick reply from " << source << ": " << to_string(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
UserId via_bot_user_id;
|
|
|
|
if (message->flags_ & telegram_api::message::VIA_BOT_ID_MASK) {
|
|
|
|
via_bot_user_id = UserId(message->via_bot_id_);
|
|
|
|
if (!via_bot_user_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive invalid " << via_bot_user_id << " from " << source;
|
|
|
|
via_bot_user_id = UserId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto media_album_id = message->grouped_id_;
|
|
|
|
|
|
|
|
MessageSelfDestructType ttl;
|
|
|
|
bool disable_web_page_preview = false;
|
|
|
|
auto content = get_message_content(
|
|
|
|
td_,
|
|
|
|
get_message_text(td_->contacts_manager_.get(), std::move(message->message_), std::move(message->entities_),
|
|
|
|
true, td_->auth_manager_->is_bot(), 0, media_album_id != 0, source),
|
|
|
|
std::move(message->media_), my_dialog_id, message->date_, true, via_bot_user_id, &ttl,
|
|
|
|
&disable_web_page_preview, source);
|
|
|
|
|
|
|
|
auto reply_header = MessageReplyHeader(td_, std::move(message->reply_to_), my_dialog_id, message_id, -1, false);
|
|
|
|
if (reply_header.story_full_id_ != StoryFullId()) {
|
|
|
|
LOG(ERROR) << "Receive reply to " << reply_header.story_full_id_;
|
|
|
|
reply_header.story_full_id_ = {};
|
|
|
|
}
|
2024-02-21 20:27:46 +01:00
|
|
|
if (reply_header.replied_message_info_.is_external() ||
|
|
|
|
reply_header.replied_message_info_.get_reply_message_full_id(DialogId(), true).get_dialog_id() !=
|
|
|
|
DialogId()) {
|
|
|
|
LOG(ERROR) << "Receive reply to " << reply_header.replied_message_info_;
|
|
|
|
reply_header.replied_message_info_ = {};
|
|
|
|
}
|
|
|
|
auto reply_to_message_id = reply_header.replied_message_info_.get_same_chat_reply_to_message_id(true);
|
2024-02-21 20:06:07 +01:00
|
|
|
|
|
|
|
if (!ttl.is_empty()) {
|
2024-02-21 22:48:05 +01:00
|
|
|
LOG(ERROR) << "Wrong " << ttl << " received in " << message_id << " from " << source;
|
2024-02-22 19:40:07 +01:00
|
|
|
break;
|
2024-02-21 20:06:07 +01:00
|
|
|
}
|
|
|
|
|
2024-02-21 22:48:05 +01:00
|
|
|
auto content_type = content->get_type();
|
2024-02-21 20:06:07 +01:00
|
|
|
if (is_expired_message_content(content_type)) {
|
|
|
|
LOG(ERROR) << "Receive " << content_type << " from " << source;
|
2024-02-22 19:40:07 +01:00
|
|
|
break;
|
2024-02-21 20:06:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = make_unique<QuickReplyMessage>();
|
2024-02-29 14:39:38 +01:00
|
|
|
result->shortcut_id = shortcut_id;
|
2024-02-21 20:06:07 +01:00
|
|
|
result->message_id = message_id;
|
2024-02-22 19:40:07 +01:00
|
|
|
result->edit_date = max(message->edit_date_, 0);
|
2024-02-21 20:06:07 +01:00
|
|
|
result->disable_web_page_preview = disable_web_page_preview;
|
2024-02-21 20:27:46 +01:00
|
|
|
result->reply_to_message_id = reply_to_message_id;
|
2024-02-21 20:06:07 +01:00
|
|
|
result->via_bot_user_id = via_bot_user_id;
|
|
|
|
result->disable_notification = message->silent_;
|
|
|
|
result->legacy_layer = (message->legacy_ ? MTPROTO_LAYER : 0);
|
|
|
|
result->invert_media = message->invert_media_;
|
|
|
|
result->content = std::move(content);
|
|
|
|
|
|
|
|
if (media_album_id != 0) {
|
|
|
|
if (!is_allowed_media_group_content(content_type)) {
|
|
|
|
if (content_type != MessageContentType::Unsupported) {
|
|
|
|
LOG(ERROR) << "Receive media group identifier " << media_album_id << " in " << message_id
|
|
|
|
<< " with content "
|
|
|
|
<< oneline(to_string(get_quick_reply_message_message_content_object(result.get())));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result->media_album_id = media_album_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dependencies dependencies;
|
|
|
|
add_quick_reply_message_dependencies(dependencies, result.get());
|
|
|
|
for (auto dependent_dialog_id : dependencies.get_dialog_ids()) {
|
|
|
|
td_->dialog_manager_->force_create_dialog(dependent_dialog_id, source, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
case telegram_api::messageService::ID:
|
|
|
|
LOG(ERROR) << "Receive " << to_string(message_ptr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-02-21 20:27:46 +01:00
|
|
|
void QuickReplyManager::add_quick_reply_message_dependencies(Dependencies &dependencies,
|
|
|
|
const QuickReplyMessage *m) const {
|
2024-02-21 20:06:07 +01:00
|
|
|
auto is_bot = td_->auth_manager_->is_bot();
|
|
|
|
dependencies.add(m->via_bot_user_id);
|
|
|
|
add_message_content_dependencies(dependencies, m->content.get(), is_bot);
|
|
|
|
}
|
|
|
|
|
2024-02-23 18:22:33 +01:00
|
|
|
bool QuickReplyManager::can_edit_quick_reply_message(const QuickReplyMessage *m) const {
|
2024-02-27 18:52:59 +01:00
|
|
|
return m->message_id.is_server() && !m->via_bot_user_id.is_valid() &&
|
2024-02-23 18:22:33 +01:00
|
|
|
is_editable_message_content(m->content->get_type());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuickReplyManager::can_resend_quick_reply_message(const QuickReplyMessage *m) const {
|
2024-02-21 20:43:29 +01:00
|
|
|
if (m->send_error_code != 429) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (m->via_bot_user_id.is_valid() || m->hide_via_bot) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::MessageSendingState> QuickReplyManager::get_message_sending_state_object(
|
|
|
|
const QuickReplyMessage *m) const {
|
|
|
|
CHECK(m != nullptr);
|
|
|
|
if (m->message_id.is_yet_unsent()) {
|
|
|
|
return td_api::make_object<td_api::messageSendingStatePending>(m->sending_id);
|
|
|
|
}
|
|
|
|
if (m->is_failed_to_send) {
|
2024-02-23 18:22:33 +01:00
|
|
|
auto can_retry = can_resend_quick_reply_message(m);
|
2024-02-21 20:43:29 +01:00
|
|
|
auto error_code = m->send_error_code > 0 ? m->send_error_code : 400;
|
|
|
|
auto need_another_reply_quote =
|
|
|
|
can_retry && error_code == 400 && m->send_error_message == CSlice("QUOTE_TEXT_INVALID");
|
|
|
|
return td_api::make_object<td_api::messageSendingStateFailed>(
|
|
|
|
td_api::make_object<td_api::error>(error_code, m->send_error_message), can_retry, false,
|
|
|
|
need_another_reply_quote, false, max(m->try_resend_at - Time::now(), 0.0));
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::MessageContent> QuickReplyManager::get_quick_reply_message_message_content_object(
|
|
|
|
const QuickReplyMessage *m) const {
|
2024-02-21 22:48:05 +01:00
|
|
|
return get_message_content_object(m->content.get(), td_, DialogId(), 0, false, true, -1, m->invert_media,
|
|
|
|
m->disable_web_page_preview);
|
2024-02-21 20:43:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::quickReplyMessage> QuickReplyManager::get_quick_reply_message_object(
|
|
|
|
const QuickReplyMessage *m, const char *source) const {
|
|
|
|
CHECK(m != nullptr);
|
2024-02-23 18:22:33 +01:00
|
|
|
auto can_be_edited = can_edit_quick_reply_message(m);
|
2024-02-21 20:43:29 +01:00
|
|
|
return td_api::make_object<td_api::quickReplyMessage>(
|
2024-02-27 18:52:59 +01:00
|
|
|
m->message_id.get(), get_message_sending_state_object(m), can_be_edited, m->reply_to_message_id.get(),
|
|
|
|
td_->contacts_manager_->get_user_id_object(m->via_bot_user_id, "via_bot_user_id"), m->media_album_id,
|
|
|
|
get_quick_reply_message_message_content_object(m));
|
2024-02-21 20:43:29 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 17:45:49 +01:00
|
|
|
int32 QuickReplyManager::get_shortcut_message_count(const Shortcut *s) {
|
2024-02-29 13:55:38 +01:00
|
|
|
return s->server_total_count_ + s->local_total_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QuickReplyManager::have_all_shortcut_messages(const Shortcut *s) {
|
|
|
|
return static_cast<int32>(s->messages_.size()) == get_shortcut_message_count(s);
|
2024-02-22 17:45:49 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 15:32:12 +01:00
|
|
|
td_api::object_ptr<td_api::quickReplyShortcut> QuickReplyManager::get_quick_reply_shortcut_object(
|
|
|
|
const Shortcut *s, const char *source) const {
|
|
|
|
CHECK(s != nullptr);
|
|
|
|
CHECK(!s->messages_.empty());
|
2024-02-26 16:18:53 +01:00
|
|
|
return td_api::make_object<td_api::quickReplyShortcut>(s->shortcut_id_.get(), s->name_,
|
|
|
|
get_quick_reply_message_object(s->messages_[0].get(), source),
|
|
|
|
get_shortcut_message_count(s));
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 21:04:36 +01:00
|
|
|
void QuickReplyManager::get_quick_reply_shortcuts(Promise<Unit> &&promise) {
|
2024-02-22 15:32:12 +01:00
|
|
|
if (shortcuts_.are_inited_) {
|
2024-02-22 21:04:36 +01:00
|
|
|
return promise.set_value(Unit());
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
load_quick_reply_shortcuts(std::move(promise));
|
|
|
|
}
|
|
|
|
|
2024-02-22 21:04:36 +01:00
|
|
|
void QuickReplyManager::load_quick_reply_shortcuts(Promise<Unit> &&promise) {
|
2024-02-22 15:32:12 +01:00
|
|
|
shortcuts_.load_queries_.push_back(std::move(promise));
|
|
|
|
if (shortcuts_.load_queries_.size() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reload_quick_reply_shortcuts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::reload_quick_reply_shortcuts() {
|
|
|
|
auto promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::messages_QuickReplies>> r_shortcuts) {
|
|
|
|
send_closure(actor_id, &QuickReplyManager::on_reload_quick_reply_shortcuts, std::move(r_shortcuts));
|
|
|
|
});
|
2024-02-23 12:49:29 +01:00
|
|
|
td_->create_handler<GetQuickRepliesQuery>(std::move(promise))->send(get_shortcuts_hash());
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::on_reload_quick_reply_shortcuts(
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::messages_QuickReplies>> r_shortcuts) {
|
|
|
|
G()->ignore_result_if_closing(r_shortcuts);
|
|
|
|
if (r_shortcuts.is_error()) {
|
|
|
|
return on_load_quick_reply_fail(r_shortcuts.move_as_error());
|
|
|
|
}
|
|
|
|
auto shortcuts_ptr = r_shortcuts.move_as_ok();
|
|
|
|
switch (shortcuts_ptr->get_id()) {
|
|
|
|
case telegram_api::messages_quickRepliesNotModified::ID:
|
|
|
|
if (!shortcuts_.are_inited_) {
|
|
|
|
shortcuts_.are_inited_ = true;
|
2024-02-27 21:47:29 +01:00
|
|
|
save_quick_reply_shortcuts();
|
2024-02-22 20:55:09 +01:00
|
|
|
send_update_quick_reply_shortcuts();
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case telegram_api::messages_quickReplies::ID: {
|
|
|
|
auto shortcuts = telegram_api::move_object_as<telegram_api::messages_quickReplies>(shortcuts_ptr);
|
|
|
|
td_->contacts_manager_->on_get_users(std::move(shortcuts->users_), "messages.quickReplies");
|
|
|
|
td_->contacts_manager_->on_get_chats(std::move(shortcuts->chats_), "messages.quickReplies");
|
|
|
|
|
|
|
|
FlatHashMap<MessageId, telegram_api::object_ptr<telegram_api::Message>, MessageIdHash> message_id_to_message;
|
|
|
|
for (auto &message : shortcuts->messages_) {
|
|
|
|
auto message_id = MessageId::get_message_id(message, false);
|
|
|
|
if (!message_id.is_valid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
message_id_to_message[message_id] = std::move(message);
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:16:56 +01:00
|
|
|
FlatHashSet<QuickReplyShortcutId, QuickReplyShortcutIdHash> old_shortcut_ids;
|
2024-02-22 19:23:19 +01:00
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
old_shortcut_ids.insert(shortcut->shortcut_id_);
|
|
|
|
}
|
2024-02-24 01:16:56 +01:00
|
|
|
FlatHashSet<QuickReplyShortcutId, QuickReplyShortcutIdHash> added_shortcut_ids;
|
2024-02-22 15:32:12 +01:00
|
|
|
FlatHashSet<string> added_shortcut_names;
|
|
|
|
vector<unique_ptr<Shortcut>> new_shortcuts;
|
2024-02-24 01:16:56 +01:00
|
|
|
vector<QuickReplyShortcutId> changed_shortcut_ids;
|
2024-02-29 13:55:38 +01:00
|
|
|
vector<QuickReplyShortcutId> changed_message_shortcut_ids;
|
2024-02-24 01:16:56 +01:00
|
|
|
vector<QuickReplyShortcutId> deleted_shortcut_ids;
|
2024-02-22 15:32:12 +01:00
|
|
|
for (auto &quick_reply : shortcuts->quick_replies_) {
|
2024-02-24 01:16:56 +01:00
|
|
|
auto shortcut_id = QuickReplyShortcutId(quick_reply->shortcut_id_);
|
2024-02-24 01:28:55 +01:00
|
|
|
if (!shortcut_id.is_server() || quick_reply->shortcut_.empty() || quick_reply->count_ <= 0 ||
|
2024-02-22 15:32:12 +01:00
|
|
|
quick_reply->top_message_ <= 0) {
|
|
|
|
LOG(ERROR) << "Receive " << to_string(quick_reply);
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-24 01:16:56 +01:00
|
|
|
if (added_shortcut_ids.count(shortcut_id) || added_shortcut_names.count(quick_reply->shortcut_)) {
|
2024-02-22 15:32:12 +01:00
|
|
|
LOG(ERROR) << "Receive duplicate " << to_string(quick_reply);
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-24 01:16:56 +01:00
|
|
|
if (deleted_shortcut_ids_.count(shortcut_id)) {
|
2024-02-24 01:06:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
2024-02-24 01:16:56 +01:00
|
|
|
added_shortcut_ids.insert(shortcut_id);
|
2024-02-22 15:32:12 +01:00
|
|
|
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()) {
|
2024-02-24 01:16:56 +01:00
|
|
|
LOG(ERROR) << "Can't find last " << first_message_id << " in " << shortcut_id;
|
2024-02-22 15:32:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto message = create_message(std::move(it->second), "on_reload_quick_reply_shortcuts");
|
|
|
|
message_id_to_message.erase(it);
|
|
|
|
if (message == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-24 01:16:56 +01:00
|
|
|
if (message->shortcut_id != shortcut_id) {
|
|
|
|
LOG(ERROR) << "Receive message from " << message->shortcut_id << " instead of " << shortcut_id;
|
2024-02-24 01:06:20 +01:00
|
|
|
continue;
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto shortcut = td::make_unique<Shortcut>();
|
|
|
|
shortcut->name_ = std::move(quick_reply->shortcut_);
|
2024-02-24 01:16:56 +01:00
|
|
|
shortcut->shortcut_id_ = shortcut_id;
|
2024-02-22 21:08:38 +01:00
|
|
|
shortcut->server_total_count_ = quick_reply->count_;
|
2024-02-22 15:32:12 +01:00
|
|
|
shortcut->messages_.push_back(std::move(message));
|
2024-02-22 17:45:49 +01:00
|
|
|
|
2024-02-24 01:16:56 +01:00
|
|
|
auto old_shortcut = get_shortcut(shortcut_id);
|
2024-02-22 19:57:43 +01:00
|
|
|
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) {
|
2024-02-24 01:16:56 +01:00
|
|
|
changed_shortcut_ids.push_back(shortcut_id);
|
2024-02-22 19:57:43 +01:00
|
|
|
}
|
2024-02-29 13:55:38 +01:00
|
|
|
changed_message_shortcut_ids.push_back(shortcut_id);
|
2024-02-22 17:45:49 +01:00
|
|
|
}
|
2024-02-24 01:16:56 +01:00
|
|
|
old_shortcut_ids.erase(shortcut_id);
|
2024-02-22 17:45:49 +01:00
|
|
|
|
2024-02-22 15:32:12 +01:00
|
|
|
new_shortcuts.push_back(std::move(shortcut));
|
|
|
|
}
|
2024-02-22 19:23:19 +01:00
|
|
|
for (auto shortcut_id : old_shortcut_ids) {
|
|
|
|
auto old_shortcut = get_shortcut(shortcut_id);
|
|
|
|
CHECK(old_shortcut != nullptr);
|
2024-02-29 17:01:40 +01:00
|
|
|
auto is_changed = td::remove_if(old_shortcut->messages_, [&](const unique_ptr<QuickReplyMessage> &message) {
|
|
|
|
if (message->message_id.is_server()) {
|
|
|
|
delete_message_files(shortcut_id, message.get());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2024-02-22 19:23:19 +01:00
|
|
|
});
|
|
|
|
if (old_shortcut->messages_.empty()) {
|
|
|
|
CHECK(is_changed);
|
|
|
|
send_update_quick_reply_shortcut_deleted(old_shortcut);
|
|
|
|
} else {
|
|
|
|
// some local messages has left
|
2024-02-26 11:48:19 +01:00
|
|
|
if (added_shortcut_names.count(old_shortcut->name_)) {
|
|
|
|
// a local shortcut has been created server-side
|
|
|
|
for (auto &shortcut : new_shortcuts) {
|
|
|
|
if (shortcut->name_ == old_shortcut->name_) {
|
|
|
|
CHECK(shortcut->local_total_count_ == 0);
|
|
|
|
shortcut->local_total_count_ = static_cast<int32>(old_shortcut->messages_.size());
|
|
|
|
append(shortcut->messages_, std::move(old_shortcut->messages_));
|
|
|
|
sort_quick_reply_messages(shortcut->messages_);
|
|
|
|
send_update_quick_reply_shortcut_deleted(old_shortcut);
|
2024-02-29 13:55:38 +01:00
|
|
|
changed_shortcut_ids.push_back(shortcut->shortcut_id_);
|
|
|
|
changed_message_shortcut_ids.push_back(shortcut->shortcut_id_);
|
2024-02-26 11:48:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:23:19 +01:00
|
|
|
auto shortcut = td::make_unique<Shortcut>();
|
|
|
|
shortcut->name_ = std::move(old_shortcut->name_);
|
|
|
|
shortcut->shortcut_id_ = old_shortcut->shortcut_id_;
|
2024-02-22 21:08:38 +01:00
|
|
|
shortcut->server_total_count_ = 0;
|
|
|
|
shortcut->local_total_count_ = static_cast<int32>(old_shortcut->messages_.size());
|
2024-02-22 19:23:19 +01:00
|
|
|
shortcut->messages_ = std::move(old_shortcut->messages_);
|
|
|
|
if (is_changed) {
|
2024-02-29 13:55:38 +01:00
|
|
|
changed_shortcut_ids.push_back(shortcut->shortcut_id_);
|
|
|
|
changed_message_shortcut_ids.push_back(shortcut->shortcut_id_);
|
2024-02-22 19:23:19 +01:00
|
|
|
}
|
|
|
|
new_shortcuts.push_back(std::move(shortcut));
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 12:22:01 +01:00
|
|
|
bool is_list_changed = is_shortcut_list_changed(new_shortcuts);
|
2024-02-22 15:32:12 +01:00
|
|
|
shortcuts_.shortcuts_ = std::move(new_shortcuts);
|
|
|
|
shortcuts_.are_inited_ = true;
|
2024-02-22 19:23:19 +01:00
|
|
|
|
2024-02-27 21:47:29 +01:00
|
|
|
save_quick_reply_shortcuts();
|
2024-02-22 19:23:19 +01:00
|
|
|
for (auto shortcut_id : changed_shortcut_ids) {
|
2024-02-29 13:55:38 +01:00
|
|
|
send_update_quick_reply_shortcut(get_shortcut(shortcut_id), "on_reload_quick_reply_shortcuts");
|
|
|
|
}
|
|
|
|
for (auto shortcut_id : changed_message_shortcut_ids) {
|
|
|
|
const auto *s = get_shortcut(shortcut_id);
|
2024-02-29 14:10:15 +01:00
|
|
|
send_update_quick_reply_shortcut_messages(s, "on_reload_quick_reply_shortcuts");
|
2024-02-22 19:23:19 +01:00
|
|
|
}
|
2024-02-22 20:55:09 +01:00
|
|
|
if (is_list_changed) {
|
|
|
|
send_update_quick_reply_shortcuts();
|
|
|
|
}
|
2024-02-22 15:32:12 +01:00
|
|
|
break;
|
|
|
|
}
|
2024-02-28 15:19:28 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
on_load_quick_reply_success();
|
|
|
|
}
|
|
|
|
|
2024-02-24 12:22:01 +01:00
|
|
|
bool QuickReplyManager::is_shortcut_list_changed(const vector<unique_ptr<Shortcut>> &new_shortcuts) const {
|
|
|
|
if (!shortcuts_.are_inited_ || shortcuts_.shortcuts_.size() != new_shortcuts.size()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < new_shortcuts.size(); i++) {
|
2024-02-27 16:10:11 +01:00
|
|
|
if (shortcuts_.shortcuts_[i]->shortcut_id_ != new_shortcuts[i]->shortcut_id_) {
|
2024-02-24 12:22:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-22 15:32:12 +01:00
|
|
|
void QuickReplyManager::on_load_quick_reply_success() {
|
2024-02-28 15:42:41 +01:00
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
reload_quick_reply_messages(shortcut->shortcut_id_, Auto());
|
|
|
|
}
|
2024-02-22 21:04:36 +01:00
|
|
|
set_promises(shortcuts_.load_queries_);
|
2024-02-22 15:32:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::on_load_quick_reply_fail(Status error) {
|
|
|
|
fail_promises(shortcuts_.load_queries_, std::move(error));
|
|
|
|
}
|
|
|
|
|
2024-02-23 12:49:29 +01:00
|
|
|
int64 QuickReplyManager::get_shortcuts_hash() const {
|
|
|
|
vector<uint64> numbers;
|
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
for (auto &message : shortcut->messages_) {
|
|
|
|
if (message->message_id.is_server()) {
|
2024-02-24 01:16:56 +01:00
|
|
|
numbers.push_back(shortcut->shortcut_id_.get());
|
2024-02-23 12:49:29 +01:00
|
|
|
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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get_vector_hash(numbers);
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:10:11 +01:00
|
|
|
void QuickReplyManager::delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise) {
|
|
|
|
auto it = get_shortcut_it(shortcut_id);
|
2024-02-24 11:37:02 +01:00
|
|
|
if (it == shortcuts_.shortcuts_.end()) {
|
2024-02-24 01:02:42 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Shortcut not found"));
|
|
|
|
}
|
2024-02-28 15:19:28 +01:00
|
|
|
send_update_quick_reply_shortcut_deleted(it->get());
|
2024-02-24 11:37:02 +01:00
|
|
|
shortcuts_.shortcuts_.erase(it);
|
2024-02-27 21:47:29 +01:00
|
|
|
save_quick_reply_shortcuts();
|
2024-02-28 15:19:28 +01:00
|
|
|
send_update_quick_reply_shortcuts();
|
2024-02-24 01:02:42 +01:00
|
|
|
|
2024-02-24 01:28:55 +01:00
|
|
|
if (!shortcut_id.is_server()) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_quick_reply_shortcut_from_server(shortcut_id, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::delete_quick_reply_shortcut_from_server(QuickReplyShortcutId shortcut_id,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
CHECK(shortcut_id.is_server());
|
|
|
|
|
|
|
|
deleted_shortcut_ids_.insert(shortcut_id);
|
2024-02-24 01:02:42 +01:00
|
|
|
td_->create_handler<DeleteQuickReplyShortcutQuery>(std::move(promise))->send(shortcut_id);
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:10:11 +01:00
|
|
|
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) {
|
2024-02-24 12:40:25 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Shortcut not found"));
|
|
|
|
}
|
2024-02-27 16:10:11 +01:00
|
|
|
unique_shortcut_ids.insert(shortcut_id);
|
2024-02-24 12:40:25 +01:00
|
|
|
}
|
2024-02-27 16:10:11 +01:00
|
|
|
if (unique_shortcut_ids.size() != shortcut_ids.size()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Duplicate shortcut identifiers specified"));
|
2024-02-24 12:40:25 +01:00
|
|
|
}
|
|
|
|
if (!shortcuts_.are_inited_) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
2024-02-27 16:10:11 +01:00
|
|
|
auto old_shortcut_ids = get_shortcut_ids();
|
|
|
|
auto old_server_shortcut_ids = get_server_shortcut_ids();
|
2024-02-24 12:40:25 +01:00
|
|
|
vector<unique_ptr<Shortcut>> shortcuts;
|
2024-02-27 16:10:11 +01:00
|
|
|
for (const auto &shortcut_id : shortcut_ids) {
|
|
|
|
auto it = get_shortcut_it(shortcut_id);
|
2024-02-24 12:40:25 +01:00
|
|
|
CHECK(it != shortcuts_.shortcuts_.end() && *it != nullptr);
|
|
|
|
shortcuts.push_back(std::move(*it));
|
|
|
|
}
|
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
if (shortcut != nullptr) {
|
2024-02-27 16:10:11 +01:00
|
|
|
CHECK(unique_shortcut_ids.count(shortcut->shortcut_id_) == 0);
|
2024-02-24 12:40:25 +01:00
|
|
|
shortcuts.push_back(std::move(shortcut));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shortcuts_.shortcuts_ = std::move(shortcuts);
|
2024-02-27 21:47:29 +01:00
|
|
|
if (old_shortcut_ids == get_shortcut_ids()) {
|
2024-02-24 12:40:25 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
2024-02-27 21:47:29 +01:00
|
|
|
save_quick_reply_shortcuts();
|
2024-02-24 12:40:25 +01:00
|
|
|
send_update_quick_reply_shortcuts();
|
|
|
|
|
|
|
|
auto new_server_shortcut_ids = get_server_shortcut_ids();
|
2024-02-27 16:10:11 +01:00
|
|
|
if (new_server_shortcut_ids == old_server_shortcut_ids) {
|
2024-02-24 12:40:25 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
reorder_quick_reply_shortcuts_on_server(new_server_shortcut_ids, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::reorder_quick_reply_shortcuts_on_server(vector<QuickReplyShortcutId> shortcut_ids,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
td_->create_handler<ReorderQuickRepliesQuery>(std::move(promise))->send(std::move(shortcut_ids));
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:39:38 +01:00
|
|
|
void QuickReplyManager::update_quick_reply_message(telegram_api::object_ptr<telegram_api::Message> &&message_ptr) {
|
|
|
|
auto message = create_message(std::move(message_ptr), "update_quick_reply_message");
|
|
|
|
if (message == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto shortcut_id = message->shortcut_id;
|
|
|
|
auto *s = get_shortcut(shortcut_id);
|
|
|
|
if (s == nullptr) {
|
|
|
|
return reload_quick_reply_messages(shortcut_id, Promise<Unit>());
|
|
|
|
}
|
2024-02-29 15:05:00 +01:00
|
|
|
on_get_quick_reply_message(s, std::move(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::on_get_quick_reply_message(Shortcut *s, unique_ptr<QuickReplyMessage> message) {
|
2024-02-29 14:39:38 +01:00
|
|
|
auto it = get_message_it(s, message->message_id);
|
|
|
|
if (it == s->messages_.end()) {
|
|
|
|
s->messages_.push_back(std::move(message));
|
|
|
|
sort_quick_reply_messages(s->messages_);
|
|
|
|
send_update_quick_reply_shortcut(s, "update_quick_reply_message 1");
|
|
|
|
} else {
|
|
|
|
if (get_quick_reply_unique_id(it->get()) == get_quick_reply_unique_id(message.get())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*it = std::move(message);
|
|
|
|
if (it == s->messages_.begin()) {
|
|
|
|
send_update_quick_reply_shortcut(s, "update_quick_reply_message 2");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
send_update_quick_reply_shortcut_messages(s, "update_quick_reply_message 2");
|
2024-02-29 15:05:00 +01:00
|
|
|
save_quick_reply_shortcuts();
|
2024-02-29 14:39:38 +01:00
|
|
|
}
|
|
|
|
|
2024-02-29 12:43:32 +01:00
|
|
|
void QuickReplyManager::delete_quick_reply_messages(QuickReplyShortcutId shortcut_id,
|
|
|
|
const vector<MessageId> &message_ids) {
|
|
|
|
auto s = get_shortcut(shortcut_id);
|
|
|
|
if (s == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2024-02-29 15:27:12 +01:00
|
|
|
for (auto message_id : message_ids) {
|
|
|
|
if (!message_id.is_server()) {
|
|
|
|
LOG(ERROR) << "Receive delete of " << message_ids;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 15:21:55 +01:00
|
|
|
delete_quick_reply_messages(s, message_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::delete_quick_reply_messages(Shortcut *s, const vector<MessageId> &message_ids) {
|
2024-02-29 12:43:32 +01:00
|
|
|
bool is_changed = false;
|
|
|
|
bool is_list_changed = false;
|
|
|
|
for (auto &message_id : message_ids) {
|
|
|
|
auto it = get_message_it(s, message_id);
|
2024-02-29 15:27:12 +01:00
|
|
|
if (it != s->messages_.end()) {
|
2024-02-29 17:01:40 +01:00
|
|
|
delete_message_files(s->shortcut_id_, it->get());
|
2024-02-29 12:43:32 +01:00
|
|
|
if (it == s->messages_.begin()) {
|
|
|
|
is_list_changed = true;
|
|
|
|
}
|
|
|
|
is_changed = true;
|
|
|
|
s->messages_.erase(it);
|
|
|
|
}
|
2024-02-29 15:27:12 +01:00
|
|
|
if (message_id.is_server()) {
|
|
|
|
deleted_message_full_ids_.insert({s->shortcut_id_, message_id});
|
|
|
|
}
|
2024-02-29 12:43:32 +01:00
|
|
|
}
|
|
|
|
if (s->messages_.empty()) {
|
|
|
|
send_update_quick_reply_shortcut_deleted(s);
|
2024-02-29 15:21:55 +01:00
|
|
|
shortcuts_.shortcuts_.erase(get_shortcut_it(s->shortcut_id_));
|
2024-02-29 12:43:32 +01:00
|
|
|
CHECK(is_list_changed && is_changed);
|
2024-02-29 13:55:38 +01:00
|
|
|
} else if (is_changed) {
|
2024-02-29 14:10:15 +01:00
|
|
|
send_update_quick_reply_shortcut_messages(s, "delete_quick_reply_messages");
|
2024-02-29 12:43:32 +01:00
|
|
|
}
|
|
|
|
if (is_list_changed) {
|
|
|
|
send_update_quick_reply_shortcuts();
|
|
|
|
}
|
|
|
|
if (is_changed) {
|
|
|
|
save_quick_reply_shortcuts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:05:16 +01:00
|
|
|
void QuickReplyManager::get_quick_reply_shortcut_messages(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise) {
|
2024-02-28 15:19:28 +01:00
|
|
|
auto s = get_shortcut(shortcut_id);
|
|
|
|
if (s == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Shortcut not found"));
|
|
|
|
}
|
2024-02-29 13:55:38 +01:00
|
|
|
if (have_all_shortcut_messages(s)) {
|
2024-02-29 14:05:16 +01:00
|
|
|
return promise.set_value(Unit());
|
2024-02-28 15:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(shortcut_id.is_server());
|
|
|
|
reload_quick_reply_messages(shortcut_id, std::move(promise));
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:05:16 +01:00
|
|
|
void QuickReplyManager::reload_quick_reply_messages(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise) {
|
2024-02-28 15:19:28 +01:00
|
|
|
auto &queries = get_shortcut_messages_queries_[shortcut_id];
|
|
|
|
queries.push_back(std::move(promise));
|
|
|
|
if (queries.size() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto query_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), shortcut_id](
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::messages_Messages>> r_messages) {
|
|
|
|
send_closure(actor_id, &QuickReplyManager::on_reload_quick_reply_messages, shortcut_id, std::move(r_messages));
|
|
|
|
});
|
2024-02-28 15:38:17 +01:00
|
|
|
td_->create_handler<GetQuickReplyMessagesQuery>(std::move(query_promise))
|
|
|
|
->send(shortcut_id, vector<MessageId>(), get_quick_reply_messages_hash(get_shortcut(shortcut_id)));
|
2024-02-28 15:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::on_reload_quick_reply_messages(
|
|
|
|
QuickReplyShortcutId shortcut_id, Result<telegram_api::object_ptr<telegram_api::messages_Messages>> r_messages) {
|
|
|
|
G()->ignore_result_if_closing(r_messages);
|
|
|
|
auto queries_it = get_shortcut_messages_queries_.find(shortcut_id);
|
|
|
|
CHECK(queries_it != get_shortcut_messages_queries_.end());
|
|
|
|
CHECK(!queries_it->second.empty());
|
|
|
|
auto promises = std::move(queries_it->second);
|
|
|
|
get_shortcut_messages_queries_.erase(queries_it);
|
|
|
|
if (r_messages.is_error()) {
|
|
|
|
return fail_promises(promises, r_messages.move_as_error());
|
|
|
|
}
|
|
|
|
auto messages_ptr = r_messages.move_as_ok();
|
|
|
|
switch (messages_ptr->get_id()) {
|
|
|
|
case telegram_api::messages_messagesSlice::ID:
|
|
|
|
case telegram_api::messages_channelMessages::ID:
|
|
|
|
LOG(ERROR) << "Receive " << to_string(messages_ptr);
|
|
|
|
break;
|
|
|
|
case telegram_api::messages_messagesNotModified::ID:
|
|
|
|
break;
|
|
|
|
case telegram_api::messages_messages::ID: {
|
|
|
|
auto messages = telegram_api::move_object_as<telegram_api::messages_messages>(messages_ptr);
|
|
|
|
td_->contacts_manager_->on_get_users(std::move(messages->users_), "on_reload_quick_reply_messages");
|
|
|
|
td_->contacts_manager_->on_get_chats(std::move(messages->chats_), "on_reload_quick_reply_messages");
|
|
|
|
|
|
|
|
vector<unique_ptr<QuickReplyMessage>> quick_reply_messages;
|
|
|
|
for (auto &server_message : messages->messages_) {
|
|
|
|
auto message = create_message(std::move(server_message), "on_reload_quick_reply_messages");
|
|
|
|
if (message == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (message->shortcut_id != shortcut_id) {
|
|
|
|
LOG(ERROR) << "Receive message from " << message->shortcut_id << " instead of " << shortcut_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
quick_reply_messages.push_back(std::move(message));
|
|
|
|
}
|
|
|
|
auto it = get_shortcut_it(shortcut_id);
|
|
|
|
if (quick_reply_messages.empty()) {
|
|
|
|
if (it != shortcuts_.shortcuts_.end()) {
|
|
|
|
send_update_quick_reply_shortcut_deleted(it->get());
|
|
|
|
shortcuts_.shortcuts_.erase(it);
|
|
|
|
save_quick_reply_shortcuts();
|
|
|
|
send_update_quick_reply_shortcuts();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:39:38 +01:00
|
|
|
auto *old_shortcut = it != shortcuts_.shortcuts_.end() ? it->get() : nullptr;
|
2024-02-28 15:19:28 +01:00
|
|
|
auto shortcut = td::make_unique<Shortcut>();
|
|
|
|
shortcut->name_ = old_shortcut->name_;
|
|
|
|
shortcut->shortcut_id_ = shortcut_id;
|
|
|
|
shortcut->server_total_count_ = quick_reply_messages.size();
|
|
|
|
shortcut->messages_ = std::move(quick_reply_messages);
|
|
|
|
|
|
|
|
auto is_object_changed = false;
|
2024-02-29 14:39:38 +01:00
|
|
|
if (old_shortcut == nullptr || update_shortcut_from(shortcut.get(), old_shortcut, false, &is_object_changed)) {
|
2024-02-29 13:55:38 +01:00
|
|
|
CHECK(have_all_shortcut_messages(shortcut.get()));
|
2024-02-29 14:39:38 +01:00
|
|
|
if (old_shortcut == nullptr) {
|
|
|
|
send_update_quick_reply_shortcut(shortcut.get(), "on_reload_quick_reply_messages 1");
|
|
|
|
}
|
2024-02-29 14:10:15 +01:00
|
|
|
send_update_quick_reply_shortcut_messages(shortcut.get(), "on_reload_quick_reply_messages");
|
2024-02-29 13:55:38 +01:00
|
|
|
}
|
2024-02-29 14:39:38 +01:00
|
|
|
if (old_shortcut == nullptr) {
|
|
|
|
shortcuts_.shortcuts_.push_back(std::move(shortcut));
|
|
|
|
} else {
|
|
|
|
*it = std::move(shortcut);
|
|
|
|
}
|
2024-02-28 15:19:28 +01:00
|
|
|
if (is_object_changed) {
|
2024-02-29 14:39:38 +01:00
|
|
|
send_update_quick_reply_shortcut(it->get(), "on_reload_quick_reply_messages 2");
|
2024-02-28 15:19:28 +01:00
|
|
|
}
|
|
|
|
save_quick_reply_shortcuts();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
auto s = get_shortcut(shortcut_id);
|
|
|
|
if (s == nullptr) {
|
|
|
|
return fail_promises(promises, Status::Error(400, "Shortcut not found"));
|
|
|
|
}
|
|
|
|
for (auto &promise : promises) {
|
2024-02-29 14:05:16 +01:00
|
|
|
promise.set_value(Unit());
|
2024-02-28 15:19:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 15:38:17 +01:00
|
|
|
int64 QuickReplyManager::get_quick_reply_messages_hash(const Shortcut *s) {
|
|
|
|
CHECK(s != nullptr);
|
|
|
|
vector<uint64> numbers;
|
2024-02-29 13:55:38 +01:00
|
|
|
for (const auto &message : s->messages_) {
|
2024-02-28 15:38:17 +01:00
|
|
|
if (message->message_id.is_server()) {
|
|
|
|
numbers.push_back(message->message_id.get_server_message_id().get());
|
|
|
|
numbers.push_back(message->edit_date);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get_vector_hash(numbers);
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:05:00 +01:00
|
|
|
void QuickReplyManager::reload_quick_reply_message(QuickReplyShortcutId shortcut_id, MessageId message_id,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
auto s = get_shortcut(shortcut_id);
|
|
|
|
if (s == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Shortcut not found"));
|
|
|
|
}
|
|
|
|
if (!message_id.is_server()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Message can't be reloaded"));
|
|
|
|
}
|
|
|
|
auto query_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), shortcut_id, message_id, promise = std::move(promise)](
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::messages_Messages>> r_messages) mutable {
|
|
|
|
send_closure(actor_id, &QuickReplyManager::on_reload_quick_reply_message, shortcut_id, message_id,
|
|
|
|
std::move(r_messages), std::move(promise));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetQuickReplyMessagesQuery>(std::move(query_promise))
|
|
|
|
->send(shortcut_id, vector<MessageId>{message_id}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::on_reload_quick_reply_message(
|
|
|
|
QuickReplyShortcutId shortcut_id, MessageId message_id,
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::messages_Messages>> r_messages, Promise<Unit> &&promise) {
|
|
|
|
G()->ignore_result_if_closing(r_messages);
|
|
|
|
if (r_messages.is_error()) {
|
|
|
|
return promise.set_error(r_messages.move_as_error());
|
|
|
|
}
|
|
|
|
auto s = get_shortcut(shortcut_id);
|
|
|
|
if (s == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Shortcut not found"));
|
|
|
|
}
|
|
|
|
auto messages_ptr = r_messages.move_as_ok();
|
|
|
|
switch (messages_ptr->get_id()) {
|
|
|
|
case telegram_api::messages_messagesSlice::ID:
|
|
|
|
case telegram_api::messages_channelMessages::ID:
|
|
|
|
case telegram_api::messages_messagesNotModified::ID:
|
|
|
|
LOG(ERROR) << "Receive " << to_string(messages_ptr);
|
|
|
|
return promise.set_error(Status::Error(400, "Receive wrong response"));
|
|
|
|
case telegram_api::messages_messages::ID: {
|
|
|
|
auto messages = telegram_api::move_object_as<telegram_api::messages_messages>(messages_ptr);
|
|
|
|
td_->contacts_manager_->on_get_users(std::move(messages->users_), "on_reload_quick_reply_messages");
|
|
|
|
td_->contacts_manager_->on_get_chats(std::move(messages->chats_), "on_reload_quick_reply_messages");
|
|
|
|
|
|
|
|
if (messages->messages_.size() > 1u) {
|
|
|
|
LOG(ERROR) << "Receive " << to_string(messages_ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
unique_ptr<QuickReplyMessage> message;
|
|
|
|
if (messages->messages_.size() == 1u) {
|
|
|
|
message = create_message(std::move(messages->messages_[0]), "on_reload_quick_reply_message");
|
|
|
|
}
|
|
|
|
if (message == nullptr) {
|
2024-02-29 15:21:55 +01:00
|
|
|
delete_quick_reply_messages(s, {message_id});
|
2024-02-29 15:05:00 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Message not found"));
|
|
|
|
}
|
|
|
|
if (message->shortcut_id != shortcut_id) {
|
|
|
|
LOG(ERROR) << "Receive message from " << message->shortcut_id << " instead of " << shortcut_id;
|
|
|
|
return promise.set_error(Status::Error(400, "Message not found"));
|
|
|
|
}
|
|
|
|
on_get_quick_reply_message(s, std::move(message));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:16:56 +01:00
|
|
|
QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(QuickReplyShortcutId shortcut_id) {
|
2024-02-22 17:45:49 +01:00
|
|
|
if (!shortcuts_.are_inited_) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
if (shortcut->shortcut_id_ == shortcut_id) {
|
|
|
|
return shortcut.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:02:42 +01:00
|
|
|
QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(const string &name) {
|
|
|
|
if (!shortcuts_.are_inited_) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
if (shortcut->name_ == name) {
|
|
|
|
return shortcut.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:10:11 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:37:02 +01:00
|
|
|
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) {
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return shortcuts_.shortcuts_.end();
|
|
|
|
}
|
|
|
|
|
2024-02-29 12:43:32 +01:00
|
|
|
vector<unique_ptr<QuickReplyManager::QuickReplyMessage>>::iterator QuickReplyManager::get_message_it(
|
|
|
|
Shortcut *s, MessageId message_id) {
|
|
|
|
CHECK(s != nullptr);
|
|
|
|
for (auto it = s->messages_.begin(); it != s->messages_.end(); ++it) {
|
|
|
|
if ((*it)->message_id == message_id) {
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s->messages_.end();
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:10:11 +01:00
|
|
|
vector<QuickReplyShortcutId> QuickReplyManager::get_shortcut_ids() const {
|
|
|
|
return transform(shortcuts_.shortcuts_, [](const unique_ptr<Shortcut> &shortcut) { return shortcut->shortcut_id_; });
|
|
|
|
}
|
|
|
|
|
2024-02-24 12:40:25 +01:00
|
|
|
vector<QuickReplyShortcutId> QuickReplyManager::get_server_shortcut_ids() const {
|
|
|
|
vector<QuickReplyShortcutId> shortcut_ids;
|
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
if (shortcut->shortcut_id_.is_server()) {
|
|
|
|
shortcut_ids.push_back(shortcut->shortcut_id_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return shortcut_ids;
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:45:49 +01:00
|
|
|
void QuickReplyManager::sort_quick_reply_messages(vector<unique_ptr<QuickReplyMessage>> &messages) {
|
|
|
|
std::sort(messages.begin(), messages.end(),
|
|
|
|
[](const unique_ptr<QuickReplyMessage> &lhs, const unique_ptr<QuickReplyMessage> &rhs) {
|
|
|
|
return lhs->message_id < rhs->message_id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:47:34 +01:00
|
|
|
QuickReplyManager::QuickReplyMessageUniqueId QuickReplyManager::get_quick_reply_unique_id(const QuickReplyMessage *m) {
|
|
|
|
return QuickReplyMessageUniqueId(m->message_id, m->edit_date);
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:44:13 +01:00
|
|
|
vector<QuickReplyManager::QuickReplyMessageUniqueId> QuickReplyManager::get_quick_reply_unique_ids(
|
2024-02-22 17:45:49 +01:00
|
|
|
const vector<unique_ptr<QuickReplyMessage>> &messages) {
|
2024-02-22 19:47:34 +01:00
|
|
|
return transform(
|
|
|
|
messages, [](const unique_ptr<QuickReplyMessage> &message) { return get_quick_reply_unique_id(message.get()); });
|
2024-02-22 17:45:49 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 19:44:13 +01:00
|
|
|
vector<QuickReplyManager::QuickReplyMessageUniqueId> QuickReplyManager::get_server_quick_reply_unique_ids(
|
2024-02-22 17:45:49 +01:00
|
|
|
const vector<unique_ptr<QuickReplyMessage>> &messages) {
|
2024-02-22 19:44:13 +01:00
|
|
|
auto message_ids = get_quick_reply_unique_ids(messages);
|
|
|
|
td::remove_if(message_ids, [](const QuickReplyMessageUniqueId &message_id) { return !message_id.first.is_server(); });
|
2024-02-22 17:45:49 +01:00
|
|
|
return message_ids;
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:57:43 +01:00
|
|
|
bool QuickReplyManager::update_shortcut_from(Shortcut *new_shortcut, Shortcut *old_shortcut, bool is_partial,
|
|
|
|
bool *is_object_changed) {
|
2024-02-22 17:45:49 +01:00
|
|
|
CHECK(old_shortcut != nullptr);
|
|
|
|
CHECK(new_shortcut != nullptr);
|
|
|
|
CHECK(old_shortcut->shortcut_id_ == new_shortcut->shortcut_id_);
|
|
|
|
CHECK(!old_shortcut->messages_.empty());
|
|
|
|
CHECK(!new_shortcut->messages_.empty());
|
2024-02-22 19:57:43 +01:00
|
|
|
auto old_unique_id = get_quick_reply_unique_id(old_shortcut->messages_[0].get());
|
2024-02-22 17:45:49 +01:00
|
|
|
auto old_message_count = get_shortcut_message_count(old_shortcut);
|
|
|
|
bool is_changed = false;
|
|
|
|
if (is_partial) {
|
|
|
|
// only the first server message is known
|
|
|
|
// delete all definitely deleted server messages and insert the new message in the correct place
|
2024-02-22 19:44:13 +01:00
|
|
|
auto old_message_ids = get_quick_reply_unique_ids(old_shortcut->messages_);
|
2024-02-22 17:45:49 +01:00
|
|
|
auto new_first_message_id = new_shortcut->messages_[0]->message_id;
|
|
|
|
auto it = old_shortcut->messages_.begin();
|
|
|
|
while (it != old_shortcut->messages_.end() && (*it)->message_id < new_first_message_id) {
|
|
|
|
if ((*it)->message_id.is_server()) {
|
2024-02-29 17:01:40 +01:00
|
|
|
delete_message_files(old_shortcut->shortcut_id_, it->get());
|
2024-02-22 17:45:49 +01:00
|
|
|
it = old_shortcut->messages_.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (it == old_shortcut->messages_.end() || (*it)->message_id != new_first_message_id) {
|
|
|
|
old_shortcut->messages_.insert(it, std::move(new_shortcut->messages_[0]));
|
2024-02-22 19:40:07 +01:00
|
|
|
} else {
|
|
|
|
*it = std::move(new_shortcut->messages_[0]);
|
2024-02-22 17:45:49 +01:00
|
|
|
}
|
|
|
|
new_shortcut->messages_ = std::move(old_shortcut->messages_);
|
2024-02-22 19:44:13 +01:00
|
|
|
is_changed = (old_message_ids != get_quick_reply_unique_ids(new_shortcut->messages_));
|
2024-02-22 17:45:49 +01:00
|
|
|
} else {
|
2024-02-22 19:44:13 +01:00
|
|
|
auto old_server_message_ids = get_server_quick_reply_unique_ids(old_shortcut->messages_);
|
|
|
|
auto new_server_message_ids = get_server_quick_reply_unique_ids(new_shortcut->messages_);
|
2024-02-22 17:45:49 +01:00
|
|
|
if (old_server_message_ids == new_server_message_ids) {
|
|
|
|
new_shortcut->messages_ = std::move(old_shortcut->messages_);
|
|
|
|
} else {
|
|
|
|
is_changed = true;
|
2024-02-29 17:01:40 +01:00
|
|
|
for (auto &old_message : old_shortcut->messages_) {
|
|
|
|
if (!old_message->message_id.is_server()) {
|
|
|
|
new_shortcut->messages_.push_back(std::move(old_message));
|
|
|
|
} else {
|
|
|
|
bool is_deleted = true;
|
|
|
|
for (auto &new_message : new_shortcut->messages_) {
|
|
|
|
if (new_message->message_id == old_message->message_id) {
|
|
|
|
is_deleted = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_deleted) {
|
|
|
|
delete_message_files(old_shortcut->shortcut_id_, old_message.get());
|
|
|
|
}
|
2024-02-22 17:45:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort_quick_reply_messages(new_shortcut->messages_);
|
|
|
|
}
|
|
|
|
}
|
2024-02-22 19:57:43 +01:00
|
|
|
*is_object_changed = old_unique_id != get_quick_reply_unique_id(new_shortcut->messages_[0].get()) ||
|
|
|
|
new_shortcut->name_ != old_shortcut->name_ ||
|
|
|
|
old_message_count != get_shortcut_message_count(new_shortcut);
|
2024-02-29 13:55:38 +01:00
|
|
|
return is_changed;
|
2024-02-22 17:45:49 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 21:47:29 +01:00
|
|
|
string QuickReplyManager::get_quick_reply_shortcuts_database_key() {
|
|
|
|
return "quick_reply_shortcuts";
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::save_quick_reply_shortcuts() {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(get_quick_reply_shortcuts_database_key(),
|
|
|
|
log_event_store(shortcuts_).as_slice().str());
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:23:19 +01:00
|
|
|
td_api::object_ptr<td_api::updateQuickReplyShortcut> QuickReplyManager::get_update_quick_reply_shortcut_object(
|
|
|
|
const Shortcut *s, const char *source) const {
|
|
|
|
return td_api::make_object<td_api::updateQuickReplyShortcut>(get_quick_reply_shortcut_object(s, source));
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::send_update_quick_reply_shortcut(const Shortcut *s, const char *source) {
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_quick_reply_shortcut_object(s, source));
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::updateQuickReplyShortcutDeleted>
|
|
|
|
QuickReplyManager::get_update_quick_reply_shortcut_deleted_object(const Shortcut *s) const {
|
|
|
|
CHECK(s != nullptr);
|
2024-02-27 16:10:11 +01:00
|
|
|
return td_api::make_object<td_api::updateQuickReplyShortcutDeleted>(s->shortcut_id_.get());
|
2024-02-22 19:23:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::send_update_quick_reply_shortcut_deleted(const Shortcut *s) {
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_quick_reply_shortcut_deleted_object(s));
|
|
|
|
}
|
|
|
|
|
2024-02-22 20:55:09 +01:00
|
|
|
td_api::object_ptr<td_api::updateQuickReplyShortcuts> QuickReplyManager::get_update_quick_reply_shortcuts_object()
|
|
|
|
const {
|
|
|
|
CHECK(shortcuts_.are_inited_);
|
2024-02-27 16:10:11 +01:00
|
|
|
return td_api::make_object<td_api::updateQuickReplyShortcuts>(transform(
|
|
|
|
shortcuts_.shortcuts_, [](const unique_ptr<Shortcut> &shortcut) { return shortcut->shortcut_id_.get(); }));
|
2024-02-22 20:55:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::send_update_quick_reply_shortcuts() {
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_quick_reply_shortcuts_object());
|
|
|
|
}
|
|
|
|
|
2024-02-29 13:55:38 +01:00
|
|
|
td_api::object_ptr<td_api::updateQuickReplyShortcutMessages>
|
2024-02-29 14:10:15 +01:00
|
|
|
QuickReplyManager::get_update_quick_reply_shortcut_messages_object(const Shortcut *s, const char *source) const {
|
2024-02-29 13:55:38 +01:00
|
|
|
CHECK(s != nullptr);
|
2024-02-29 14:10:15 +01:00
|
|
|
auto messages = transform(s->messages_, [this, source](const unique_ptr<QuickReplyMessage> &message) {
|
|
|
|
return get_quick_reply_message_object(message.get(), source);
|
2024-02-29 13:55:38 +01:00
|
|
|
});
|
|
|
|
return td_api::make_object<td_api::updateQuickReplyShortcutMessages>(s->shortcut_id_.get(), std::move(messages));
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:10:15 +01:00
|
|
|
void QuickReplyManager::send_update_quick_reply_shortcut_messages(const Shortcut *s, const char *source) {
|
2024-02-29 13:55:38 +01:00
|
|
|
if (have_all_shortcut_messages(s)) {
|
2024-02-29 14:10:15 +01:00
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_quick_reply_shortcut_messages_object(s, source));
|
2024-02-29 13:55:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 17:01:40 +01:00
|
|
|
vector<FileId> QuickReplyManager::get_message_file_ids(const QuickReplyMessage *m) const {
|
|
|
|
if (m == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return get_message_content_file_ids(m->content.get(), td_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuickReplyManager::delete_message_files(QuickReplyShortcutId shortcut_id, const QuickReplyMessage *m) const {
|
|
|
|
for (auto file_id : get_message_file_ids(m)) {
|
|
|
|
send_closure(G()->file_manager(), &FileManager::delete_file, file_id, Promise<Unit>(), "delete_message_files");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:41:05 +01:00
|
|
|
FileSourceId QuickReplyManager::get_quick_reply_message_file_source_id(QuickReplyMessageFullId message_full_id) {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return FileSourceId();
|
|
|
|
}
|
|
|
|
if (!message_full_id.is_server()) {
|
|
|
|
return FileSourceId();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &file_source_id = message_full_id_to_file_source_id_[message_full_id];
|
|
|
|
if (!file_source_id.is_valid()) {
|
|
|
|
file_source_id = td_->file_reference_manager_->create_quick_reply_message_file_source(message_full_id);
|
|
|
|
}
|
|
|
|
return file_source_id;
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:26:17 +01:00
|
|
|
void QuickReplyManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
updates.push_back(get_update_quick_reply_shortcut_object(shortcut.get(), "get_current_state"));
|
|
|
|
}
|
2024-02-22 20:55:09 +01:00
|
|
|
|
|
|
|
if (shortcuts_.are_inited_) {
|
|
|
|
updates.push_back(get_update_quick_reply_shortcuts_object());
|
|
|
|
}
|
2024-02-29 13:55:38 +01:00
|
|
|
|
|
|
|
for (auto &shortcut : shortcuts_.shortcuts_) {
|
|
|
|
if (have_all_shortcut_messages(shortcut.get())) {
|
2024-02-29 14:10:15 +01:00
|
|
|
updates.push_back(get_update_quick_reply_shortcut_messages_object(shortcut.get(), "get_current_state"));
|
2024-02-29 13:55:38 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-22 19:26:17 +01:00
|
|
|
}
|
|
|
|
|
2024-02-21 18:43:55 +01:00
|
|
|
} // namespace td
|