2023-05-01 21:19:16 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2023-05-08 19:09:01 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
2023-05-19 16:00:33 +02:00
|
|
|
#include "td/telegram/files/FileId.h"
|
2023-05-20 00:23:33 +02:00
|
|
|
#include "td/telegram/files/FileSourceId.h"
|
2023-06-13 17:20:26 +02:00
|
|
|
#include "td/telegram/FullMessageId.h"
|
2023-05-18 16:20:22 +02:00
|
|
|
#include "td/telegram/MessageEntity.h"
|
2023-05-18 18:57:50 +02:00
|
|
|
#include "td/telegram/StoryFullId.h"
|
2023-05-08 19:09:01 +02:00
|
|
|
#include "td/telegram/StoryId.h"
|
2023-05-18 17:41:07 +02:00
|
|
|
#include "td/telegram/StoryInteractionInfo.h"
|
2023-05-19 12:41:15 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
2023-05-08 19:09:01 +02:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
#include "td/telegram/UserPrivacySettingRule.h"
|
|
|
|
|
2023-05-01 21:19:16 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2023-05-19 22:34:08 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2023-05-08 19:09:01 +02:00
|
|
|
#include "td/utils/WaitFreeHashMap.h"
|
2023-06-02 15:46:10 +02:00
|
|
|
#include "td/utils/WaitFreeHashSet.h"
|
2023-05-01 21:19:16 +02:00
|
|
|
|
2023-05-19 22:25:52 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2023-05-01 21:19:16 +02:00
|
|
|
namespace td {
|
|
|
|
|
2023-06-01 16:41:46 +02:00
|
|
|
struct BinlogEvent;
|
2023-05-18 16:20:22 +02:00
|
|
|
class StoryContent;
|
2023-05-01 21:19:16 +02:00
|
|
|
class Td;
|
|
|
|
|
|
|
|
class StoryManager final : public Actor {
|
2023-05-25 14:46:04 +02:00
|
|
|
struct Story {
|
|
|
|
int32 date_ = 0;
|
|
|
|
int32 expire_date_ = 0;
|
|
|
|
bool is_pinned_ = false;
|
|
|
|
bool is_public_ = false;
|
|
|
|
bool is_for_close_friends_ = false;
|
2023-06-01 12:55:11 +02:00
|
|
|
mutable bool is_update_sent_ = false; // whether the story is known to the app
|
2023-05-25 14:46:04 +02:00
|
|
|
StoryInteractionInfo interaction_info_;
|
|
|
|
UserPrivacySettingRules privacy_rules_;
|
|
|
|
unique_ptr<StoryContent> content_;
|
|
|
|
FormattedText caption_;
|
2023-05-30 13:41:36 +02:00
|
|
|
mutable int64 edit_generation_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BeingEditedStory {
|
|
|
|
unique_ptr<StoryContent> content_;
|
|
|
|
FormattedText caption_;
|
|
|
|
bool edit_caption_ = false;
|
|
|
|
vector<Promise<Unit>> promises_;
|
2023-05-25 14:46:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PendingStory {
|
|
|
|
DialogId dialog_id_;
|
|
|
|
StoryId story_id_;
|
|
|
|
uint64 log_event_id_ = 0;
|
|
|
|
uint32 send_story_num_ = 0;
|
|
|
|
int64 random_id_ = 0;
|
2023-05-25 20:21:10 +02:00
|
|
|
bool was_reuploaded_ = false;
|
2023-05-25 14:46:04 +02:00
|
|
|
unique_ptr<Story> story_;
|
|
|
|
|
|
|
|
PendingStory(DialogId dialog_id, StoryId story_id, uint64 log_event_id, uint32 send_story_num, int64 random_id,
|
|
|
|
unique_ptr<Story> &&story);
|
|
|
|
};
|
|
|
|
|
2023-06-14 15:06:48 +02:00
|
|
|
struct PendingStoryViews {
|
|
|
|
FlatHashSet<StoryId, StoryIdHash> story_ids_;
|
|
|
|
bool has_query_ = false;
|
|
|
|
};
|
|
|
|
|
2023-06-14 18:27:08 +02:00
|
|
|
struct ActiveStories {
|
|
|
|
StoryId max_read_story_id_;
|
|
|
|
vector<StoryId> story_ids_;
|
|
|
|
};
|
|
|
|
|
2023-05-01 21:19:16 +02:00
|
|
|
public:
|
|
|
|
StoryManager(Td *td, ActorShared<> parent);
|
2023-05-18 16:20:22 +02:00
|
|
|
StoryManager(const StoryManager &) = delete;
|
|
|
|
StoryManager &operator=(const StoryManager &) = delete;
|
|
|
|
StoryManager(StoryManager &&) = delete;
|
|
|
|
StoryManager &operator=(StoryManager &&) = delete;
|
|
|
|
~StoryManager() final;
|
2023-05-01 21:19:16 +02:00
|
|
|
|
2023-06-05 15:55:06 +02:00
|
|
|
void get_story(DialogId owner_dialog_id, StoryId story_id, Promise<td_api::object_ptr<td_api::story>> &&promise);
|
|
|
|
|
2023-05-25 14:03:29 +02:00
|
|
|
void send_story(td_api::object_ptr<td_api::InputStoryContent> &&input_story_content,
|
|
|
|
td_api::object_ptr<td_api::formattedText> &&input_caption,
|
2023-06-09 15:41:40 +02:00
|
|
|
td_api::object_ptr<td_api::userPrivacySettingRules> &&rules, int32 active_period, bool is_pinned,
|
2023-05-25 14:03:29 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::story>> &&promise);
|
|
|
|
|
2023-05-25 14:46:04 +02:00
|
|
|
void on_send_story_file_part_missing(unique_ptr<PendingStory> &&pending_story, int bad_part);
|
|
|
|
|
2023-05-30 13:41:36 +02:00
|
|
|
void edit_story(StoryId story_id, td_api::object_ptr<td_api::InputStoryContent> &&input_story_content,
|
|
|
|
td_api::object_ptr<td_api::formattedText> &&input_caption, Promise<Unit> &&promise);
|
|
|
|
|
2023-05-25 17:18:28 +02:00
|
|
|
void set_story_privacy_rules(StoryId story_id, td_api::object_ptr<td_api::userPrivacySettingRules> &&rules,
|
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
2023-05-26 13:21:36 +02:00
|
|
|
void toggle_story_is_pinned(StoryId story_id, bool is_pinned, Promise<Unit> &&promise);
|
|
|
|
|
2023-06-01 16:28:49 +02:00
|
|
|
void delete_story(StoryId story_id, Promise<Unit> &&promise);
|
|
|
|
|
2023-06-05 15:07:44 +02:00
|
|
|
void toggle_dialog_stories_hidden(DialogId dialog_id, bool are_hidden, Promise<Unit> &&promise);
|
|
|
|
|
2023-05-20 22:24:36 +02:00
|
|
|
void get_dialog_pinned_stories(DialogId owner_dialog_id, StoryId from_story_id, int32 limit,
|
|
|
|
Promise<td_api::object_ptr<td_api::stories>> &&promise);
|
|
|
|
|
2023-06-09 16:21:34 +02:00
|
|
|
void get_story_archive(StoryId from_story_id, int32 limit, Promise<td_api::object_ptr<td_api::stories>> &&promise);
|
|
|
|
|
2023-06-14 18:27:08 +02:00
|
|
|
void get_dialog_expiring_stories(DialogId owner_dialog_id,
|
|
|
|
Promise<td_api::object_ptr<td_api::activeStories>> &&promise);
|
2023-05-21 11:39:53 +02:00
|
|
|
|
2023-06-14 14:17:24 +02:00
|
|
|
void open_story(DialogId owner_dialog_id, StoryId story_id, Promise<Unit> &&promise);
|
|
|
|
|
2023-06-12 18:23:58 +02:00
|
|
|
StoryId on_get_story(DialogId owner_dialog_id, telegram_api::object_ptr<telegram_api::StoryItem> &&story_item_ptr);
|
2023-05-08 19:09:01 +02:00
|
|
|
|
2023-06-06 15:11:12 +02:00
|
|
|
std::pair<int32, vector<StoryId>> on_get_stories(DialogId owner_dialog_id, vector<int32> &&expected_story_ids,
|
2023-05-19 22:25:52 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::stories_stories> &&stories);
|
|
|
|
|
2023-06-06 14:58:27 +02:00
|
|
|
bool have_story(StoryFullId story_full_id) const;
|
|
|
|
|
2023-06-13 14:45:10 +02:00
|
|
|
bool have_story_force(StoryFullId story_full_id) const;
|
|
|
|
|
2023-06-06 15:11:12 +02:00
|
|
|
bool is_inaccessible_story(StoryFullId story_full_id) const;
|
|
|
|
|
2023-06-06 15:07:29 +02:00
|
|
|
int32 get_story_duration(StoryFullId story_full_id) const;
|
|
|
|
|
2023-06-13 17:20:26 +02:00
|
|
|
void register_story(StoryFullId story_full_id, FullMessageId full_message_id, const char *source);
|
|
|
|
|
|
|
|
void unregister_story(StoryFullId story_full_id, FullMessageId full_message_id, const char *source);
|
|
|
|
|
2023-05-19 12:41:15 +02:00
|
|
|
td_api::object_ptr<td_api::story> get_story_object(StoryFullId story_full_id) const;
|
|
|
|
|
2023-05-20 22:24:36 +02:00
|
|
|
td_api::object_ptr<td_api::stories> get_stories_object(int32 total_count,
|
|
|
|
const vector<StoryFullId> &story_full_ids) const;
|
|
|
|
|
2023-05-20 00:23:33 +02:00
|
|
|
FileSourceId get_story_file_source_id(StoryFullId story_full_id);
|
|
|
|
|
2023-06-12 18:23:58 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputMedia> get_input_media(StoryFullId story_full_id) const;
|
|
|
|
|
2023-05-19 22:34:08 +02:00
|
|
|
void reload_story(StoryFullId story_full_id, Promise<Unit> &&promise);
|
|
|
|
|
2023-06-01 16:41:46 +02:00
|
|
|
void on_binlog_events(vector<BinlogEvent> &&events);
|
|
|
|
|
2023-05-01 21:19:16 +02:00
|
|
|
private:
|
2023-05-25 14:03:29 +02:00
|
|
|
class UploadMediaCallback;
|
|
|
|
|
|
|
|
class SendStoryQuery;
|
2023-05-30 13:41:36 +02:00
|
|
|
class EditStoryQuery;
|
2023-05-25 14:03:29 +02:00
|
|
|
|
2023-06-01 16:41:46 +02:00
|
|
|
class DeleteStoryOnServerLogEvent;
|
|
|
|
|
2023-05-01 21:19:16 +02:00
|
|
|
void tear_down() final;
|
|
|
|
|
2023-05-19 12:41:15 +02:00
|
|
|
bool is_story_owned(DialogId owner_dialog_id) const;
|
|
|
|
|
2023-06-15 11:00:19 +02:00
|
|
|
bool is_active_story(StoryFullId story_full_id) const;
|
|
|
|
|
|
|
|
bool is_active_story(const Story *story) const;
|
|
|
|
|
2023-05-18 18:57:50 +02:00
|
|
|
const Story *get_story(StoryFullId story_full_id) const;
|
2023-05-08 19:09:01 +02:00
|
|
|
|
2023-05-18 18:57:50 +02:00
|
|
|
Story *get_story_editable(StoryFullId story_full_id);
|
2023-05-08 19:09:01 +02:00
|
|
|
|
2023-05-26 13:21:36 +02:00
|
|
|
void on_story_changed(StoryFullId story_full_id, const Story *story, bool is_changed, bool need_save_to_database);
|
|
|
|
|
2023-06-14 18:27:08 +02:00
|
|
|
td_api::object_ptr<td_api::storyInfo> get_story_info_object(StoryFullId story_full_id) const;
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::storyInfo> get_story_info_object(StoryFullId story_full_id, const Story *story) const;
|
|
|
|
|
2023-05-19 12:41:15 +02:00
|
|
|
td_api::object_ptr<td_api::story> get_story_object(StoryFullId story_full_id, const Story *story) const;
|
2023-06-01 15:27:00 +02:00
|
|
|
|
2023-06-15 11:42:42 +02:00
|
|
|
td_api::object_ptr<td_api::activeStories> get_active_stories_object(DialogId owner_dialog_id) const;
|
2023-06-14 18:27:08 +02:00
|
|
|
|
2023-06-01 15:27:00 +02:00
|
|
|
StoryId on_get_story(DialogId owner_dialog_id, telegram_api::object_ptr<telegram_api::storyItem> &&story_item);
|
|
|
|
|
2023-06-14 19:10:57 +02:00
|
|
|
StoryId on_get_skipped_story(DialogId owner_dialog_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::storyItemSkipped> &&story_item);
|
|
|
|
|
2023-06-14 19:17:40 +02:00
|
|
|
StoryId on_get_deleted_story(DialogId owner_dialog_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::storyItemDeleted> &&story_item);
|
|
|
|
|
2023-06-01 15:27:00 +02:00
|
|
|
void on_delete_story(DialogId owner_dialog_id, StoryId story_id);
|
2023-05-19 12:41:15 +02:00
|
|
|
|
2023-06-15 11:42:42 +02:00
|
|
|
DialogId on_get_user_stories(DialogId owner_dialog_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::userStories> &&user_stories);
|
2023-05-21 11:39:53 +02:00
|
|
|
|
2023-05-20 22:24:36 +02:00
|
|
|
void on_get_dialog_pinned_stories(DialogId owner_dialog_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::stories_stories> &&stories,
|
|
|
|
Promise<td_api::object_ptr<td_api::stories>> &&promise);
|
|
|
|
|
2023-06-09 16:21:34 +02:00
|
|
|
void on_get_story_archive(telegram_api::object_ptr<telegram_api::stories_stories> &&stories,
|
|
|
|
Promise<td_api::object_ptr<td_api::stories>> &&promise);
|
|
|
|
|
2023-05-21 11:39:53 +02:00
|
|
|
void on_get_dialog_expiring_stories(DialogId owner_dialog_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::stories_userStories> &&stories,
|
2023-06-14 18:27:08 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::activeStories>> &&promise);
|
2023-05-21 11:39:53 +02:00
|
|
|
|
2023-05-19 16:00:33 +02:00
|
|
|
vector<FileId> get_story_file_ids(const Story *story) const;
|
|
|
|
|
2023-06-01 16:41:46 +02:00
|
|
|
static uint64 save_delete_story_on_server_log_event(DialogId dialog_id, StoryId story_id);
|
|
|
|
|
2023-06-01 16:28:49 +02:00
|
|
|
void delete_story_on_server(DialogId dialog_id, StoryId story_id, uint64 log_event_id, Promise<Unit> &&promise);
|
|
|
|
|
2023-05-19 16:00:33 +02:00
|
|
|
void delete_story_files(const Story *story) const;
|
|
|
|
|
2023-05-20 00:23:33 +02:00
|
|
|
void change_story_files(StoryFullId story_full_id, const Story *story, const vector<FileId> &old_file_ids);
|
|
|
|
|
2023-06-05 15:55:06 +02:00
|
|
|
void do_get_story(StoryFullId story_full_id, Result<Unit> &&result,
|
|
|
|
Promise<td_api::object_ptr<td_api::story>> &&promise);
|
|
|
|
|
2023-05-25 20:09:31 +02:00
|
|
|
void do_send_story(unique_ptr<PendingStory> &&pending_story, vector<int> bad_parts);
|
2023-05-25 14:03:29 +02:00
|
|
|
|
|
|
|
void on_upload_story(FileId file_id, telegram_api::object_ptr<telegram_api::InputFile> input_file);
|
|
|
|
|
|
|
|
void on_upload_story_error(FileId file_id, Status status);
|
|
|
|
|
2023-05-30 13:41:36 +02:00
|
|
|
void do_edit_story(FileId file_id, unique_ptr<PendingStory> &&pending_story,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputFile> input_file);
|
|
|
|
|
|
|
|
void on_story_edited(FileId file_id, unique_ptr<PendingStory> pending_story, Result<Unit> result);
|
|
|
|
|
2023-05-26 13:21:36 +02:00
|
|
|
void on_toggle_story_is_pinned(StoryId story_id, bool is_pinned, Promise<Unit> &&promise);
|
|
|
|
|
2023-06-15 11:42:42 +02:00
|
|
|
void on_update_active_stories(DialogId owner_dialog_id, StoryId max_read_story_id, vector<StoryId> &&story_ids);
|
|
|
|
|
|
|
|
void send_update_active_stories(DialogId owner_dialog_id);
|
|
|
|
|
2023-06-14 15:06:48 +02:00
|
|
|
void increment_story_views(DialogId owner_dialog_id, PendingStoryViews &story_views);
|
|
|
|
|
|
|
|
void on_increment_story_views(DialogId owner_dialog_id);
|
|
|
|
|
2023-05-25 14:03:29 +02:00
|
|
|
std::shared_ptr<UploadMediaCallback> upload_media_callback_;
|
|
|
|
|
2023-05-20 00:23:33 +02:00
|
|
|
WaitFreeHashMap<StoryFullId, FileSourceId, StoryFullIdHash> story_full_id_to_file_source_id_;
|
|
|
|
|
2023-05-18 18:57:50 +02:00
|
|
|
WaitFreeHashMap<StoryFullId, unique_ptr<Story>, StoryFullIdHash> stories_;
|
2023-05-08 19:09:01 +02:00
|
|
|
|
2023-06-06 15:11:12 +02:00
|
|
|
WaitFreeHashSet<StoryFullId, StoryFullIdHash> inaccessible_story_full_ids_;
|
|
|
|
|
2023-06-02 15:46:10 +02:00
|
|
|
WaitFreeHashSet<StoryFullId, StoryFullIdHash> deleted_story_full_ids_;
|
|
|
|
|
2023-06-13 17:20:26 +02:00
|
|
|
WaitFreeHashMap<StoryFullId, WaitFreeHashSet<FullMessageId, FullMessageIdHash>, StoryFullIdHash> story_messages_;
|
|
|
|
|
2023-06-15 11:42:42 +02:00
|
|
|
WaitFreeHashMap<DialogId, unique_ptr<ActiveStories>, DialogIdHash> active_stories_;
|
|
|
|
|
2023-05-30 13:41:36 +02:00
|
|
|
FlatHashMap<StoryFullId, unique_ptr<BeingEditedStory>, StoryFullIdHash> being_edited_stories_;
|
|
|
|
|
2023-06-14 15:06:48 +02:00
|
|
|
FlatHashMap<DialogId, PendingStoryViews, DialogIdHash> pending_story_views_;
|
|
|
|
|
2023-05-25 14:03:29 +02:00
|
|
|
uint32 send_story_count_ = 0;
|
|
|
|
|
|
|
|
FlatHashMap<FileId, unique_ptr<PendingStory>, FileIdHash> being_uploaded_files_;
|
|
|
|
|
2023-05-01 21:19:16 +02:00
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|