aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/AutosaveManager.cpp
blob: d2029e2e1799346ca60577b189850f995e99a0c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/AutosaveManager.h"

#include "td/telegram/AccessRights.h"
#include "td/telegram/ChatManager.h"
#include "td/telegram/Dependencies.h"
#include "td/telegram/DialogManager.h"
#include "td/telegram/Global.h"
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UserManager.h"

#include "td/db/SqliteKeyValueAsync.h"

#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/tl_helpers.h"

namespace td {

class GetAutoSaveSettingsQuery final : public Td::ResultHandler {
  Promise<telegram_api::object_ptr<telegram_api::account_autoSaveSettings>> promise_;

 public:
  explicit GetAutoSaveSettingsQuery(Promise<telegram_api::object_ptr<telegram_api::account_autoSaveSettings>> &&promise)
      : promise_(std::move(promise)) {
  }

  void send() {
    send_query(G()->net_query_creator().create(telegram_api::account_getAutoSaveSettings(), {{"me"}}));
  }

  void on_result(BufferSlice packet) final {
    auto result_ptr = fetch_result<telegram_api::account_getAutoSaveSettings>(packet);
    if (result_ptr.is_error()) {
      return on_error(result_ptr.move_as_error());
    }

    auto ptr = result_ptr.move_as_ok();
    LOG(INFO) << "Receive result for GetAutoSaveSettingsQuery: " << to_string(ptr);
    promise_.set_value(std::move(ptr));
  }

  void on_error(Status status) final {
    promise_.set_error(std::move(status));
  }
};

class SaveAutoSaveSettingsQuery final : public Td::ResultHandler {
  Promise<Unit> promise_;

 public:
  explicit SaveAutoSaveSettingsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
  }

  void send(bool users, bool chats, bool broadcasts, DialogId dialog_id,
            telegram_api::object_ptr<telegram_api::autoSaveSettings> settings) {
    int32 flags = 0;
    telegram_api::object_ptr<telegram_api::InputPeer> input_peer;
    if (!users && !chats && !broadcasts) {
      flags |= telegram_api::account_saveAutoSaveSettings::PEER_MASK;
      input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
      CHECK(input_peer != nullptr);
    }
    send_query(G()->net_query_creator().create(
        telegram_api::account_saveAutoSaveSettings(flags, users, chats, broadcasts, std::move(input_peer),
                                                   std::move(settings)),
        {{"me"}}));
  }

  void on_result(BufferSlice packet) final {
    auto result_ptr = fetch_result<telegram_api::account_saveAutoSaveSettings>(packet);
    if (result_ptr.is_error()) {
      return on_error(result_ptr.move_as_error());
    }

    promise_.set_value(Unit());
  }

  void on_error(Status status) final {
    promise_.set_error(std::move(status));
    td_->autosave_manager_->reload_autosave_settings();
  }
};

class DeleteAutoSaveExceptionsQuery final : public Td::ResultHandler {
  Promise<Unit> promise_;

 public:
  explicit DeleteAutoSaveExceptionsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
  }

  void send() {
    send_query(G()->net_query_creator().create(telegram_api::account_deleteAutoSaveExceptions(), {{"me"}}));
  }

  void on_result(BufferSlice packet) final {
    auto result_ptr = fetch_result<telegram_api::account_deleteAutoSaveExceptions>(packet);
    if (result_ptr.is_error()) {
      return on_error(result_ptr.move_as_error());
    }

    promise_.set_value(Unit());
  }

  void on_error(Status status) final {
    promise_.set_error(std::move(status));
    td_->autosave_manager_->reload_autosave_settings();
  }
};

AutosaveManager::AutosaveManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}

void AutosaveManager::tear_down() {
  parent_.reset();
}

AutosaveManager::DialogAutosaveSettings::DialogAutosaveSettings(const telegram_api::autoSaveSettings *settings) {
  CHECK(settings != nullptr);
  are_inited_ = true;
  autosave_photos_ = settings->photos_;
  autosave_videos_ = settings->videos_;
  max_video_file_size_ = clamp(settings->video_max_size_, MIN_MAX_VIDEO_FILE_SIZE, MAX_MAX_VIDEO_FILE_SIZE);
}

