2024-02-04 10:41:53 +01:00
|
|
|
//
|
2024-05-29 11:17:15 +02:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2024-02-04 10:41:53 +01:00
|
|
|
//
|
|
|
|
// 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-07 15:50:57 +01:00
|
|
|
#include "td/telegram/DialogId.h"
|
2024-02-04 21:06:48 +01:00
|
|
|
#include "td/telegram/MessageId.h"
|
2024-02-05 08:06:58 +01:00
|
|
|
#include "td/telegram/MessagesInfo.h"
|
2024-02-04 21:06:48 +01:00
|
|
|
#include "td/telegram/SavedMessagesTopicId.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
2024-02-07 15:50:57 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2024-02-04 21:06:48 +01:00
|
|
|
|
2024-02-04 10:41:53 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2024-02-07 15:50:57 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2024-02-04 21:06:48 +01:00
|
|
|
#include "td/utils/Promise.h"
|
2024-02-07 15:50:57 +01:00
|
|
|
#include "td/utils/Status.h"
|
2024-02-04 10:41:53 +01:00
|
|
|
|
2024-02-07 15:50:57 +01:00
|
|
|
#include <limits>
|
2024-02-05 23:21:55 +01:00
|
|
|
#include <set>
|
|
|
|
|
2024-02-04 10:41:53 +01:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Td;
|
|
|
|
|
|
|
|
class SavedMessagesManager final : public Actor {
|
|
|
|
public:
|
|
|
|
SavedMessagesManager(Td *td, ActorShared<> parent);
|
|
|
|
|
2024-02-07 12:01:29 +01:00
|
|
|
SavedMessagesTopicId get_topic_id(int64 topic_id) const;
|
|
|
|
|
2024-02-07 12:59:24 +01:00
|
|
|
vector<SavedMessagesTopicId> get_topic_ids(const vector<int64> &topic_ids) const;
|
|
|
|
|
2024-02-07 12:01:29 +01:00
|
|
|
int64 get_saved_messages_topic_id_object(SavedMessagesTopicId saved_messages_topic_id);
|
|
|
|
|
2024-02-05 22:11:36 +01:00
|
|
|
void set_topic_last_message_id(SavedMessagesTopicId saved_messages_topic_id, MessageId last_message_id,
|
|
|
|
int32 last_message_date);
|
2024-02-04 22:56:20 +01:00
|
|
|
|
2024-02-06 14:08:41 +01:00
|
|
|
void on_topic_message_updated(SavedMessagesTopicId saved_messages_topic_id, MessageId message_id);
|
|
|
|
|
2024-02-04 22:56:20 +01:00
|
|
|
void on_topic_message_deleted(SavedMessagesTopicId saved_messages_topic_id, MessageId message_id);
|
|
|
|
|
2024-02-07 14:10:56 +01:00
|
|
|
void on_topic_draft_message_updated(SavedMessagesTopicId saved_messages_topic_id, int32 draft_message_date);
|
|
|
|
|
2024-02-07 10:30:34 +01:00
|
|
|
void load_saved_messages_topics(int32 limit, Promise<Unit> &&promsie);
|
2024-02-04 21:06:48 +01:00
|
|
|
|
2024-02-07 10:30:34 +01:00
|
|
|
void on_get_saved_messages_topics(bool is_pinned, int32 limit,
|
2024-02-05 20:22:17 +01:00
|
|
|
telegram_api::object_ptr<telegram_api::messages_SavedDialogs> &&saved_dialogs_ptr,
|
2024-02-07 10:30:34 +01:00
|
|
|
Promise<Unit> &&promise);
|
2024-02-04 21:06:48 +01:00
|
|
|
|
|
|
|
void get_saved_messages_topic_history(SavedMessagesTopicId saved_messages_topic_id, MessageId from_message_id,
|
|
|
|
int32 offset, int32 limit,
|
|
|
|
Promise<td_api::object_ptr<td_api::messages>> &&promise);
|
|
|
|
|
|
|
|
void delete_saved_messages_topic_history(SavedMessagesTopicId saved_messages_topic_id, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void get_saved_messages_topic_message_by_date(SavedMessagesTopicId saved_messages_topic_id, int32 date,
|
|
|
|
Promise<td_api::object_ptr<td_api::message>> &&promise);
|
|
|
|
|
|
|
|
void delete_saved_messages_topic_messages_by_date(SavedMessagesTopicId saved_messages_topic_id, int32 min_date,
|
|
|
|
int32 max_date, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void toggle_saved_messages_topic_is_pinned(SavedMessagesTopicId saved_messages_topic_id, bool is_pinned,
|
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void set_pinned_saved_messages_topics(vector<SavedMessagesTopicId> saved_messages_topic_ids, Promise<Unit> &&promise);
|
|
|
|
|
2024-02-06 13:12:03 +01:00
|
|
|
void reload_pinned_saved_messages_topics();
|
2024-02-04 21:06:48 +01:00
|
|
|
|
2024-02-05 23:21:55 +01:00
|
|
|
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
|
|
|
|
|
2023-10-08 16:59:20 +02:00
|
|
|
void memory_stats(vector<string> &output);
|
|
|
|
|
2024-02-04 10:41:53 +01:00
|
|
|
private:
|
2024-02-04 21:06:48 +01:00
|
|
|
static constexpr int32 MAX_GET_HISTORY = 100; // server side limit
|
|
|
|
|
2024-02-06 12:26:48 +01:00
|
|
|
static constexpr int64 MIN_PINNED_TOPIC_ORDER = static_cast<int64>(2147000000) << 32;
|
|
|
|
|
2024-02-04 22:56:20 +01:00
|
|
|
struct SavedMessagesTopic {
|
2024-02-05 08:30:04 +01:00
|
|
|
SavedMessagesTopicId saved_messages_topic_id_;
|
2024-02-04 22:56:20 +01:00
|
|
|
MessageId last_message_id_;
|
2024-02-05 22:11:36 +01:00
|
|
|
int32 last_message_date_ = 0;
|
2024-02-07 14:10:56 +01:00
|
|
|
int32 draft_message_date_ = 0;
|
2024-02-05 20:22:17 +01:00
|
|
|
int64 pinned_order_ = 0;
|
2024-02-05 23:21:55 +01:00
|
|
|
int64 private_order_ = 0;
|
2024-02-07 12:01:29 +01:00
|
|
|
bool is_changed_ = false;
|
2024-02-04 22:56:20 +01:00
|
|
|
};
|
|
|
|
|
2024-02-05 23:21:55 +01:00
|
|
|
class TopicDate {
|
|
|
|
int64 order_;
|
|
|
|
SavedMessagesTopicId topic_id_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TopicDate(int64 order, SavedMessagesTopicId topic_id) : order_(order), topic_id_(topic_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const TopicDate &other) const {
|
|
|
|
return order_ > other.order_ ||
|
|
|
|
(order_ == other.order_ && topic_id_.get_unique_id() > other.topic_id_.get_unique_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<=(const TopicDate &other) const {
|
|
|
|
return order_ > other.order_ ||
|
|
|
|
(order_ == other.order_ && topic_id_.get_unique_id() >= other.topic_id_.get_unique_id());
|
|
|
|
}
|
2024-02-06 12:26:48 +01:00
|
|
|
|
2024-02-07 10:30:34 +01:00
|
|
|
bool operator==(const TopicDate &other) const {
|
|
|
|
return order_ == other.order_ && topic_id_ == other.topic_id_;
|
|
|
|
}
|
|
|
|
|
2024-02-07 17:45:06 +01:00
|
|
|
bool operator!=(const TopicDate &other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2024-02-06 12:26:48 +01:00
|
|
|
SavedMessagesTopicId get_topic_id() const {
|
|
|
|
return topic_id_;
|
|
|
|
}
|
2024-02-05 23:21:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const TopicDate MIN_TOPIC_DATE;
|
|
|
|
static const TopicDate MAX_TOPIC_DATE;
|
|
|
|
|
|
|
|
struct TopicList {
|
2024-02-07 17:45:06 +01:00
|
|
|
int32 server_total_count_ = -1;
|
|
|
|
int32 sent_total_count_ = -1;
|
|
|
|
|
2024-02-07 10:49:00 +01:00
|
|
|
vector<SavedMessagesTopicId> pinned_saved_messages_topic_ids_;
|
|
|
|
bool are_pinned_saved_messages_topics_inited_ = false;
|
|
|
|
|
2024-02-05 23:21:55 +01:00
|
|
|
std::set<TopicDate> ordered_topics_;
|
|
|
|
|
|
|
|
TopicDate last_topic_date_ = MIN_TOPIC_DATE; // in memory
|
2024-02-07 10:30:34 +01:00
|
|
|
|
2024-02-07 10:42:10 +01:00
|
|
|
vector<Promise<Unit>> load_pinned_queries_;
|
2024-02-07 10:45:13 +01:00
|
|
|
vector<Promise<Unit>> load_queries_;
|
2024-02-07 10:42:10 +01:00
|
|
|
|
2024-02-07 10:30:34 +01:00
|
|
|
int32 offset_date_ = std::numeric_limits<int32>::max();
|
|
|
|
DialogId offset_dialog_id_;
|
|
|
|
MessageId offset_message_id_;
|
2024-02-05 23:21:55 +01:00
|
|
|
};
|
|
|
|
|
2024-02-04 10:41:53 +01:00
|
|
|
void tear_down() final;
|
|
|
|
|
2024-02-04 22:56:20 +01:00
|
|
|
SavedMessagesTopic *get_topic(SavedMessagesTopicId saved_messages_topic_id);
|
|
|
|
|
|
|
|
SavedMessagesTopic *add_topic(SavedMessagesTopicId saved_messages_topic_id);
|
|
|
|
|
2024-02-07 10:30:34 +01:00
|
|
|
void get_pinned_saved_dialogs(int32 limit, Promise<Unit> &&promise);
|
|
|
|
|
2024-02-07 10:42:10 +01:00
|
|
|
void on_get_pinned_saved_dialogs(Result<Unit> &&result);
|
|
|
|
|
2024-02-07 10:30:34 +01:00
|
|
|
void get_saved_dialogs(int32 limit, Promise<Unit> &&promise);
|
|
|
|
|
2024-02-07 10:45:13 +01:00
|
|
|
void on_get_saved_dialogs(Result<Unit> &&result);
|
|
|
|
|
2024-02-05 08:06:58 +01:00
|
|
|
void on_get_saved_messages_topic_history(SavedMessagesTopicId saved_messages_topic_id, MessageId from_message_id,
|
|
|
|
Result<MessagesInfo> &&r_info,
|
|
|
|
Promise<td_api::object_ptr<td_api::messages>> &&promise);
|
|
|
|
|
2024-02-05 22:11:36 +01:00
|
|
|
void do_set_topic_last_message_id(SavedMessagesTopic *topic, MessageId last_message_id, int32 last_message_date);
|
2024-02-04 22:56:20 +01:00
|
|
|
|
2024-02-05 20:22:17 +01:00
|
|
|
int64 get_next_pinned_saved_messages_topic_order();
|
|
|
|
|
|
|
|
bool set_pinned_saved_messages_topics(vector<SavedMessagesTopicId> added_saved_messages_topic_ids);
|
|
|
|
|
|
|
|
bool set_saved_messages_topic_is_pinned(SavedMessagesTopicId saved_messages_topic_id, bool is_pinned);
|
|
|
|
|
|
|
|
bool set_saved_messages_topic_is_pinned(SavedMessagesTopic *topic, bool is_pinned);
|
|
|
|
|
2024-02-05 20:31:16 +01:00
|
|
|
int32 get_pinned_saved_messages_topic_limit() const;
|
|
|
|
|
2024-02-06 12:26:48 +01:00
|
|
|
int64 get_topic_order(int32 message_date, MessageId message_id);
|
|
|
|
|
2024-02-07 12:33:01 +01:00
|
|
|
int64 get_topic_public_order(const SavedMessagesTopic *topic) const;
|
|
|
|
|
2024-02-06 12:26:48 +01:00
|
|
|
void set_last_topic_date(TopicDate topic_date);
|
|
|
|
|
2024-02-07 14:24:05 +01:00
|
|
|
void on_topic_changed(SavedMessagesTopic *topic, const char *source);
|
2024-02-04 22:56:20 +01:00
|
|
|
|
2024-02-07 12:01:29 +01:00
|
|
|
td_api::object_ptr<td_api::savedMessagesTopic> get_saved_messages_topic_object(const SavedMessagesTopic *topic) const;
|
2024-02-05 22:58:13 +01:00
|
|
|
|
2024-02-06 12:26:48 +01:00
|
|
|
td_api::object_ptr<td_api::updateSavedMessagesTopic> get_update_saved_messages_topic_object(
|
|
|
|
const SavedMessagesTopic *topic) const;
|
|
|
|
|
2024-02-07 14:24:05 +01:00
|
|
|
void send_update_saved_messages_topic(const SavedMessagesTopic *topic, const char *source) const;
|
2024-02-06 12:26:48 +01:00
|
|
|
|
2024-02-07 17:45:06 +01:00
|
|
|
td_api::object_ptr<td_api::updateSavedMessagesTopicCount> get_update_saved_messages_topic_count_object() const;
|
|
|
|
|
|
|
|
void update_saved_messages_topic_sent_total_count(const char *source);
|
|
|
|
|
2024-02-04 10:41:53 +01:00
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
2024-02-04 22:56:20 +01:00
|
|
|
|
|
|
|
FlatHashMap<SavedMessagesTopicId, unique_ptr<SavedMessagesTopic>, SavedMessagesTopicIdHash> saved_messages_topics_;
|
2024-02-05 20:22:17 +01:00
|
|
|
|
2024-02-06 12:26:48 +01:00
|
|
|
int64 current_pinned_saved_messages_topic_order_ = MIN_PINNED_TOPIC_ORDER;
|
2024-02-05 23:21:55 +01:00
|
|
|
|
|
|
|
TopicList topic_list_;
|
2024-02-04 10:41:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|