From d98aaa571f7acbde9af527131096b9a224185055 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 14 Feb 2019 21:44:20 +0300 Subject: [PATCH] Add td_api::getChatNotificationSettingsExceptions. GitOrigin-RevId: f2ea5ced7cb2c62841dd961c4ce433f39c202f92 --- td/generate/scheme/td_api.tl | 3 + td/generate/scheme/td_api.tlo | Bin 144672 -> 144824 bytes td/telegram/ContactsManager.cpp | 2 +- td/telegram/MessagesManager.cpp | 63 +++++++++++++++++--- td/telegram/MessagesManager.h | 3 +- td/telegram/Td.cpp | 45 ++++++++++++++ td/telegram/Td.h | 2 + td/telegram/UpdatesManager.cpp | 100 +++++++++++++++++--------------- td/telegram/UpdatesManager.h | 11 +++- td/telegram/cli.cpp | 6 ++ 10 files changed, 176 insertions(+), 59 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2e909e022..f1028398b 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3047,6 +3047,9 @@ getChatAdministrators chat_id:int53 = Users; clearAllDraftMessages exclude_secret_chats:Bool = Ok; +//@description Returns list of chats with non-default notification settings @scope If specified, only chats from the specified scope will be returned @compare_sound If true, also chats with non-default sound will be returned +getChatNotificationSettingsExceptions scope:NotificationSettingsScope compare_sound:Bool = Chats; + //@description Returns the notification settings for chats of a given type @scope Types of chats for which to return the notification settings information getScopeNotificationSettings scope:NotificationSettingsScope = ScopeNotificationSettings; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 920478f7cce3fbefd7dc668dc27f86cd55c0821e..33113b797597a8f3ca402ab32627bb7df997bf60 100644 GIT binary patch delta 82 zcmV-Y0ImO^>Ik^&2!ON!+?4@Fx80QiA2$dQ4*m-zXJvGg-31sBMR;RnaCB*JZgT(t o4dZ5;mxyBlEC>x_Z*6d4a%GdOSR9kkTnLv?&H)g&{LBF)XganZ=Kufz delta 25 hcmdn-nq$E$j)pCaccwDBZofN~QQmy}l(UQ~833Rn3r_$5 diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 6a46c6a1d..f55d1ad8c 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1505,7 +1505,7 @@ class ImportDialogInviteLinkQuery : public Td::ResultHandler { auto ptr = result_ptr.move_as_ok(); LOG(INFO) << "Receive result for importChatInvite: " << to_string(ptr); - auto dialog_ids = td->updates_manager_->get_chats(ptr.get()); + auto dialog_ids = UpdatesManager::get_chat_dialog_ids(ptr.get()); if (dialog_ids.size() != 1u) { LOG(ERROR) << "Receive wrong result for ImportDialogInviteLinkQuery: " << to_string(ptr); return on_error(id, Status::Error(500, "Internal Server Error")); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index ab568f902..b12c24c65 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -1932,7 +1932,7 @@ class SendMultiMediaActor : public NetActorOnce { auto ptr = result_ptr.move_as_ok(); LOG(INFO) << "Receive result for sendMultiMedia for " << format::as_array(random_ids_) << ": " << to_string(ptr); - auto sent_random_ids = td->updates_manager_->get_sent_messages_random_ids(ptr.get()); + auto sent_random_ids = UpdatesManager::get_sent_messages_random_ids(ptr.get()); bool is_result_wrong = false; auto sent_random_ids_size = sent_random_ids.size(); for (auto &random_id : random_ids_) { @@ -1950,7 +1950,7 @@ class SendMultiMediaActor : public NetActorOnce { is_result_wrong = true; } if (!is_result_wrong) { - auto sent_messages = td->updates_manager_->get_new_messages(ptr.get()); + auto sent_messages = UpdatesManager::get_new_messages(ptr.get()); if (sent_random_ids_size != sent_messages.size()) { is_result_wrong = true; } @@ -2562,7 +2562,7 @@ class ForwardMessagesActor : public NetActorOnce { auto ptr = result_ptr.move_as_ok(); LOG(INFO) << "Receive result for forwardMessages for " << format::as_array(random_ids_) << ": " << to_string(ptr); - auto sent_random_ids = td->updates_manager_->get_sent_messages_random_ids(ptr.get()); + auto sent_random_ids = UpdatesManager::get_sent_messages_random_ids(ptr.get()); bool is_result_wrong = false; auto sent_random_ids_size = sent_random_ids.size(); for (auto &random_id : random_ids_) { @@ -2580,7 +2580,7 @@ class ForwardMessagesActor : public NetActorOnce { is_result_wrong = true; } if (!is_result_wrong) { - auto sent_messages = td->updates_manager_->get_new_messages(ptr.get()); + auto sent_messages = UpdatesManager::get_new_messages(ptr.get()); if (sent_random_ids_size != sent_messages.size()) { is_result_wrong = true; } @@ -2846,6 +2846,47 @@ class GetDialogNotifySettingsQuery : public Td::ResultHandler { } }; +class GetNotifySettingsExceptionsQuery : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetNotifySettingsExceptionsQuery(Promise> &&promise) : promise_(std::move(promise)) { + } + + void send(NotificationSettingsScope scope, bool filter_scope, bool compare_sound) { + // +account.getNotifyExceptions flags:# compare_sound:flags.1?true peer:flags.0?InputNotifyPeer = Updates; + + int32 flags = 0; + tl_object_ptr input_notify_peer; + if (filter_scope) { + flags |= telegram_api::account_getNotifyExceptions::PEER_MASK; + input_notify_peer = get_input_notify_peer(scope); + } + if (compare_sound) { + flags |= telegram_api::account_getNotifyExceptions::COMPARE_SOUND_MASK; + } + send_query(G()->net_query_creator().create(create_storer( + telegram_api::account_getNotifyExceptions(flags, false /* ignored */, std::move(input_notify_peer))))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + auto updates_ptr = result_ptr.move_as_ok(); + auto dialog_ids = UpdatesManager::get_update_notify_settings_dialog_ids(updates_ptr.get()); + td->updates_manager_->on_get_updates(std::move(updates_ptr)); + + promise_.set_value(std::move(dialog_ids)); + } + + void on_error(uint64 id, Status status) override { + promise_.set_error(std::move(status)); + } +}; + class GetScopeNotifySettingsQuery : public Td::ResultHandler { Promise promise_; NotificationSettingsScope scope_; @@ -12863,6 +12904,12 @@ int32 MessagesManager::get_scope_mute_until(DialogId dialog_id) const { } } +void MessagesManager::get_dialog_notification_settings_exceptions(NotificationSettingsScope scope, bool filter_scope, + bool compare_sound, + Promise> &&promise) { + td_->create_handler(std::move(promise))->send(scope, filter_scope, compare_sound); +} + const ScopeNotificationSettings *MessagesManager::get_scope_notification_settings(NotificationSettingsScope scope, Promise &&promise) { const ScopeNotificationSettings *notification_settings = get_scope_notification_settings(scope); @@ -18780,8 +18827,8 @@ void MessagesManager::check_send_message_result(int64 random_id, DialogId dialog const telegram_api::Updates *updates_ptr, const char *source) { CHECK(updates_ptr != nullptr); CHECK(source != nullptr); - auto sent_messages = td_->updates_manager_->get_new_messages(updates_ptr); - auto sent_messages_random_ids = td_->updates_manager_->get_sent_messages_random_ids(updates_ptr); + auto sent_messages = UpdatesManager::get_new_messages(updates_ptr); + auto sent_messages_random_ids = UpdatesManager::get_sent_messages_random_ids(updates_ptr); if (sent_messages.size() != 1u || sent_messages_random_ids.size() != 1u || *sent_messages_random_ids.begin() != random_id || get_message_dialog_id(*sent_messages[0]) != dialog_id) { LOG(ERROR) << "Receive wrong result for sending message with random_id " << random_id << " from " << source @@ -19503,8 +19550,8 @@ void MessagesManager::set_dialog_pinned_message_id(Dialog *d, MessageId pinned_m void MessagesManager::on_create_new_dialog_success(int64 random_id, tl_object_ptr &&updates, DialogType expected_type, Promise &&promise) { - auto sent_messages = td_->updates_manager_->get_new_messages(updates.get()); - auto sent_messages_random_ids = td_->updates_manager_->get_sent_messages_random_ids(updates.get()); + auto sent_messages = UpdatesManager::get_new_messages(updates.get()); + auto sent_messages_random_ids = UpdatesManager::get_sent_messages_random_ids(updates.get()); if (sent_messages.size() != 1u || sent_messages_random_ids.size() != 1u) { LOG(ERROR) << "Receive wrong result for create group or channel chat " << oneline(to_string(updates)); return on_create_new_dialog_fail(random_id, Status::Error(500, "Unsupported server response"), std::move(promise)); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 71d32e1e7..83b70a08a 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -519,7 +519,8 @@ class MessagesManager : public Actor { td_api::object_ptr get_update_scope_notification_settings_object( NotificationSettingsScope scope) const; - const DialogNotificationSettings *get_dialog_notification_settings(DialogId dialog_id, Promise &&promise); + void get_dialog_notification_settings_exceptions(NotificationSettingsScope scope, bool filter_scope, + bool compare_sound, Promise> &&promise); const ScopeNotificationSettings *get_scope_notification_settings(NotificationSettingsScope scope, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 9728da385..7bd4356d2 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2086,6 +2086,40 @@ class GetUserProfilePhotosRequest : public RequestActor<> { } }; +class GetChatNotificationSettingsExceptionsRequest : public RequestActor> { + NotificationSettingsScope scope_; + bool filter_scope_; + bool compare_sound_; + + vector dialog_ids_; + + void do_run(Promise> &&promise) override { + if (get_tries() < 2) { + promise.set_value(std::move(dialog_ids_)); + return; + } + td->messages_manager_->get_dialog_notification_settings_exceptions(scope_, filter_scope_, compare_sound_, + std::move(promise)); + } + + void do_set_result(vector &&result) override { + dialog_ids_ = std::move(result); + } + + void do_send_result() override { + send_result(MessagesManager::get_chats_object(dialog_ids_)); + } + + public: + GetChatNotificationSettingsExceptionsRequest(ActorShared td, uint64 request_id, NotificationSettingsScope scope, + bool filter_scope, bool compare_sound) + : RequestActor(std::move(td), request_id) + , scope_(scope) + , filter_scope_(filter_scope) + , compare_sound_(compare_sound) { + } +}; + class GetScopeNotificationSettingsRequest : public RequestActor<> { NotificationSettingsScope scope_; @@ -6119,6 +6153,17 @@ void Td::on_request(uint64 id, td_api::removeSavedAnimation &request) { CREATE_REQUEST(RemoveSavedAnimationRequest, std::move(request.animation_)); } +void Td::on_request(uint64 id, const td_api::getChatNotificationSettingsExceptions &request) { + CHECK_IS_USER(); + bool filter_scope = false; + NotificationSettingsScope scope = NotificationSettingsScope::Private; + if (request.scope_ != nullptr) { + filter_scope = true; + scope = get_notification_settings_scope(request.scope_); + } + CREATE_REQUEST(GetChatNotificationSettingsExceptionsRequest, scope, filter_scope, request.compare_sound_); +} + void Td::on_request(uint64 id, const td_api::getScopeNotificationSettings &request) { CHECK_IS_USER(); if (request.scope_ == nullptr) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 90cba5558..140be9e9c 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -789,6 +789,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, td_api::removeFavoriteSticker &request); + void on_request(uint64 id, const td_api::getChatNotificationSettingsExceptions &request); + void on_request(uint64 id, const td_api::getScopeNotificationSettings &request); void on_request(uint64 id, td_api::setChatNotificationSettings &request); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index b6ee0b61b..7bc5c5174 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -780,9 +780,8 @@ void UpdatesManager::on_get_updates_state(tl_object_ptr UpdatesManager::get_sent_messages_random_ids(const telegram_api::Updates *updates_ptr) { - std::unordered_set random_ids; - const vector> *updates; +const vector> *UpdatesManager::get_updates( + const telegram_api::Updates *updates_ptr) { switch (updates_ptr->get_id()) { case telegram_api::updatesTooLong::ID: case telegram_api::updateShortMessage::ID: @@ -790,24 +789,27 @@ std::unordered_set UpdatesManager::get_sent_messages_random_ids(const tel case telegram_api::updateShort::ID: case telegram_api::updateShortSentMessage::ID: LOG(ERROR) << "Receive " << oneline(to_string(*updates_ptr)) << " instead of updates"; - return random_ids; - case telegram_api::updatesCombined::ID: { - updates = &static_cast(updates_ptr)->updates_; - break; - } - case telegram_api::updates::ID: { - updates = &static_cast(updates_ptr)->updates_; - break; - } + return nullptr; + case telegram_api::updatesCombined::ID: + return &static_cast(updates_ptr)->updates_; + case telegram_api::updates::ID: + return &static_cast(updates_ptr)->updates_; default: UNREACHABLE(); - return random_ids; + return nullptr; } - for (auto &update : *updates) { - if (update->get_id() == telegram_api::updateMessageID::ID) { - int64 random_id = static_cast(update.get())->random_id_; - if (!random_ids.insert(random_id).second) { - LOG(ERROR) << "Receive twice updateMessageID for " << random_id; +} + +std::unordered_set UpdatesManager::get_sent_messages_random_ids(const telegram_api::Updates *updates_ptr) { + std::unordered_set random_ids; + auto updates = get_updates(updates_ptr); + if (updates != nullptr) { + for (auto &update : *updates) { + if (update->get_id() == telegram_api::updateMessageID::ID) { + int64 random_id = static_cast(update.get())->random_id_; + if (!random_ids.insert(random_id).second) { + LOG(ERROR) << "Receive twice updateMessageID for " << random_id; + } } } } @@ -817,39 +819,45 @@ std::unordered_set UpdatesManager::get_sent_messages_random_ids(const tel vector *> UpdatesManager::get_new_messages( const telegram_api::Updates *updates_ptr) { vector *> messages; - const vector> *updates; - switch (updates_ptr->get_id()) { - case telegram_api::updatesTooLong::ID: - case telegram_api::updateShortMessage::ID: - case telegram_api::updateShortChatMessage::ID: - case telegram_api::updateShort::ID: - case telegram_api::updateShortSentMessage::ID: - LOG(ERROR) << "Receive " << oneline(to_string(*updates_ptr)) << " instead of updates"; - return messages; - case telegram_api::updatesCombined::ID: { - updates = &static_cast(updates_ptr)->updates_; - break; - } - case telegram_api::updates::ID: { - updates = &static_cast(updates_ptr)->updates_; - break; - } - default: - UNREACHABLE(); - return messages; - } - for (auto &update : *updates) { - auto constructor_id = update->get_id(); - if (constructor_id == telegram_api::updateNewMessage::ID) { - messages.emplace_back(&static_cast(update.get())->message_); - } else if (constructor_id == telegram_api::updateNewChannelMessage::ID) { - messages.emplace_back(&static_cast(update.get())->message_); + auto updates = get_updates(updates_ptr); + if (updates != nullptr) { + for (auto &update : *updates) { + auto constructor_id = update->get_id(); + if (constructor_id == telegram_api::updateNewMessage::ID) { + messages.emplace_back(&static_cast(update.get())->message_); + } else if (constructor_id == telegram_api::updateNewChannelMessage::ID) { + messages.emplace_back(&static_cast(update.get())->message_); + } } } return messages; } -vector UpdatesManager::get_chats(const telegram_api::Updates *updates_ptr) { +vector UpdatesManager::get_update_notify_settings_dialog_ids(const telegram_api::Updates *updates_ptr) { + vector dialog_ids; + auto updates = get_updates(updates_ptr); + if (updates != nullptr) { + dialog_ids.reserve(updates->size()); + for (auto &update : *updates) { + DialogId dialog_id; + if (update->get_id() == telegram_api::updateNotifySettings::ID) { + auto notify_peer = static_cast(update.get())->peer_.get(); + if (notify_peer->get_id() == telegram_api::notifyPeer::ID) { + dialog_id = DialogId(static_cast(notify_peer)->peer_); + } + } + + if (dialog_id.is_valid()) { + dialog_ids.push_back(dialog_id); + } else { + LOG(ERROR) << "Receive unexpected " << to_string(update); + } + } + } + return dialog_ids; +} + +vector UpdatesManager::get_chat_dialog_ids(const telegram_api::Updates *updates_ptr) { const vector> *chats = nullptr; switch (updates_ptr->get_id()) { case telegram_api::updatesTooLong::ID: diff --git a/td/telegram/UpdatesManager.h b/td/telegram/UpdatesManager.h index a6d2e4922..1ae4016d2 100644 --- a/td/telegram/UpdatesManager.h +++ b/td/telegram/UpdatesManager.h @@ -38,11 +38,14 @@ class UpdatesManager : public Actor { void on_get_difference(tl_object_ptr &&difference_ptr); - std::unordered_set get_sent_messages_random_ids(const telegram_api::Updates *updates_ptr); + static std::unordered_set get_sent_messages_random_ids(const telegram_api::Updates *updates_ptr); - vector *> get_new_messages(const telegram_api::Updates *updates_ptr); + static vector *> get_new_messages( + const telegram_api::Updates *updates_ptr); - vector get_chats(const telegram_api::Updates *updates_ptr); + static vector get_update_notify_settings_dialog_ids(const telegram_api::Updates *updates_ptr); + + static vector get_chat_dialog_ids(const telegram_api::Updates *updates_ptr); void get_difference(const char *source); @@ -176,6 +179,8 @@ class UpdatesManager : public Actor { void after_get_difference(); + static const vector> *get_updates(const telegram_api::Updates *updates_ptr); + bool is_acceptable_message_entities(const vector> &message_entities) const; bool is_acceptable_message(const telegram_api::Message *message_ptr) const; diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index d282cdef0..1df268919 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -886,6 +886,9 @@ class CliClient final : public Actor { } static tl_object_ptr get_notification_settings_scope(Slice scope) { + if (scope.empty()) { + return nullptr; + } if (scope == "channels" || scope == "ch") { return make_tl_object(); } @@ -3397,6 +3400,9 @@ class CliClient final : public Actor { send_request(make_tl_object(as_chat_id(chat_id))); } else if (op == "dpp") { send_request(make_tl_object(to_integer(args))); + } else if (op == "gcnse" || op == "gcnses") { + send_request(make_tl_object(get_notification_settings_scope(args), + op == "gcnses")); } else if (op == "gsns") { send_request(make_tl_object(get_notification_settings_scope(args))); } else if (op == "scns" || op == "ssns") {