AutosaveManager::DialogAutosaveSettings::DialogAutosaveSettings(const td_api::scopeAutosaveSettings *settings) {
  if (settings == nullptr) {
    return;
  }
  are_inited_ = true;
  autosave_photos_ = settings->autosave_photos_;
  autosave_videos_ = settings->autosave_videos_;
  max_video_file_size_ = clamp(settings->max_video_file_size_, MIN_MAX_VIDEO_FILE_SIZE, MAX_MAX_VIDEO_FILE_SIZE);
}

telegram_api::object_ptr<telegram_api::autoSaveSettings>
AutosaveManager::DialogAutosaveSettings::get_input_auto_save_settings() const {
  int32 flags = 0;
  if (are_inited_) {
    flags |= telegram_api::autoSaveSettings::VIDEO_MAX_SIZE_MASK;
  }
  return telegram_api::make_object<telegram_api::autoSaveSettings>(flags, autosave_photos_, autosave_videos_,
                                                                   max_video_file_size_);
}

td_api::object_ptr<td_api::scopeAutosaveSettings>
AutosaveManager::DialogAutosaveSettings::get_scope_autosave_settings_object() const {
  if (!are_inited_) {
    return nullptr;
  }
  return td_api::make_object<td_api::scopeAutosaveSettings>(autosave_photos_, autosave_videos_, max_video_file_size_);
}

td_api::object_ptr<td_api::autosaveSettingsException>
AutosaveManager::DialogAutosaveSettings::get_autosave_settings_exception_object(const Td *td,
                                                                                DialogId dialog_id) const {
  return td_api::make_object<td_api::autosaveSettingsException>(
      td->dialog_manager_->get_chat_id_object(dialog_id, "autosaveSettingsException"),
      get_scope_autosave_settings_object());
}

bool AutosaveManager::DialogAutosaveSettings::operator==(const DialogAutosaveSettings &other) const {
  return are_inited_ == other.are_inited_ && autosave_photos_ == other.autosave_photos_ &&
         autosave_videos_ == other.autosave_videos_ && max_video_file_size_ == other.max_video_file_size_;
}

bool AutosaveManager::DialogAutosaveSettings::operator!=(const DialogAutosaveSettings &other) const {
  return !operator==(other);
}

template <class StorerT>
void AutosaveManager::DialogAutosaveSettings::store(StorerT &storer) const {
  CHECK(are_inited_);
  BEGIN_STORE_FLAGS();
  STORE_FLAG(autosave_photos_);
  STORE_FLAG(autosave_videos_);
  END_STORE_FLAGS();
  td::store(max_video_file_size_, storer);
}

template <class ParserT>
void AutosaveManager::DialogAutosaveSettings::parse(ParserT &parser) {
  are_inited_ = true;
  BEGIN_PARSE_FLAGS();
  PARSE_FLAG(autosave_photos_);
  PARSE_FLAG(autosave_videos_);
  END_PARSE_FLAGS();
  td::parse(max_video_file_size_, parser);
}

td_api::object_ptr<td_api::autosaveSettings> AutosaveManager::AutosaveSettings::get_autosave_settings_object(
    const Td *td) const {
  CHECK(are_inited_);
  auto exceptions = transform(exceptions_, [td](const auto &exception) {
    return exception.second.get_autosave_settings_exception_object(td, exception.first);
  });
  return td_api::make_object<td_api::autosaveSettings>(
      user_settings_.get_scope_autosave_settings_object(), chat_settings_.get_scope_autosave_settings_object(),
      broadcast_settings_.get_scope_autosave_settings_object(), std::move(exceptions));
}

template <class StorerT>
void AutosaveManager::AutosaveSettings::store(StorerT &storer) const {
  CHECK(are_inited_);
  bool has_exceptions = !exceptions_.empty();
  BEGIN_STORE_FLAGS();
  STORE_FLAG(has_exceptions);
  END_STORE_FLAGS();
  td::store(user_settings_, storer);
  td::store(chat_settings_, storer);
  td::store(broadcast_settings_, storer);
  if (has_exceptions) {
    td::store(narrow_cast<uint32>(exceptions_.size()), storer);
    for (auto &exception : exceptions_) {
      td::store(exception.first, storer);
      td::store(exception.second, storer);
    }
  }
}

