diff --git a/td/telegram/DialogDb.cpp b/td/telegram/DialogDb.cpp index 301e5ff0d..7365f5b81 100644 --- a/td/telegram/DialogDb.cpp +++ b/td/telegram/DialogDb.cpp @@ -201,8 +201,8 @@ class DialogDbImpl : public DialogDbSyncInterface { get_last_notification_date(get_notification_group_stmt_, 1)); } - Result> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit) override { - // TODO use folder_id + Result get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, + int32 limit) override { SCOPE_EXIT { get_dialogs_stmt_.reset(); }; @@ -212,18 +212,18 @@ class DialogDbImpl : public DialogDbSyncInterface { get_dialogs_stmt_.bind_int64(3, dialog_id.get()).ensure(); get_dialogs_stmt_.bind_int32(4, limit).ensure(); - vector dialogs; + DialogDbGetDialogsResult result; TRY_STATUS(get_dialogs_stmt_.step()); while (get_dialogs_stmt_.has_row()) { BufferSlice data(get_dialogs_stmt_.view_blob(0)); - auto loaded_dialog_id = get_dialogs_stmt_.view_int64(1); - auto loaded_dialog_order = get_dialogs_stmt_.view_int64(2); - LOG(INFO) << "Load chat " << loaded_dialog_id << " with order " << loaded_dialog_order; - dialogs.emplace_back(std::move(data)); + result.next_dialog_id = DialogId(get_dialogs_stmt_.view_int64(1)); + result.next_order = get_dialogs_stmt_.view_int64(2); + LOG(INFO) << "Load " << result.next_dialog_id << " with order " << result.next_order; + result.dialogs.emplace_back(std::move(data)); TRY_STATUS(get_dialogs_stmt_.step()); } - return std::move(dialogs); + return std::move(result); } Result> get_notification_groups_by_last_notification_date( NotificationGroupKey notification_group_key, int32 limit) override { @@ -319,7 +319,7 @@ class DialogDbAsync : public DialogDbAsyncInterface { send_closure_later(impl_, &Impl::get_dialog, dialog_id, std::move(promise)); } void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit, - Promise> promise) override { + Promise promise) override { send_closure_later(impl_, &Impl::get_dialogs, folder_id, order, dialog_id, limit, std::move(promise)); } void close(Promise<> promise) override { @@ -357,7 +357,7 @@ class DialogDbAsync : public DialogDbAsyncInterface { promise.set_result(sync_db_->get_dialog(dialog_id)); } void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit, - Promise> promise) { + Promise promise) { add_read_query(); promise.set_result(sync_db_->get_dialogs(folder_id, order, dialog_id, limit)); } diff --git a/td/telegram/DialogDb.h b/td/telegram/DialogDb.h index 7f7d0d789..75b334017 100644 --- a/td/telegram/DialogDb.h +++ b/td/telegram/DialogDb.h @@ -25,6 +25,12 @@ namespace td { class SqliteConnectionSafe; class SqliteDb; +struct DialogDbGetDialogsResult { + vector dialogs; + int64 next_order = 0; + DialogId next_dialog_id; +}; + class DialogDbSyncInterface { public: DialogDbSyncInterface() = default; @@ -37,7 +43,8 @@ class DialogDbSyncInterface { virtual Result get_dialog(DialogId dialog_id) = 0; - virtual Result> get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit) = 0; + virtual Result get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, + int32 limit) = 0; virtual Result> get_notification_groups_by_last_notification_date( NotificationGroupKey notification_group_key, int32 limit) = 0; @@ -71,7 +78,7 @@ class DialogDbAsyncInterface { virtual void get_dialog(DialogId dialog_id, Promise promise) = 0; virtual void get_dialogs(FolderId folder_id, int64 order, DialogId dialog_id, int32 limit, - Promise> promise) = 0; + Promise promise) = 0; virtual void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key, int32 limit, diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 138d53c0c..d64f733ba 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -12114,27 +12114,26 @@ void MessagesManager::load_dialog_list_from_database(FolderId folder_id, int32 l folder_id, list.last_loaded_database_dialog_date_.get_order(), list.last_loaded_database_dialog_date_.get_dialog_id(), limit, PromiseCreator::lambda([actor_id = actor_id(this), folder_id, limit, - promise = std::move(promise)](vector result) mutable { + promise = std::move(promise)](DialogDbGetDialogsResult result) mutable { send_closure(actor_id, &MessagesManager::on_get_dialogs_from_database, folder_id, limit, std::move(result), std::move(promise)); })); } -void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 limit, vector &&dialogs, +void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 limit, DialogDbGetDialogsResult &&dialogs, Promise &&promise) { auto &list = get_dialog_list(folder_id); - LOG(INFO) << "Receive " << dialogs.size() << " from expected " << limit << " chats in " << folder_id - << " in from database"; + LOG(INFO) << "Receive " << dialogs.dialogs.size() << " from expected " << limit << " chats in " << folder_id + << " in from database with next order " << dialogs.next_order << " and next " << dialogs.next_dialog_id; int32 new_get_dialogs_limit = 0; - int32 have_more_dialogs_in_database = (limit == static_cast(dialogs.size())); + int32 have_more_dialogs_in_database = (limit == static_cast(dialogs.dialogs.size())); if (have_more_dialogs_in_database && limit < list.load_dialog_list_limit_max_) { new_get_dialogs_limit = list.load_dialog_list_limit_max_ - limit; } list.load_dialog_list_limit_max_ = 0; - DialogDate max_dialog_date = MIN_DIALOG_DATE; size_t dialogs_skipped = 0; - for (auto &dialog : dialogs) { + for (auto &dialog : dialogs.dialogs) { Dialog *d = on_load_dialog_from_database(DialogId(), std::move(dialog)); if (d == nullptr) { dialogs_skipped++; @@ -12147,13 +12146,10 @@ void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 lim continue; } - DialogDate dialog_date(d->order, d->dialog_id); - if (max_dialog_date < dialog_date) { - max_dialog_date = dialog_date; - } - LOG(INFO) << "Chat " << dialog_date << " is loaded from database"; + LOG(INFO) << "Chat " << d->dialog_id << " with order " << d->order << " is loaded from database"; } + DialogDate max_dialog_date(dialogs.next_order, dialogs.next_dialog_id); if (!have_more_dialogs_in_database) { list.last_loaded_database_dialog_date_ = MAX_DIALOG_DATE; LOG(INFO) << "Set last loaded database dialog date to " << list.last_loaded_database_dialog_date_; @@ -12168,7 +12164,7 @@ void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 lim update_last_dialog_date(folder_id); } else { LOG(ERROR) << "Last loaded database dialog date didn't increased, skipped " << dialogs_skipped << " chats out of " - << dialogs.size(); + << dialogs.dialogs.size(); } if (!(list.last_loaded_database_dialog_date_ < list.last_database_server_dialog_date_)) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 9904d9673..760cb9f52 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -14,6 +14,7 @@ #include "td/telegram/ChannelId.h" #include "td/telegram/Dependencies.h" #include "td/telegram/DialogDate.h" +#include "td/telegram/DialogDb.h" #include "td/telegram/DialogId.h" #include "td/telegram/DialogParticipant.h" #include "td/telegram/files/FileId.h" @@ -1930,7 +1931,7 @@ class MessagesManager : public Actor { Dialog *on_load_dialog_from_database(DialogId dialog_id, const BufferSlice &value); - void on_get_dialogs_from_database(FolderId folder_id, int32 limit, vector &&dialogs, + void on_get_dialogs_from_database(FolderId folder_id, int32 limit, DialogDbGetDialogsResult &&dialogs, Promise &&promise); void send_get_dialog_query(DialogId dialog_id, Promise &&promise, uint64 logevent_id = 0);