tdlight/td/telegram/QuickReplyManager.h

281 lines
10 KiB
C
Raw Normal View History

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)
//
#pragma once
2024-02-21 20:06:07 +01:00
#include "td/telegram/DialogId.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileSourceId.h"
2024-02-21 20:06:07 +01:00
#include "td/telegram/MessageId.h"
2024-02-29 15:11:39 +01:00
#include "td/telegram/QuickReplyMessageFullId.h"
2024-02-24 01:16:56 +01:00
#include "td/telegram/QuickReplyShortcutId.h"
2024-02-21 20:06:07 +01:00
#include "td/telegram/td_api.h"
#include "td/telegram/UserId.h"
2024-02-21 18:43:55 +01:00
#include "td/actor/actor.h"
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
2024-02-24 01:06:20 +01:00
#include "td/utils/FlatHashSet.h"
#include "td/utils/Promise.h"
2024-02-21 18:43:55 +01:00
2024-02-22 19:44:13 +01:00
#include <utility>
2024-02-21 18:43:55 +01:00
namespace td {
2024-02-21 20:06:07 +01:00
class Dependencies;
class MessageContent;
2024-02-21 18:43:55 +01:00
class Td;
class QuickReplyManager final : public Actor {
public:
QuickReplyManager(Td *td, ActorShared<> parent);
void get_quick_reply_shortcuts(Promise<Unit> &&promise);
2024-02-22 15:32:12 +01:00
void delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
2024-02-24 01:02:42 +01:00
void reorder_quick_reply_shortcuts(const vector<QuickReplyShortcutId> &shortcut_ids, Promise<Unit> &&promise);
void update_quick_reply_message(telegram_api::object_ptr<telegram_api::Message> &&message_ptr);
void delete_quick_reply_messages_from_updates(QuickReplyShortcutId shortcut_id, const vector<MessageId> &message_ids);
void get_quick_reply_shortcut_messages(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
void delete_quick_reply_shortcut_messages(QuickReplyShortcutId shortcut_id, const vector<MessageId> &message_ids,
Promise<Unit> &&promise);
2024-02-22 15:32:12 +01:00
void reload_quick_reply_shortcuts();
void reload_quick_reply_messages(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
void reload_quick_reply_message(QuickReplyShortcutId shortcut_id, MessageId message_id, Promise<Unit> &&promise);
FileSourceId get_quick_reply_message_file_source_id(QuickReplyMessageFullId message_full_id);
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
2024-02-21 18:43:55 +01:00
private:
2024-02-21 20:06:07 +01:00
struct QuickReplyMessage {
2024-02-22 15:32:12 +01:00
QuickReplyMessage() = default;
QuickReplyMessage(const QuickReplyMessage &) = delete;
QuickReplyMessage &operator=(const QuickReplyMessage &) = delete;
QuickReplyMessage(QuickReplyMessage &&) = delete;
QuickReplyMessage &operator=(QuickReplyMessage &&) = delete;
~QuickReplyMessage();
2024-02-21 20:06:07 +01:00
MessageId message_id;
2024-02-24 01:16:56 +01:00
QuickReplyShortcutId shortcut_id;
2024-02-21 20:06:07 +01:00
int32 sending_id = 0; // for yet unsent messages
2024-02-22 19:40:07 +01:00
int32 edit_date = 0;
2024-02-21 20:06:07 +01:00
int64 random_id = 0; // for send_message
MessageId reply_to_message_id;
2024-02-21 20:06:07 +01:00
string send_emoji; // for send_message
2024-02-21 20:06:07 +01:00
UserId via_bot_user_id;
bool is_failed_to_send = false;
bool disable_notification = false;
bool noforwards = false;
bool invert_media = false;
bool from_background = false; // for send_message
bool disable_web_page_preview = false; // for send_message
bool hide_via_bot = false; // for resend_message
int32 legacy_layer = 0;
int32 send_error_code = 0;
string send_error_message;
double try_resend_at = 0;
int64 media_album_id = 0;
unique_ptr<MessageContent> content;
mutable uint64 send_message_log_event_id = 0;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
2024-02-22 15:32:12 +01:00
};
2024-02-21 20:06:07 +01:00
2024-02-22 15:32:12 +01:00
struct Shortcut {
Shortcut() = default;
Shortcut(const Shortcut &) = delete;
Shortcut &operator=(const Shortcut &) = delete;
Shortcut(Shortcut &&) = delete;
Shortcut &operator=(Shortcut &&) = delete;
~Shortcut();
string name_;
2024-02-24 01:16:56 +01:00
QuickReplyShortcutId shortcut_id_;
int32 server_total_count_ = 0;
int32 local_total_count_ = 0;
2024-02-22 15:32:12 +01:00
vector<unique_ptr<QuickReplyMessage>> messages_;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
2024-02-22 15:32:12 +01:00
};
struct Shortcuts {
vector<unique_ptr<Shortcut>> shortcuts_;
bool are_inited_ = false;
vector<Promise<Unit>> load_queries_;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
2024-02-21 20:06:07 +01:00
};
2024-02-21 18:43:55 +01:00
void tear_down() final;
2024-02-21 20:06:07 +01:00
void add_quick_reply_message_dependencies(Dependencies &dependencies, const QuickReplyMessage *m) const;
unique_ptr<QuickReplyMessage> create_message(telegram_api::object_ptr<telegram_api::Message> message_ptr,
const char *source) const;
2024-02-23 18:22:33 +01:00
bool can_edit_quick_reply_message(const QuickReplyMessage *m) const;
bool can_resend_quick_reply_message(const QuickReplyMessage *m) const;
2024-02-21 20:43:29 +01:00
td_api::object_ptr<td_api::MessageSendingState> get_message_sending_state_object(const QuickReplyMessage *m) const;
td_api::object_ptr<td_api::MessageContent> get_quick_reply_message_message_content_object(
const QuickReplyMessage *m) const;
td_api::object_ptr<td_api::quickReplyMessage> get_quick_reply_message_object(const QuickReplyMessage *m,
const char *source) const;
2024-02-22 15:32:12 +01:00
td_api::object_ptr<td_api::quickReplyShortcut> get_quick_reply_shortcut_object(const Shortcut *s,
const char *source) const;
static int32 get_shortcut_message_count(const Shortcut *s);
static bool have_all_shortcut_messages(const Shortcut *s);
void load_quick_reply_shortcuts(Promise<Unit> &&promise);
2024-02-22 15:32:12 +01:00
void on_reload_quick_reply_shortcuts(
Result<telegram_api::object_ptr<telegram_api::messages_QuickReplies>> r_shortcuts);
void on_load_quick_reply_success();
void on_load_quick_reply_fail(Status error);
2024-02-23 12:49:29 +01:00
int64 get_shortcuts_hash() const;
void on_reload_quick_reply_messages(QuickReplyShortcutId shortcut_id,
Result<telegram_api::object_ptr<telegram_api::messages_Messages>> r_messages);
static int64 get_quick_reply_messages_hash(const Shortcut *s);
void 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);
void on_get_quick_reply_message(Shortcut *s, unique_ptr<QuickReplyMessage> message);
void update_quick_reply_message(QuickReplyShortcutId shortcut_id, unique_ptr<QuickReplyMessage> &old_message,
unique_ptr<QuickReplyMessage> &&new_message);
void delete_quick_reply_messages(Shortcut *s, const vector<MessageId> &message_ids, const char *source);
2024-02-24 01:16:56 +01:00
Shortcut *get_shortcut(QuickReplyShortcutId shortcut_id);
2024-02-24 01:02:42 +01:00
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);
vector<unique_ptr<QuickReplyMessage>>::iterator get_message_it(Shortcut *s, MessageId message_id);
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);
2024-02-22 19:44:13 +01:00
using QuickReplyMessageUniqueId = std::pair<MessageId, int32>;
static QuickReplyMessageUniqueId get_quick_reply_unique_id(const QuickReplyMessage *m);
2024-02-22 19:44:13 +01:00
static vector<QuickReplyMessageUniqueId> get_quick_reply_unique_ids(
2024-02-22 19:40:07 +01:00
const vector<unique_ptr<QuickReplyMessage>> &messages);
2024-02-22 19:44:13 +01:00
static vector<QuickReplyMessageUniqueId> get_server_quick_reply_unique_ids(
2024-02-22 19:40:07 +01:00
const vector<unique_ptr<QuickReplyMessage>> &messages);
bool update_shortcut_from(Shortcut *new_shortcut, Shortcut *old_shortcut, bool is_partial, bool *is_object_changed);
td_api::object_ptr<td_api::updateQuickReplyShortcut> get_update_quick_reply_shortcut_object(const Shortcut *s,
const char *source) const;
void send_update_quick_reply_shortcut(const Shortcut *s, const char *source);
td_api::object_ptr<td_api::updateQuickReplyShortcutDeleted> get_update_quick_reply_shortcut_deleted_object(
const Shortcut *s) const;
void send_update_quick_reply_shortcut_deleted(const Shortcut *s);
2024-02-22 20:55:09 +01:00
td_api::object_ptr<td_api::updateQuickReplyShortcuts> get_update_quick_reply_shortcuts_object() const;
void send_update_quick_reply_shortcuts();
td_api::object_ptr<td_api::updateQuickReplyShortcutMessages> get_update_quick_reply_shortcut_messages_object(
const Shortcut *s, const char *source) const;
void send_update_quick_reply_shortcut_messages(const Shortcut *s, const char *source);
2024-02-24 01:28:55 +01:00
void delete_quick_reply_shortcut_from_server(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
void reorder_quick_reply_shortcuts_on_server(vector<QuickReplyShortcutId> shortcut_ids, Promise<Unit> &&promise);
void delete_quick_reply_messages_on_server(QuickReplyShortcutId shortcut_id, const vector<MessageId> &message_ids,
Promise<Unit> &&promise);
2024-02-27 21:47:29 +01:00
string get_quick_reply_shortcuts_database_key();
void save_quick_reply_shortcuts();
vector<FileId> get_message_file_ids(const QuickReplyMessage *m) const;
void delete_message_files(QuickReplyShortcutId shortcut_id, const QuickReplyMessage *m) const;
2024-02-29 19:05:56 +01:00
void change_message_files(QuickReplyMessageFullId message_full_id, const QuickReplyMessage *m,
const vector<FileId> &old_file_ids);
2024-02-22 15:32:12 +01:00
Shortcuts shortcuts_;
2024-02-24 01:16:56 +01:00
FlatHashSet<QuickReplyShortcutId, QuickReplyShortcutIdHash> deleted_shortcut_ids_;
2024-02-24 01:06:20 +01:00
FlatHashMap<QuickReplyShortcutId, vector<Promise<Unit>>, QuickReplyShortcutIdHash> get_shortcut_messages_queries_;
FlatHashSet<QuickReplyMessageFullId, QuickReplyMessageFullIdHash> deleted_message_full_ids_;
FlatHashMap<QuickReplyMessageFullId, FileSourceId, QuickReplyMessageFullIdHash> message_full_id_to_file_source_id_;
2024-02-21 18:43:55 +01:00
Td *td_;
ActorShared<> parent_;
};
} // namespace td