template <class ParserT>
void AutosaveManager::AutosaveSettings::parse(ParserT &parser) {
  are_inited_ = true;
  bool has_exceptions;
  BEGIN_PARSE_FLAGS();
  PARSE_FLAG(has_exceptions);
  END_PARSE_FLAGS();
  td::parse(user_settings_, parser);
  td::parse(chat_settings_, parser);
  td::parse(broadcast_settings_, parser);
  if (has_exceptions) {
    uint32 size;
    td::parse(size, parser);
    for (size_t i = 0; i < size; i++) {
      DialogId dialog_id;
      DialogAutosaveSettings settings;
      td::parse(dialog_id, parser);
      td::parse(settings, parser);
      if (dialog_id.is_valid()) {
        exceptions_.emplace(dialog_id, std::move(settings));
      }
    }
  }
}

void AutosaveManager::get_autosave_settings(Promise<td_api::object_ptr<td_api::autosaveSettings>> &&promise) {
  if (settings_.are_inited_) {
    return promise.set_value(settings_.get_autosave_settings_object(td_));
  }

  load_autosave_settings(std::move(promise));
}

string AutosaveManager::get_autosave_settings_database_key() {
  return "autosave_settings";
}

void AutosaveManager::load_autosave_settings(Promise<td_api::object_ptr<td_api::autosaveSettings>> &&promise) {
  load_settings_queries_.push_back(std::move(promise));
  if (load_settings_queries_.size() != 1) {
    return;
  }

  if (G()->use_message_database()) {
    G()->td_db()->get_sqlite_pmc()->get(
        get_autosave_settings_database_key(), PromiseCreator::lambda([actor_id = actor_id(this)](string value) mutable {
          send_closure(actor_id, &AutosaveManager::on_load_autosave_settings_from_database, std::move(value));
        }));
    return;
  }

  reload_autosave_settings();
}

void AutosaveManager::on_load_autosave_settings_from_database(string value) {
  if (G()->close_flag()) {
    return fail_promises(load_settings_queries_, Global::request_aborted_error());
  }
  if (settings_.are_inited_) {
    CHECK(load_settings_queries_.empty());
    return;
  }
  if (value.empty()) {
    LOG(INFO) << "Autosave settings aren't found in database";
    return reload_autosave_settings();
  }

  LOG(INFO) << "Successfully loaded autosave settings from database";

  auto status = log_event_parse(settings_, value);
  if (status.is_error()) {
    LOG(ERROR) << "Can't load autosave settings: " << status;
    settings_ = {};
    return reload_autosave_settings();
  }

  Dependencies dependencies;
  for (auto &exception : settings_.exceptions_) {
    dependencies.add_dialog_and_dependencies(exception.first);
  }
  if (!dependencies.resolve_force(td_, "on_load_autosave_settings_from_database")) {
    G()->td_db()->get_binlog_pmc()->erase(get_autosave_settings_database_key());
    settings_ = {};
    return reload_autosave_settings();
  }

  settings_.are_inited_ = true;
  send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopePrivateChats>(),
                                settings_.user_settings_);
  send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeGroupChats>(),
                                settings_.chat_settings_);
  send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeChannelChats>(),
                                settings_.broadcast_settings_);
  for (auto &exception : settings_.exceptions_) {
    send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeChat>(exception.first.get()),
                                  exception.second);
  }

  auto promises = std::move(load_settings_queries_);
  for (auto &promise : promises) {
    promise.set_value(settings_.get_autosave_settings_object(td_));
  }
}

void AutosaveManager::reload_autosave_settings() {
  if (G()->close_flag()) {
    return fail_promises(load_settings_queries_, Global::request_aborted_error());
  }
  if (settings_.are_being_reloaded_) {
    settings_.need_reload_ = true;
    return;
  }
  settings_.are_being_reloaded_ = true;

  auto query_promise = PromiseCreator::lambda(
      [actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::account_autoSaveSettings>> r_settings) {
        send_closure(actor_id, &AutosaveManager::on_get_autosave_settings, std::move(r_settings));
      });
  td_->create_handler<GetAutoSaveSettingsQuery>(std::move(query_promise))->send();
}

