diff --git a/td/telegram/MessagesDb.cpp b/td/telegram/MessagesDb.cpp index 25533f75..6d656390 100644 --- a/td/telegram/MessagesDb.cpp +++ b/td/telegram/MessagesDb.cpp @@ -193,6 +193,9 @@ class MessagesDbImpl : public MessagesDbSyncInterface { "message_id > ?2 ORDER BY message_id ASC LIMIT ?3")); TRY_RESULT(get_messages_desc_stmt, db_.get_statement("SELECT data, message_id FROM messages WHERE dialog_id = ?1 " "AND message_id < ?2 ORDER BY message_id DESC LIMIT ?3")); + TRY_RESULT(get_messages_from_notification_id_stmt, + db_.get_statement("SELECT data, message_id FROM messages WHERE dialog_id = ?1 " + "AND notification_id < ?2 ORDER BY notification_id DESC LIMIT ?3")); TRY_RESULT( get_messages_fts_stmt, db_.get_statement("SELECT dialog_id, data, search_id FROM messages WHERE search_id IN (SELECT rowid FROM " @@ -239,10 +242,12 @@ class MessagesDbImpl : public MessagesDbSyncInterface { get_messages_stmt_.asc_stmt_ = std::move(get_messages_asc_stmt); get_messages_stmt_.desc_stmt_ = std::move(get_messages_desc_stmt); + get_messages_from_notification_id_stmt_ = std::move(get_messages_from_notification_id_stmt); get_messages_fts_stmt_ = std::move(get_messages_fts_stmt); // LOG(ERROR) << get_message_stmt_.explain().ok(); + // LOG(ERROR) << get_messages_from_notification_id_stmt.explain().ok(); // LOG(ERROR) << get_message_by_random_id_stmt_.explain().ok(); // LOG(ERROR) << get_message_by_unique_message_id_stmt_.explain().ok(); @@ -529,6 +534,28 @@ class MessagesDbImpl : public MessagesDbSyncInterface { return get_messages_impl(get_messages_stmt_, query.dialog_id, query.from_message_id, query.offset, query.limit); } + Result> get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, + int32 limit) { + auto &stmt = get_messages_from_notification_id_stmt_; + SCOPE_EXIT { + stmt.reset(); + }; + stmt.bind_int64(1, dialog_id.get()).ensure(); + stmt.bind_int32(2, from_notification_id.get()).ensure(); + stmt.bind_int32(3, limit).ensure(); + + std::vector result; + stmt.step().ensure(); + while (stmt.has_row()) { + auto data_slice = stmt.view_blob(0); + result.emplace_back(data_slice); + auto message_id = stmt.view_int64(1); + LOG(INFO) << "Load " << MessageId(message_id) << " in " << dialog_id << " from database"; + stmt.step().ensure(); + } + return std::move(result); + } + static string prepare_query(Slice query) { auto is_word_character = [](uint32 a) { switch (get_unicode_simple_category(a)) { @@ -717,6 +744,7 @@ class MessagesDbImpl : public MessagesDbSyncInterface { SqliteStatement desc_stmt_; }; GetMessagesStmt get_messages_stmt_; + SqliteStatement get_messages_from_notification_id_stmt_; std::array get_messages_from_index_stmts_; std::array get_calls_stmts_; @@ -875,6 +903,11 @@ class MessagesDbAsync : public MessagesDbAsyncInterface { void get_messages(MessagesDbMessagesQuery query, Promise promise) override { send_closure_later(impl_, &Impl::get_messages, std::move(query), std::move(promise)); } + void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit, + Promise> promise) override { + send_closure_later(impl_, &Impl::get_messages_from_notification_id, dialog_id, from_notification_id, limit, + std::move(promise)); + } void get_calls(MessagesDbCallsQuery query, Promise promise) override { send_closure_later(impl_, &Impl::get_calls, std::move(query), std::move(promise)); } @@ -947,6 +980,11 @@ class MessagesDbAsync : public MessagesDbAsyncInterface { add_read_query(); promise.set_result(sync_db_->get_messages(std::move(query))); } + void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit, + Promise> promise) { + add_read_query(); + promise.set_result(sync_db_->get_messages_from_notification_id(dialog_id, from_notification_id, limit)); + } void get_calls(MessagesDbCallsQuery query, Promise promise) { add_read_query(); promise.set_result(sync_db_->get_calls(std::move(query))); diff --git a/td/telegram/MessagesDb.h b/td/telegram/MessagesDb.h index 2f780bd5..75879974 100644 --- a/td/telegram/MessagesDb.h +++ b/td/telegram/MessagesDb.h @@ -106,6 +106,9 @@ class MessagesDbSyncInterface { MessageId last_message_id, int32 date) = 0; virtual Result get_messages(MessagesDbMessagesQuery query) = 0; + virtual Result> get_messages_from_notification_id(DialogId dialog_id, + NotificationId from_notification_id, + int32 limit) = 0; virtual Result>, int32>> get_expiring_messages( int32 expire_from, int32 expire_till, int32 limit) = 0; @@ -148,9 +151,11 @@ class MessagesDbAsyncInterface { virtual void get_dialog_message_by_date(DialogId dialog_id, MessageId first_message_id, MessageId last_message_id, int32 date, Promise promise) = 0; - virtual void get_messages(MessagesDbMessagesQuery query, Promise) = 0; + virtual void get_messages(MessagesDbMessagesQuery query, Promise promise) = 0; + virtual void get_messages_from_notification_id(DialogId dialog_id, NotificationId from_notification_id, int32 limit, + Promise> promise) = 0; - virtual void get_calls(MessagesDbCallsQuery, Promise) = 0; + virtual void get_calls(MessagesDbCallsQuery, Promise promise) = 0; virtual void get_messages_fts(MessagesDbFtsQuery query, Promise promise) = 0; virtual void get_expiring_messages( diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index ed9fff38..f0f02e1f 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17456,12 +17456,35 @@ MessagesManager::MessageNotificationGroup MessagesManager::get_message_notificat VLOG(notifications) << "Found " << d->dialog_id << " by " << group_id; result.dialog_id = d->dialog_id; result.total_count = get_dialog_pending_notification_count(d); - // TODO load result.notifications + result.notifications = get_message_notifications_from_database( + d, NotificationId::max(), td_->notification_manager_->get_max_notification_group_size()); } return result; } +vector MessagesManager::get_message_notifications_from_database(Dialog *d, + NotificationId from_notification_id, + int32 limit) { + CHECK(d != nullptr); + auto result = G()->td_db()->get_messages_db_sync()->get_messages_from_notification_id(d->dialog_id, + from_notification_id, limit); + if (result.is_error()) { + return {}; + } + auto messages = result.move_as_ok(); + + vector res; + res.reserve(messages.size()); + for (auto &message : messages) { + auto m = on_get_message_from_database(d->dialog_id, d, std::move(message)); + if (m != nullptr && m->notification_id.is_valid()) { + res.emplace_back(m->notification_id, m->date, create_new_message_notification(m->message_id)); + } + } + return res; +} + int32 MessagesManager::get_dialog_pending_notification_count(Dialog *d) { CHECK(d != nullptr); if (is_dialog_muted(d)) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 8d22600d..0093602c 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1463,6 +1463,9 @@ class MessagesManager : public Actor { NotificationGroupId get_dialog_message_notification_group_id(Dialog *d); + vector get_message_notifications_from_database(Dialog *d, NotificationId from_notification_id, + int32 limit); + int32 get_dialog_pending_notification_count(Dialog *d); bool add_new_message_notification(Dialog *d, Message *m, bool force); diff --git a/td/telegram/NotificationId.h b/td/telegram/NotificationId.h index 3b78b2f9..968d4673 100644 --- a/td/telegram/NotificationId.h +++ b/td/telegram/NotificationId.h @@ -10,6 +10,7 @@ #include "td/utils/StringBuilder.h" #include +#include #include namespace td { @@ -18,12 +19,16 @@ class NotificationId { public: NotificationId() = default; - explicit NotificationId(int32 notification_id) : id(notification_id) { + explicit constexpr NotificationId(int32 notification_id) : id(notification_id) { } template ::value>> NotificationId(T notification_id) = delete; + static constexpr NotificationId max() { + return NotificationId(std::numeric_limits::max()); + } + bool is_valid() const { return id > 0; } diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index 87330176..b048aefe 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -131,6 +131,10 @@ NotificationManager::NotificationGroups::iterator NotificationManager::get_group return groups_.emplace(std::move(group_key), std::move(group)).first; } +int32 NotificationManager::get_max_notification_group_size() const { + return max_notification_group_size_; +} + NotificationId NotificationManager::get_max_notification_id() const { return current_notification_id_; } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 4eb7fe06..801cec08 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -40,6 +40,8 @@ class NotificationManager : public Actor { NotificationManager(Td *td, ActorShared<> parent); + int32 get_max_notification_group_size() const; + NotificationId get_max_notification_id() const; NotificationId get_next_notification_id();