diff --git a/td/telegram/StoryDb.cpp b/td/telegram/StoryDb.cpp index 603a101b9..4040388b2 100644 --- a/td/telegram/StoryDb.cpp +++ b/td/telegram/StoryDb.cpp @@ -107,6 +107,12 @@ class StoryDbImpl final : public StoryDbSyncInterface { TRY_RESULT_ASSIGN(delete_active_stories_stmt_, db_.get_statement("DELETE FROM active_stories WHERE dialog_id = ?1")); + TRY_RESULT_ASSIGN( + get_active_story_list_stmt_, + db_.get_statement("SELECT data, dialog_id, dialog_order FROM active_stories WHERE " + "story_list_id = ?1 AND (dialog_order < ?2 OR (dialog_order = ?2 AND dialog_id < ?3)) ORDER " + "BY dialog_order DESC, dialog_id DESC LIMIT ?4")); + TRY_RESULT_ASSIGN(get_active_stories_stmt_, db_.get_statement("SELECT data FROM active_stories WHERE dialog_id = ?1")); @@ -257,6 +263,33 @@ class StoryDbImpl final : public StoryDbSyncInterface { return BufferSlice(get_active_stories_stmt_.view_blob(0)); } + StoryDbGetActiveStoryListResult get_active_story_list(StoryListId story_list_id, int64 order, DialogId dialog_id, + int32 limit) final { + SCOPE_EXIT { + get_active_story_list_stmt_.reset(); + }; + + get_active_story_list_stmt_.bind_int32(1, story_list_id == StoryListId::archive() ? 1 : 0).ensure(); + get_active_story_list_stmt_.bind_int64(2, order).ensure(); + get_active_story_list_stmt_.bind_int64(3, dialog_id.get()).ensure(); + get_active_story_list_stmt_.bind_int32(4, limit).ensure(); + + StoryDbGetActiveStoryListResult result; + result.next_dialog_id_ = dialog_id; + result.next_order_ = order; + get_active_story_list_stmt_.step().ensure(); + while (get_active_story_list_stmt_.has_row()) { + BufferSlice data(get_active_story_list_stmt_.view_blob(0)); + result.next_dialog_id_ = DialogId(get_active_story_list_stmt_.view_int64(1)); + result.next_order_ = get_active_story_list_stmt_.view_int64(2); + LOG(INFO) << "Load active stories in " << result.next_dialog_id_ << " with order " << result.next_order_; + result.active_stories_.emplace_back(result.next_dialog_id_, std::move(data)); + get_active_story_list_stmt_.step().ensure(); + } + + return result; + } + void add_active_story_list_state(StoryListId story_list_id, BufferSlice data) final { SCOPE_EXIT { add_active_story_list_state_stmt_.reset(); @@ -298,6 +331,7 @@ class StoryDbImpl final : public StoryDbSyncInterface { SqliteStatement add_active_stories_stmt_; SqliteStatement delete_active_stories_stmt_; SqliteStatement get_active_stories_stmt_; + SqliteStatement get_active_story_list_stmt_; SqliteStatement add_active_story_list_state_stmt_; SqliteStatement get_active_story_list_state_stmt_; @@ -366,6 +400,11 @@ class StoryDbAsync final : public StoryDbAsyncInterface { send_closure_later(impl_, &Impl::get_active_stories, dialog_id, std::move(promise)); } + void get_active_story_list(StoryListId story_list_id, int64 order, DialogId dialog_id, int32 limit, + Promise promise) final { + send_closure_later(impl_, &Impl::get_active_story_list, story_list_id, order, dialog_id, limit, std::move(promise)); + } + void add_active_story_list_state(StoryListId story_list_id, BufferSlice data, Promise promise) final { send_closure_later(impl_, &Impl::add_active_story_list_state, story_list_id, std::move(data), std::move(promise)); } @@ -445,6 +484,12 @@ class StoryDbAsync final : public StoryDbAsyncInterface { promise.set_result(sync_db_->get_active_stories(dialog_id)); } + void get_active_story_list(StoryListId story_list_id, int64 order, DialogId dialog_id, int32 limit, + Promise promise) { + add_read_query(); + promise.set_value(sync_db_->get_active_story_list(story_list_id, order, dialog_id, limit)); + } + void add_active_story_list_state(StoryListId story_list_id, BufferSlice data, Promise promise) { add_write_query([this, story_list_id, data = std::move(data), promise = std::move(promise)](Unit) mutable { sync_db_->add_active_story_list_state(story_list_id, std::move(data)); diff --git a/td/telegram/StoryDb.h b/td/telegram/StoryDb.h index c15c2f4f6..43275f2ef 100644 --- a/td/telegram/StoryDb.h +++ b/td/telegram/StoryDb.h @@ -17,6 +17,7 @@ #include "td/utils/Status.h" #include +#include namespace td { @@ -31,6 +32,12 @@ struct StoryDbStory { } }; +struct StoryDbGetActiveStoryListResult { + vector> active_stories_; + int64 next_order_ = 0; + DialogId next_dialog_id_; +}; + class StoryDbSyncInterface { public: StoryDbSyncInterface() = default; @@ -57,6 +64,9 @@ class StoryDbSyncInterface { virtual Result get_active_stories(DialogId dialog_id) = 0; + virtual StoryDbGetActiveStoryListResult get_active_story_list(StoryListId story_list_id, int64 order, + DialogId dialog_id, int32 limit) = 0; + virtual void add_active_story_list_state(StoryListId story_list_id, BufferSlice data) = 0; virtual Result get_active_story_list_state(StoryListId story_list_id) = 0; @@ -101,6 +111,9 @@ class StoryDbAsyncInterface { virtual void get_active_stories(DialogId dialog_id, Promise promise) = 0; + virtual void get_active_story_list(StoryListId story_list_id, int64 order, DialogId dialog_id, int32 limit, + Promise promise) = 0; + virtual void add_active_story_list_state(StoryListId story_list_id, BufferSlice data, Promise promise) = 0; virtual void get_active_story_list_state(StoryListId story_list_id, Promise promise) = 0;