void AutosaveManager::on_get_autosave_settings(
    Result<telegram_api::object_ptr<telegram_api::account_autoSaveSettings>> r_settings) {
  G()->ignore_result_if_closing(r_settings);

  CHECK(settings_.are_being_reloaded_);
  settings_.are_being_reloaded_ = false;
  SCOPE_EXIT {
    if (settings_.need_reload_) {
      settings_.need_reload_ = false;
      reload_autosave_settings();
    }
  };
  if (r_settings.is_error()) {
    return fail_promises(load_settings_queries_, r_settings.move_as_error());
  }

  auto settings = r_settings.move_as_ok();
  td_->user_manager_->on_get_users(std::move(settings->users_), "on_get_autosave_settings");
  td_->chat_manager_->on_get_chats(std::move(settings->chats_), "on_get_autosave_settings");

  DialogAutosaveSettings new_user_settings(settings->users_settings_.get());
  DialogAutosaveSettings new_chat_settings(settings->chats_settings_.get());
  DialogAutosaveSettings new_broadcast_settings(settings->broadcasts_settings_.get());

  settings_.are_inited_ = true;
  if (settings_.user_settings_ != new_user_settings) {
    settings_.user_settings_ = std::move(new_user_settings);
    send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopePrivateChats>(),
                                  settings_.user_settings_);
  }
  if (settings_.chat_settings_ != new_chat_settings) {
    settings_.chat_settings_ = std::move(new_chat_settings);
    send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeGroupChats>(),
                                  settings_.chat_settings_);
  }
  if (settings_.broadcast_settings_ != new_broadcast_settings) {
    settings_.broadcast_settings_ = std::move(new_broadcast_settings);
    send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeChannelChats>(),
                                  settings_.broadcast_settings_);
  }
  FlatHashSet<DialogId, DialogIdHash> exception_dialog_ids;
  for (auto &exception : settings_.exceptions_) {
    exception_dialog_ids.insert(exception.first);
  }
  for (auto &exception : settings->exceptions_) {
    DialogId dialog_id(exception->peer_);
    if (!dialog_id.is_valid()) {
      continue;
    }
    td_->dialog_manager_->force_create_dialog(dialog_id, "on_get_autosave_settings");
    DialogAutosaveSettings new_settings(exception->settings_.get());
    auto &current_settings = settings_.exceptions_[dialog_id];
    if (current_settings != new_settings) {
      current_settings = std::move(new_settings);
      send_update_autosave_settings(
          td_api::make_object<td_api::autosaveSettingsScopeChat>(
              td_->dialog_manager_->get_chat_id_object(dialog_id, "autosaveSettingsScopeChat")),
          current_settings);
    }
    exception_dialog_ids.erase(dialog_id);
  }
  for (auto dialog_id : exception_dialog_ids) {
    settings_.exceptions_.erase(dialog_id);
    send_update_autosave_settings(
        td_api::make_object<td_api::autosaveSettingsScopeChat>(
            td_->dialog_manager_->get_chat_id_object(dialog_id, "autosaveSettingsScopeChat 2")),
        DialogAutosaveSettings());
  }

  save_autosave_settings();

  auto promises = std::move(load_settings_queries_);
  for (auto &promise : promises) {
    promise.set_value(settings_.get_autosave_settings_object(td_));
  }
}

void AutosaveManager::save_autosave_settings() {
  if (G()->use_message_database()) {
    LOG(INFO) << "Save autosave settings to database";
    G()->td_db()->get_sqlite_pmc()->set(get_autosave_settings_database_key(),
                                        log_event_store(settings_).as_slice().str(), Auto());
  }
}

