1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #pragma once
8 
9 #include "td/telegram/DialogId.h"
10 #include "td/telegram/FolderId.h"
11 #include "td/telegram/NotificationGroupId.h"
12 #include "td/telegram/NotificationGroupKey.h"
13 
14 #include "td/db/KeyValueSyncInterface.h"
15 
16 #include "td/actor/PromiseFuture.h"
17 
18 #include "td/utils/buffer.h"
19 #include "td/utils/common.h"
20 #include "td/utils/Status.h"
21 
22 #include <memory>
23 #include <utility>
24 
25 namespace td {
26 
27 class SqliteConnectionSafe;
28 class SqliteDb;
29 
30 struct DialogDbGetDialogsResult {
31   vector<BufferSlice> dialogs;
32   int64 next_order = 0;
33   DialogId next_dialog_id;
34 };
35 
36 class DialogDbSyncInterface {
37  public:
38   DialogDbSyncInterface() = default;
39   DialogDbSyncInterface(const DialogDbSyncInterface &) = delete;
40   DialogDbSyncInterface &operator=(const DialogDbSyncInterface &) = delete;
41   virtual ~DialogDbSyncInterface() = default;
42 
43   virtual Status add_dialog(DialogId dialog_id, FolderId folder_id, int64 order, BufferSlice data,
44                             vector<NotificationGroupKey> notification_groups) = 0;
45 
46   virtual Result<BufferSlice> get_dialog(DialogId dialog_id) = 0;
47 
48   virtual Result<DialogDbGetDialogsResult> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id,
49                                                        int32 limit) = 0;
50 
51   virtual Result<vector<NotificationGroupKey>> get_notification_groups_by_last_notification_date(
52       NotificationGroupKey notification_group_key, int32 limit) = 0;
53 
54   virtual Result<NotificationGroupKey> get_notification_group(NotificationGroupId notification_group_id) = 0;
55 
56   virtual Result<int32> get_secret_chat_count(FolderId folder_id) = 0;
57 
58   virtual Status begin_read_transaction() = 0;
59   virtual Status begin_write_transaction() = 0;
60   virtual Status commit_transaction() = 0;
61 };
62 
63 class DialogDbSyncSafeInterface {
64  public:
65   DialogDbSyncSafeInterface() = default;
66   DialogDbSyncSafeInterface(const DialogDbSyncSafeInterface &) = delete;
67   DialogDbSyncSafeInterface &operator=(const DialogDbSyncSafeInterface &) = delete;
68   virtual ~DialogDbSyncSafeInterface() = default;
69 
70   virtual DialogDbSyncInterface &get() = 0;
71 };
72 
73 class DialogDbAsyncInterface {
74  public:
75   DialogDbAsyncInterface() = default;
76   DialogDbAsyncInterface(const DialogDbAsyncInterface &) = delete;
77   DialogDbAsyncInterface &operator=(const DialogDbAsyncInterface &) = delete;
78   virtual ~DialogDbAsyncInterface() = default;
79 
80   virtual void add_dialog(DialogId dialog_id, FolderId folder_id, int64 order, BufferSlice data,
81                           vector<NotificationGroupKey> notification_groups, Promise<> promise) = 0;
82 
83   virtual void get_dialog(DialogId dialog_id, Promise<BufferSlice> promise) = 0;
84 
85   virtual void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit,
86                            Promise<DialogDbGetDialogsResult> promise) = 0;
87 
88   virtual void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key,
89                                                                  int32 limit,
90                                                                  Promise<vector<NotificationGroupKey>> promise) = 0;
91 
92   virtual void get_notification_group(NotificationGroupId notification_group_id,
93                                       Promise<NotificationGroupKey> promise) = 0;
94 
95   virtual void get_secret_chat_count(FolderId folder_id, Promise<int32> promise) = 0;
96 
97   virtual void close(Promise<> promise) = 0;
98 };
99 
100 Status init_dialog_db(SqliteDb &db, int version, KeyValueSyncInterface &binlog_pmc,
101                       bool &was_created) TD_WARN_UNUSED_RESULT;
102 Status drop_dialog_db(SqliteDb &db, int version) TD_WARN_UNUSED_RESULT;
103 
104 std::shared_ptr<DialogDbSyncSafeInterface> create_dialog_db_sync(
105     std::shared_ptr<SqliteConnectionSafe> sqlite_connection);
106 
107 std::shared_ptr<DialogDbAsyncInterface> create_dialog_db_async(std::shared_ptr<DialogDbSyncSafeInterface> sync_db,
108                                                                int32 scheduler_id);
109 
110 }  // namespace td
111