From 2aeeb09b4d14cc74cefe94482b66dfaaa2660d13 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 15 Jun 2023 15:25:32 +0300 Subject: [PATCH] Add log event for story reading. --- td/telegram/StoryManager.cpp | 47 ++++++++++++++++++++++++++++++--- td/telegram/StoryManager.h | 5 +++- td/telegram/TdDb.cpp | 1 + td/telegram/logevent/LogEvent.h | 1 + 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/td/telegram/StoryManager.cpp b/td/telegram/StoryManager.cpp index a056d19b7..b0936b742 100644 --- a/td/telegram/StoryManager.cpp +++ b/td/telegram/StoryManager.cpp @@ -715,7 +715,7 @@ void StoryManager::open_story(DialogId owner_dialog_id, StoryId story_id, Promis } if (need_read_story && on_update_read_stories(owner_dialog_id, story_id)) { - read_stories_on_server(owner_dialog_id, story_id); + read_stories_on_server(owner_dialog_id, story_id, 0); } promise.set_value(Unit()); @@ -753,8 +753,36 @@ void StoryManager::on_increment_story_views(DialogId owner_dialog_id) { increment_story_views(owner_dialog_id, story_views); } -void StoryManager::read_stories_on_server(DialogId owner_dialog_id, StoryId story_id) { - td_->create_handler(Promise())->send(owner_dialog_id, story_id); +class StoryManager::ReadStoriesOnServerLogEvent { + public: + DialogId dialog_id_; + StoryId max_story_id_; + + template + void store(StorerT &storer) const { + td::store(dialog_id_, storer); + td::store(max_story_id_, storer); + } + + template + void parse(ParserT &parser) { + td::parse(dialog_id_, parser); + td::parse(max_story_id_, parser); + } +}; + +uint64 StoryManager::save_read_stories_on_server_log_event(DialogId dialog_id, StoryId max_story_id) { + ReadStoriesOnServerLogEvent log_event{dialog_id, max_story_id}; + return binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::ReadStoriesOnServer, + get_log_event_storer(log_event)); +} + +void StoryManager::read_stories_on_server(DialogId owner_dialog_id, StoryId story_id, uint64 log_event_id) { + if (log_event_id == 0 && G()->use_chat_info_database()) { + log_event_id = save_read_stories_on_server_log_event(owner_dialog_id, story_id); + } + + td_->create_handler(get_erase_log_event_promise(log_event_id))->send(owner_dialog_id, story_id); } bool StoryManager::have_story(StoryFullId story_full_id) const { @@ -1756,9 +1784,22 @@ void StoryManager::on_binlog_events(vector &&events) { break; } + td_->messages_manager_->have_dialog_info_force(dialog_id); delete_story_on_server(dialog_id, log_event.story_id_, event.id_, Auto()); break; } + case LogEvent::HandlerType::ReadStoriesOnServer: { + ReadStoriesOnServerLogEvent log_event; + log_event_parse(log_event, event.get_data()).ensure(); + + auto dialog_id = log_event.dialog_id_; + if (!td_->messages_manager_->have_dialog_info_force(dialog_id)) { + binlog_erase(G()->td_db()->get_binlog(), event.id_); + break; + } + read_stories_on_server(dialog_id, log_event.max_story_id_, event.id_); + break; + } default: LOG(FATAL) << "Unsupported log event type " << event.type_; } diff --git a/td/telegram/StoryManager.h b/td/telegram/StoryManager.h index 45482123d..b81a9f872 100644 --- a/td/telegram/StoryManager.h +++ b/td/telegram/StoryManager.h @@ -159,6 +159,7 @@ class StoryManager final : public Actor { class EditStoryQuery; class DeleteStoryOnServerLogEvent; + class ReadStoriesOnServerLogEvent; void tear_down() final; @@ -239,7 +240,9 @@ class StoryManager final : public Actor { void on_increment_story_views(DialogId owner_dialog_id); - void read_stories_on_server(DialogId owner_dialog_id, StoryId story_id); + static uint64 save_read_stories_on_server_log_event(DialogId dialog_id, StoryId max_story_id); + + void read_stories_on_server(DialogId owner_dialog_id, StoryId story_id, uint64 log_event_id); std::shared_ptr upload_media_callback_; diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index 0b0b1c494..7f5d993fd 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -127,6 +127,7 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue &binlog_p events.to_messages_manager.push_back(event.clone()); break; case LogEvent::HandlerType::DeleteStoryOnServer: + case LogEvent::HandlerType::ReadStoriesOnServer: events.to_story_manager.push_back(event.clone()); break; case LogEvent::HandlerType::UpdateScopeNotificationSettingsOnServer: diff --git a/td/telegram/logevent/LogEvent.h b/td/telegram/logevent/LogEvent.h index ad5f39e2e..4b7d5d247 100644 --- a/td/telegram/logevent/LogEvent.h +++ b/td/telegram/logevent/LogEvent.h @@ -108,6 +108,7 @@ class LogEvent { EditMessagePushNotification = 0x201, SaveAppLog = 0x300, DeleteStoryOnServer = 0x400, + ReadStoriesOnServer = 0x401, ConfigPmcMagic = 0x1f18, BinlogPmcMagic = 0x4327 };