void AutosaveManager::set_autosave_settings(td_api::object_ptr<td_api::AutosaveSettingsScope> &&scope,
                                            td_api::object_ptr<td_api::scopeAutosaveSettings> &&settings,
                                            Promise<Unit> &&promise) {
  if (scope == nullptr) {
    return promise.set_error(400, "Scope must be non-empty");
  }
  if (!settings_.are_inited_) {
    return promise.set_error(400, "Autosave settings must be loaded first");
  }
  auto new_settings = DialogAutosaveSettings(settings.get());
  DialogAutosaveSettings *old_settings = nullptr;
  bool users = false;
  bool chats = false;
  bool broadcasts = false;
  DialogId dialog_id;
  switch (scope->get_id()) {
    case td_api::autosaveSettingsScopePrivateChats::ID:
      users = true;
      old_settings = &settings_.user_settings_;
      break;
    case td_api::autosaveSettingsScopeGroupChats::ID:
      chats = true;
      old_settings = &settings_.chat_settings_;
      break;
    case td_api::autosaveSettingsScopeChannelChats::ID:
      broadcasts = true;
      old_settings = &settings_.broadcast_settings_;
      break;
    case td_api::autosaveSettingsScopeChat::ID:
      dialog_id = DialogId(static_cast<const td_api::autosaveSettingsScopeChat *>(scope.get())->chat_id_);
      TRY_STATUS_PROMISE(promise, td_->dialog_manager_->check_dialog_access(dialog_id, false, AccessRights::Read,
                                                                            "set_autosave_settings"));
      old_settings = &settings_.exceptions_[dialog_id];
      break;
    default:
      UNREACHABLE();
  }
  if (!dialog_id.is_valid() && !new_settings.are_inited_) {
    new_settings.are_inited_ = true;
    new_settings.max_video_file_size_ = DialogAutosaveSettings::DEFAULT_MAX_VIDEO_FILE_SIZE;
  }
  if (*old_settings == new_settings) {
    return promise.set_value(Unit());
  }
  if (new_settings.are_inited_) {
    *old_settings = std::move(new_settings);
    send_update_autosave_settings(std::move(scope), *old_settings);
  } else {
    CHECK(dialog_id.is_valid());
    settings_.exceptions_.erase(dialog_id);
    send_update_autosave_settings(std::move(scope), DialogAutosaveSettings());
  }

  save_autosave_settings();

  td_->create_handler<SaveAutoSaveSettingsQuery>(std::move(promise))
      ->send(users, chats, broadcasts, dialog_id, new_settings.get_input_auto_save_settings());
}

void AutosaveManager::clear_autosave_settings_exceptions(Promise<Unit> &&promise) {
  if (!settings_.are_inited_) {
    return promise.set_error(400, "Autosave settings must be loaded first");
  }
  for (const auto &exception : settings_.exceptions_) {
    send_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeChat>(exception.first.get()),
                                  DialogAutosaveSettings());
  }
  settings_.exceptions_.clear();
  save_autosave_settings();
  td_->create_handler<DeleteAutoSaveExceptionsQuery>(std::move(promise))->send();
}

td_api::object_ptr<td_api::updateAutosaveSettings> AutosaveManager::get_update_autosave_settings(
    td_api::object_ptr<td_api::AutosaveSettingsScope> &&scope, const DialogAutosaveSettings &settings) {
  return td_api::make_object<td_api::updateAutosaveSettings>(std::move(scope),
                                                             settings.get_scope_autosave_settings_object());
}

void AutosaveManager::send_update_autosave_settings(td_api::object_ptr<td_api::AutosaveSettingsScope> &&scope,
                                                    const DialogAutosaveSettings &settings) {
  send_closure(G()->td(), &Td::send_update, get_update_autosave_settings(std::move(scope), settings));
}

void AutosaveManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
  if (!settings_.are_inited_) {
    return;
  }
  updates.push_back(get_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopePrivateChats>(),
                                                 settings_.user_settings_));
  updates.push_back(get_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeGroupChats>(),
                                                 settings_.chat_settings_));
  updates.push_back(get_update_autosave_settings(td_api::make_object<td_api::autosaveSettingsScopeChannelChats>(),
                                                 settings_.broadcast_settings_));
  for (const auto &exception : settings_.exceptions_) {
    updates.push_back(get_update_autosave_settings(
        td_api::make_object<td_api::autosaveSettingsScopeChat>(exception.first.get()), exception.second));
  }
}

}  // namespace td