diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 58cee59c8..5afec7bd2 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7192,6 +7192,11 @@ deleteSavedMessagesTopicHistory saved_messages_topic:SavedMessagesTopic = Ok; //@max_date The maximum date of the messages to delete deleteSavedMessagesTopicMessagesByDate saved_messages_topic:SavedMessagesTopic 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 +//@is_pinned Pass true to pin the topic; pass false to unpin it +toggleSavedMessagesTopicIsPinned saved_messages_topic:SavedMessagesTopic is_pinned:Bool = Ok; + //@description Returns a list of common group chats with a given user. Chats are sorted by their type and creation date //@user_id User identifier diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 77246266b..d91a022b7 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -349,6 +349,40 @@ class GetSavedMessageByDateQuery final : public Td::ResultHandler { } }; +class ToggleSavedDialogPinQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit ToggleSavedDialogPinQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(SavedMessagesTopicId saved_messages_topic_id, bool is_pinned) { + auto saved_input_peer = saved_messages_topic_id.get_input_dialog_peer(td_); + CHECK(saved_input_peer != nullptr); + + int32 flags = 0; + if (is_pinned) { + flags |= telegram_api::messages_toggleSavedDialogPin::PINNED_MASK; + } + send_query(G()->net_query_creator().create( + telegram_api::messages_toggleSavedDialogPin(flags, false /*ignored*/, std::move(saved_input_peer)))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + td_->messages_manager_->on_update_pinned_saved_messages_topics(); + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class GetDialogUnreadMarksQuery final : public Td::ResultHandler { public: void send() { @@ -16292,6 +16326,12 @@ void MessagesManager::delete_saved_messages_topic_messages_by_date(SavedMessages run_affected_history_query_until_complete(my_dialog_id, std::move(query), true, std::move(promise)); } +void MessagesManager::toggle_saved_messages_topic_is_pinned(SavedMessagesTopicId saved_messages_topic_id, + bool is_pinned, Promise &&promise) { + TRY_STATUS_PROMISE(promise, saved_messages_topic_id.is_valid_status(td_)); + td_->create_handler(std::move(promise))->send(saved_messages_topic_id, is_pinned); +} + void MessagesManager::on_update_pinned_saved_messages_topics() { if (td_->auth_manager_->is_bot()) { // just in case diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 3a31af95a..ea4c0988b 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -549,6 +549,9 @@ class MessagesManager final : public Actor { void delete_saved_messages_topic_messages_by_date(SavedMessagesTopicId saved_messages_topic_id, int32 min_date, int32 max_date, Promise &&promise); + void toggle_saved_messages_topic_is_pinned(SavedMessagesTopicId saved_messages_topic_id, bool is_pinned, + Promise &&promise); + void on_update_pinned_saved_messages_topics(); vector search_public_dialogs(const string &query, Promise &&promise); diff --git a/td/telegram/SavedMessagesTopicId.cpp b/td/telegram/SavedMessagesTopicId.cpp index d250650f3..520f85330 100644 --- a/td/telegram/SavedMessagesTopicId.cpp +++ b/td/telegram/SavedMessagesTopicId.cpp @@ -106,6 +106,11 @@ telegram_api::object_ptr SavedMessagesTopicId::get_inpu return td->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Know); } +telegram_api::object_ptr SavedMessagesTopicId::get_input_dialog_peer( + const Td *td) const { + return telegram_api::make_object(get_input_peer(td)); +} + void SavedMessagesTopicId::add_dependencies(Dependencies &dependencies) const { if (dialog_id_ == HIDDEN_AUTHOR_DIALOG_ID) { dependencies.add_dialog_dependencies(dialog_id_); diff --git a/td/telegram/SavedMessagesTopicId.h b/td/telegram/SavedMessagesTopicId.h index f20481e73..391fec120 100644 --- a/td/telegram/SavedMessagesTopicId.h +++ b/td/telegram/SavedMessagesTopicId.h @@ -53,6 +53,8 @@ class SavedMessagesTopicId { telegram_api::object_ptr get_input_peer(const Td *td) const; + telegram_api::object_ptr get_input_dialog_peer(const Td *td) const; + void add_dependencies(Dependencies &dependencies) const; template diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 187e2e128..82736cd6d 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5064,6 +5064,13 @@ void Td::on_request(uint64 id, const td_api::deleteSavedMessagesTopicMessagesByD std::move(promise)); } +void Td::on_request(uint64 id, const td_api::toggleSavedMessagesTopicIsPinned &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + messages_manager_->toggle_saved_messages_topic_is_pinned(SavedMessagesTopicId(this, request.saved_messages_topic_), + request.is_pinned_, std::move(promise)); +} + void Td::on_request(uint64 id, td_api::searchPublicChat &request) { CLEAN_INPUT_STRING(request.username_); CREATE_REQUEST(SearchPublicChatRequest, request.username_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index ff8d33006..2f5fd40d4 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -683,6 +683,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::deleteSavedMessagesTopicMessagesByDate &request); + void on_request(uint64 id, const td_api::toggleSavedMessagesTopicIsPinned &request); + void on_request(uint64 id, td_api::searchPublicChat &request); void on_request(uint64 id, td_api::searchPublicChats &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index e836ca9d7..f227a2ece 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2775,6 +2775,11 @@ class CliClient final : public Actor { get_args(args, min_date, max_date); send_request(td_api::make_object(get_saved_messages_topic(), 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)); } else if (op == "gcc" || op == "GetCommonChats") { UserId user_id; ChatId offset_chat_id;