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/AccessRights.h"
10 #include "td/telegram/BotCommand.h"
11 #include "td/telegram/ChannelId.h"
12 #include "td/telegram/ChatId.h"
13 #include "td/telegram/Contact.h"
14 #include "td/telegram/DialogAdministrator.h"
15 #include "td/telegram/DialogId.h"
16 #include "td/telegram/DialogInviteLink.h"
17 #include "td/telegram/DialogLocation.h"
18 #include "td/telegram/DialogParticipant.h"
19 #include "td/telegram/files/FileId.h"
20 #include "td/telegram/files/FileSourceId.h"
21 #include "td/telegram/FolderId.h"
22 #include "td/telegram/FullMessageId.h"
23 #include "td/telegram/Location.h"
24 #include "td/telegram/MessageId.h"
25 #include "td/telegram/net/DcId.h"
26 #include "td/telegram/Photo.h"
27 #include "td/telegram/PublicDialogType.h"
28 #include "td/telegram/QueryCombiner.h"
29 #include "td/telegram/RestrictionReason.h"
30 #include "td/telegram/SecretChatId.h"
31 #include "td/telegram/StickerSetId.h"
32 #include "td/telegram/SuggestedAction.h"
33 #include "td/telegram/td_api.h"
34 #include "td/telegram/telegram_api.h"
35 #include "td/telegram/UserId.h"
36 
37 #include "td/actor/actor.h"
38 #include "td/actor/MultiPromise.h"
39 #include "td/actor/PromiseFuture.h"
40 #include "td/actor/Timeout.h"
41 
42 #include "td/utils/common.h"
43 #include "td/utils/Hints.h"
44 #include "td/utils/Status.h"
45 #include "td/utils/StringBuilder.h"
46 #include "td/utils/Time.h"
47 
48 #include <functional>
49 #include <memory>
50 #include <unordered_map>
51 #include <unordered_set>
52 #include <utility>
53 
54 namespace td {
55 
56 struct BinlogEvent;
57 
58 class Td;
59 
60 class ContactsManager final : public Actor {
61  public:
62   ContactsManager(Td *td, ActorShared<> parent);
63   ContactsManager(const ContactsManager &) = delete;
64   ContactsManager &operator=(const ContactsManager &) = delete;
65   ContactsManager(ContactsManager &&) = delete;
66   ContactsManager &operator=(ContactsManager &&) = delete;
67   ~ContactsManager() final;
68 
69   static UserId load_my_id();
70 
71   static UserId get_user_id(const tl_object_ptr<telegram_api::User> &user);
72   static ChatId get_chat_id(const tl_object_ptr<telegram_api::Chat> &chat);
73   static ChannelId get_channel_id(const tl_object_ptr<telegram_api::Chat> &chat);
74 
75   tl_object_ptr<telegram_api::InputUser> get_input_user(UserId user_id) const;
76   bool have_input_user(UserId user_id) const;
77 
78   // TODO get_input_chat ???
79 
80   tl_object_ptr<telegram_api::InputChannel> get_input_channel(ChannelId channel_id) const;
81 
82   tl_object_ptr<telegram_api::InputPeer> get_input_peer_user(UserId user_id, AccessRights access_rights) const;
83   bool have_input_peer_user(UserId user_id, AccessRights access_rights) const;
84 
85   tl_object_ptr<telegram_api::InputPeer> get_input_peer_chat(ChatId chat_id, AccessRights access_rights) const;
86   bool have_input_peer_chat(ChatId chat_id, AccessRights access_rights) const;
87 
88   tl_object_ptr<telegram_api::InputPeer> get_input_peer_channel(ChannelId channel_id, AccessRights access_rights) const;
89   bool have_input_peer_channel(ChannelId channel_id, AccessRights access_rights) const;
90 
91   tl_object_ptr<telegram_api::inputEncryptedChat> get_input_encrypted_chat(SecretChatId secret_chat_id,
92                                                                            AccessRights access_rights) const;
93   bool have_input_encrypted_peer(SecretChatId secret_chat_id, AccessRights access_rights) const;
94 
95   const DialogPhoto *get_user_dialog_photo(UserId user_id);
96   const DialogPhoto *get_chat_dialog_photo(ChatId chat_id) const;
97   const DialogPhoto *get_channel_dialog_photo(ChannelId channel_id) const;
98   const DialogPhoto *get_secret_chat_dialog_photo(SecretChatId secret_chat_id);
99 
100   string get_user_title(UserId user_id) const;
101   string get_chat_title(ChatId chat_id) const;
102   string get_channel_title(ChannelId channel_id) const;
103   string get_secret_chat_title(SecretChatId secret_chat_id) const;
104 
105   RestrictedRights get_user_default_permissions(UserId user_id) const;
106   RestrictedRights get_chat_default_permissions(ChatId chat_id) const;
107   RestrictedRights get_channel_default_permissions(ChannelId channel_id) const;
108   RestrictedRights get_secret_chat_default_permissions(SecretChatId secret_chat_id) const;
109 
110   bool get_chat_has_protected_content(ChatId chat_id) const;
111   bool get_channel_has_protected_content(ChannelId channel_id) const;
112 
113   string get_user_private_forward_name(UserId user_id);
114 
115   string get_dialog_about(DialogId dialog_id);
116 
117   bool is_update_about_username_change_received(UserId user_id) const;
118 
119   void for_each_secret_chat_with_user(UserId user_id, const std::function<void(SecretChatId)> &f);
120 
121   string get_user_username(UserId user_id) const;
122   string get_channel_username(ChannelId channel_id) const;
123   string get_secret_chat_username(SecretChatId secret_chat_id) const;
124 
125   int32 get_secret_chat_date(SecretChatId secret_chat_id) const;
126   int32 get_secret_chat_ttl(SecretChatId secret_chat_id) const;
127   UserId get_secret_chat_user_id(SecretChatId secret_chat_id) const;
128   bool get_secret_chat_is_outbound(SecretChatId secret_chat_id) const;
129   SecretChatState get_secret_chat_state(SecretChatId secret_chat_id) const;
130   int32 get_secret_chat_layer(SecretChatId secret_chat_id) const;
131   FolderId get_secret_chat_initial_folder_id(SecretChatId secret_chat_id) const;
132 
133   void on_imported_contacts(int64 random_id, Result<tl_object_ptr<telegram_api::contacts_importedContacts>> result);
134 
135   void on_deleted_contacts(const vector<UserId> &deleted_contact_user_ids);
136 
137   void on_get_contacts(tl_object_ptr<telegram_api::contacts_Contacts> &&new_contacts);
138 
139   void on_get_contacts_failed(Status error);
140 
141   void on_get_contacts_statuses(vector<tl_object_ptr<telegram_api::contactStatus>> &&statuses);
142 
143   void reload_contacts(bool force);
144 
145   void on_get_user(tl_object_ptr<telegram_api::User> &&user, const char *source, bool is_me = false,
146                    bool expect_support = false);
147   void on_get_users(vector<tl_object_ptr<telegram_api::User>> &&users, const char *source);
148 
149   void on_binlog_user_event(BinlogEvent &&event);
150   void on_binlog_chat_event(BinlogEvent &&event);
151   void on_binlog_channel_event(BinlogEvent &&event);
152   void on_binlog_secret_chat_event(BinlogEvent &&event);
153 
154   void on_get_user_full(tl_object_ptr<telegram_api::userFull> &&user);
155 
156   void on_get_user_photos(UserId user_id, int32 offset, int32 limit, int32 total_count,
157                           vector<tl_object_ptr<telegram_api::Photo>> photos);
158 
159   void on_get_chat(tl_object_ptr<telegram_api::Chat> &&chat, const char *source);
160   void on_get_chats(vector<tl_object_ptr<telegram_api::Chat>> &&chats, const char *source);
161 
162   void on_get_chat_full(tl_object_ptr<telegram_api::ChatFull> &&chat_full, Promise<Unit> &&promise);
163   void on_get_chat_full_failed(ChatId chat_id);
164   void on_get_channel_full_failed(ChannelId channel_id);
165 
166   void on_update_profile_success(int32 flags, const string &first_name, const string &last_name, const string &about);
167 
168   void on_update_user_name(UserId user_id, string &&first_name, string &&last_name, string &&username);
169   void on_update_user_phone_number(UserId user_id, string &&phone_number);
170   void on_update_user_photo(UserId user_id, tl_object_ptr<telegram_api::UserProfilePhoto> &&photo_ptr);
171   void on_update_user_online(UserId user_id, tl_object_ptr<telegram_api::UserStatus> &&status);
172   void on_update_user_local_was_online(UserId user_id, int32 local_was_online);
173   void on_update_user_is_blocked(UserId user_id, bool is_blocked);
174   void on_update_user_common_chat_count(UserId user_id, int32 common_chat_count);
175   void on_update_user_need_phone_number_privacy_exception(UserId user_id, bool need_phone_number_privacy_exception);
176 
177   void on_set_profile_photo(tl_object_ptr<telegram_api::photos_photo> &&photo, int64 old_photo_id);
178   void on_delete_profile_photo(int64 profile_photo_id, Promise<Unit> promise);
179 
180   void on_ignored_restriction_reasons_changed();
181 
182   void on_get_chat_participants(tl_object_ptr<telegram_api::ChatParticipants> &&participants, bool from_update);
183   void on_update_chat_add_user(ChatId chat_id, UserId inviter_user_id, UserId user_id, int32 date, int32 version);
184   void on_update_chat_description(ChatId chat_id, string &&description);
185   void on_update_chat_edit_administrator(ChatId chat_id, UserId user_id, bool is_administrator, int32 version);
186   void on_update_chat_delete_user(ChatId chat_id, UserId user_id, int32 version);
187   void on_update_chat_default_permissions(ChatId chat_id, RestrictedRights default_permissions, int32 version);
188   void on_update_chat_pinned_message(ChatId chat_id, MessageId pinned_message_id, int32 version);
189 
190   void on_update_channel_username(ChannelId channel_id, string &&username);
191   void on_update_channel_description(ChannelId channel_id, string &&description);
192   void on_update_channel_sticker_set(ChannelId channel_id, StickerSetId sticker_set_id);
193   void on_update_channel_linked_channel_id(ChannelId channel_id, ChannelId group_channel_id);
194   void on_update_channel_location(ChannelId channel_id, const DialogLocation &location);
195   void on_update_channel_slow_mode_delay(ChannelId channel_id, int32 slow_mode_delay, Promise<Unit> &&promise);
196   void on_update_channel_slow_mode_next_send_date(ChannelId channel_id, int32 slow_mode_next_send_date);
197   void on_update_channel_is_all_history_available(ChannelId channel_id, bool is_all_history_available,
198                                                   Promise<Unit> &&promise);
199   void on_update_channel_default_permissions(ChannelId channel_id, RestrictedRights default_permissions);
200   void on_update_channel_administrator_count(ChannelId channel_id, int32 administrator_count);
201 
202   void on_update_bot_stopped(UserId user_id, int32 date, bool is_stopped);
203   void on_update_chat_participant(ChatId chat_id, UserId user_id, int32 date, DialogInviteLink invite_link,
204                                   tl_object_ptr<telegram_api::ChatParticipant> old_participant,
205                                   tl_object_ptr<telegram_api::ChatParticipant> new_participant);
206   void on_update_channel_participant(ChannelId channel_id, UserId user_id, int32 date, DialogInviteLink invite_link,
207                                      tl_object_ptr<telegram_api::ChannelParticipant> old_participant,
208                                      tl_object_ptr<telegram_api::ChannelParticipant> new_participant);
209   void on_update_chat_invite_requester(DialogId dialog_id, UserId user_id, string about, int32 date,
210                                        DialogInviteLink invite_link);
211 
212   int32 on_update_peer_located(vector<tl_object_ptr<telegram_api::PeerLocated>> &&peers, bool from_update);
213 
214   void on_update_bot_commands(DialogId dialog_id, UserId bot_user_id,
215                               vector<tl_object_ptr<telegram_api::botCommand>> &&bot_commands);
216 
217   void on_update_dialog_administrators(DialogId dialog_id, vector<DialogAdministrator> &&administrators,
218                                        bool have_access, bool from_database);
219 
220   void speculative_add_channel_participants(ChannelId channel_id, const vector<UserId> &added_user_ids,
221                                             UserId inviter_user_id, int32 date, bool by_me);
222 
223   void speculative_delete_channel_participant(ChannelId channel_id, UserId deleted_user_id, bool by_me);
224 
225   void invalidate_channel_full(ChannelId channel_id, bool need_drop_slow_mode_delay);
226 
227   bool on_get_channel_error(ChannelId channel_id, const Status &status, const string &source);
228 
229   void on_get_permanent_dialog_invite_link(DialogId dialog_id, const DialogInviteLink &invite_link);
230 
231   void on_get_dialog_invite_link_info(const string &invite_link,
232                                       tl_object_ptr<telegram_api::ChatInvite> &&chat_invite_ptr,
233                                       Promise<Unit> &&promise);
234 
235   void invalidate_invite_link_info(const string &invite_link);
236 
237   void on_get_created_public_channels(PublicDialogType type, vector<tl_object_ptr<telegram_api::Chat>> &&chats);
238 
239   void on_get_dialogs_for_discussion(vector<tl_object_ptr<telegram_api::Chat>> &&chats);
240 
241   void on_get_inactive_channels(vector<tl_object_ptr<telegram_api::Chat>> &&chats);
242 
243   void remove_inactive_channel(ChannelId channel_id);
244 
245   UserId get_my_id() const;
246 
247   void set_my_online_status(bool is_online, bool send_update, bool is_local);
248 
249   struct MyOnlineStatusInfo {
250     bool is_online_local = false;
251     bool is_online_remote = false;
252     int32 was_online_local = 0;
253     int32 was_online_remote = 0;
254   };
255 
256   MyOnlineStatusInfo get_my_online_status() const;
257 
258   static UserId get_service_notifications_user_id();
259 
260   UserId add_service_notifications_user();
261 
262   static UserId get_replies_bot_user_id();
263 
264   static UserId get_anonymous_bot_user_id();
265 
266   UserId add_anonymous_bot_user();
267 
268   void on_update_online_status_privacy();
269 
270   void on_update_phone_number_privacy();
271 
272   void invalidate_user_full(UserId user_id);
273 
274   enum class CheckDialogUsernameResult : uint8 { Ok, Invalid, Occupied, PublicDialogsTooMuch, PublicGroupsUnavailable };
275 
276   void check_dialog_username(DialogId dialog_id, const string &username, Promise<CheckDialogUsernameResult> &&promise);
277 
278   static td_api::object_ptr<td_api::CheckChatUsernameResult> get_check_chat_username_result_object(
279       CheckDialogUsernameResult result);
280 
281   void add_contact(Contact contact, bool share_phone_number, Promise<Unit> &&promise);
282 
283   std::pair<vector<UserId>, vector<int32>> import_contacts(const vector<Contact> &contacts, int64 &random_id,
284                                                            Promise<Unit> &&promise);
285 
286   std::pair<int32, vector<UserId>> search_contacts(const string &query, int32 limit, Promise<Unit> &&promise);
287 
288   void remove_contacts(const vector<UserId> &user_ids, Promise<Unit> &&promise);
289 
290   void remove_contacts_by_phone_number(vector<string> user_phone_numbers, vector<UserId> user_ids,
291                                        Promise<Unit> &&promise);
292 
293   int32 get_imported_contact_count(Promise<Unit> &&promise);
294 
295   std::pair<vector<UserId>, vector<int32>> change_imported_contacts(vector<Contact> &contacts, int64 &random_id,
296                                                                     Promise<Unit> &&promise);
297 
298   void clear_imported_contacts(Promise<Unit> &&promise);
299 
300   void on_update_contacts_reset();
301 
302   void share_phone_number(UserId user_id, Promise<Unit> &&promise);
303 
304   void search_dialogs_nearby(const Location &location, Promise<td_api::object_ptr<td_api::chatsNearby>> &&promise);
305 
306   void set_location(const Location &location, Promise<Unit> &&promise);
307 
308   void set_location_visibility();
309 
310   void get_is_location_visible(Promise<Unit> &&promise);
311 
312   FileId get_profile_photo_file_id(int64 photo_id) const;
313 
314   void set_profile_photo(const td_api::object_ptr<td_api::InputChatPhoto> &input_photo, Promise<Unit> &&promise);
315 
316   void send_update_profile_photo_query(FileId file_id, int64 old_photo_id, Promise<Unit> &&promise);
317 
318   void delete_profile_photo(int64 profile_photo_id, Promise<Unit> &&promise);
319 
320   void set_name(const string &first_name, const string &last_name, Promise<Unit> &&promise);
321 
322   void set_bio(const string &bio, Promise<Unit> &&promise);
323 
324   void set_username(const string &username, Promise<Unit> &&promise);
325 
326   void set_chat_description(ChatId chat_id, const string &description, Promise<Unit> &&promise);
327 
328   void set_channel_username(ChannelId channel_id, const string &username, Promise<Unit> &&promise);
329 
330   void set_channel_sticker_set(ChannelId channel_id, StickerSetId sticker_set_id, Promise<Unit> &&promise);
331 
332   void toggle_channel_sign_messages(ChannelId channel_id, bool sign_messages, Promise<Unit> &&promise);
333 
334   void toggle_channel_is_all_history_available(ChannelId channel_id, bool is_all_history_available,
335                                                Promise<Unit> &&promise);
336 
337   void convert_channel_to_gigagroup(ChannelId channel_id, Promise<Unit> &&promise);
338 
339   void set_channel_description(ChannelId channel_id, const string &description, Promise<Unit> &&promise);
340 
341   void set_channel_discussion_group(DialogId dialog_id, DialogId discussion_dialog_id, Promise<Unit> &&promise);
342 
343   void set_channel_location(DialogId dialog_id, const DialogLocation &location, Promise<Unit> &&promise);
344 
345   void set_channel_slow_mode_delay(DialogId dialog_id, int32 slow_mode_delay, Promise<Unit> &&promise);
346 
347   void report_channel_spam(ChannelId channel_id, const vector<MessageId> &message_ids, Promise<Unit> &&promise);
348 
349   void delete_dialog(DialogId dialog_id, Promise<Unit> &&promise);
350 
351   void get_channel_statistics_dc_id(DialogId dialog_id, bool for_full_statistics, Promise<DcId> &&promise);
352 
353   void get_channel_statistics(DialogId dialog_id, bool is_dark,
354                               Promise<td_api::object_ptr<td_api::ChatStatistics>> &&promise);
355 
356   bool can_get_channel_message_statistics(DialogId dialog_id) const;
357 
358   void get_channel_message_statistics(FullMessageId full_message_id, bool is_dark,
359                                       Promise<td_api::object_ptr<td_api::messageStatistics>> &&promise);
360 
361   void load_statistics_graph(DialogId dialog_id, string token, int64 x,
362                              Promise<td_api::object_ptr<td_api::StatisticalGraph>> &&promise);
363 
364   struct CanTransferOwnershipResult {
365     enum class Type : uint8 { Ok, PasswordNeeded, PasswordTooFresh, SessionTooFresh };
366     Type type = Type::Ok;
367     int32 retry_after = 0;
368   };
369   void can_transfer_ownership(Promise<CanTransferOwnershipResult> &&promise);
370 
371   static td_api::object_ptr<td_api::CanTransferOwnershipResult> get_can_transfer_ownership_result_object(
372       CanTransferOwnershipResult result);
373 
374   void transfer_dialog_ownership(DialogId dialog_id, UserId user_id, const string &password, Promise<Unit> &&promise);
375 
376   void export_dialog_invite_link(DialogId dialog_id, string title, int32 expire_date, int32 usage_limit,
377                                  bool creates_join_request, bool is_permanent,
378                                  Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise);
379 
380   void edit_dialog_invite_link(DialogId dialog_id, const string &link, string title, int32 expire_date,
381                                int32 usage_limit, bool creates_join_request,
382                                Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise);
383 
384   void get_dialog_invite_link(DialogId dialog_id, const string &invite_link,
385                               Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise);
386 
387   void get_dialog_invite_link_counts(DialogId dialog_id,
388                                      Promise<td_api::object_ptr<td_api::chatInviteLinkCounts>> &&promise);
389 
390   void get_dialog_invite_links(DialogId dialog_id, UserId creator_user_id, bool is_revoked, int32 offset_date,
391                                const string &offset_invite_link, int32 limit,
392                                Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&promise);
393 
394   void get_dialog_invite_link_users(DialogId dialog_id, const string &invite_link,
395                                     td_api::object_ptr<td_api::chatInviteLinkMember> offset_member, int32 limit,
396                                     Promise<td_api::object_ptr<td_api::chatInviteLinkMembers>> &&promise);
397 
398   void get_dialog_join_requests(DialogId dialog_id, const string &invite_link, const string &query,
399                                 td_api::object_ptr<td_api::chatJoinRequest> offset_request, int32 limit,
400                                 Promise<td_api::object_ptr<td_api::chatJoinRequests>> &&promise);
401 
402   void process_dialog_join_request(DialogId dialog_id, UserId user_id, bool approve, Promise<Unit> &&promise);
403 
404   void process_dialog_join_requests(DialogId dialog_id, const string &invite_link, bool approve,
405                                     Promise<Unit> &&promise);
406 
407   void revoke_dialog_invite_link(DialogId dialog_id, const string &link,
408                                  Promise<td_api::object_ptr<td_api::chatInviteLinks>> &&promise);
409 
410   void delete_revoked_dialog_invite_link(DialogId dialog_id, const string &invite_link, Promise<Unit> &&promise);
411 
412   void delete_all_revoked_dialog_invite_links(DialogId dialog_id, UserId creator_user_id, Promise<Unit> &&promise);
413 
414   void check_dialog_invite_link(const string &invite_link, Promise<Unit> &&promise) const;
415 
416   void import_dialog_invite_link(const string &invite_link, Promise<DialogId> &&promise);
417 
418   ChannelId migrate_chat_to_megagroup(ChatId chat_id, Promise<Unit> &promise);
419 
420   void get_created_public_dialogs(PublicDialogType type, Promise<td_api::object_ptr<td_api::chats>> &&promise,
421                                   bool from_binlog);
422 
423   void check_created_public_dialogs_limit(PublicDialogType type, Promise<Unit> &&promise);
424 
425   vector<DialogId> get_dialogs_for_discussion(Promise<Unit> &&promise);
426 
427   vector<DialogId> get_inactive_channels(Promise<Unit> &&promise);
428 
429   void dismiss_dialog_suggested_action(SuggestedAction action, Promise<Unit> &&promise);
430 
431   bool is_user_contact(UserId user_id, bool is_mutual = false) const;
432 
433   bool is_user_deleted(UserId user_id) const;
434 
435   bool is_user_support(UserId user_id) const;
436 
437   bool is_user_bot(UserId user_id) const;
438 
439   struct BotData {
440     string username;
441     bool can_join_groups;
442     bool can_read_all_group_messages;
443     bool is_inline;
444     bool need_location;
445   };
446   Result<BotData> get_bot_data(UserId user_id) const TD_WARN_UNUSED_RESULT;
447 
448   bool is_user_online(UserId user_id, int32 tolerance = 0) const;
449 
450   bool is_user_status_exact(UserId user_id) const;
451 
452   bool can_report_user(UserId user_id) const;
453 
454   bool have_user(UserId user_id) const;
455   bool have_min_user(UserId user_id) const;
456   bool have_user_force(UserId user_id);
457 
458   bool is_dialog_info_received_from_server(DialogId dialog_id) const;
459 
460   void reload_dialog_info(DialogId dialog_id, Promise<Unit> &&promise);
461 
462   static void send_get_me_query(Td *td, Promise<Unit> &&promise);
463   UserId get_me(Promise<Unit> &&promise);
464   bool get_user(UserId user_id, int left_tries, Promise<Unit> &&promise);
465   void reload_user(UserId user_id, Promise<Unit> &&promise);
466   void load_user_full(UserId user_id, bool force, Promise<Unit> &&promise, const char *source);
467   void reload_user_full(UserId user_id);
468 
469   std::pair<int32, vector<const Photo *>> get_user_profile_photos(UserId user_id, int32 offset, int32 limit,
470                                                                   Promise<Unit> &&promise);
471   void reload_user_profile_photo(UserId user_id, int64 photo_id, Promise<Unit> &&promise);
472   FileSourceId get_user_profile_photo_file_source_id(UserId user_id, int64 photo_id);
473 
474   bool have_chat(ChatId chat_id) const;
475   bool have_chat_force(ChatId chat_id);
476   bool get_chat(ChatId chat_id, int left_tries, Promise<Unit> &&promise);
477   void reload_chat(ChatId chat_id, Promise<Unit> &&promise);
478   void load_chat_full(ChatId chat_id, bool force, Promise<Unit> &&promise, const char *source);
479   FileSourceId get_chat_full_file_source_id(ChatId chat_id);
480   void reload_chat_full(ChatId chat_id, Promise<Unit> &&promise);
481 
482   int32 get_chat_participant_count(ChatId chat_id) const;
483   bool get_chat_is_active(ChatId chat_id) const;
484   ChannelId get_chat_migrated_to_channel_id(ChatId chat_id) const;
485   DialogParticipantStatus get_chat_status(ChatId chat_id) const;
486   DialogParticipantStatus get_chat_permissions(ChatId chat_id) const;
487   bool is_appointed_chat_administrator(ChatId chat_id) const;
488 
489   bool have_channel(ChannelId channel_id) const;
490   bool have_min_channel(ChannelId channel_id) const;
491   bool have_channel_force(ChannelId channel_id);
492   bool get_channel(ChannelId channel_id, int left_tries, Promise<Unit> &&promise);
493   void reload_channel(ChannelId channel_id, Promise<Unit> &&promise);
494   void load_channel_full(ChannelId channel_id, bool force, Promise<Unit> &&promise, const char *source);
495   FileSourceId get_channel_full_file_source_id(ChannelId channel_id);
496   void reload_channel_full(ChannelId channel_id, Promise<Unit> &&promise, const char *source);
497 
498   bool is_channel_public(ChannelId channel_id) const;
499 
500   bool have_secret_chat(SecretChatId secret_chat_id) const;
501   bool have_secret_chat_force(SecretChatId secret_chat_id);
502   bool get_secret_chat(SecretChatId secret_chat_id, bool force, Promise<Unit> &&promise);
503   bool get_secret_chat_full(SecretChatId secret_chat_id, Promise<Unit> &&promise);
504 
505   enum class ChannelType : uint8 { Broadcast, Megagroup, Unknown };
506 
507   ChannelType get_channel_type(ChannelId channel_id) const;
508   int32 get_channel_date(ChannelId channel_id) const;
509   DialogParticipantStatus get_channel_status(ChannelId channel_id) const;
510   DialogParticipantStatus get_channel_permissions(ChannelId channel_id) const;
511   int32 get_channel_participant_count(ChannelId channel_id) const;
512   bool get_channel_sign_messages(ChannelId channel_id) const;
513   bool get_channel_has_linked_channel(ChannelId channel_id) const;
514   ChannelId get_channel_linked_channel_id(ChannelId channel_id);
515   int32 get_channel_slow_mode_delay(ChannelId channel_id);
516 
517   void add_dialog_participant(DialogId dialog_id, UserId user_id, int32 forward_limit, Promise<Unit> &&promise);
518 
519   void add_dialog_participants(DialogId dialog_id, const vector<UserId> &user_ids, Promise<Unit> &&promise);
520 
521   void set_dialog_participant_status(DialogId dialog_id, DialogId participant_dialog_id,
522                                      DialogParticipantStatus &&status, Promise<Unit> &&promise);
523 
524   void ban_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, int32 banned_until_date,
525                               bool revoke_messages, Promise<Unit> &&promise);
526 
527   void get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id,
528                               Promise<td_api::object_ptr<td_api::chatMember>> &&promise);
529 
530   void search_dialog_participants(DialogId dialog_id, const string &query, int32 limit, DialogParticipantsFilter filter,
531                                   Promise<DialogParticipants> &&promise);
532 
533   vector<DialogAdministrator> get_dialog_administrators(DialogId dialog_id, int left_tries, Promise<Unit> &&promise);
534 
535   void get_channel_participants(ChannelId channel_id, tl_object_ptr<td_api::SupergroupMembersFilter> &&filter,
536                                 string additional_query, int32 offset, int32 limit, int32 additional_limit,
537                                 Promise<DialogParticipants> &&promise);
538 
539   int64 get_user_id_object(UserId user_id, const char *source) const;
540 
541   tl_object_ptr<td_api::user> get_user_object(UserId user_id) const;
542 
543   vector<int64> get_user_ids_object(const vector<UserId> &user_ids, const char *source) const;
544 
545   tl_object_ptr<td_api::users> get_users_object(int32 total_count, const vector<UserId> &user_ids) const;
546 
547   tl_object_ptr<td_api::userFullInfo> get_user_full_info_object(UserId user_id) const;
548 
549   int64 get_basic_group_id_object(ChatId chat_id, const char *source) const;
550 
551   tl_object_ptr<td_api::basicGroup> get_basic_group_object(ChatId chat_id);
552 
553   tl_object_ptr<td_api::basicGroupFullInfo> get_basic_group_full_info_object(ChatId chat_id) const;
554 
555   int64 get_supergroup_id_object(ChannelId channel_id, const char *source) const;
556 
557   tl_object_ptr<td_api::supergroup> get_supergroup_object(ChannelId channel_id) const;
558 
559   tl_object_ptr<td_api::supergroupFullInfo> get_supergroup_full_info_object(ChannelId channel_id) const;
560 
561   int32 get_secret_chat_id_object(SecretChatId secret_chat_id, const char *source) const;
562 
563   tl_object_ptr<td_api::secretChat> get_secret_chat_object(SecretChatId secret_chat_id);
564 
565   void on_update_secret_chat(SecretChatId secret_chat_id, int64 access_hash, UserId user_id, SecretChatState state,
566                              bool is_outbound, int32 ttl, int32 date, string key_hash, int32 layer,
567                              FolderId initial_folder_id);
568 
569   tl_object_ptr<td_api::chatMember> get_chat_member_object(const DialogParticipant &dialog_participant) const;
570 
571   tl_object_ptr<td_api::chatInviteLinkInfo> get_chat_invite_link_info_object(const string &invite_link);
572 
573   UserId get_support_user(Promise<Unit> &&promise);
574 
575   void repair_chat_participants(ChatId chat_id);
576 
577   void after_get_difference();
578 
579   void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
580 
581   static tl_object_ptr<td_api::dateRange> convert_date_range(
582       const tl_object_ptr<telegram_api::statsDateRangeDays> &obj);
583 
584   static tl_object_ptr<td_api::StatisticalGraph> convert_stats_graph(tl_object_ptr<telegram_api::StatsGraph> obj);
585 
586   static double get_percentage_value(double part, double total);
587 
588   static tl_object_ptr<td_api::statisticalValue> convert_stats_absolute_value(
589       const tl_object_ptr<telegram_api::statsAbsValueAndPrev> &obj);
590 
591   tl_object_ptr<td_api::chatStatisticsSupergroup> convert_megagroup_stats(
592       tl_object_ptr<telegram_api::stats_megagroupStats> obj);
593 
594   static tl_object_ptr<td_api::chatStatisticsChannel> convert_broadcast_stats(
595       tl_object_ptr<telegram_api::stats_broadcastStats> obj);
596 
597   static tl_object_ptr<td_api::messageStatistics> convert_message_stats(
598       tl_object_ptr<telegram_api::stats_messageStats> obj);
599 
600  private:
601   struct User {
602     string first_name;
603     string last_name;
604     string username;
605     string phone_number;
606     int64 access_hash = -1;
607 
608     ProfilePhoto photo;
609 
610     vector<RestrictionReason> restriction_reasons;
611     string inline_query_placeholder;
612     int32 bot_info_version = -1;
613 
614     int32 was_online = 0;
615     int32 local_was_online = 0;
616 
617     string language_code;
618 
619     std::unordered_set<int64> photo_ids;
620 
621     std::unordered_map<DialogId, int32, DialogIdHash> online_member_dialogs;  // id -> time
622 
623     static constexpr uint32 CACHE_VERSION = 4;
624     uint32 cache_version = 0;
625 
626     bool is_min_access_hash = true;
627     bool is_received = false;
628     bool is_verified = false;
629     bool is_support = false;
630     bool is_deleted = true;
631     bool is_bot = true;
632     bool can_join_groups = true;
633     bool can_read_all_group_messages = true;
634     bool is_inline_bot = false;
635     bool need_location_bot = false;
636     bool is_scam = false;
637     bool is_fake = false;
638     bool is_contact = false;
639     bool is_mutual_contact = false;
640     bool need_apply_min_photo = false;
641 
642     bool is_photo_inited = false;
643 
644     bool is_repaired = false;  // whether cached value is rechecked
645 
646     bool is_name_changed = true;
647     bool is_username_changed = true;
648     bool is_photo_changed = true;
649     bool is_is_contact_changed = true;
650     bool is_is_deleted_changed = true;
651     bool is_changed = true;             // have new changes that need to be sent to the client and database
652     bool need_save_to_database = true;  // have new changes that need only to be saved to the database
653     bool is_status_changed = true;
654     bool is_online_status_changed = true;  // whether online/offline has changed
655     bool is_update_user_sent = false;
656 
657     bool is_saved = false;         // is current user version being saved/is saved to the database
658     bool is_being_saved = false;   // is current user being saved to the database
659     bool is_status_saved = false;  // is current user status being saved/is saved to the database
660 
661     bool is_received_from_server = false;  // true, if the user was received from the server and not the database
662 
663     uint64 log_event_id = 0;
664 
665     template <class StorerT>
666     void store(StorerT &storer) const;
667 
668     template <class ParserT>
669     void parse(ParserT &parser);
670   };
671 
672   // do not forget to update drop_user_full and on_get_user_full
673   struct UserFull {
674     Photo photo;
675 
676     string about;
677     string description;
678     string private_forward_name;
679 
680     vector<BotCommand> commands;
681 
682     int32 common_chat_count = 0;
683 
684     bool is_blocked = false;
685     bool can_be_called = false;
686     bool supports_video_calls = false;
687     bool has_private_calls = false;
688     bool can_pin_messages = true;
689     bool need_phone_number_privacy_exception = false;
690 
691     bool is_common_chat_count_changed = true;
692     bool is_changed = true;             // have new changes that need to be sent to the client and database
693     bool need_send_update = true;       // have new changes that need only to be sent to the client
694     bool need_save_to_database = true;  // have new changes that need only to be saved to the database
695     bool is_update_user_full_sent = false;
696 
697     double expires_at = 0.0;
698 
is_expiredUserFull699     bool is_expired() const {
700       return expires_at < Time::now();
701     }
702 
703     template <class StorerT>
704     void store(StorerT &storer) const;
705 
706     template <class ParserT>
707     void parse(ParserT &parser);
708   };
709 
710   struct Chat {
711     string title;
712     DialogPhoto photo;
713     int32 participant_count = 0;
714     int32 date = 0;
715     int32 version = -1;
716     int32 default_permissions_version = -1;
717     int32 pinned_message_version = -1;
718     ChannelId migrated_to_channel_id;
719 
720     DialogParticipantStatus status = DialogParticipantStatus::Banned(0);
721     RestrictedRights default_permissions{false, false, false, false, false, false, false, false, false, false, false};
722 
723     static constexpr uint32 CACHE_VERSION = 4;
724     uint32 cache_version = 0;
725 
726     bool is_active = false;
727     bool noforwards = false;
728 
729     bool is_title_changed = true;
730     bool is_photo_changed = true;
731     bool is_default_permissions_changed = true;
732     bool is_status_changed = true;
733     bool is_is_active_changed = true;
734     bool is_noforwards_changed = true;
735     bool is_changed = true;             // have new changes that need to be sent to the client and database
736     bool need_save_to_database = true;  // have new changes that need only to be saved to the database
737     bool is_update_basic_group_sent = false;
738 
739     bool is_repaired = false;  // whether cached value is rechecked
740 
741     bool is_saved = false;        // is current chat version being saved/is saved to the database
742     bool is_being_saved = false;  // is current chat being saved to the database
743 
744     bool is_received_from_server = false;  // true, if the chat was received from the server and not the database
745 
746     uint64 log_event_id = 0;
747 
748     template <class StorerT>
749     void store(StorerT &storer) const;
750 
751     template <class ParserT>
752     void parse(ParserT &parser);
753   };
754 
755   // do not forget to update drop_chat_full and on_get_chat_full
756   struct ChatFull {
757     int32 version = -1;
758     UserId creator_user_id;
759     vector<DialogParticipant> participants;
760 
761     Photo photo;
762     vector<FileId> registered_photo_file_ids;
763     FileSourceId file_source_id;
764 
765     string description;
766 
767     DialogInviteLink invite_link;
768 
769     vector<BotCommands> bot_commands;
770 
771     bool can_set_username = false;
772 
773     bool is_changed = true;             // have new changes that need to be sent to the client and database
774     bool need_send_update = true;       // have new changes that need only to be sent to the client
775     bool need_save_to_database = true;  // have new changes that need only to be saved to the database
776     bool is_update_chat_full_sent = false;
777 
778     template <class StorerT>
779     void store(StorerT &storer) const;
780 
781     template <class ParserT>
782     void parse(ParserT &parser);
783   };
784 
785   struct Channel {
786     int64 access_hash = 0;
787     string title;
788     DialogPhoto photo;
789     string username;
790     vector<RestrictionReason> restriction_reasons;
791     DialogParticipantStatus status = DialogParticipantStatus::Banned(0);
792     RestrictedRights default_permissions{false, false, false, false, false, false, false, false, false, false, false};
793     int32 date = 0;
794     int32 participant_count = 0;
795 
796     static constexpr uint32 CACHE_VERSION = 8;
797     uint32 cache_version = 0;
798 
799     bool has_linked_channel = false;
800     bool has_location = false;
801     bool sign_messages = false;
802     bool is_slow_mode_enabled = false;
803     bool noforwards = false;
804 
805     bool is_megagroup = false;
806     bool is_gigagroup = false;
807     bool is_verified = false;
808     bool is_scam = false;
809     bool is_fake = false;
810 
811     bool is_title_changed = true;
812     bool is_username_changed = true;
813     bool is_photo_changed = true;
814     bool is_default_permissions_changed = true;
815     bool is_status_changed = true;
816     bool is_has_location_changed = true;
817     bool is_noforwards_changed = true;
818     bool is_creator_changed = true;
819     bool had_read_access = true;
820     bool was_member = false;
821     bool is_changed = true;             // have new changes that need to be sent to the client and database
822     bool need_save_to_database = true;  // have new changes that need only to be saved to the database
823     bool is_update_supergroup_sent = false;
824 
825     bool is_repaired = false;  // whether cached value is rechecked
826 
827     bool is_saved = false;        // is current channel version being saved/is saved to the database
828     bool is_being_saved = false;  // is current channel being saved to the database
829 
830     bool is_received_from_server = false;  // true, if the channel was received from the server and not the database
831 
832     uint64 log_event_id = 0;
833 
834     template <class StorerT>
835     void store(StorerT &storer) const;
836 
837     template <class ParserT>
838     void parse(ParserT &parser);
839   };
840 
841   // do not forget to update invalidate_channel_full and on_get_chat_full
842   struct ChannelFull {
843     Photo photo;
844     vector<FileId> registered_photo_file_ids;
845     FileSourceId file_source_id;
846 
847     string description;
848     int32 participant_count = 0;
849     int32 administrator_count = 0;
850     int32 restricted_count = 0;
851     int32 banned_count = 0;
852 
853     DialogInviteLink invite_link;
854 
855     vector<BotCommands> bot_commands;
856 
857     uint32 speculative_version = 1;
858     uint32 repair_request_version = 0;
859 
860     StickerSetId sticker_set_id;
861 
862     ChannelId linked_channel_id;
863 
864     DialogLocation location;
865 
866     DcId stats_dc_id;
867 
868     int32 slow_mode_delay = 0;
869     int32 slow_mode_next_send_date = 0;
870 
871     MessageId migrated_from_max_message_id;
872     ChatId migrated_from_chat_id;
873 
874     vector<UserId> bot_user_ids;
875 
876     bool can_get_participants = false;
877     bool can_set_username = false;
878     bool can_set_sticker_set = false;
879     bool can_set_location = false;
880     bool can_view_statistics = false;
881     bool is_can_view_statistics_inited = false;
882     bool is_all_history_available = true;
883 
884     bool is_slow_mode_next_send_date_changed = true;
885     bool is_changed = true;             // have new changes that need to be sent to the client and database
886     bool need_send_update = true;       // have new changes that need only to be sent to the client
887     bool need_save_to_database = true;  // have new changes that need only to be saved to the database
888     bool is_update_channel_full_sent = false;
889 
890     double expires_at = 0.0;
891 
is_expiredChannelFull892     bool is_expired() const {
893       return expires_at < Time::now();
894     }
895 
896     template <class StorerT>
897     void store(StorerT &storer) const;
898 
899     template <class ParserT>
900     void parse(ParserT &parser);
901   };
902 
903   struct SecretChat {
904     int64 access_hash = 0;
905     UserId user_id;
906     SecretChatState state = SecretChatState::Unknown;
907     string key_hash;
908     int32 ttl = 0;
909     int32 date = 0;
910     int32 layer = 0;
911     FolderId initial_folder_id;
912 
913     bool is_outbound = false;
914 
915     bool is_ttl_changed = true;
916     bool is_state_changed = true;
917     bool is_changed = true;             // have new changes that need to be sent to the client and database
918     bool need_save_to_database = true;  // have new changes that need only to be saved to the database
919 
920     bool is_saved = false;        // is current secret chat version being saved/is saved to the database
921     bool is_being_saved = false;  // is current secret chat being saved to the database
922 
923     uint64 log_event_id = 0;
924 
925     template <class StorerT>
926     void store(StorerT &storer) const;
927 
928     template <class ParserT>
929     void parse(ParserT &parser);
930   };
931 
932   struct InviteLinkInfo {
933     // known dialog
934     DialogId dialog_id;
935 
936     // unknown dialog
937     string title;
938     Photo photo;
939     string description;
940     int32 participant_count = 0;
941     vector<UserId> participant_user_ids;
942     bool creates_join_request = false;
943     bool is_chat = false;
944     bool is_channel = false;
945     bool is_public = false;
946     bool is_megagroup = false;
947   };
948 
949   struct UserPhotos {
950     vector<Photo> photos;
951     int32 count = -1;
952     int32 offset = -1;
953     bool getting_now = false;
954   };
955 
956   struct DialogNearby {
957     DialogId dialog_id;
958     int32 distance;
959 
DialogNearbyDialogNearby960     DialogNearby(DialogId dialog_id, int32 distance) : dialog_id(dialog_id), distance(distance) {
961     }
962 
963     bool operator<(const DialogNearby &other) const {
964       return distance < other.distance || (distance == other.distance && dialog_id.get() < other.dialog_id.get());
965     }
966 
967     bool operator==(const DialogNearby &other) const {
968       return distance == other.distance && dialog_id == other.dialog_id;
969     }
970 
971     bool operator!=(const DialogNearby &other) const {
972       return !(*this == other);
973     }
974   };
975 
976   class UserLogEvent;
977   class ChatLogEvent;
978   class ChannelLogEvent;
979   class SecretChatLogEvent;
980 
981   static constexpr int32 MAX_GET_PROFILE_PHOTOS = 100;        // server side limit
982   static constexpr size_t MAX_NAME_LENGTH = 64;               // server side limit for first/last name
983   static constexpr size_t MAX_DESCRIPTION_LENGTH = 255;       // server side limit for chat/channel description
984   static constexpr size_t MAX_BIO_LENGTH = 70;                // server side limit
985   static constexpr size_t MAX_INVITE_LINK_TITLE_LENGTH = 32;  // server side limit
986   static constexpr int32 MAX_GET_CHANNEL_PARTICIPANTS = 200;  // server side limit
987 
988   static constexpr int32 CHANNEL_PARTICIPANT_CACHE_TIME = 1800;  // some reasonable limit
989 
990   static constexpr int32 USER_FLAG_HAS_ACCESS_HASH = 1 << 0;
991   static constexpr int32 USER_FLAG_HAS_FIRST_NAME = 1 << 1;
992   static constexpr int32 USER_FLAG_HAS_LAST_NAME = 1 << 2;
993   static constexpr int32 USER_FLAG_HAS_USERNAME = 1 << 3;
994   static constexpr int32 USER_FLAG_HAS_PHONE_NUMBER = 1 << 4;
995   static constexpr int32 USER_FLAG_HAS_PHOTO = 1 << 5;
996   static constexpr int32 USER_FLAG_HAS_STATUS = 1 << 6;
997   static constexpr int32 USER_FLAG_HAS_BOT_INFO_VERSION = 1 << 14;
998   static constexpr int32 USER_FLAG_IS_ME = 1 << 10;
999   static constexpr int32 USER_FLAG_IS_CONTACT = 1 << 11;
1000   static constexpr int32 USER_FLAG_IS_MUTUAL_CONTACT = 1 << 12;
1001   static constexpr int32 USER_FLAG_IS_DELETED = 1 << 13;
1002   static constexpr int32 USER_FLAG_IS_BOT = 1 << 14;
1003   static constexpr int32 USER_FLAG_IS_BOT_WITH_PRIVACY_DISABLED = 1 << 15;
1004   static constexpr int32 USER_FLAG_IS_PRIVATE_BOT = 1 << 16;
1005   static constexpr int32 USER_FLAG_IS_VERIFIED = 1 << 17;
1006   static constexpr int32 USER_FLAG_IS_RESTRICTED = 1 << 18;
1007   static constexpr int32 USER_FLAG_IS_INLINE_BOT = 1 << 19;
1008   static constexpr int32 USER_FLAG_IS_INACCESSIBLE = 1 << 20;
1009   static constexpr int32 USER_FLAG_NEED_LOCATION_BOT = 1 << 21;
1010   static constexpr int32 USER_FLAG_HAS_LANGUAGE_CODE = 1 << 22;
1011   static constexpr int32 USER_FLAG_IS_SUPPORT = 1 << 23;
1012   static constexpr int32 USER_FLAG_IS_SCAM = 1 << 24;
1013   static constexpr int32 USER_FLAG_NEED_APPLY_MIN_PHOTO = 1 << 25;
1014   static constexpr int32 USER_FLAG_IS_FAKE = 1 << 26;
1015 
1016   static constexpr int32 USER_FULL_FLAG_IS_BLOCKED = 1 << 0;
1017   static constexpr int32 USER_FULL_FLAG_HAS_ABOUT = 1 << 1;
1018   static constexpr int32 USER_FULL_FLAG_HAS_PHOTO = 1 << 2;
1019   static constexpr int32 USER_FULL_FLAG_HAS_BOT_INFO = 1 << 3;
1020   static constexpr int32 USER_FULL_FLAG_HAS_PINNED_MESSAGE = 1 << 6;
1021   static constexpr int32 USER_FULL_FLAG_CAN_PIN_MESSAGE = 1 << 7;
1022   static constexpr int32 USER_FULL_FLAG_HAS_FOLDER_ID = 1 << 11;
1023   static constexpr int32 USER_FULL_FLAG_HAS_SCHEDULED_MESSAGES = 1 << 12;
1024   static constexpr int32 USER_FULL_FLAG_HAS_MESSAGE_TTL = 1 << 14;
1025   static constexpr int32 USER_FULL_FLAG_HAS_PRIVATE_FORWARD_NAME = 1 << 16;
1026 
1027   static constexpr int32 CHAT_FLAG_USER_IS_CREATOR = 1 << 0;
1028   static constexpr int32 CHAT_FLAG_USER_WAS_KICKED = 1 << 1;
1029   static constexpr int32 CHAT_FLAG_USER_HAS_LEFT = 1 << 2;
1030   // static constexpr int32 CHAT_FLAG_ADMINISTRATORS_ENABLED = 1 << 3;
1031   // static constexpr int32 CHAT_FLAG_IS_ADMINISTRATOR = 1 << 4;
1032   static constexpr int32 CHAT_FLAG_IS_DEACTIVATED = 1 << 5;
1033   static constexpr int32 CHAT_FLAG_WAS_MIGRATED = 1 << 6;
1034   static constexpr int32 CHAT_FLAG_HAS_ACTIVE_GROUP_CALL = 1 << 23;
1035   static constexpr int32 CHAT_FLAG_IS_GROUP_CALL_NON_EMPTY = 1 << 24;
1036 
1037   static constexpr int32 CHAT_FULL_FLAG_HAS_PINNED_MESSAGE = 1 << 6;
1038   static constexpr int32 CHAT_FULL_FLAG_HAS_SCHEDULED_MESSAGES = 1 << 8;
1039   static constexpr int32 CHAT_FULL_FLAG_HAS_FOLDER_ID = 1 << 11;
1040   static constexpr int32 CHAT_FULL_FLAG_HAS_ACTIVE_GROUP_CALL = 1 << 12;
1041   static constexpr int32 CHAT_FULL_FLAG_HAS_MESSAGE_TTL = 1 << 14;
1042   static constexpr int32 CHAT_FULL_FLAG_HAS_PENDING_REQUEST_COUNT = 1 << 17;
1043 
1044   static constexpr int32 CHANNEL_FLAG_USER_IS_CREATOR = 1 << 0;
1045   static constexpr int32 CHANNEL_FLAG_USER_HAS_LEFT = 1 << 2;
1046   static constexpr int32 CHANNEL_FLAG_IS_BROADCAST = 1 << 5;
1047   static constexpr int32 CHANNEL_FLAG_IS_PUBLIC = 1 << 6;
1048   static constexpr int32 CHANNEL_FLAG_IS_VERIFIED = 1 << 7;
1049   static constexpr int32 CHANNEL_FLAG_IS_MEGAGROUP = 1 << 8;
1050   static constexpr int32 CHANNEL_FLAG_IS_RESTRICTED = 1 << 9;
1051   // static constexpr int32 CHANNEL_FLAG_ANYONE_CAN_INVITE = 1 << 10;
1052   static constexpr int32 CHANNEL_FLAG_SIGN_MESSAGES = 1 << 11;
1053   static constexpr int32 CHANNEL_FLAG_IS_MIN = 1 << 12;
1054   static constexpr int32 CHANNEL_FLAG_HAS_ACCESS_HASH = 1 << 13;
1055   static constexpr int32 CHANNEL_FLAG_HAS_ADMIN_RIGHTS = 1 << 14;
1056   static constexpr int32 CHANNEL_FLAG_HAS_BANNED_RIGHTS = 1 << 15;
1057   static constexpr int32 CHANNEL_FLAG_HAS_UNBAN_DATE = 1 << 16;
1058   static constexpr int32 CHANNEL_FLAG_HAS_PARTICIPANT_COUNT = 1 << 17;
1059   static constexpr int32 CHANNEL_FLAG_IS_SCAM = 1 << 19;
1060   static constexpr int32 CHANNEL_FLAG_HAS_LINKED_CHAT = 1 << 20;
1061   static constexpr int32 CHANNEL_FLAG_HAS_LOCATION = 1 << 21;
1062   static constexpr int32 CHANNEL_FLAG_IS_SLOW_MODE_ENABLED = 1 << 22;
1063   static constexpr int32 CHANNEL_FLAG_HAS_ACTIVE_GROUP_CALL = 1 << 23;
1064   static constexpr int32 CHANNEL_FLAG_IS_GROUP_CALL_NON_EMPTY = 1 << 24;
1065   static constexpr int32 CHANNEL_FLAG_IS_FAKE = 1 << 25;
1066   static constexpr int32 CHANNEL_FLAG_IS_GIGAGROUP = 1 << 26;
1067 
1068   static constexpr int32 CHANNEL_FULL_FLAG_HAS_PARTICIPANT_COUNT = 1 << 0;
1069   static constexpr int32 CHANNEL_FULL_FLAG_HAS_ADMINISTRATOR_COUNT = 1 << 1;
1070   static constexpr int32 CHANNEL_FULL_FLAG_HAS_BANNED_COUNT = 1 << 2;
1071   static constexpr int32 CHANNEL_FULL_FLAG_CAN_GET_PARTICIPANTS = 1 << 3;
1072   static constexpr int32 CHANNEL_FULL_FLAG_MIGRATED_FROM = 1 << 4;
1073   static constexpr int32 CHANNEL_FULL_FLAG_HAS_PINNED_MESSAGE = 1 << 5;
1074   static constexpr int32 CHANNEL_FULL_FLAG_CAN_SET_USERNAME = 1 << 6;
1075   static constexpr int32 CHANNEL_FULL_FLAG_CAN_SET_STICKER_SET = 1 << 7;
1076   static constexpr int32 CHANNEL_FULL_FLAG_HAS_STICKER_SET = 1 << 8;
1077   static constexpr int32 CHANNEL_FULL_FLAG_HAS_AVAILABLE_MIN_MESSAGE_ID = 1 << 9;
1078   static constexpr int32 CHANNEL_FULL_FLAG_IS_ALL_HISTORY_HIDDEN = 1 << 10;
1079   static constexpr int32 CHANNEL_FULL_FLAG_HAS_FOLDER_ID = 1 << 11;
1080   static constexpr int32 CHANNEL_FULL_FLAG_HAS_STATISTICS_DC_ID = 1 << 12;
1081   static constexpr int32 CHANNEL_FULL_FLAG_HAS_ONLINE_MEMBER_COUNT = 1 << 13;
1082   static constexpr int32 CHANNEL_FULL_FLAG_HAS_LINKED_CHANNEL_ID = 1 << 14;
1083   static constexpr int32 CHANNEL_FULL_FLAG_HAS_LOCATION = 1 << 15;
1084   static constexpr int32 CHANNEL_FULL_FLAG_CAN_SET_LOCATION = 1 << 16;
1085   static constexpr int32 CHANNEL_FULL_FLAG_HAS_SLOW_MODE_DELAY = 1 << 17;
1086   static constexpr int32 CHANNEL_FULL_FLAG_HAS_SLOW_MODE_NEXT_SEND_DATE = 1 << 18;
1087   static constexpr int32 CHANNEL_FULL_FLAG_HAS_SCHEDULED_MESSAGES = 1 << 19;
1088   static constexpr int32 CHANNEL_FULL_FLAG_CAN_VIEW_STATISTICS = 1 << 20;
1089   static constexpr int32 CHANNEL_FULL_FLAG_HAS_ACTIVE_GROUP_CALL = 1 << 21;
1090   static constexpr int32 CHANNEL_FULL_FLAG_IS_BLOCKED = 1 << 22;
1091   static constexpr int32 CHANNEL_FULL_FLAG_HAS_EXPORTED_INVITE = 1 << 23;
1092   static constexpr int32 CHANNEL_FULL_FLAG_HAS_MESSAGE_TTL = 1 << 24;
1093   static constexpr int32 CHANNEL_FULL_FLAG_HAS_PENDING_REQUEST_COUNT = 1 << 28;
1094   static constexpr int32 CHANNEL_FULL_FLAG_HAS_DEFAULT_SEND_AS = 1 << 29;
1095 
1096   static constexpr int32 CHAT_INVITE_FLAG_IS_CHANNEL = 1 << 0;
1097   static constexpr int32 CHAT_INVITE_FLAG_IS_BROADCAST = 1 << 1;
1098   static constexpr int32 CHAT_INVITE_FLAG_IS_PUBLIC = 1 << 2;
1099   static constexpr int32 CHAT_INVITE_FLAG_IS_MEGAGROUP = 1 << 3;
1100   static constexpr int32 CHAT_INVITE_FLAG_HAS_USERS = 1 << 4;
1101 
1102   static constexpr int32 USER_FULL_EXPIRE_TIME = 60;
1103   static constexpr int32 CHANNEL_FULL_EXPIRE_TIME = 60;
1104 
1105   static constexpr int32 ACCOUNT_UPDATE_FIRST_NAME = 1 << 0;
1106   static constexpr int32 ACCOUNT_UPDATE_LAST_NAME = 1 << 1;
1107   static constexpr int32 ACCOUNT_UPDATE_ABOUT = 1 << 2;
1108 
1109   static bool have_input_peer_user(const User *u, AccessRights access_rights);
1110   static bool have_input_peer_chat(const Chat *c, AccessRights access_rights);
1111   bool have_input_peer_channel(const Channel *c, ChannelId channel_id, AccessRights access_rights,
1112                                bool from_linked = false) const;
1113   static bool have_input_encrypted_peer(const SecretChat *secret_chat, AccessRights access_rights);
1114 
1115   const User *get_user(UserId user_id) const;
1116   User *get_user(UserId user_id);
1117   User *get_user_force(UserId user_id);
1118   User *get_user_force_impl(UserId user_id);
1119 
1120   User *add_user(UserId user_id, const char *source);
1121 
1122   const UserFull *get_user_full(UserId user_id) const;
1123   UserFull *get_user_full(UserId user_id);
1124   UserFull *get_user_full_force(UserId user_id);
1125 
1126   UserFull *add_user_full(UserId user_id);
1127 
1128   void send_get_user_full_query(UserId user_id, tl_object_ptr<telegram_api::InputUser> &&input_user,
1129                                 Promise<Unit> &&promise, const char *source);
1130 
1131   const Chat *get_chat(ChatId chat_id) const;
1132   Chat *get_chat(ChatId chat_id);
1133   Chat *get_chat_force(ChatId chat_id);
1134 
1135   Chat *add_chat(ChatId chat_id);
1136 
1137   const ChatFull *get_chat_full(ChatId chat_id) const;
1138   ChatFull *get_chat_full(ChatId chat_id);
1139   ChatFull *get_chat_full_force(ChatId chat_id, const char *source);
1140 
1141   ChatFull *add_chat_full(ChatId chat_id);
1142 
1143   void send_get_chat_full_query(ChatId chat_id, Promise<Unit> &&promise, const char *source);
1144 
1145   const Channel *get_channel(ChannelId channel_id) const;
1146   Channel *get_channel(ChannelId channel_id);
1147   Channel *get_channel_force(ChannelId channel_id);
1148 
1149   Channel *add_channel(ChannelId channel_id, const char *source);
1150 
1151   const ChannelFull *get_channel_full(ChannelId channel_id) const;
1152   const ChannelFull *get_channel_full_const(ChannelId channel_id) const;
1153   ChannelFull *get_channel_full(ChannelId channel_id, bool only_local, const char *source);
1154   ChannelFull *get_channel_full_force(ChannelId channel_id, bool only_local, const char *source);
1155 
1156   ChannelFull *add_channel_full(ChannelId channel_id);
1157 
1158   void send_get_channel_full_query(ChannelFull *channel_full, ChannelId channel_id, Promise<Unit> &&promise,
1159                                    const char *source);
1160 
1161   const SecretChat *get_secret_chat(SecretChatId secret_chat_id) const;
1162   SecretChat *get_secret_chat(SecretChatId secret_chat_id);
1163   SecretChat *get_secret_chat_force(SecretChatId secret_chat_id);
1164 
1165   SecretChat *add_secret_chat(SecretChatId secret_chat_id);
1166 
1167   static DialogParticipantStatus get_chat_status(const Chat *c);
1168   DialogParticipantStatus get_chat_permissions(const Chat *c) const;
1169 
1170   static ChannelType get_channel_type(const Channel *c);
1171   static DialogParticipantStatus get_channel_status(const Channel *c);
1172   DialogParticipantStatus get_channel_permissions(const Channel *c) const;
1173   static bool get_channel_sign_messages(const Channel *c);
1174   static bool get_channel_has_linked_channel(const Channel *c);
1175 
1176   void set_my_id(UserId my_id);
1177 
1178   static bool is_valid_username(const string &username);
1179 
1180   void on_update_user_name(User *u, UserId user_id, string &&first_name, string &&last_name, string &&username);
1181   static void on_update_user_phone_number(User *u, UserId user_id, string &&phone_number);
1182   void on_update_user_photo(User *u, UserId user_id, tl_object_ptr<telegram_api::UserProfilePhoto> &&photo,
1183                             const char *source);
1184   void on_update_user_is_contact(User *u, UserId user_id, bool is_contact, bool is_mutual_contact);
1185   void on_update_user_online(User *u, UserId user_id, tl_object_ptr<telegram_api::UserStatus> &&status);
1186   void on_update_user_local_was_online(User *u, UserId user_id, int32 local_was_online);
1187 
1188   void do_update_user_photo(User *u, UserId user_id, tl_object_ptr<telegram_api::UserProfilePhoto> &&photo,
1189                             const char *source);
1190   void do_update_user_photo(User *u, UserId user_id, ProfilePhoto &&new_photo, bool invalidate_photo_cache,
1191                             const char *source);
1192 
1193   void upload_profile_photo(FileId file_id, bool is_animation, double main_frame_timestamp, Promise<Unit> &&promise,
1194                             int reupload_count = 0, vector<int> bad_parts = {});
1195 
1196   void on_upload_profile_photo(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file);
1197   void on_upload_profile_photo_error(FileId file_id, Status status);
1198 
1199   void register_user_photo(User *u, UserId user_id, const Photo &photo);
1200 
1201   static void on_update_user_full_is_blocked(UserFull *user_full, UserId user_id, bool is_blocked);
1202   static void on_update_user_full_common_chat_count(UserFull *user_full, UserId user_id, int32 common_chat_count);
1203   static void on_update_user_full_commands(UserFull *user_full, UserId user_id,
1204                                            vector<tl_object_ptr<telegram_api::botCommand>> &&bot_commands);
1205   void on_update_user_full_need_phone_number_privacy_exception(UserFull *user_full, UserId user_id,
1206                                                                bool need_phone_number_privacy_exception) const;
1207 
1208   void add_profile_photo_to_cache(UserId user_id, Photo &&photo);
1209   bool delete_profile_photo_from_cache(UserId user_id, int64 profile_photo_id, bool send_updates);
1210   void drop_user_photos(UserId user_id, bool is_empty, bool drop_user_full_photo, const char *source);
1211   void drop_user_full(UserId user_id);
1212 
1213   void on_update_chat_status(Chat *c, ChatId chat_id, DialogParticipantStatus status);
1214   static void on_update_chat_default_permissions(Chat *c, ChatId chat_id, RestrictedRights default_permissions,
1215                                                  int32 version);
1216   void on_update_chat_participant_count(Chat *c, ChatId chat_id, int32 participant_count, int32 version,
1217                                         const string &debug_str);
1218   void on_update_chat_photo(Chat *c, ChatId chat_id, tl_object_ptr<telegram_api::ChatPhoto> &&chat_photo_ptr);
1219   static void on_update_chat_title(Chat *c, ChatId chat_id, string &&title);
1220   static void on_update_chat_active(Chat *c, ChatId chat_id, bool is_active);
1221   static void on_update_chat_migrated_to_channel_id(Chat *c, ChatId chat_id, ChannelId migrated_to_channel_id);
1222   static void on_update_chat_noforwards(Chat *c, ChatId chat_id, bool noforwards);
1223 
1224   void on_update_chat_full_photo(ChatFull *chat_full, ChatId chat_id, Photo photo);
1225   bool on_update_chat_full_participants_short(ChatFull *chat_full, ChatId chat_id, int32 version);
1226   void on_update_chat_full_participants(ChatFull *chat_full, ChatId chat_id, vector<DialogParticipant> participants,
1227                                         int32 version, bool from_update);
1228   void on_update_chat_full_invite_link(ChatFull *chat_full,
1229                                        tl_object_ptr<telegram_api::chatInviteExported> &&invite_link);
1230 
1231   void on_update_channel_photo(Channel *c, ChannelId channel_id,
1232                                tl_object_ptr<telegram_api::ChatPhoto> &&chat_photo_ptr);
1233   static void on_update_channel_title(Channel *c, ChannelId channel_id, string &&title);
1234   void on_update_channel_username(Channel *c, ChannelId channel_id, string &&username);
1235   void on_update_channel_status(Channel *c, ChannelId channel_id, DialogParticipantStatus &&status);
1236   static void on_update_channel_default_permissions(Channel *c, ChannelId channel_id,
1237                                                     RestrictedRights default_permissions);
1238   static void on_update_channel_has_location(Channel *c, ChannelId channel_id, bool has_location);
1239   static void on_update_channel_noforwards(Channel *c, ChannelId channel_id, bool noforwards);
1240 
1241   void on_update_channel_bot_user_ids(ChannelId channel_id, vector<UserId> &&bot_user_ids);
1242 
1243   void on_update_channel_full_photo(ChannelFull *channel_full, ChannelId channel_id, Photo photo);
1244   void on_update_channel_full_invite_link(ChannelFull *channel_full,
1245                                           tl_object_ptr<telegram_api::chatInviteExported> &&invite_link);
1246   void on_update_channel_full_linked_channel_id(ChannelFull *channel_full, ChannelId channel_id,
1247                                                 ChannelId linked_channel_id);
1248   void on_update_channel_full_location(ChannelFull *channel_full, ChannelId channel_id, const DialogLocation &location);
1249   void on_update_channel_full_slow_mode_delay(ChannelFull *channel_full, ChannelId channel_id, int32 slow_mode_delay,
1250                                               int32 slow_mode_next_send_date);
1251   static void on_update_channel_full_slow_mode_next_send_date(ChannelFull *channel_full,
1252                                                               int32 slow_mode_next_send_date);
1253   static void on_update_channel_full_bot_user_ids(ChannelFull *channel_full, ChannelId channel_id,
1254                                                   vector<UserId> &&bot_user_ids);
1255 
1256   void on_channel_status_changed(Channel *c, ChannelId channel_id, const DialogParticipantStatus &old_status,
1257                                  const DialogParticipantStatus &new_status);
1258   void on_channel_username_changed(const Channel *c, ChannelId channel_id, const string &old_username,
1259                                    const string &new_username);
1260 
1261   void remove_linked_channel_id(ChannelId channel_id);
1262   ChannelId get_linked_channel_id(ChannelId channel_id) const;
1263 
1264   static bool speculative_add_count(int32 &count, int32 delta_count, int32 min_count = 0);
1265 
1266   void speculative_add_channel_participant_count(ChannelId channel_id, int32 delta_participant_count, bool by_me);
1267 
1268   void speculative_add_channel_user(ChannelId channel_id, UserId user_id, const DialogParticipantStatus &new_status,
1269                                     const DialogParticipantStatus &old_status);
1270 
1271   void drop_chat_photos(ChatId chat_id, bool is_empty, bool drop_chat_full_photo, const char *source);
1272   void drop_chat_full(ChatId chat_id);
1273 
1274   void drop_channel_photos(ChannelId channel_id, bool is_empty, bool drop_channel_full_photo, const char *source);
1275 
1276   void do_invalidate_channel_full(ChannelFull *channel_full, ChannelId channel_id, bool need_drop_slow_mode_delay);
1277 
1278   void update_user_online_member_count(User *u);
1279   void update_chat_online_member_count(const ChatFull *chat_full, ChatId chat_id, bool is_from_server);
1280   void update_channel_online_member_count(ChannelId channel_id, bool is_from_server);
1281   void update_dialog_online_member_count(const vector<DialogParticipant> &participants, DialogId dialog_id,
1282                                          bool is_from_server);
1283 
1284   void on_chat_update(telegram_api::chatEmpty &chat, const char *source);
1285   void on_chat_update(telegram_api::chat &chat, const char *source);
1286   void on_chat_update(telegram_api::chatForbidden &chat, const char *source);
1287   void on_chat_update(telegram_api::channel &channel, const char *source);
1288   void on_chat_update(telegram_api::channelForbidden &channel, const char *source);
1289 
1290   void save_user(User *u, UserId user_id, bool from_binlog);
1291   static string get_user_database_key(UserId user_id);
1292   static string get_user_database_value(const User *u);
1293   void save_user_to_database(User *u, UserId user_id);
1294   void save_user_to_database_impl(User *u, UserId user_id, string value);
1295   void on_save_user_to_database(UserId user_id, bool success);
1296   void load_user_from_database(User *u, UserId user_id, Promise<Unit> promise);
1297   void load_user_from_database_impl(UserId user_id, Promise<Unit> promise);
1298   void on_load_user_from_database(UserId user_id, string value, bool force);
1299 
1300   void save_chat(Chat *c, ChatId chat_id, bool from_binlog);
1301   static string get_chat_database_key(ChatId chat_id);
1302   static string get_chat_database_value(const Chat *c);
1303   void save_chat_to_database(Chat *c, ChatId chat_id);
1304   void save_chat_to_database_impl(Chat *c, ChatId chat_id, string value);
1305   void on_save_chat_to_database(ChatId chat_id, bool success);
1306   void load_chat_from_database(Chat *c, ChatId chat_id, Promise<Unit> promise);
1307   void load_chat_from_database_impl(ChatId chat_id, Promise<Unit> promise);
1308   void on_load_chat_from_database(ChatId chat_id, string value, bool force);
1309 
1310   void save_channel(Channel *c, ChannelId channel_id, bool from_binlog);
1311   static string get_channel_database_key(ChannelId channel_id);
1312   static string get_channel_database_value(const Channel *c);
1313   void save_channel_to_database(Channel *c, ChannelId channel_id);
1314   void save_channel_to_database_impl(Channel *c, ChannelId channel_id, string value);
1315   void on_save_channel_to_database(ChannelId channel_id, bool success);
1316   void load_channel_from_database(Channel *c, ChannelId channel_id, Promise<Unit> promise);
1317   void load_channel_from_database_impl(ChannelId channel_id, Promise<Unit> promise);
1318   void on_load_channel_from_database(ChannelId channel_id, string value, bool force);
1319 
1320   void save_secret_chat(SecretChat *c, SecretChatId secret_chat_id, bool from_binlog);
1321   static string get_secret_chat_database_key(SecretChatId secret_chat_id);
1322   static string get_secret_chat_database_value(const SecretChat *c);
1323   void save_secret_chat_to_database(SecretChat *c, SecretChatId secret_chat_id);
1324   void save_secret_chat_to_database_impl(SecretChat *c, SecretChatId secret_chat_id, string value);
1325   void on_save_secret_chat_to_database(SecretChatId secret_chat_id, bool success);
1326   void load_secret_chat_from_database(SecretChat *c, SecretChatId secret_chat_id, Promise<Unit> promise);
1327   void load_secret_chat_from_database_impl(SecretChatId secret_chat_id, Promise<Unit> promise);
1328   void on_load_secret_chat_from_database(SecretChatId secret_chat_id, string value, bool force);
1329 
1330   static void save_user_full(const UserFull *user_full, UserId user_id);
1331   static string get_user_full_database_key(UserId user_id);
1332   static string get_user_full_database_value(const UserFull *user_full);
1333   void on_load_user_full_from_database(UserId user_id, string value);
1334 
1335   static void save_chat_full(const ChatFull *chat_full, ChatId chat_id);
1336   static string get_chat_full_database_key(ChatId chat_id);
1337   static string get_chat_full_database_value(const ChatFull *chat_full);
1338   void on_load_chat_full_from_database(ChatId chat_id, string value);
1339 
1340   static void save_channel_full(const ChannelFull *channel_full, ChannelId channel_id);
1341   static string get_channel_full_database_key(ChannelId channel_id);
1342   static string get_channel_full_database_value(const ChannelFull *channel_full);
1343   void on_load_channel_full_from_database(ChannelId channel_id, string value, const char *source);
1344 
1345   void update_user(User *u, UserId user_id, bool from_binlog = false, bool from_database = false);
1346   void update_chat(Chat *c, ChatId chat_id, bool from_binlog = false, bool from_database = false);
1347   void update_channel(Channel *c, ChannelId channel_id, bool from_binlog = false, bool from_database = false);
1348   void update_secret_chat(SecretChat *c, SecretChatId secret_chat_id, bool from_binlog = false,
1349                           bool from_database = false);
1350 
1351   void update_user_full(UserFull *user_full, UserId user_id, const char *source, bool from_database = false);
1352   void update_chat_full(ChatFull *chat_full, ChatId chat_id, const char *source, bool from_database = false);
1353   void update_channel_full(ChannelFull *channel_full, ChannelId channel_id, const char *source,
1354                            bool from_database = false);
1355 
1356   static bool is_chat_full_outdated(const ChatFull *chat_full, const Chat *c, ChatId chat_id);
1357 
1358   bool is_user_contact(const User *u, UserId user_id, bool is_mutual) const;
1359 
1360   int32 get_user_was_online(const User *u, UserId user_id) const;
1361 
1362   int64 get_contacts_hash();
1363 
1364   void update_contacts_hints(const User *u, UserId user_id, bool from_database);
1365 
1366   void save_next_contacts_sync_date();
1367 
1368   void save_contacts_to_database();
1369 
1370   void load_contacts(Promise<Unit> &&promise);
1371 
1372   void on_load_contacts_from_database(string value);
1373 
1374   void on_get_contacts_finished(size_t expected_contact_count);
1375 
1376   void do_import_contacts(vector<Contact> contacts, int64 random_id, Promise<Unit> &&promise);
1377 
1378   void on_import_contacts_finished(int64 random_id, vector<UserId> imported_contact_user_ids,
1379                                    vector<int32> unimported_contact_invites);
1380 
1381   void load_imported_contacts(Promise<Unit> &&promise);
1382 
1383   void on_load_imported_contacts_from_database(string value);
1384 
1385   void on_load_imported_contacts_finished();
1386 
1387   void on_clear_imported_contacts(vector<Contact> &&contacts, vector<size_t> contacts_unique_id,
1388                                   std::pair<vector<size_t>, vector<Contact>> &&to_add, Promise<Unit> &&promise);
1389 
1390   void send_update_chat_member(DialogId dialog_id, UserId agent_user_id, int32 date,
1391                                const DialogInviteLink &invite_link, const DialogParticipant &old_dialog_participant,
1392                                const DialogParticipant &new_dialog_participant);
1393 
1394   static vector<td_api::object_ptr<td_api::chatNearby>> get_chats_nearby_object(
1395       const vector<DialogNearby> &dialogs_nearby);
1396 
1397   void send_update_users_nearby() const;
1398 
1399   void on_get_dialogs_nearby(Result<tl_object_ptr<telegram_api::Updates>> result,
1400                              Promise<td_api::object_ptr<td_api::chatsNearby>> &&promise);
1401 
1402   void try_send_set_location_visibility_query();
1403 
1404   void on_set_location_visibility_expire_date(int32 set_expire_date, int32 error_code);
1405 
1406   void set_location_visibility_expire_date(int32 expire_date);
1407 
1408   void on_get_is_location_visible(Result<tl_object_ptr<telegram_api::Updates>> &&result, Promise<Unit> &&promise);
1409 
1410   void update_is_location_visible();
1411 
1412   static bool is_channel_public(const Channel *c);
1413 
1414   static void return_created_public_dialogs(Promise<td_api::object_ptr<td_api::chats>> &&promise,
1415                                             const vector<ChannelId> &channel_ids);
1416 
1417   void reload_created_public_dialogs(PublicDialogType type, Promise<td_api::object_ptr<td_api::chats>> &&promise);
1418 
1419   void finish_get_created_public_dialogs(PublicDialogType type, Result<Unit> &&result);
1420 
1421   void update_created_public_channels(Channel *c, ChannelId channel_id);
1422 
1423   void save_created_public_channels(PublicDialogType type);
1424 
1425   void update_created_public_broadcasts();
1426 
1427   void export_dialog_invite_link_impl(DialogId dialog_id, string title, int32 expire_date, int32 usage_limit,
1428                                       bool creates_join_request, bool is_permanent,
1429                                       Promise<td_api::object_ptr<td_api::chatInviteLink>> &&promise);
1430 
1431   void remove_dialog_access_by_invite_link(DialogId dialog_id);
1432 
1433   Status can_manage_dialog_invite_links(DialogId dialog_id, bool creator_only = false);
1434 
1435   bool update_permanent_invite_link(DialogInviteLink &invite_link, DialogInviteLink new_invite_link);
1436 
1437   void add_chat_participant(ChatId chat_id, UserId user_id, int32 forward_limit, Promise<Unit> &&promise);
1438 
1439   void add_channel_participant(ChannelId channel_id, UserId user_id, const DialogParticipantStatus &old_status,
1440                                Promise<Unit> &&promise);
1441 
1442   void add_channel_participants(ChannelId channel_id, const vector<UserId> &user_ids, Promise<Unit> &&promise);
1443 
1444   vector<BotCommands> get_bot_commands(vector<tl_object_ptr<telegram_api::botInfo>> &&bot_infos,
1445                                        const vector<DialogParticipant> *participants);
1446 
1447   const DialogParticipant *get_chat_participant(ChatId chat_id, UserId user_id) const;
1448 
1449   static const DialogParticipant *get_chat_full_participant(const ChatFull *chat_full, DialogId dialog_id);
1450 
1451   std::pair<int32, vector<DialogId>> search_among_dialogs(const vector<DialogId> &dialog_ids, const string &query,
1452                                                           int32 limit) const;
1453 
1454   DialogParticipants search_private_chat_participants(UserId my_user_id, UserId peer_user_id, const string &query,
1455                                                       int32 limit, DialogParticipantsFilter filter) const;
1456 
1457   void do_get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id,
1458                                  Promise<DialogParticipant> &&promise);
1459 
1460   void finish_get_dialog_participant(DialogParticipant &&dialog_participant,
1461                                      Promise<td_api::object_ptr<td_api::chatMember>> &&promise);
1462 
1463   void get_chat_participant(ChatId chat_id, UserId user_id, Promise<DialogParticipant> &&promise);
1464 
1465   void finish_get_chat_participant(ChatId chat_id, UserId user_id, Promise<DialogParticipant> &&promise);
1466 
1467   void get_channel_participant(ChannelId channel_id, DialogId participant_dialog_id,
1468                                Promise<DialogParticipant> &&promise);
1469 
1470   void finish_get_channel_participant(ChannelId channel_id, DialogParticipant &&dialog_participant,
1471                                       Promise<DialogParticipant> &&promise);
1472 
1473   static string get_dialog_administrators_database_key(DialogId dialog_id);
1474 
1475   void load_dialog_administrators(DialogId dialog_id, Promise<Unit> &&promise);
1476 
1477   void on_load_dialog_administrators_from_database(DialogId dialog_id, string value, Promise<Unit> &&promise);
1478 
1479   void on_load_administrator_users_finished(DialogId dialog_id, vector<DialogAdministrator> administrators,
1480                                             Result<> result, Promise<Unit> promise);
1481 
1482   void reload_dialog_administrators(DialogId dialog_id, int64 hash, Promise<Unit> &&promise);
1483 
1484   void remove_dialog_suggested_action(SuggestedAction action);
1485 
1486   void on_dismiss_suggested_action(SuggestedAction action, Result<Unit> &&result);
1487 
1488   static td_api::object_ptr<td_api::updateUser> get_update_unknown_user_object(UserId user_id);
1489 
1490   td_api::object_ptr<td_api::UserStatus> get_user_status_object(UserId user_id, const User *u) const;
1491 
1492   tl_object_ptr<td_api::user> get_user_object(UserId user_id, const User *u) const;
1493 
1494   tl_object_ptr<td_api::userFullInfo> get_user_full_info_object(UserId user_id, const UserFull *user_full) const;
1495 
1496   static td_api::object_ptr<td_api::updateBasicGroup> get_update_unknown_basic_group_object(ChatId chat_id);
1497 
1498   tl_object_ptr<td_api::basicGroup> get_basic_group_object(ChatId chat_id, const Chat *c);
1499 
1500   tl_object_ptr<td_api::basicGroup> get_basic_group_object_const(ChatId chat_id, const Chat *c) const;
1501 
1502   tl_object_ptr<td_api::basicGroupFullInfo> get_basic_group_full_info_object(const ChatFull *chat_full) const;
1503 
1504   static td_api::object_ptr<td_api::updateSupergroup> get_update_unknown_supergroup_object(ChannelId channel_id);
1505 
1506   static tl_object_ptr<td_api::supergroup> get_supergroup_object(ChannelId channel_id, const Channel *c);
1507 
1508   tl_object_ptr<td_api::supergroupFullInfo> get_supergroup_full_info_object(const ChannelFull *channel_full,
1509                                                                             ChannelId channel_id) const;
1510 
1511   static tl_object_ptr<td_api::SecretChatState> get_secret_chat_state_object(SecretChatState state);
1512 
1513   static td_api::object_ptr<td_api::updateSecretChat> get_update_unknown_secret_chat_object(
1514       SecretChatId secret_chat_id);
1515 
1516   tl_object_ptr<td_api::secretChat> get_secret_chat_object(SecretChatId secret_chat_id, const SecretChat *secret_chat);
1517 
1518   tl_object_ptr<td_api::secretChat> get_secret_chat_object_const(SecretChatId secret_chat_id,
1519                                                                  const SecretChat *secret_chat) const;
1520 
1521   vector<ChannelId> get_channel_ids(vector<tl_object_ptr<telegram_api::Chat>> &&chats, const char *source);
1522 
1523   vector<DialogId> get_dialog_ids(vector<tl_object_ptr<telegram_api::Chat>> &&chats, const char *source);
1524 
1525   void update_dialogs_for_discussion(DialogId dialog_id, bool is_suitable);
1526 
1527   void set_chat_participant_status(ChatId chat_id, UserId user_id, DialogParticipantStatus status,
1528                                    Promise<Unit> &&promise);
1529 
1530   void set_channel_participant_status(ChannelId channel_id, DialogId participant_dialog_id,
1531                                       DialogParticipantStatus status, Promise<Unit> &&promise);
1532 
1533   void send_edit_chat_admin_query(ChatId chat_id, UserId user_id, bool is_administrator, Promise<Unit> &&promise);
1534 
1535   void delete_chat_participant(ChatId chat_id, UserId user_id, bool revoke_messages, Promise<Unit> &&promise);
1536 
1537   void search_chat_participants(ChatId chat_id, const string &query, int32 limit, DialogParticipantsFilter filter,
1538                                 Promise<DialogParticipants> &&promise);
1539 
1540   void do_search_chat_participants(ChatId chat_id, const string &query, int32 limit, DialogParticipantsFilter filter,
1541                                    Promise<DialogParticipants> &&promise);
1542 
1543   void on_get_channel_participants(ChannelId channel_id, ChannelParticipantsFilter filter, int32 offset, int32 limit,
1544                                    string additional_query, int32 additional_limit,
1545                                    tl_object_ptr<telegram_api::channels_channelParticipants> &&channel_participants,
1546                                    Promise<DialogParticipants> &&promise);
1547 
1548   bool have_channel_participant_cache(ChannelId channel_id) const;
1549 
1550   void add_channel_participant_to_cache(ChannelId channel_id, const DialogParticipant &dialog_participant,
1551                                         bool allow_replace);
1552 
1553   const DialogParticipant *get_channel_participant_from_cache(ChannelId channel_id, DialogId participant_dialog_id);
1554 
1555   void set_channel_participant_status_impl(ChannelId channel_id, DialogId participant_dialog_id,
1556                                            DialogParticipantStatus status, DialogParticipantStatus old_status,
1557                                            Promise<Unit> &&promise);
1558 
1559   void promote_channel_participant(ChannelId channel_id, UserId user_id, const DialogParticipantStatus &status,
1560                                    const DialogParticipantStatus &old_status, Promise<Unit> &&promise);
1561 
1562   void restrict_channel_participant(ChannelId channel_id, DialogId participant_dialog_id,
1563                                     DialogParticipantStatus &&status, DialogParticipantStatus &&old_status,
1564                                     Promise<Unit> &&promise);
1565 
1566   void transfer_channel_ownership(ChannelId channel_id, UserId user_id,
1567                                   tl_object_ptr<telegram_api::InputCheckPasswordSRP> input_check_password,
1568                                   Promise<Unit> &&promise);
1569 
1570   void delete_chat(ChatId chat_id, Promise<Unit> &&promise);
1571 
1572   void delete_channel(ChannelId channel_id, Promise<Unit> &&promise);
1573 
1574   void get_channel_statistics_dc_id_impl(ChannelId channel_id, bool for_full_statistics, Promise<DcId> &&promise);
1575 
1576   void send_get_channel_stats_query(DcId dc_id, ChannelId channel_id, bool is_dark,
1577                                     Promise<td_api::object_ptr<td_api::ChatStatistics>> &&promise);
1578 
1579   void send_get_channel_message_stats_query(DcId dc_id, FullMessageId full_message_id, bool is_dark,
1580                                             Promise<td_api::object_ptr<td_api::messageStatistics>> &&promise);
1581 
1582   void send_load_async_graph_query(DcId dc_id, string token, int64 x,
1583                                    Promise<td_api::object_ptr<td_api::StatisticalGraph>> &&promise);
1584 
1585   static void on_user_online_timeout_callback(void *contacts_manager_ptr, int64 user_id_long);
1586 
1587   static void on_channel_unban_timeout_callback(void *contacts_manager_ptr, int64 channel_id_long);
1588 
1589   static void on_user_nearby_timeout_callback(void *contacts_manager_ptr, int64 user_id_long);
1590 
1591   static void on_slow_mode_delay_timeout_callback(void *contacts_manager_ptr, int64 channel_id_long);
1592 
1593   static void on_invite_link_info_expire_timeout_callback(void *contacts_manager_ptr, int64 dialog_id_long);
1594 
1595   static void on_channel_participant_cache_timeout_callback(void *contacts_manager_ptr, int64 channel_id_long);
1596 
1597   void on_user_online_timeout(UserId user_id);
1598 
1599   void on_channel_unban_timeout(ChannelId channel_id);
1600 
1601   void on_user_nearby_timeout(UserId user_id);
1602 
1603   void on_slow_mode_delay_timeout(ChannelId channel_id);
1604 
1605   void on_invite_link_info_expire_timeout(DialogId dialog_id);
1606 
1607   void on_channel_participant_cache_timeout(ChannelId channel_id);
1608 
1609   void tear_down() final;
1610 
1611   Td *td_;
1612   ActorShared<> parent_;
1613   UserId my_id_;
1614   UserId support_user_id_;
1615   int32 my_was_online_local_ = 0;
1616 
1617   std::unordered_map<UserId, unique_ptr<User>, UserIdHash> users_;
1618   std::unordered_map<UserId, unique_ptr<UserFull>, UserIdHash> users_full_;
1619   std::unordered_map<UserId, UserPhotos, UserIdHash> user_photos_;
1620   mutable std::unordered_set<UserId, UserIdHash> unknown_users_;
1621   std::unordered_map<UserId, tl_object_ptr<telegram_api::UserProfilePhoto>, UserIdHash> pending_user_photos_;
1622   struct UserIdPhotoIdHash {
operatorUserIdPhotoIdHash1623     std::size_t operator()(const std::pair<UserId, int64> &pair) const {
1624       return UserIdHash()(pair.first) * 2023654985u + std::hash<int64>()(pair.second);
1625     }
1626   };
1627   std::unordered_map<std::pair<UserId, int64>, FileSourceId, UserIdPhotoIdHash> user_profile_photo_file_source_ids_;
1628   std::unordered_map<int64, FileId> my_photo_file_id_;
1629 
1630   std::unordered_map<ChatId, unique_ptr<Chat>, ChatIdHash> chats_;
1631   std::unordered_map<ChatId, unique_ptr<ChatFull>, ChatIdHash> chats_full_;
1632   mutable std::unordered_set<ChatId, ChatIdHash> unknown_chats_;
1633   std::unordered_map<ChatId, FileSourceId, ChatIdHash> chat_full_file_source_ids_;
1634 
1635   std::unordered_set<ChannelId, ChannelIdHash> min_channels_;
1636   std::unordered_map<ChannelId, unique_ptr<Channel>, ChannelIdHash> channels_;
1637   std::unordered_map<ChannelId, unique_ptr<ChannelFull>, ChannelIdHash> channels_full_;
1638   mutable std::unordered_set<ChannelId, ChannelIdHash> unknown_channels_;
1639   std::unordered_set<ChannelId, ChannelIdHash> invalidated_channels_full_;
1640   std::unordered_map<ChannelId, FileSourceId, ChannelIdHash> channel_full_file_source_ids_;
1641 
1642   std::unordered_map<SecretChatId, unique_ptr<SecretChat>, SecretChatIdHash> secret_chats_;
1643   mutable std::unordered_set<SecretChatId, SecretChatIdHash> unknown_secret_chats_;
1644 
1645   std::unordered_map<UserId, vector<SecretChatId>, UserIdHash> secret_chats_with_user_;
1646 
1647   struct DialogAccessByInviteLink {
1648     std::unordered_set<string> invite_links;
1649     int32 accessible_before = 0;
1650   };
1651   std::unordered_map<string, unique_ptr<InviteLinkInfo>> invite_link_infos_;
1652   std::unordered_map<DialogId, DialogAccessByInviteLink, DialogIdHash> dialog_access_by_invite_link_;
1653 
1654   bool created_public_channels_inited_[2] = {false, false};
1655   vector<ChannelId> created_public_channels_[2];
1656   vector<Promise<td_api::object_ptr<td_api::chats>>> get_created_public_channels_queries_[2];
1657 
1658   bool dialogs_for_discussion_inited_ = false;
1659   vector<DialogId> dialogs_for_discussion_;
1660 
1661   bool inactive_channels_inited_ = false;
1662   vector<ChannelId> inactive_channels_;
1663 
1664   std::unordered_map<UserId, vector<Promise<Unit>>, UserIdHash> load_user_from_database_queries_;
1665   std::unordered_set<UserId, UserIdHash> loaded_from_database_users_;
1666   std::unordered_set<UserId, UserIdHash> unavailable_user_fulls_;
1667 
1668   std::unordered_map<ChatId, vector<Promise<Unit>>, ChatIdHash> load_chat_from_database_queries_;
1669   std::unordered_set<ChatId, ChatIdHash> loaded_from_database_chats_;
1670   std::unordered_set<ChatId, ChatIdHash> unavailable_chat_fulls_;
1671 
1672   std::unordered_map<ChannelId, vector<Promise<Unit>>, ChannelIdHash> load_channel_from_database_queries_;
1673   std::unordered_set<ChannelId, ChannelIdHash> loaded_from_database_channels_;
1674   std::unordered_set<ChannelId, ChannelIdHash> unavailable_channel_fulls_;
1675 
1676   std::unordered_map<SecretChatId, vector<Promise<Unit>>, SecretChatIdHash> load_secret_chat_from_database_queries_;
1677   std::unordered_set<SecretChatId, SecretChatIdHash> loaded_from_database_secret_chats_;
1678 
1679   QueryCombiner get_user_full_queries_{"GetUserFullCombiner", 2.0};
1680   QueryCombiner get_chat_full_queries_{"GetChatFullCombiner", 2.0};
1681 
1682   std::unordered_map<DialogId, vector<DialogAdministrator>, DialogIdHash> dialog_administrators_;
1683 
1684   std::unordered_map<DialogId, vector<SuggestedAction>, DialogIdHash> dialog_suggested_actions_;
1685   std::unordered_map<DialogId, vector<Promise<Unit>>, DialogIdHash> dismiss_suggested_action_queries_;
1686 
1687   class UploadProfilePhotoCallback;
1688   std::shared_ptr<UploadProfilePhotoCallback> upload_profile_photo_callback_;
1689 
1690   struct UploadedProfilePhoto {
1691     double main_frame_timestamp;
1692     bool is_animation;
1693     int reupload_count;
1694     Promise<Unit> promise;
1695 
UploadedProfilePhotoUploadedProfilePhoto1696     UploadedProfilePhoto(double main_frame_timestamp, bool is_animation, int32 reupload_count, Promise<Unit> promise)
1697         : main_frame_timestamp(main_frame_timestamp)
1698         , is_animation(is_animation)
1699         , reupload_count(reupload_count)
1700         , promise(std::move(promise)) {
1701     }
1702   };
1703   std::unordered_map<FileId, UploadedProfilePhoto, FileIdHash> uploaded_profile_photos_;  // file_id -> promise
1704 
1705   struct ImportContactsTask {
1706     Promise<Unit> promise_;
1707     vector<Contact> input_contacts_;
1708     vector<UserId> imported_user_ids_;
1709     vector<int32> unimported_contact_invites_;
1710   };
1711   std::unordered_map<int64, unique_ptr<ImportContactsTask>> import_contact_tasks_;
1712 
1713   std::unordered_map<int64, std::pair<vector<UserId>, vector<int32>>> imported_contacts_;
1714 
1715   std::unordered_map<ChannelId, vector<DialogParticipant>, ChannelIdHash> cached_channel_participants_;
1716 
1717   // bot-administrators only
1718   struct ChannelParticipantInfo {
1719     DialogParticipant participant_;
1720 
1721     int32 last_access_date_ = 0;
1722   };
1723   struct ChannelParticipants {
1724     std::unordered_map<DialogId, ChannelParticipantInfo, DialogIdHash> participants_;
1725   };
1726   std::unordered_map<ChannelId, ChannelParticipants, ChannelIdHash> channel_participants_;
1727 
1728   bool are_contacts_loaded_ = false;
1729   int32 next_contacts_sync_date_ = 0;
1730   Hints contacts_hints_;  // search contacts by first name, last name and username
1731   vector<Promise<Unit>> load_contacts_queries_;
1732   MultiPromiseActor load_contact_users_multipromise_{"LoadContactUsersMultiPromiseActor"};
1733   int32 saved_contact_count_ = -1;
1734 
1735   int32 was_online_local_ = 0;
1736   int32 was_online_remote_ = 0;
1737 
1738   bool are_imported_contacts_loaded_ = false;
1739   vector<Promise<Unit>> load_imported_contacts_queries_;
1740   MultiPromiseActor load_imported_contact_users_multipromise_{"LoadImportedContactUsersMultiPromiseActor"};
1741   vector<Contact> all_imported_contacts_;
1742   bool are_imported_contacts_changing_ = false;
1743   bool need_clear_imported_contacts_ = false;
1744 
1745   vector<DialogNearby> users_nearby_;
1746   vector<DialogNearby> channels_nearby_;
1747   std::unordered_set<UserId, UserIdHash> all_users_nearby_;
1748 
1749   int32 location_visibility_expire_date_ = 0;
1750   int32 pending_location_visibility_expire_date_ = -1;
1751   bool is_set_location_visibility_request_sent_ = false;
1752   Location last_user_location_;
1753 
1754   std::unordered_map<ChannelId, ChannelId, ChannelIdHash> linked_channel_ids_;
1755 
1756   std::unordered_set<UserId, UserIdHash> restricted_user_ids_;
1757   std::unordered_set<ChannelId, ChannelIdHash> restricted_channel_ids_;
1758 
1759   vector<Contact> next_all_imported_contacts_;
1760   vector<size_t> imported_contacts_unique_id_;
1761   vector<size_t> imported_contacts_pos_;
1762 
1763   vector<UserId> imported_contact_user_ids_;  // result of change_imported_contacts
1764   vector<int32> unimported_contact_invites_;  // result of change_imported_contacts
1765 
1766   MultiTimeout user_online_timeout_{"UserOnlineTimeout"};
1767   MultiTimeout channel_unban_timeout_{"ChannelUnbanTimeout"};
1768   MultiTimeout user_nearby_timeout_{"UserNearbyTimeout"};
1769   MultiTimeout slow_mode_delay_timeout_{"SlowModeDelayTimeout"};
1770   MultiTimeout invite_link_info_expire_timeout_{"InviteLinkInfoExpireTimeout"};
1771   MultiTimeout channel_participant_cache_timeout_{"ChannelParticipantCacheTimeout"};
1772 };
1773 
1774 }  // namespace td
1775