From f28d40605a3b2c41c29b23dd70f4a354644eb3f4 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 7 Feb 2024 14:01:29 +0300 Subject: [PATCH] Improve Saved Messages topic API and naming. --- td/generate/scheme/td_api.tl | 80 ++++++++++++++-------------- td/telegram/MessagesManager.cpp | 13 ++--- td/telegram/ReactionManager.cpp | 4 +- td/telegram/SavedMessagesManager.cpp | 36 ++++++++++--- td/telegram/SavedMessagesManager.h | 9 ++-- td/telegram/SavedMessagesTopicId.cpp | 32 ++--------- td/telegram/SavedMessagesTopicId.h | 4 +- td/telegram/Td.cpp | 33 ++++++------ td/telegram/cli.cpp | 64 ++++++++++------------ 9 files changed, 139 insertions(+), 136 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 43e2a02a5..b39d0a176 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1355,7 +1355,7 @@ inputMessageReplyToStory story_sender_chat_id:int53 story_id:int32 = InputMessag //@unread_reactions Information about unread reactions added to the message //@reply_to Information about the message or the story this message is replying to; may be null if none //@message_thread_id If non-zero, the identifier of the message thread the message belongs to; unique within the chat to which the message belongs -//@saved_messages_topic Information about topic of the message in the Saved Messages chat; may be null for messages not from Saved Messages +//@saved_messages_topic_id Identifier of the Saved Messages topic for the message; 0 for messages not from Saved Messages //@self_destruct_type The message's self-destruct type; may be null if none //@self_destruct_in Time left before the message self-destruct timer expires, in seconds; 0 if self-destruction isn't scheduled yet //@auto_delete_in Time left before the message will be automatically deleted by message_auto_delete_time setting of the chat, in seconds; 0 if never @@ -1365,7 +1365,7 @@ inputMessageReplyToStory story_sender_chat_id:int53 story_id:int32 = InputMessag //@restriction_reason If non-empty, contains a human-readable description of the reason why access to this message must be restricted //@content Content of the message //@reply_markup Reply markup for the message; may be null if none -message id:int53 sender_id:MessageSender chat_id:int53 sending_state:MessageSendingState scheduling_state:MessageSchedulingState is_outgoing:Bool is_pinned:Bool can_be_edited:Bool can_be_forwarded:Bool can_be_replied_in_another_chat:Bool can_be_saved:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_get_added_reactions:Bool can_get_statistics:Bool can_get_message_thread:Bool can_get_read_date:Bool can_get_viewers:Bool can_get_media_timestamp_links:Bool can_report_reactions:Bool has_timestamped_media:Bool is_channel_post:Bool is_topic_message:Bool contains_unread_mention:Bool date:int32 edit_date:int32 forward_info:messageForwardInfo import_info:messageImportInfo interaction_info:messageInteractionInfo unread_reactions:vector reply_to:MessageReplyTo message_thread_id:int53 saved_messages_topic:SavedMessagesTopic self_destruct_type:MessageSelfDestructType self_destruct_in:double auto_delete_in:double via_bot_user_id:int53 author_signature:string media_album_id:int64 restriction_reason:string content:MessageContent reply_markup:ReplyMarkup = Message; +message id:int53 sender_id:MessageSender chat_id:int53 sending_state:MessageSendingState scheduling_state:MessageSchedulingState is_outgoing:Bool is_pinned:Bool can_be_edited:Bool can_be_forwarded:Bool can_be_replied_in_another_chat:Bool can_be_saved:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_get_added_reactions:Bool can_get_statistics:Bool can_get_message_thread:Bool can_get_read_date:Bool can_get_viewers:Bool can_get_media_timestamp_links:Bool can_report_reactions:Bool has_timestamped_media:Bool is_channel_post:Bool is_topic_message:Bool contains_unread_mention:Bool date:int32 edit_date:int32 forward_info:messageForwardInfo import_info:messageImportInfo interaction_info:messageInteractionInfo unread_reactions:vector reply_to:MessageReplyTo message_thread_id:int53 saved_messages_topic_id:int53 self_destruct_type:MessageSelfDestructType self_destruct_in:double auto_delete_in:double via_bot_user_id:int53 author_signature:string media_album_id:int64 restriction_reason:string content:MessageContent reply_markup:ReplyMarkup = Message; //@description Contains a list of messages @total_count Approximate total number of messages found @messages List of messages; messages may be null messages total_count:int32 messages:vector = Messages; @@ -1890,24 +1890,25 @@ webAppInfo launch_id:int64 url:string = WebAppInfo; messageThreadInfo chat_id:int53 message_thread_id:int53 reply_info:messageReplyInfo unread_message_count:int32 messages:vector draft_message:draftMessage = MessageThreadInfo; -//@class SavedMessagesTopic @description Contains information about a Saved Messages topic +//@class SavedMessagesTopicType @description Describes type of a Saved Messages topic //@description Topic containing messages sent by the current user of forwarded from an unknown chat -savedMessagesTopicMyNotes = SavedMessagesTopic; +savedMessagesTopicTypeMyNotes = SavedMessagesTopicType; //@description Topic containing messages forwarded from a user with hidden privacy -savedMessagesTopicAuthorHidden = SavedMessagesTopic; +savedMessagesTopicTypeAuthorHidden = SavedMessagesTopicType; //@description Topic containing messages forwarded from a specific chat @chat_id Identifier of the chat -savedMessagesTopicSavedFromChat chat_id:int53 = SavedMessagesTopic; +savedMessagesTopicTypeSavedFromChat chat_id:int53 = SavedMessagesTopicType; -//@description Contains information about a found Saved Messages topic -//@topic The topic +//@description Contains information about a Saved Messages topic +//@id Unique topic identifier +//@type Type of the topic //@is_pinned True, if the topic is pinned //@order A parameter used to determine order of the topic in the topic list. Topics must be sorted by the order in descending order //@last_message Last message in the topic; may be null if none or unknown -foundSavedMessagesTopic topic:SavedMessagesTopic is_pinned:Bool order:int64 last_message:message = FoundSavedMessagesTopic; +savedMessagesTopic id:int53 type:SavedMessagesTopicType is_pinned:Bool order:int64 last_message:message = SavedMessagesTopic; //@description Describes a forum topic icon @color Color of the topic icon in RGB format @custom_emoji_id Unique identifier of the custom emoji shown on the topic icon; 0 if none @@ -6504,8 +6505,9 @@ updateChatFolders chat_folders:vector main_chat_list_position:in //@online_member_count New number of online members in the chat, or 0 if unknown updateChatOnlineMemberCount chat_id:int53 online_member_count:int32 = Update; -//@description Basic information about a Saved Messages topic has changed @topic New information about the topic -updateSavedMessagesTopic topic:foundSavedMessagesTopic = Update; +//@description Basic information about a Saved Messages topic has changed. This update is guaranteed to come before the topic identifier is returned to the application +//@topic New data about the topic +updateSavedMessagesTopic topic:savedMessagesTopic = Update; //@description Basic information about a topic in a forum chat was changed @chat_id Chat identifier @info New information about the topic updateForumTopicInfo chat_id:int53 info:forumTopicInfo = Update; @@ -6738,9 +6740,9 @@ updateActiveEmojiReactions emojis:vector = Update; updateDefaultReactionType reaction_type:ReactionType = Update; //@description Tags used in Saved Messages or a Saved Messages topic have changed -//@saved_messages_topic Saved Messages topic which tags were changed; may be null if tags for the whole chat has changed +//@saved_messages_topic_id Identifier of Saved Messages topic which tags were changed; 0 if tags for the whole chat has changed //@tags The new tags -updateSavedMessagesTags saved_messages_topic:SavedMessagesTopic tags:savedMessagesTags = Update; +updateSavedMessagesTags saved_messages_topic_id:int53 tags:savedMessagesTags = Update; //@description The parameters of speech recognition without Telegram Premium subscription has changed //@max_media_duration The maximum allowed duration of media for speech recognition without Telegram Premium subscription @@ -7247,34 +7249,34 @@ getInactiveSupergroupChats = Chats; loadSavedMessagesTopics limit:int32 = Ok; //@description Returns messages in a Saved Messages topic. The messages are returned in a reverse chronological order (i.e., in order of decreasing message_id) -//@saved_messages_topic Saved Messages topic which messages will be fetched +//@saved_messages_topic_id Identifier of Saved Messages topic which messages will be fetched //@from_message_id Identifier of the message starting from which messages must be fetched; use 0 to get results from the last message //@offset Specify 0 to get results from exactly the message from_message_id or a negative offset up to 99 to get additionally some newer messages //@limit The maximum number of messages to be returned; must be positive and can't be greater than 100. If the offset is negative, the limit must be greater than or equal to -offset. //-For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit -getSavedMessagesTopicHistory saved_messages_topic:SavedMessagesTopic from_message_id:int53 offset:int32 limit:int32 = Messages; +getSavedMessagesTopicHistory saved_messages_topic_id:int53 from_message_id:int53 offset:int32 limit:int32 = Messages; //@description Returns the last message sent in a Saved Messages topic no later than the specified date -//@saved_messages_topic Saved Messages topic which message will be returned +//@saved_messages_topic_id Identifier of Saved Messages topic which message will be returned //@date Point in time (Unix timestamp) relative to which to search for messages -getSavedMessagesTopicMessageByDate saved_messages_topic:SavedMessagesTopic date:int32 = Message; +getSavedMessagesTopicMessageByDate saved_messages_topic_id:int53 date:int32 = Message; -//@description Deletes all messages in a Saved Messages topic @saved_messages_topic Saved Messages topic which messages will be deleted -deleteSavedMessagesTopicHistory saved_messages_topic:SavedMessagesTopic = Ok; +//@description Deletes all messages in a Saved Messages topic @saved_messages_topic_id Identifier of Saved Messages topic which messages will be deleted +deleteSavedMessagesTopicHistory saved_messages_topic_id:int53 = Ok; //@description Deletes all messages between the specified dates in a Saved Messages topic. Messages sent in the last 30 seconds will not be deleted -//@saved_messages_topic Saved Messages topic which messages will be deleted +//@saved_messages_topic_id Identifier of Saved Messages topic which messages will be deleted //@min_date The minimum date of the messages to delete //@max_date The maximum date of the messages to delete -deleteSavedMessagesTopicMessagesByDate saved_messages_topic:SavedMessagesTopic min_date:int32 max_date:int32 = Ok; +deleteSavedMessagesTopicMessagesByDate saved_messages_topic_id:int53 min_date:int32 max_date:int32 = Ok; //@description Changes the pinned state of a Saved Messages topic. There can be up to getOption("pinned_saved_messages_topic_count_max") pinned topics. The limit can be increased with Telegram Premium -//@saved_messages_topic Saved Messages topic to pin or unpin +//@saved_messages_topic_id Identifier of Saved Messages topic to pin or unpin //@is_pinned Pass true to pin the topic; pass false to unpin it -toggleSavedMessagesTopicIsPinned saved_messages_topic:SavedMessagesTopic is_pinned:Bool = Ok; +toggleSavedMessagesTopicIsPinned saved_messages_topic_id:int53 is_pinned:Bool = Ok; -//@description Changes the order of pinned Saved Messages topics @saved_messages_topics The new list of pinned Saved Messages topics -setPinnedSavedMessagesTopics saved_messages_topics:vector = Ok; +//@description Changes the order of pinned Saved Messages topics @saved_messages_topic_ids Identifiers of the new pinned Saved Messages topics +setPinnedSavedMessagesTopics saved_messages_topic_ids:vector = Ok; //@description Returns a list of common group chats with a given user. Chats are sorted by their type and creation date @@ -7327,8 +7329,8 @@ deleteChat chat_id:int53 = Ok; //-For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit //@filter Additional filter for messages to search; pass null to search for all messages //@message_thread_id If not 0, only messages in the specified thread will be returned; supergroups only -//@saved_messages_topic If not null, only messages in the specified Saved Messages topic will be returned; pass null to return all messages, or for chats other than Saved Messages -searchChatMessages chat_id:int53 query:string sender_id:MessageSender from_message_id:int53 offset:int32 limit:int32 filter:SearchMessagesFilter message_thread_id:int53 saved_messages_topic:SavedMessagesTopic = FoundChatMessages; +//@saved_messages_topic_id If not 0, only messages in the specified Saved Messages topic will be returned; pass 0 to return all messages, or for chats other than Saved Messages +searchChatMessages chat_id:int53 query:string sender_id:MessageSender from_message_id:int53 offset:int32 limit:int32 filter:SearchMessagesFilter message_thread_id:int53 saved_messages_topic_id:int53 = FoundChatMessages; //@description Searches for messages in all chats except secret chats. Returns the results in reverse chronological order (i.e., in order of decreasing (date, chat_id, message_id)). //-For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit @@ -7352,14 +7354,14 @@ searchSecretMessages chat_id:int53 query:string offset:string limit:int32 filter //@description Searches for messages tagged by the given reaction and with the given words in the Saved Messages chat; for Telegram Premium users only. //-Returns the results in reverse chronological order, i.e. in order of decreasing message_id //-For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit -//@saved_messages_topic If not null, only messages in the specified Saved Messages topic will be considered; pass null to consider all messages +//@saved_messages_topic_id If not 0, only messages in the specified Saved Messages topic will be considered; pass 0 to consider all messages //@tag Tag to search for; pass null to return all suitable messages //@query Query to search for //@from_message_id Identifier of the message starting from which messages must be fetched; use 0 to get results from the last message //@offset Specify 0 to get results from exactly the message from_message_id or a negative offset to get the specified message and some newer messages //@limit The maximum number of messages to be returned; must be positive and can't be greater than 100. If the offset is negative, the limit must be greater than -offset. //-For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit -searchSavedMessages saved_messages_topic:SavedMessagesTopic tag:ReactionType query:string from_message_id:int53 offset:int32 limit:int32 = FoundChatMessages; +searchSavedMessages saved_messages_topic_id:int53 tag:ReactionType query:string from_message_id:int53 offset:int32 limit:int32 = FoundChatMessages; //@description Searches for call messages. Returns the results in reverse chronological order (i.e., in order of decreasing message_id). For optimal performance, the number of returned messages is chosen by TDLib //@offset Offset of the first entry to return as received from the previous request; use empty string to get the first chunk of results @@ -7390,30 +7392,30 @@ getChatMessageByDate chat_id:int53 date:int32 = Message; //@filter Filter for message content. Filters searchMessagesFilterEmpty, searchMessagesFilterMention, searchMessagesFilterUnreadMention, and searchMessagesFilterUnreadReaction are unsupported in this function //@from_message_id The message identifier from which to return information about message positions //@limit The expected number of message positions to be returned; 50-2000. A smaller number of positions can be returned, if there are not enough appropriate messages -//@saved_messages_topic If not null, only messages in the specified Saved Messages topic will be considered; pass null to consider all messages, or for chats other than Saved Messages -getChatSparseMessagePositions chat_id:int53 filter:SearchMessagesFilter from_message_id:int53 limit:int32 saved_messages_topic:SavedMessagesTopic = MessagePositions; +//@saved_messages_topic_id If not 0, only messages in the specified Saved Messages topic will be considered; pass 0 to consider all messages, or for chats other than Saved Messages +getChatSparseMessagePositions chat_id:int53 filter:SearchMessagesFilter from_message_id:int53 limit:int32 saved_messages_topic_id:int53 = MessagePositions; //@description Returns information about the next messages of the specified type in the chat split by days. Returns the results in reverse chronological order. Can return partial result for the last returned day. Behavior of this method depends on the value of the option "utc_time_offset" //@chat_id Identifier of the chat in which to return information about messages //@filter Filter for message content. Filters searchMessagesFilterEmpty, searchMessagesFilterMention, searchMessagesFilterUnreadMention, and searchMessagesFilterUnreadReaction are unsupported in this function //@from_message_id The message identifier from which to return information about messages; use 0 to get results from the last message -//@saved_messages_topic If not null, only messages in the specified Saved Messages topic will be considered; pass null to consider all messages, or for chats other than Saved Messages -getChatMessageCalendar chat_id:int53 filter:SearchMessagesFilter from_message_id:int53 saved_messages_topic:SavedMessagesTopic = MessageCalendar; +//@saved_messages_topic_id If not0, only messages in the specified Saved Messages topic will be considered; pass 0 to consider all messages, or for chats other than Saved Messages +getChatMessageCalendar chat_id:int53 filter:SearchMessagesFilter from_message_id:int53 saved_messages_topic_id:int53 = MessageCalendar; //@description Returns approximate number of messages of the specified type in the chat //@chat_id Identifier of the chat in which to count messages //@filter Filter for message content; searchMessagesFilterEmpty is unsupported in this function -//@saved_messages_topic If not null, only messages in the specified Saved Messages topic will be counted; pass null to count all messages, or for chats other than Saved Messages +//@saved_messages_topic_id If not 0, only messages in the specified Saved Messages topic will be counted; pass 0 to count all messages, or for chats other than Saved Messages //@return_local Pass true to get the number of messages without sending network requests, or -1 if the number of messages is unknown locally -getChatMessageCount chat_id:int53 filter:SearchMessagesFilter saved_messages_topic:SavedMessagesTopic return_local:Bool = Count; +getChatMessageCount chat_id:int53 filter:SearchMessagesFilter saved_messages_topic_id:int53 return_local:Bool = Count; //@description Returns approximate 1-based position of a message among messages, which can be found by the specified filter in the chat. Cannot be used in secret chats //@chat_id Identifier of the chat in which to find message position //@message_id Message identifier //@filter Filter for message content; searchMessagesFilterEmpty, searchMessagesFilterUnreadMention, searchMessagesFilterUnreadReaction, and searchMessagesFilterFailedToSend are unsupported in this function //@message_thread_id If not 0, only messages in the specified thread will be considered; supergroups only -//@saved_messages_topic If not null, only messages in the specified Saved Messages topic will be considered; pass null to consider all relevant messages, or for chats other than Saved Messages -getChatMessagePosition chat_id:int53 message_id:int53 filter:SearchMessagesFilter message_thread_id:int53 saved_messages_topic:SavedMessagesTopic = Count; +//@saved_messages_topic_id If not 0, only messages in the specified Saved Messages topic will be considered; pass 0 to consider all relevant messages, or for chats other than Saved Messages +getChatMessagePosition chat_id:int53 message_id:int53 filter:SearchMessagesFilter message_thread_id:int53 saved_messages_topic_id:int53 = Count; //@description Returns all scheduled messages in a chat. The messages are returned in a reverse chronological order (i.e., in order of decreasing message_id) @chat_id Chat identifier getChatScheduledMessages chat_id:int53 = Messages; @@ -7745,8 +7747,8 @@ getMessageAddedReactions chat_id:int53 message_id:int53 reaction_type:ReactionTy setDefaultReactionType reaction_type:ReactionType = Ok; //@description Returns tags used in Saved Messages or a Saved Messages topic -//@saved_messages_topic Saved Messages topic which tags will be returned; pass null to get all Saved Messages tags -getSavedMessagesTags saved_messages_topic:SavedMessagesTopic = SavedMessagesTags; +//@saved_messages_topic_id Identifier of Saved Messages topic which tags will be returned; pass 0 to get all Saved Messages tags +getSavedMessagesTags saved_messages_topic_id:int53 = SavedMessagesTags; //@description Changes label of a Saved Messages tag; for Telegram Premium users only @tag The tag which label will be changed @label New label for the tag; 0-12 characters setSavedMessagesTagLabel tag:ReactionType label:string = Ok; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index c1d04c009..0836e3d8e 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -22556,9 +22556,9 @@ td_api::object_ptr MessagesManager::get_dialog_event_log_messag m->message_id.get(), std::move(sender), get_chat_id_object(dialog_id, "get_dialog_event_log_message_object"), nullptr, nullptr, m->is_outgoing, m->is_pinned, false, false, false, can_be_saved, false, false, false, false, false, false, false, false, false, true, m->is_channel_post, m->is_topic_message, false, m->date, edit_date, - std::move(forward_info), std::move(import_info), std::move(interaction_info), Auto(), nullptr, 0, nullptr, - nullptr, 0.0, 0.0, via_bot_user_id, m->author_signature, 0, - get_restriction_reason_description(m->restriction_reasons), std::move(content), std::move(reply_markup)); + std::move(forward_info), std::move(import_info), std::move(interaction_info), Auto(), nullptr, 0, 0, nullptr, 0.0, + 0.0, via_bot_user_id, m->author_signature, 0, get_restriction_reason_description(m->restriction_reasons), + std::move(content), std::move(reply_markup)); } tl_object_ptr MessagesManager::get_message_object(MessageFullId message_full_id, const char *source) { @@ -22668,9 +22668,10 @@ tl_object_ptr MessagesManager::get_message_object(DialogId dial can_get_media_timestamp_links, can_report_reactions, has_timestamped_media, m->is_channel_post, m->is_topic_message, m->contains_unread_mention, date, edit_date, std::move(forward_info), std::move(import_info), std::move(interaction_info), std::move(unread_reactions), std::move(reply_to), top_thread_message_id, - m->saved_messages_topic_id.get_saved_messages_topic_object(td_), std::move(self_destruct_type), ttl_expires_in, - auto_delete_in, via_bot_user_id, m->author_signature, m->media_album_id, - get_restriction_reason_description(m->restriction_reasons), std::move(content), std::move(reply_markup)); + td_->saved_messages_manager_->get_saved_messages_topic_id_object(m->saved_messages_topic_id), + std::move(self_destruct_type), ttl_expires_in, auto_delete_in, via_bot_user_id, m->author_signature, + m->media_album_id, get_restriction_reason_description(m->restriction_reasons), std::move(content), + std::move(reply_markup)); } tl_object_ptr MessagesManager::get_messages_object(int32 total_count, DialogId dialog_id, diff --git a/td/telegram/ReactionManager.cpp b/td/telegram/ReactionManager.cpp index a2e5c185b..c74486c8b 100644 --- a/td/telegram/ReactionManager.cpp +++ b/td/telegram/ReactionManager.cpp @@ -16,6 +16,7 @@ #include "td/telegram/OptionManager.h" #include "td/telegram/ReactionManager.hpp" #include "td/telegram/ReactionType.hpp" +#include "td/telegram/SavedMessagesManager.h" #include "td/telegram/StickerFormat.h" #include "td/telegram/StickersManager.h" #include "td/telegram/Td.h" @@ -1102,7 +1103,8 @@ td_api::object_ptr ReactionManager::get_update_ SavedMessagesTopicId saved_messages_topic_id, const SavedReactionTags *tags) const { CHECK(tags != nullptr); return td_api::make_object( - saved_messages_topic_id.get_saved_messages_topic_object(td_), tags->get_saved_messages_tags_object()); + td_->saved_messages_manager_->get_saved_messages_topic_id_object(saved_messages_topic_id), + tags->get_saved_messages_tags_object()); } void ReactionManager::send_update_saved_messages_tags(SavedMessagesTopicId saved_messages_topic_id, diff --git a/td/telegram/SavedMessagesManager.cpp b/td/telegram/SavedMessagesManager.cpp index 92a625cd8..0b58ebb35 100644 --- a/td/telegram/SavedMessagesManager.cpp +++ b/td/telegram/SavedMessagesManager.cpp @@ -314,6 +314,28 @@ void SavedMessagesManager::tear_down() { parent_.reset(); } +SavedMessagesTopicId SavedMessagesManager::get_topic_id(int64 topic_id) const { + if (topic_id == 0) { + return SavedMessagesTopicId(); + } + auto saved_messages_topic_id = SavedMessagesTopicId(DialogId(topic_id)); + if (saved_messages_topics_.count(saved_messages_topic_id) == 0) { + // an invalid topic identifier + return SavedMessagesTopicId(DialogId(std::numeric_limits::max())); + } + return saved_messages_topic_id; +} + +int64 SavedMessagesManager::get_saved_messages_topic_id_object(SavedMessagesTopicId saved_messages_topic_id) { + if (saved_messages_topic_id == SavedMessagesTopicId()) { + return 0; + } + + add_topic(saved_messages_topic_id); + + return saved_messages_topic_id.get_unique_id(); +} + SavedMessagesManager::SavedMessagesTopic *SavedMessagesManager::get_topic( SavedMessagesTopicId saved_messages_topic_id) { CHECK(saved_messages_topic_id.is_valid()); @@ -331,6 +353,7 @@ SavedMessagesManager::SavedMessagesTopic *SavedMessagesManager::add_topic( if (result == nullptr) { result = make_unique(); result->saved_messages_topic_id_ = saved_messages_topic_id; + send_update_saved_messages_topic(result.get()); } return result.get(); } @@ -614,25 +637,26 @@ void SavedMessagesManager::on_get_saved_messages_topics( promise.set_value(Unit()); } -td_api::object_ptr SavedMessagesManager::get_found_saved_messages_topic_object( +td_api::object_ptr SavedMessagesManager::get_saved_messages_topic_object( const SavedMessagesTopic *topic) const { CHECK(topic != nullptr); td_api::object_ptr last_message_object; if (topic->last_message_id_ != MessageId()) { last_message_object = td_->messages_manager_->get_message_object( - {td_->dialog_manager_->get_my_dialog_id(), topic->last_message_id_}, "get_found_saved_messages_topic_object"); + {td_->dialog_manager_->get_my_dialog_id(), topic->last_message_id_}, "get_saved_messages_topic_object"); } auto public_order = TopicDate(topic->private_order_, topic->saved_messages_topic_id_) <= topic_list_.last_topic_date_ ? topic->private_order_ : static_cast(0); - return td_api::make_object( - topic->saved_messages_topic_id_.get_saved_messages_topic_object(td_), topic->pinned_order_ != 0, public_order, - std::move(last_message_object)); + return td_api::make_object( + topic->saved_messages_topic_id_.get_unique_id(), + topic->saved_messages_topic_id_.get_saved_messages_topic_type_object(td_), topic->pinned_order_ != 0, + public_order, std::move(last_message_object)); } td_api::object_ptr SavedMessagesManager::get_update_saved_messages_topic_object( const SavedMessagesTopic *topic) const { - return td_api::make_object(get_found_saved_messages_topic_object(topic)); + return td_api::make_object(get_saved_messages_topic_object(topic)); } void SavedMessagesManager::send_update_saved_messages_topic(const SavedMessagesTopic *topic) const { diff --git a/td/telegram/SavedMessagesManager.h b/td/telegram/SavedMessagesManager.h index 47257027a..e53707fdd 100644 --- a/td/telegram/SavedMessagesManager.h +++ b/td/telegram/SavedMessagesManager.h @@ -26,6 +26,10 @@ class SavedMessagesManager final : public Actor { public: SavedMessagesManager(Td *td, ActorShared<> parent); + SavedMessagesTopicId get_topic_id(int64 topic_id) const; + + int64 get_saved_messages_topic_id_object(SavedMessagesTopicId saved_messages_topic_id); + void set_topic_last_message_id(SavedMessagesTopicId saved_messages_topic_id, MessageId last_message_id, int32 last_message_date); @@ -71,7 +75,7 @@ class SavedMessagesManager final : public Actor { int32 last_message_date_ = 0; int64 pinned_order_ = 0; int64 private_order_ = 0; - bool is_changed_ = true; + bool is_changed_ = false; }; class TopicDate { @@ -156,8 +160,7 @@ class SavedMessagesManager final : public Actor { void on_topic_changed(SavedMessagesTopic *topic); - td_api::object_ptr get_found_saved_messages_topic_object( - const SavedMessagesTopic *topic) const; + td_api::object_ptr get_saved_messages_topic_object(const SavedMessagesTopic *topic) const; td_api::object_ptr get_update_saved_messages_topic_object( const SavedMessagesTopic *topic) const; diff --git a/td/telegram/SavedMessagesTopicId.cpp b/td/telegram/SavedMessagesTopicId.cpp index 9b4512680..da0bb7961 100644 --- a/td/telegram/SavedMessagesTopicId.cpp +++ b/td/telegram/SavedMessagesTopicId.cpp @@ -47,41 +47,19 @@ SavedMessagesTopicId::SavedMessagesTopicId(DialogId my_dialog_id, const MessageF dialog_id_ = my_dialog_id; } -SavedMessagesTopicId::SavedMessagesTopicId(const Td *td, - const td_api::object_ptr &saved_messages_topic) { - if (saved_messages_topic == nullptr) { - return; - } - switch (saved_messages_topic->get_id()) { - case td_api::savedMessagesTopicMyNotes::ID: - dialog_id_ = td->dialog_manager_->get_my_dialog_id(); - break; - case td_api::savedMessagesTopicAuthorHidden::ID: - dialog_id_ = HIDDEN_AUTHOR_DIALOG_ID; - break; - case td_api::savedMessagesTopicSavedFromChat::ID: - dialog_id_ = - DialogId(static_cast(saved_messages_topic.get())->chat_id_); - break; - default: - UNREACHABLE(); - break; - } -} - -td_api::object_ptr SavedMessagesTopicId::get_saved_messages_topic_object( +td_api::object_ptr SavedMessagesTopicId::get_saved_messages_topic_type_object( const Td *td) const { if (dialog_id_ == DialogId()) { return nullptr; } if (dialog_id_ == td->dialog_manager_->get_my_dialog_id()) { - return td_api::make_object(); + return td_api::make_object(); } if (is_author_hidden()) { - return td_api::make_object(); + return td_api::make_object(); } - return td_api::make_object( - td->messages_manager_->get_chat_id_object(dialog_id_, "savedMessagesTopicSavedFromChat")); + return td_api::make_object( + td->messages_manager_->get_chat_id_object(dialog_id_, "savedMessagesTopicTypeSavedFromChat")); } bool SavedMessagesTopicId::have_input_peer(Td *td) const { diff --git a/td/telegram/SavedMessagesTopicId.h b/td/telegram/SavedMessagesTopicId.h index 341da5802..8f2cce33e 100644 --- a/td/telegram/SavedMessagesTopicId.h +++ b/td/telegram/SavedMessagesTopicId.h @@ -41,8 +41,6 @@ class SavedMessagesTopicId { SavedMessagesTopicId(DialogId my_dialog_id, const MessageForwardInfo *message_forward_info, DialogId real_forward_from_dialog_id); - SavedMessagesTopicId(const Td *td, const td_api::object_ptr &saved_messages_topic); - bool is_valid() const { return dialog_id_.is_valid(); } @@ -57,7 +55,7 @@ class SavedMessagesTopicId { return dialog_id_.get(); } - td_api::object_ptr get_saved_messages_topic_object(const Td *td) const; + td_api::object_ptr get_saved_messages_topic_type_object(const Td *td) const; telegram_api::object_ptr get_input_peer(const Td *td) const; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 8686bd501..2aa7a0167 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5066,30 +5066,30 @@ void Td::on_request(uint64 id, const td_api::loadSavedMessagesTopics &request) { void Td::on_request(uint64 id, const td_api::getSavedMessagesTopicHistory &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); - saved_messages_manager_->get_saved_messages_topic_history(SavedMessagesTopicId(this, request.saved_messages_topic_), - MessageId(request.from_message_id_), request.offset_, - request.limit_, std::move(promise)); + saved_messages_manager_->get_saved_messages_topic_history( + saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), MessageId(request.from_message_id_), + request.offset_, request.limit_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::getSavedMessagesTopicMessageByDate &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); saved_messages_manager_->get_saved_messages_topic_message_by_date( - SavedMessagesTopicId(this, request.saved_messages_topic_), request.date_, std::move(promise)); + saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), request.date_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::deleteSavedMessagesTopicHistory &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); saved_messages_manager_->delete_saved_messages_topic_history( - SavedMessagesTopicId(this, request.saved_messages_topic_), std::move(promise)); + saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), std::move(promise)); } void Td::on_request(uint64 id, const td_api::deleteSavedMessagesTopicMessagesByDate &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); saved_messages_manager_->delete_saved_messages_topic_messages_by_date( - SavedMessagesTopicId(this, request.saved_messages_topic_), request.min_date_, request.max_date_, + saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), request.min_date_, request.max_date_, std::move(promise)); } @@ -5097,14 +5097,14 @@ void Td::on_request(uint64 id, const td_api::toggleSavedMessagesTopicIsPinned &r CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); saved_messages_manager_->toggle_saved_messages_topic_is_pinned( - SavedMessagesTopicId(this, request.saved_messages_topic_), request.is_pinned_, std::move(promise)); + saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), request.is_pinned_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::setPinnedSavedMessagesTopics &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - auto saved_messages_topic_ids = transform(request.saved_messages_topics_, - [this](const auto &topic) { return SavedMessagesTopicId(this, topic); }); + auto saved_messages_topic_ids = transform( + request.saved_messages_topic_ids_, [this](int64 topic) { return saved_messages_manager_->get_topic_id(topic); }); saved_messages_manager_->set_pinned_saved_messages_topics(std::move(saved_messages_topic_ids), std::move(promise)); } @@ -5301,7 +5301,7 @@ void Td::on_request(uint64 id, const td_api::getMessageThreadHistory &request) { void Td::on_request(uint64 id, td_api::getChatMessageCalendar &request) { CHECK_IS_USER(); CREATE_REQUEST(GetChatMessageCalendarRequest, request.chat_id_, - SavedMessagesTopicId(this, request.saved_messages_topic_), request.from_message_id_, + saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), request.from_message_id_, std::move(request.filter_)); } @@ -5310,7 +5310,8 @@ void Td::on_request(uint64 id, td_api::searchChatMessages &request) { CLEAN_INPUT_STRING(request.query_); CREATE_REQUEST(SearchChatMessagesRequest, request.chat_id_, std::move(request.query_), std::move(request.sender_id_), request.from_message_id_, request.offset_, request.limit_, std::move(request.filter_), - request.message_thread_id_, SavedMessagesTopicId(this, request.saved_messages_topic_), ReactionType()); + request.message_thread_id_, saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), + ReactionType()); } void Td::on_request(uint64 id, td_api::searchSecretMessages &request) { @@ -5339,7 +5340,7 @@ void Td::on_request(uint64 id, td_api::searchSavedMessages &request) { CLEAN_INPUT_STRING(request.query_); CREATE_REQUEST(SearchChatMessagesRequest, dialog_manager_->get_my_dialog_id().get(), std::move(request.query_), nullptr, request.from_message_id_, request.offset_, request.limit_, nullptr, 0, - SavedMessagesTopicId(this, request.saved_messages_topic_), ReactionType(request.tag_)); + saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), ReactionType(request.tag_)); } void Td::on_request(uint64 id, const td_api::searchCallMessages &request) { @@ -5380,7 +5381,7 @@ void Td::on_request(uint64 id, const td_api::getChatSparseMessagePositions &requ CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); messages_manager_->get_dialog_sparse_message_positions( - DialogId(request.chat_id_), SavedMessagesTopicId(this, request.saved_messages_topic_), + DialogId(request.chat_id_), saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), get_message_search_filter(request.filter_), MessageId(request.from_message_id_), request.limit_, std::move(promise)); } @@ -5396,7 +5397,7 @@ void Td::on_request(uint64 id, const td_api::getChatMessageCount &request) { } }); messages_manager_->get_dialog_message_count( - DialogId(request.chat_id_), SavedMessagesTopicId(this, request.saved_messages_topic_), + DialogId(request.chat_id_), saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), get_message_search_filter(request.filter_), request.return_local_, std::move(query_promise)); } @@ -5412,7 +5413,7 @@ void Td::on_request(uint64 id, const td_api::getChatMessagePosition &request) { }); messages_manager_->get_dialog_message_position( {DialogId(request.chat_id_), MessageId(request.message_id_)}, get_message_search_filter(request.filter_), - MessageId(request.message_thread_id_), SavedMessagesTopicId(this, request.saved_messages_topic_), + MessageId(request.message_thread_id_), saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), std::move(query_promise)); } @@ -5490,7 +5491,7 @@ void Td::on_request(uint64 id, const td_api::setDefaultReactionType &request) { void Td::on_request(uint64 id, const td_api::getSavedMessagesTags &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); - reaction_manager_->get_saved_messages_tags(SavedMessagesTopicId(this, request.saved_messages_topic_), + reaction_manager_->get_saved_messages_tags(saved_messages_manager_->get_topic_id(request.saved_messages_topic_id_), std::move(promise)); } diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 8bf1ad22b..3ba716ea2 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -342,7 +342,7 @@ class CliClient final : public Actor { LOG(ERROR) << (last_message_id >> 20); send_request(td_api::make_object( search_chat_id_, string(), nullptr, last_message_id, 0, 100, as_search_messages_filter("pvi"), 0, - get_saved_messages_topic())); + get_saved_messages_topic_id())); } else { search_chat_id_ = 0; } @@ -946,21 +946,15 @@ class CliClient final : public Actor { return nullptr; } - td_api::object_ptr as_saved_messages_topic(int64 saved_messages_topic) const { - if (saved_messages_topic == 0) { - return nullptr; + int64 as_saved_messages_topic_id(int64 saved_messages_topic_id) const { + if (saved_messages_topic_id == -1) { + return 2666000; } - if (saved_messages_topic == my_id_) { - return td_api::make_object(); - } - if (saved_messages_topic == -1) { - return td_api::make_object(); - } - return td_api::make_object(saved_messages_topic); + return saved_messages_topic_id; } - td_api::object_ptr get_saved_messages_topic() const { - return as_saved_messages_topic(saved_messages_topic_); + int64 get_saved_messages_topic_id() const { + return as_saved_messages_topic_id(saved_messages_topic_id_); } td_api::object_ptr get_link_preview_options() const { @@ -2763,27 +2757,27 @@ class CliClient final : public Actor { int32 offset; string limit; get_args(args, from_message_id, offset, limit); - send_request(td_api::make_object(get_saved_messages_topic(), + send_request(td_api::make_object(get_saved_messages_topic_id(), from_message_id, offset, as_limit(limit))); } else if (op == "gsmtmbd") { - send_request(td_api::make_object(get_saved_messages_topic(), + send_request(td_api::make_object(get_saved_messages_topic_id(), to_integer(args))); } else if (op == "dsmth" && args.empty()) { - send_request(td_api::make_object(get_saved_messages_topic())); + send_request(td_api::make_object(get_saved_messages_topic_id())); } else if (op == "dsmtmbd") { int32 min_date; int32 max_date; get_args(args, min_date, max_date); - send_request(td_api::make_object(get_saved_messages_topic(), + send_request(td_api::make_object(get_saved_messages_topic_id(), min_date, max_date)); } else if (op == "tsmtip") { bool is_pinned; get_args(args, is_pinned); send_request( - td_api::make_object(get_saved_messages_topic(), is_pinned)); + td_api::make_object(get_saved_messages_topic_id(), is_pinned)); } else if (op == "spsmt") { send_request(td_api::make_object( - transform(autosplit(args), [this](Slice str) { return as_saved_messages_topic(as_chat_id(str)); }))); + transform(autosplit(args), [this](Slice str) { return as_saved_messages_topic_id(as_chat_id(str)); }))); } else if (op == "gcc" || op == "GetCommonChats") { UserId user_id; ChatId offset_chat_id; @@ -2862,7 +2856,7 @@ class CliClient final : public Actor { send_request(td_api::make_object( chat_id, message_id, as_reaction_type(reaction), offset, as_limit(limit))); } else if (op == "gsmts") { - send_request(td_api::make_object(get_saved_messages_topic())); + send_request(td_api::make_object(get_saved_messages_topic_id())); } else if (op == "ssmtl") { string reaction; string label; @@ -2892,11 +2886,11 @@ class CliClient final : public Actor { get_args(args, chat_id, filter); send_request(td_api::make_object(chat_id, "", nullptr, 0, 0, 100, as_search_messages_filter(filter), - message_thread_id_, get_saved_messages_topic())); + message_thread_id_, get_saved_messages_topic_id())); } else if (op == "spvf") { search_chat_id_ = as_chat_id(args); send_request(td_api::make_object( - search_chat_id_, "", nullptr, 0, 0, 100, as_search_messages_filter("pvi"), 0, get_saved_messages_topic())); + search_chat_id_, "", nullptr, 0, 0, 100, as_search_messages_filter("pvi"), 0, get_saved_messages_topic_id())); } else if (op == "Search" || op == "SearchA" || op == "SearchM") { string query; string limit; @@ -2917,14 +2911,14 @@ class CliClient final : public Actor { SearchQuery query; get_args(args, chat_id, query); send_request(td_api::make_object(chat_id, query.query, nullptr, 0, 0, query.limit, - nullptr, 0, get_saved_messages_topic())); + nullptr, 0, get_saved_messages_topic_id())); } else if (op == "SMME") { ChatId chat_id; string limit; get_args(args, chat_id, limit); send_request(td_api::make_object( chat_id, "", td_api::make_object(my_id_), 0, 0, as_limit(limit), nullptr, 0, - get_saved_messages_topic())); + get_saved_messages_topic_id())); } else if (op == "SMU" || op == "SMC") { ChatId chat_id; string sender_id; @@ -2933,7 +2927,7 @@ class CliClient final : public Actor { get_args(args, chat_id, sender_id, from_message_id, limit); send_request(td_api::make_object(chat_id, "", as_message_sender(sender_id), from_message_id, 0, as_limit(limit), nullptr, 0, - get_saved_messages_topic())); + get_saved_messages_topic_id())); } else if (op == "SM") { ChatId chat_id; string filter; @@ -2943,7 +2937,7 @@ class CliClient final : public Actor { get_args(args, chat_id, filter, limit, offset_message_id, offset); send_request(td_api::make_object(chat_id, "", nullptr, offset_message_id, offset, as_limit(limit), as_search_messages_filter(filter), - 0, get_saved_messages_topic())); + 0, get_saved_messages_topic_id())); } else if (op == "SC") { string limit; string offset; @@ -2968,7 +2962,7 @@ class CliClient final : public Actor { MessageId from_message_id; get_args(args, chat_id, filter, from_message_id); send_request(td_api::make_object(chat_id, as_search_messages_filter(filter), - from_message_id, get_saved_messages_topic())); + from_message_id, get_saved_messages_topic_id())); } else if (op == "SearchAudio" || op == "SearchDocument" || op == "SearchPhoto" || op == "SearchChatPhoto") { ChatId chat_id; MessageId offset_message_id; @@ -2976,15 +2970,15 @@ class CliClient final : public Actor { get_args(args, chat_id, offset_message_id, query); send_request(td_api::make_object(chat_id, query.query, nullptr, offset_message_id, 0, query.limit, as_search_messages_filter(op), 0, - get_saved_messages_topic())); + get_saved_messages_topic_id())); } else if (op == "ssms") { string tag; MessageId from_message_id; int32 offset; SearchQuery query; get_args(args, tag, from_message_id, offset, query); - send_request(td_api::make_object(get_saved_messages_topic(), as_reaction_type(tag), - query.query, from_message_id, offset, query.limit)); + send_request(td_api::make_object( + get_saved_messages_topic_id(), as_reaction_type(tag), query.query, from_message_id, offset, query.limit)); } else if (op == "gcmbd") { ChatId chat_id; int32 date; @@ -2997,21 +2991,21 @@ class CliClient final : public Actor { string limit; get_args(args, chat_id, filter, from_message_id, limit); send_request(td_api::make_object( - chat_id, as_search_messages_filter(filter), from_message_id, as_limit(limit), get_saved_messages_topic())); + chat_id, as_search_messages_filter(filter), from_message_id, as_limit(limit), get_saved_messages_topic_id())); } else if (op == "gcmc") { ChatId chat_id; string filter; bool return_local; get_args(args, chat_id, filter, return_local); send_request(td_api::make_object(chat_id, as_search_messages_filter(filter), - get_saved_messages_topic(), return_local)); + get_saved_messages_topic_id(), return_local)); } else if (op == "gcmp") { ChatId chat_id; MessageId message_id; string filter; get_args(args, chat_id, message_id, filter); send_request(td_api::make_object( - chat_id, message_id, as_search_messages_filter(filter), message_thread_id_, get_saved_messages_topic())); + chat_id, message_id, as_search_messages_filter(filter), message_thread_id_, get_saved_messages_topic_id())); } else if (op == "gup" || op == "gupp") { UserId user_id; int32 offset; @@ -4681,7 +4675,7 @@ class CliClient final : public Actor { get_args(args, link_preview_is_disabled_, link_preview_url_, link_preview_force_small_media_, link_preview_force_large_media_, link_preview_show_above_text_); } else if (op == "ssmt") { - saved_messages_topic_ = as_chat_id(args); + saved_messages_topic_id_ = as_chat_id(args); } else if (op == "sm" || op == "sms" || op == "smf") { ChatId chat_id; string message; @@ -6547,7 +6541,7 @@ class CliClient final : public Actor { bool link_preview_force_small_media_ = false; bool link_preview_force_large_media_ = false; bool link_preview_show_above_text_ = false; - int64 saved_messages_topic_ = 0; + int64 saved_messages_topic_id_ = 0; ConcurrentScheduler *scheduler_{nullptr};