2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +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
|
|
|
|
|
|
|
|
#include "td/telegram/DialogId.h"
|
2019-11-26 17:53:10 +01:00
|
|
|
#include "td/telegram/FullMessageId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/MessageId.h"
|
2021-11-01 10:52:51 +01:00
|
|
|
#include "td/telegram/MessageSearchFilter.h"
|
2018-11-28 18:18:50 +01:00
|
|
|
#include "td/telegram/NotificationId.h"
|
2019-11-26 17:33:18 +01:00
|
|
|
#include "td/telegram/ServerMessageId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-09-28 22:57:34 +02:00
|
|
|
|
2019-01-07 01:17:11 +01:00
|
|
|
class SqliteConnectionSafe;
|
|
|
|
class SqliteDb;
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbMessagesQuery {
|
2018-12-31 20:04:05 +01:00
|
|
|
DialogId dialog_id;
|
2021-11-01 10:52:51 +01:00
|
|
|
MessageSearchFilter filter{MessageSearchFilter::Empty};
|
2018-12-31 20:04:05 +01:00
|
|
|
MessageId from_message_id;
|
|
|
|
int32 offset{0};
|
|
|
|
int32 limit{100};
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbDialogMessage {
|
2021-10-01 14:23:38 +02:00
|
|
|
MessageId message_id;
|
2021-10-01 11:55:12 +02:00
|
|
|
BufferSlice data;
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbMessage {
|
2018-12-31 20:04:05 +01:00
|
|
|
DialogId dialog_id;
|
2021-10-01 14:23:38 +02:00
|
|
|
MessageId message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferSlice data;
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbDialogCalendarQuery {
|
2021-11-01 11:50:45 +01:00
|
|
|
DialogId dialog_id;
|
|
|
|
MessageSearchFilter filter{MessageSearchFilter::Empty};
|
|
|
|
MessageId from_message_id;
|
|
|
|
int32 tz_offset{0};
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbCalendar {
|
|
|
|
vector<MessageDbDialogMessage> messages;
|
2021-11-01 11:50:45 +01:00
|
|
|
vector<int32> total_counts;
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbGetDialogSparseMessagePositionsQuery {
|
2021-11-01 14:30:03 +01:00
|
|
|
DialogId dialog_id;
|
|
|
|
MessageSearchFilter filter{MessageSearchFilter::Empty};
|
|
|
|
MessageId from_message_id;
|
|
|
|
int32 limit{0};
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbMessagePosition {
|
2021-12-01 18:54:56 +01:00
|
|
|
int32 position;
|
|
|
|
int32 date;
|
2021-11-01 14:30:03 +01:00
|
|
|
MessageId message_id;
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbMessagePositions {
|
2021-11-01 14:30:03 +01:00
|
|
|
int32 total_count{0};
|
2022-11-09 18:35:22 +01:00
|
|
|
vector<MessageDbMessagePosition> positions;
|
2021-11-01 14:30:03 +01:00
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbFtsQuery {
|
2018-12-31 20:04:05 +01:00
|
|
|
string query;
|
|
|
|
DialogId dialog_id;
|
2021-11-01 10:52:51 +01:00
|
|
|
MessageSearchFilter filter{MessageSearchFilter::Empty};
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 from_search_id{0};
|
|
|
|
int32 limit{100};
|
|
|
|
};
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbFtsResult {
|
|
|
|
vector<MessageDbMessage> messages;
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 next_search_id{1};
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbCallsQuery {
|
2021-11-01 10:52:51 +01:00
|
|
|
MessageSearchFilter filter{MessageSearchFilter::Empty};
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 from_unique_message_id{0};
|
|
|
|
int32 limit{100};
|
|
|
|
};
|
2021-11-01 10:52:51 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
struct MessageDbCallsResult {
|
|
|
|
vector<MessageDbMessage> messages;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
class MessageDbSyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbSyncInterface() = default;
|
|
|
|
MessageDbSyncInterface(const MessageDbSyncInterface &) = delete;
|
|
|
|
MessageDbSyncInterface &operator=(const MessageDbSyncInterface &) = delete;
|
|
|
|
virtual ~MessageDbSyncInterface() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-07 20:19:44 +01:00
|
|
|
virtual void add_message(FullMessageId full_message_id, ServerMessageId unique_message_id, DialogId sender_dialog_id,
|
|
|
|
int64 random_id, int32 ttl_expires_at, int32 index_mask, int64 search_id, string text,
|
|
|
|
NotificationId notification_id, MessageId top_thread_message_id, BufferSlice data) = 0;
|
|
|
|
virtual void add_scheduled_message(FullMessageId full_message_id, BufferSlice data) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-07 20:19:44 +01:00
|
|
|
virtual void delete_message(FullMessageId full_message_id) = 0;
|
|
|
|
virtual void delete_all_dialog_messages(DialogId dialog_id, MessageId from_message_id) = 0;
|
|
|
|
virtual void delete_dialog_messages_by_sender(DialogId dialog_id, DialogId sender_dialog_id) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual Result<MessageDbDialogMessage> get_message(FullMessageId full_message_id) = 0;
|
|
|
|
virtual Result<MessageDbMessage> get_message_by_unique_message_id(ServerMessageId unique_message_id) = 0;
|
|
|
|
virtual Result<MessageDbDialogMessage> get_message_by_random_id(DialogId dialog_id, int64 random_id) = 0;
|
|
|
|
virtual Result<MessageDbDialogMessage> get_dialog_message_by_date(DialogId dialog_id, MessageId first_message_id,
|
|
|
|
MessageId last_message_id, int32 date) = 0;
|
2021-10-01 11:55:12 +02:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual MessageDbCalendar get_dialog_message_calendar(MessageDbDialogCalendarQuery query) = 0;
|
2021-11-01 11:50:45 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual Result<MessageDbMessagePositions> get_dialog_sparse_message_positions(
|
|
|
|
MessageDbGetDialogSparseMessagePositionsQuery query) = 0;
|
2021-11-01 14:30:03 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual vector<MessageDbDialogMessage> get_messages(MessageDbMessagesQuery query) = 0;
|
|
|
|
virtual vector<MessageDbDialogMessage> get_scheduled_messages(DialogId dialog_id, int32 limit) = 0;
|
|
|
|
virtual vector<MessageDbDialogMessage> get_messages_from_notification_id(DialogId dialog_id,
|
|
|
|
NotificationId from_notification_id,
|
|
|
|
int32 limit) = 0;
|
2022-11-07 20:19:44 +01:00
|
|
|
|
2023-05-15 12:28:23 +02:00
|
|
|
virtual vector<MessageDbMessage> get_expiring_messages(int32 expires_till, int32 limit) = 0;
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual MessageDbCallsResult get_calls(MessageDbCallsQuery query) = 0;
|
|
|
|
virtual MessageDbFtsResult get_messages_fts(MessageDbFtsQuery query) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-07 12:18:00 +02:00
|
|
|
virtual Status begin_write_transaction() = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual Status commit_transaction() = 0;
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
class MessageDbSyncSafeInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbSyncSafeInterface() = default;
|
|
|
|
MessageDbSyncSafeInterface(const MessageDbSyncSafeInterface &) = delete;
|
|
|
|
MessageDbSyncSafeInterface &operator=(const MessageDbSyncSafeInterface &) = delete;
|
|
|
|
virtual ~MessageDbSyncSafeInterface() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual MessageDbSyncInterface &get() = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
class MessageDbAsyncInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-11-09 18:35:22 +01:00
|
|
|
MessageDbAsyncInterface() = default;
|
|
|
|
MessageDbAsyncInterface(const MessageDbAsyncInterface &) = delete;
|
|
|
|
MessageDbAsyncInterface &operator=(const MessageDbAsyncInterface &) = delete;
|
|
|
|
virtual ~MessageDbAsyncInterface() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-19 14:00:21 +01:00
|
|
|
virtual void add_message(FullMessageId full_message_id, ServerMessageId unique_message_id, DialogId sender_dialog_id,
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 random_id, int32 ttl_expires_at, int32 index_mask, int64 search_id, string text,
|
2020-09-28 14:13:25 +02:00
|
|
|
NotificationId notification_id, MessageId top_thread_message_id, BufferSlice data,
|
|
|
|
Promise<> promise) = 0;
|
2019-12-02 01:33:35 +01:00
|
|
|
virtual void add_scheduled_message(FullMessageId full_message_id, BufferSlice data, Promise<> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
virtual void delete_message(FullMessageId full_message_id, Promise<> promise) = 0;
|
|
|
|
virtual void delete_all_dialog_messages(DialogId dialog_id, MessageId from_message_id, Promise<> promise) = 0;
|
2021-11-19 14:00:21 +01:00
|
|
|
virtual void delete_dialog_messages_by_sender(DialogId dialog_id, DialogId sender_dialog_id, Promise<> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual void get_message(FullMessageId full_message_id, Promise<MessageDbDialogMessage> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void get_message_by_unique_message_id(ServerMessageId unique_message_id,
|
2022-11-09 18:35:22 +01:00
|
|
|
Promise<MessageDbMessage> promise) = 0;
|
2021-10-01 11:55:12 +02:00
|
|
|
virtual void get_message_by_random_id(DialogId dialog_id, int64 random_id,
|
2022-11-09 18:35:22 +01:00
|
|
|
Promise<MessageDbDialogMessage> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void get_dialog_message_by_date(DialogId dialog_id, MessageId first_message_id, MessageId last_message_id,
|
2022-11-09 18:35:22 +01:00
|
|
|
int32 date, Promise<MessageDbDialogMessage> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual void get_dialog_message_calendar(MessageDbDialogCalendarQuery query, Promise<MessageDbCalendar> promise) = 0;
|
2021-11-01 11:50:45 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual void get_dialog_sparse_message_positions(MessageDbGetDialogSparseMessagePositionsQuery query,
|
|
|
|
Promise<MessageDbMessagePositions> promise) = 0;
|
2021-11-01 14:30:03 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual void get_messages(MessageDbMessagesQuery query, Promise<vector<MessageDbDialogMessage>> promise) = 0;
|
2021-10-01 11:55:12 +02:00
|
|
|
virtual void get_scheduled_messages(DialogId dialog_id, int32 limit,
|
2022-11-09 18:35:22 +01:00
|
|
|
Promise<vector<MessageDbDialogMessage>> promise) = 0;
|
2018-11-28 21:19:30 +01:00
|
|
|
virtual void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit,
|
2022-11-09 18:35:22 +01:00
|
|
|
Promise<vector<MessageDbDialogMessage>> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
virtual void get_calls(MessageDbCallsQuery, Promise<MessageDbCallsResult> promise) = 0;
|
|
|
|
virtual void get_messages_fts(MessageDbFtsQuery query, Promise<MessageDbFtsResult> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-05-15 12:28:23 +02:00
|
|
|
virtual void get_expiring_messages(int32 expires_till, int32 limit, Promise<vector<MessageDbMessage>> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
virtual void close(Promise<> promise) = 0;
|
|
|
|
virtual void force_flush() = 0;
|
|
|
|
};
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
Status init_message_db(SqliteDb &db, int version) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status drop_message_db(SqliteDb &db, int version) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
std::shared_ptr<MessageDbSyncSafeInterface> create_message_db_sync(
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<SqliteConnectionSafe> sqlite_connection);
|
|
|
|
|
2022-11-09 18:35:22 +01:00
|
|
|
std::shared_ptr<MessageDbAsyncInterface> create_message_db_async(std::shared_ptr<MessageDbSyncSafeInterface> sync_db,
|
|
|
|
int32 scheduler_id = -1);
|
2018-09-28 22:57:34 +02:00
|
|
|
|
2018-07-18 03:30:29 +02:00
|
|
|
} // namespace td
|