From a56c34c4ddf3fd9a71479a3b866ae3e057ced2f3 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Nov 2021 01:18:47 +0300 Subject: [PATCH 1/7] Move get_message_sender_object to MessageSender.cpp. --- CMakeLists.txt | 2 + td/telegram/ContactsManager.cpp | 3 +- td/telegram/DialogEventLog.cpp | 15 +++----- td/telegram/GroupCallManager.cpp | 11 +++--- td/telegram/GroupCallParticipant.cpp | 4 +- td/telegram/MessageContent.cpp | 6 +-- td/telegram/MessageSender.cpp | 57 ++++++++++++++++++++++++++++ td/telegram/MessageSender.h | 30 +++++++++++++++ td/telegram/MessagesManager.cpp | 54 +++----------------------- td/telegram/MessagesManager.h | 11 ------ td/telegram/NotificationManager.cpp | 1 + td/telegram/NotificationType.cpp | 5 ++- 12 files changed, 116 insertions(+), 83 deletions(-) create mode 100644 td/telegram/MessageSender.cpp create mode 100644 td/telegram/MessageSender.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 64f4debca..f463608c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -357,6 +357,7 @@ set(TDLIB_SOURCE td/telegram/MessageReplyInfo.cpp td/telegram/MessagesDb.cpp td/telegram/MessageSearchFilter.cpp + td/telegram/MessageSender.cpp td/telegram/MessagesManager.cpp td/telegram/MessageTtlSetting.cpp td/telegram/misc.cpp @@ -558,6 +559,7 @@ set(TDLIB_SOURCE td/telegram/MessageThreadInfo.h td/telegram/MessagesDb.h td/telegram/MessageSearchFilter.h + td/telegram/MessageSender.h td/telegram/MessagesManager.h td/telegram/MessageTtlSetting.h td/telegram/misc.h diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 7b3a5de18..fd7de0f81 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -24,6 +24,7 @@ #include "td/telegram/LinkManager.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/logevent/LogEventHelper.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/MessageTtlSetting.h" #include "td/telegram/misc.h" @@ -11833,7 +11834,7 @@ tl_object_ptr ContactsManager::get_chat_member_object( td_->messages_manager_->force_create_dialog(dialog_id, "get_chat_member_object", true); } return td_api::make_object( - td_->messages_manager_->get_message_sender_object_const(dialog_id, "get_chat_member_object"), + get_message_sender_object_const(td_, dialog_id, "get_chat_member_object"), get_user_id_object(dialog_participant.inviter_user_id_, "chatMember.inviter_user_id"), dialog_participant.joined_date_, dialog_participant.status_.get_chat_member_status_object()); } diff --git a/td/telegram/DialogEventLog.cpp b/td/telegram/DialogEventLog.cpp index 14f90bb0f..d05e4bc04 100644 --- a/td/telegram/DialogEventLog.cpp +++ b/td/telegram/DialogEventLog.cpp @@ -15,6 +15,7 @@ #include "td/telegram/GroupCallManager.h" #include "td/telegram/GroupCallParticipant.h" #include "td/telegram/InputGroupCallId.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/MessageTtlSetting.h" #include "td/telegram/StickersManager.h" @@ -86,8 +87,7 @@ static td_api::object_ptr get_chat_event_action_object( return nullptr; } return td_api::make_object( - td->messages_manager_->get_message_sender_object(old_dialog_participant.dialog_id_, - "chatEventMemberRestricted"), + get_message_sender_object(td, old_dialog_participant.dialog_id_, "chatEventMemberRestricted"), old_dialog_participant.status_.get_chat_member_status_object(), new_dialog_participant.status_.get_chat_member_status_object()); } @@ -294,9 +294,7 @@ static td_api::object_ptr get_chat_event_action_object( return nullptr; } return td_api::make_object( - td->messages_manager_->get_message_sender_object(participant.dialog_id, - "chatEventVideoChatParticipantIsMutedToggled"), - true); + get_message_sender_object(td, participant.dialog_id, "chatEventVideoChatParticipantIsMutedToggled"), true); } case telegram_api::channelAdminLogEventActionParticipantUnmute::ID: { auto action = move_tl_object_as(action_ptr); @@ -305,9 +303,7 @@ static td_api::object_ptr get_chat_event_action_object( return nullptr; } return td_api::make_object( - td->messages_manager_->get_message_sender_object(participant.dialog_id, - "chatEventVideoChatParticipantIsMutedToggled"), - false); + get_message_sender_object(td, participant.dialog_id, "chatEventVideoChatParticipantIsMutedToggled"), false); } case telegram_api::channelAdminLogEventActionParticipantVolume::ID: { auto action = move_tl_object_as(action_ptr); @@ -316,8 +312,7 @@ static td_api::object_ptr get_chat_event_action_object( return nullptr; } return td_api::make_object( - td->messages_manager_->get_message_sender_object(participant.dialog_id, - "chatEventVideoChatParticipantVolumeLevelChanged"), + get_message_sender_object(td, participant.dialog_id, "chatEventVideoChatParticipantVolumeLevelChanged"), participant.volume_level); } case telegram_api::channelAdminLogEventActionToggleGroupCallSetting::ID: { diff --git a/td/telegram/GroupCallManager.cpp b/td/telegram/GroupCallManager.cpp index 3f623d9a2..82706a00f 100644 --- a/td/telegram/GroupCallManager.cpp +++ b/td/telegram/GroupCallManager.cpp @@ -12,6 +12,7 @@ #include "td/telegram/DialogAction.h" #include "td/telegram/Global.h" #include "td/telegram/MessageId.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/misc.h" #include "td/telegram/net/DcId.h" @@ -113,8 +114,7 @@ class GetGroupCallJoinAsQuery final : public Td::ResultHandler { td_->messages_manager_->force_create_dialog(dialog_id, "GetGroupCallJoinAsQuery"); } - participant_aliaces.push_back( - td_->messages_manager_->get_message_sender_object(dialog_id, "GetGroupCallJoinAsQuery")); + participant_aliaces.push_back(get_message_sender_object(td_, dialog_id, "GetGroupCallJoinAsQuery")); } promise_.set_value(td_api::make_object(static_cast(participant_aliaces.size()), @@ -4647,11 +4647,10 @@ vector> GroupCallManager::get recent_speaker_update_timeout_.add_timeout_in(group_call->group_call_id.get(), next_timeout); } - auto get_result = [recent_speaker_users, messages_manager = td_->messages_manager_.get()] { - return transform(recent_speaker_users, [messages_manager](const std::pair &recent_speaker_user) { + auto get_result = [recent_speaker_users, td = td_] { + return transform(recent_speaker_users, [td](const std::pair &recent_speaker_user) { return td_api::make_object( - messages_manager->get_message_sender_object(recent_speaker_user.first, "get_recent_speakers"), - recent_speaker_user.second); + get_message_sender_object(td, recent_speaker_user.first, "get_recent_speakers"), recent_speaker_user.second); }); }; if (recent_speakers->last_sent_users != recent_speaker_users) { diff --git a/td/telegram/GroupCallParticipant.cpp b/td/telegram/GroupCallParticipant.cpp index d0b1ba795..7a1de78b5 100644 --- a/td/telegram/GroupCallParticipant.cpp +++ b/td/telegram/GroupCallParticipant.cpp @@ -7,7 +7,7 @@ #include "td/telegram/GroupCallParticipant.h" #include "td/telegram/Global.h" -#include "td/telegram/MessagesManager.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/Td.h" #include "td/utils/logging.h" @@ -266,7 +266,7 @@ td_api::object_ptr GroupCallParticipant::get_group } return td_api::make_object( - td->messages_manager_->get_message_sender_object(dialog_id, "get_group_call_participant_object"), audio_source, + get_message_sender_object(td, dialog_id, "get_group_call_participant_object"), audio_source, presentation_audio_source, video_payload.get_group_call_participant_video_info_object(), presentation_payload.get_group_call_participant_video_info_object(), about, is_self, is_speaking, get_is_hand_raised(), can_be_muted_for_all_users, can_be_unmuted_for_all_users, can_be_muted_only_for_self, diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 109937d43..bf02834b7 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -39,6 +39,7 @@ #include "td/telegram/MessageEntity.hpp" #include "td/telegram/MessageId.h" #include "td/telegram/MessageSearchFilter.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/misc.h" #include "td/telegram/net/DcId.h" @@ -4994,9 +4995,8 @@ tl_object_ptr get_message_content_object(const MessageCo case MessageContentType::ProximityAlertTriggered: { const auto *m = static_cast(content); return make_tl_object( - td->messages_manager_->get_message_sender_object(m->traveler_dialog_id, "messageProximityAlertTriggered 1"), - td->messages_manager_->get_message_sender_object(m->watcher_dialog_id, "messageProximityAlertTriggered 2"), - m->distance); + get_message_sender_object(td, m->traveler_dialog_id, "messageProximityAlertTriggered 1"), + get_message_sender_object(td, m->watcher_dialog_id, "messageProximityAlertTriggered 2"), m->distance); } case MessageContentType::GroupCall: { const auto *m = static_cast(content); diff --git a/td/telegram/MessageSender.cpp b/td/telegram/MessageSender.cpp new file mode 100644 index 000000000..e25dff561 --- /dev/null +++ b/td/telegram/MessageSender.cpp @@ -0,0 +1,57 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// +// 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) +// +#include "td/telegram/MessageSender.h" + +#include "td/telegram/AuthManager.h" +#include "td/telegram/ContactsManager.h" +#include "td/telegram/MessagesManager.h" +#include "td/telegram/Td.h" + +namespace td { + +td_api::object_ptr get_message_sender_object_const(Td *td, UserId user_id, DialogId dialog_id, + const char *source) { + if (dialog_id.is_valid() && td->messages_manager_->have_dialog(dialog_id)) { + return td_api::make_object(dialog_id.get()); + } + if (!user_id.is_valid()) { + // can happen only if the server sends a message with wrong sender + LOG(ERROR) << "Receive message with wrong sender " << user_id << '/' << dialog_id << " from " << source; + user_id = td->contacts_manager_->add_service_notifications_user(); + } + return td_api::make_object(td->contacts_manager_->get_user_id_object(user_id, source)); +} + +td_api::object_ptr get_message_sender_object_const(Td *td, DialogId dialog_id, + const char *source) { + if (dialog_id.get_type() == DialogType::User) { + return get_message_sender_object_const(td, dialog_id.get_user_id(), DialogId(), source); + } + return get_message_sender_object_const(td, UserId(), dialog_id, source); +} + +td_api::object_ptr get_message_sender_object(Td *td, UserId user_id, DialogId dialog_id, + const char *source) { + if (dialog_id.is_valid() && !td->messages_manager_->have_dialog(dialog_id)) { + LOG(ERROR) << "Failed to find " << dialog_id; + td->messages_manager_->force_create_dialog(dialog_id, source); + } + if (!user_id.is_valid() && td->auth_manager_->is_bot()) { + td->contacts_manager_->add_anonymous_bot_user(); + td->contacts_manager_->add_service_notifications_user(); + } + return get_message_sender_object_const(td, user_id, dialog_id, source); +} + +td_api::object_ptr get_message_sender_object(Td *td, DialogId dialog_id, const char *source) { + if (dialog_id.get_type() == DialogType::User) { + return get_message_sender_object(td, dialog_id.get_user_id(), DialogId(), source); + } + return get_message_sender_object(td, UserId(), dialog_id, source); +} + +} // namespace td diff --git a/td/telegram/MessageSender.h b/td/telegram/MessageSender.h new file mode 100644 index 000000000..2427d34da --- /dev/null +++ b/td/telegram/MessageSender.h @@ -0,0 +1,30 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// +// 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 + +#include "td/telegram/DialogId.h" +#include "td/telegram/td_api.h" +#include "td/telegram/UserId.h" + +#include "td/utils/common.h" + +namespace td { + +class Td; + +td_api::object_ptr get_message_sender_object_const(Td *td, UserId user_id, DialogId dialog_id, + const char *source); + +td_api::object_ptr get_message_sender_object_const(Td *td, DialogId dialog_id, + const char *source); + +td_api::object_ptr get_message_sender_object(Td *td, UserId user_id, DialogId dialog_id, + const char *source); + +td_api::object_ptr get_message_sender_object(Td *td, DialogId dialog_id, const char *source); + +} // namespace td diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index d535f639c..9fb676ce6 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -33,6 +33,7 @@ #include "td/telegram/MessageEntity.h" #include "td/telegram/MessageEntity.hpp" #include "td/telegram/MessagesDb.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/misc.h" #include "td/telegram/net/DcId.h" #include "td/telegram/net/NetActor.h" @@ -2184,7 +2185,7 @@ class GetSearchResultCalendarQuery final : public Td::ResultHandler { } void on_error(Status status) final { - td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "SearchMessagesQuery"); + td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetSearchResultCalendarQuery"); td_->messages_manager_->on_failed_get_message_search_result_calendar(dialog_id_, random_id_); promise_.set_error(std::move(status)); } @@ -5859,49 +5860,6 @@ void MessagesManager::on_preload_folder_dialog_list_timeout_callback(void *messa FolderId(narrow_cast(folder_id_int))); } -td_api::object_ptr MessagesManager::get_message_sender_object_const(UserId user_id, - DialogId dialog_id, - const char *source) const { - if (dialog_id.is_valid() && have_dialog(dialog_id)) { - return td_api::make_object(dialog_id.get()); - } - if (!user_id.is_valid()) { - // can happen only if the server sends a message with wrong sender - LOG(ERROR) << "Receive message with wrong sender " << user_id << '/' << dialog_id << " from " << source; - user_id = td_->contacts_manager_->add_service_notifications_user(); - } - return td_api::make_object(td_->contacts_manager_->get_user_id_object(user_id, source)); -} - -td_api::object_ptr MessagesManager::get_message_sender_object(UserId user_id, DialogId dialog_id, - const char *source) { - if (dialog_id.is_valid() && !have_dialog(dialog_id)) { - LOG(ERROR) << "Failed to find " << dialog_id; - force_create_dialog(dialog_id, source); - } - if (!user_id.is_valid() && td_->auth_manager_->is_bot()) { - td_->contacts_manager_->add_anonymous_bot_user(); - td_->contacts_manager_->add_service_notifications_user(); - } - return get_message_sender_object_const(user_id, dialog_id, source); -} - -td_api::object_ptr MessagesManager::get_message_sender_object_const(DialogId dialog_id, - const char *source) const { - if (dialog_id.get_type() == DialogType::User) { - return get_message_sender_object_const(dialog_id.get_user_id(), DialogId(), source); - } - return get_message_sender_object_const(UserId(), dialog_id, source); -} - -td_api::object_ptr MessagesManager::get_message_sender_object(DialogId dialog_id, - const char *source) { - if (dialog_id.get_type() == DialogType::User) { - return get_message_sender_object(dialog_id.get_user_id(), DialogId(), source); - } - return get_message_sender_object(UserId(), dialog_id, source); -} - BufferSlice MessagesManager::get_dialog_database_value(const Dialog *d) { // can't use log_event_store, because it tries to parse stored Dialog LogEventStorerCalcLength storer_calc_length; @@ -16940,8 +16898,8 @@ void MessagesManager::on_get_blocked_dialogs(int32 offset, int32 limit, int32 to total_count = offset + narrow_cast(dialog_ids.size()); } - auto senders = transform(dialog_ids, [this](DialogId dialog_id) { - return get_message_sender_object(dialog_id, "on_get_blocked_dialogs"); + auto senders = transform(dialog_ids, [td = td_](DialogId dialog_id) { + return get_message_sender_object(td, dialog_id, "on_get_blocked_dialogs"); }); promise.set_value(td_api::make_object(total_count, std::move(senders))); } @@ -20248,7 +20206,7 @@ td_api::object_ptr MessagesManager::get_video_chat_object(con auto active_group_call_id = td_->group_call_manager_->get_group_call_id(d->active_group_call_id, d->dialog_id); auto default_participant_alias = d->default_join_group_call_as_dialog_id.is_valid() - ? get_message_sender_object_const(d->default_join_group_call_as_dialog_id, "get_video_chat_object") + ? get_message_sender_object_const(td_, d->default_join_group_call_as_dialog_id, "get_video_chat_object") : nullptr; return make_tl_object(active_group_call_id.get(), active_group_call_id.is_valid() ? !d->is_group_call_empty : false, @@ -23553,7 +23511,7 @@ tl_object_ptr MessagesManager::get_message_object(DialogId dial } else { ttl = 0; } - auto sender = get_message_sender_object_const(m->sender_user_id, m->sender_dialog_id, source); + auto sender = get_message_sender_object_const(td_, m->sender_user_id, m->sender_dialog_id, source); auto scheduling_state = is_scheduled ? get_message_scheduling_state_object(m->date) : nullptr; auto forward_info = get_message_forward_info_object(m->forward_info); auto interaction_info = get_message_interaction_info_object(dialog_id, m); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 521506743..087837efd 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -142,17 +142,6 @@ class MessagesManager final : public Actor { MessagesManager &operator=(MessagesManager &&) = delete; ~MessagesManager() final; - td_api::object_ptr get_message_sender_object_const(UserId user_id, DialogId dialog_id, - const char *source) const; - - td_api::object_ptr get_message_sender_object(UserId user_id, DialogId dialog_id, - const char *source); - - td_api::object_ptr get_message_sender_object_const(DialogId dialog_id, - const char *source) const; - - td_api::object_ptr get_message_sender_object(DialogId dialog_id, const char *source); - static vector get_message_ids(const vector &input_message_ids); static vector get_server_message_ids(const vector &message_ids); diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index a777b4433..031c79e3e 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -18,6 +18,7 @@ #include "td/telegram/files/FileManager.h" #include "td/telegram/Global.h" #include "td/telegram/logevent/LogEvent.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/misc.h" #include "td/telegram/net/ConnectionCreator.h" diff --git a/td/telegram/NotificationType.cpp b/td/telegram/NotificationType.cpp index 459fcfd7b..872ab87d2 100644 --- a/td/telegram/NotificationType.cpp +++ b/td/telegram/NotificationType.cpp @@ -10,6 +10,7 @@ #include "td/telegram/AudiosManager.h" #include "td/telegram/DocumentsManager.h" #include "td/telegram/Global.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/StickersManager.h" #include "td/telegram/Td.h" @@ -335,8 +336,8 @@ class NotificationTypePushMessage final : public NotificationType { } td_api::object_ptr get_notification_type_object(DialogId dialog_id) const final { - auto sender = G()->td().get_actor_unsafe()->messages_manager_->get_message_sender_object( - sender_user_id_, sender_dialog_id_, "get_notification_type_object"); + auto sender = get_message_sender_object(G()->td().get_actor_unsafe(), sender_user_id_, sender_dialog_id_, + "get_notification_type_object"); return td_api::make_object( message_id_.get(), std::move(sender), sender_name_, is_outgoing_, get_push_message_content_object(key_, arg_, photo_, document_)); From 5987ca69ff4ce1c9a2ba3728e776568395701d27 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Nov 2021 12:07:52 +0300 Subject: [PATCH 2/7] Move get_message_sender_dialog_id to MessageSender.cpp. --- td/telegram/ContactsManager.cpp | 49 +++++---------------------------- td/telegram/ContactsManager.h | 13 ++++----- td/telegram/MessageSender.cpp | 25 +++++++++++++++++ td/telegram/MessageSender.h | 3 ++ td/telegram/Td.cpp | 23 +++++++++------- td/telegram/Td.h | 6 ++-- 6 files changed, 57 insertions(+), 62 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index fd7de0f81..d801e0164 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -14846,32 +14846,6 @@ std::pair> ContactsManager::search_among_dialogs(const v return {narrow_cast(result.first), transform(result.second, [](int64 key) { return DialogId(key); })}; } -Result ContactsManager::get_participant_dialog_id( - const td_api::object_ptr &participant_id) { - if (participant_id == nullptr) { - return Status::Error(400, "Member identifier is not specified"); - } - switch (participant_id->get_id()) { - case td_api::messageSenderUser::ID: { - auto user_id = UserId(static_cast(participant_id.get())->user_id_); - if (!user_id.is_valid()) { - return Status::Error(400, "Invalid user identifier specified"); - } - return DialogId(user_id); - } - case td_api::messageSenderChat::ID: { - auto dialog_id = DialogId(static_cast(participant_id.get())->chat_id_); - if (!dialog_id.is_valid()) { - return Status::Error(400, "Invalid chat identifier specified"); - } - return dialog_id; - } - default: - UNREACHABLE(); - return DialogId(); - } -} - void ContactsManager::add_dialog_participant(DialogId dialog_id, UserId user_id, int32 forward_limit, Promise &&promise) { if (!td_->messages_manager_->have_dialog_force(dialog_id, "add_dialog_participant")) { @@ -14915,12 +14889,9 @@ void ContactsManager::add_dialog_participants(DialogId dialog_id, const vector &participant_id, +void ContactsManager::set_dialog_participant_status(DialogId dialog_id, DialogId participant_dialog_id, const tl_object_ptr &chat_member_status, Promise &&promise) { - TRY_RESULT_PROMISE(promise, participant_dialog_id, get_participant_dialog_id(participant_id)); - auto status = get_dialog_participant_status(chat_member_status); if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_dialog_participant_status")) { return promise.set_error(Status::Error(400, "Chat not found")); @@ -14950,11 +14921,8 @@ void ContactsManager::set_dialog_participant_status(DialogId dialog_id, } } -void ContactsManager::ban_dialog_participant(DialogId dialog_id, - const tl_object_ptr &participant_id, +void ContactsManager::ban_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, int32 banned_until_date, bool revoke_messages, Promise &&promise) { - TRY_RESULT_PROMISE(promise, participant_dialog_id, get_participant_dialog_id(participant_id)); - if (!td_->messages_manager_->have_dialog_force(dialog_id, "ban_dialog_participant")) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -14979,18 +14947,15 @@ void ContactsManager::ban_dialog_participant(DialogId dialog_id, } } -void ContactsManager::get_dialog_participant(DialogId dialog_id, - const tl_object_ptr &participant_id, +void ContactsManager::get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, Promise> &&promise) { - TRY_RESULT_PROMISE(promise, participant_dialog_id, get_participant_dialog_id(participant_id)); - auto new_promise = PromiseCreator::lambda( [actor_id = actor_id(this), promise = std::move(promise)](Result &&result) mutable { TRY_RESULT_PROMISE(promise, dialog_participant, std::move(result)); send_closure(actor_id, &ContactsManager::finish_get_dialog_participant, std::move(dialog_participant), std::move(promise)); }); - get_dialog_participant(dialog_id, participant_dialog_id, std::move(new_promise)); + do_get_dialog_participant(dialog_id, participant_dialog_id, std::move(new_promise)); } void ContactsManager::finish_get_dialog_participant(DialogParticipant &&dialog_participant, @@ -15007,10 +14972,10 @@ void ContactsManager::finish_get_dialog_participant(DialogParticipant &&dialog_p promise.set_value(get_chat_member_object(dialog_participant)); } -void ContactsManager::get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, - Promise &&promise) { +void ContactsManager::do_get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, + Promise &&promise) { LOG(INFO) << "Receive GetChatMember request to get " << participant_dialog_id << " in " << dialog_id; - if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_dialog_participant")) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "do_get_dialog_participant")) { return promise.set_error(Status::Error(400, "Chat not found")); } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 7bc092927..0d86b2aad 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -526,14 +526,14 @@ class ContactsManager final : public Actor { void add_dialog_participants(DialogId dialog_id, const vector &user_ids, Promise &&promise); - void set_dialog_participant_status(DialogId dialog_id, const tl_object_ptr &participant_id, + void set_dialog_participant_status(DialogId dialog_id, DialogId participant_dialog_id, const tl_object_ptr &chat_member_status, Promise &&promise); - void ban_dialog_participant(DialogId dialog_id, const tl_object_ptr &participant_id, - int32 banned_until_date, bool revoke_messages, Promise &&promise); + void ban_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, int32 banned_until_date, + bool revoke_messages, Promise &&promise); - void get_dialog_participant(DialogId dialog_id, const tl_object_ptr &participant_id, + void get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, Promise> &&promise); void search_dialog_participants(DialogId dialog_id, const string &query, int32 limit, DialogParticipantsFilter filter, @@ -1419,8 +1419,6 @@ class ContactsManager final : public Actor { bool update_permanent_invite_link(DialogInviteLink &invite_link, DialogInviteLink new_invite_link); - static Result get_participant_dialog_id(const td_api::object_ptr &participant_id); - void add_chat_participant(ChatId chat_id, UserId user_id, int32 forward_limit, Promise &&promise); void add_channel_participant(ChannelId channel_id, UserId user_id, const DialogParticipantStatus &old_status, @@ -1441,7 +1439,8 @@ class ContactsManager final : public Actor { DialogParticipants search_private_chat_participants(UserId my_user_id, UserId peer_user_id, const string &query, int32 limit, DialogParticipantsFilter filter) const; - void get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, Promise &&promise); + void do_get_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, + Promise &&promise); void finish_get_dialog_participant(DialogParticipant &&dialog_participant, Promise> &&promise); diff --git a/td/telegram/MessageSender.cpp b/td/telegram/MessageSender.cpp index e25dff561..1b0ddfb17 100644 --- a/td/telegram/MessageSender.cpp +++ b/td/telegram/MessageSender.cpp @@ -54,4 +54,29 @@ td_api::object_ptr get_message_sender_object(Td *td, Dial return get_message_sender_object(td, UserId(), dialog_id, source); } +Result get_message_sender_dialog_id(const td_api::object_ptr &message_sender_id) { + if (message_sender_id == nullptr) { + return Status::Error(400, "Member identifier is not specified"); + } + switch (message_sender_id->get_id()) { + case td_api::messageSenderUser::ID: { + auto user_id = UserId(static_cast(message_sender_id.get())->user_id_); + if (!user_id.is_valid()) { + return Status::Error(400, "Invalid user identifier specified"); + } + return DialogId(user_id); + } + case td_api::messageSenderChat::ID: { + auto dialog_id = DialogId(static_cast(message_sender_id.get())->chat_id_); + if (!dialog_id.is_valid()) { + return Status::Error(400, "Invalid chat identifier specified"); + } + return dialog_id; + } + default: + UNREACHABLE(); + return DialogId(); + } +} + } // namespace td diff --git a/td/telegram/MessageSender.h b/td/telegram/MessageSender.h index 2427d34da..abf7b8074 100644 --- a/td/telegram/MessageSender.h +++ b/td/telegram/MessageSender.h @@ -11,6 +11,7 @@ #include "td/telegram/UserId.h" #include "td/utils/common.h" +#include "td/utils/Status.h" namespace td { @@ -27,4 +28,6 @@ td_api::object_ptr get_message_sender_object(Td *td, User td_api::object_ptr get_message_sender_object(Td *td, DialogId dialog_id, const char *source); +Result get_message_sender_dialog_id(const td_api::object_ptr &message_sender_id); + } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 667efb006..86fc2196a 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -60,6 +60,7 @@ #include "td/telegram/MessageId.h" #include "td/telegram/MessageLinkInfo.h" #include "td/telegram/MessageSearchFilter.h" +#include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/MessageThreadInfo.h" #include "td/telegram/misc.h" @@ -6227,9 +6228,8 @@ void Td::on_request(uint64 id, const td_api::leaveChat &request) { td_api::make_object(status.get_rank(), status.is_anonymous(), false); } } - contacts_manager_->set_dialog_participant_status( - dialog_id, td_api::make_object(contacts_manager_->get_my_id().get()), new_status, - std::move(promise)); + contacts_manager_->set_dialog_participant_status(dialog_id, DialogId(contacts_manager_->get_my_id()), new_status, + std::move(promise)); } void Td::on_request(uint64 id, const td_api::addChatMember &request) { @@ -6246,16 +6246,18 @@ void Td::on_request(uint64 id, const td_api::addChatMembers &request) { std::move(promise)); } -void Td::on_request(uint64 id, td_api::setChatMemberStatus &request) { +void Td::on_request(uint64 id, const td_api::setChatMemberStatus &request) { CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->set_dialog_participant_status(DialogId(request.chat_id_), request.member_id_, request.status_, + TRY_RESULT_PROMISE(promise, participant_dialog_id, get_message_sender_dialog_id(request.member_id_)); + contacts_manager_->set_dialog_participant_status(DialogId(request.chat_id_), participant_dialog_id, request.status_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::banChatMember &request) { CREATE_OK_REQUEST_PROMISE(); - contacts_manager_->ban_dialog_participant(DialogId(request.chat_id_), request.member_id_, request.banned_until_date_, - request.revoke_messages_, std::move(promise)); + TRY_RESULT_PROMISE(promise, participant_dialog_id, get_message_sender_dialog_id(request.member_id_)); + contacts_manager_->ban_dialog_participant(DialogId(request.chat_id_), participant_dialog_id, + request.banned_until_date_, request.revoke_messages_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::canTransferOwnership &request) { @@ -6280,9 +6282,10 @@ void Td::on_request(uint64 id, td_api::transferChatOwnership &request) { std::move(promise)); } -void Td::on_request(uint64 id, td_api::getChatMember &request) { +void Td::on_request(uint64 id, const td_api::getChatMember &request) { CREATE_REQUEST_PROMISE(); - contacts_manager_->get_dialog_participant(DialogId(request.chat_id_), request.member_id_, std::move(promise)); + TRY_RESULT_PROMISE(promise, participant_dialog_id, get_message_sender_dialog_id(request.member_id_)); + contacts_manager_->get_dialog_participant(DialogId(request.chat_id_), participant_dialog_id, std::move(promise)); } void Td::on_request(uint64 id, td_api::searchChatMembers &request) { @@ -6300,7 +6303,7 @@ void Td::on_request(uint64 id, td_api::searchChatMembers &request) { DialogParticipantsFilter(request.filter_), std::move(query_promise)); } -void Td::on_request(uint64 id, td_api::getChatAdministrators &request) { +void Td::on_request(uint64 id, const td_api::getChatAdministrators &request) { CREATE_REQUEST(GetChatAdministratorsRequest, request.chat_id_); } diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 27e7e2863..bb27a8a89 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -852,7 +852,7 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::addChatMembers &request); - void on_request(uint64 id, td_api::setChatMemberStatus &request); + void on_request(uint64 id, const td_api::setChatMemberStatus &request); void on_request(uint64 id, const td_api::banChatMember &request); @@ -860,11 +860,11 @@ class Td final : public Actor { void on_request(uint64 id, td_api::transferChatOwnership &request); - void on_request(uint64 id, td_api::getChatMember &request); + void on_request(uint64 id, const td_api::getChatMember &request); void on_request(uint64 id, td_api::searchChatMembers &request); - void on_request(uint64 id, td_api::getChatAdministrators &request); + void on_request(uint64 id, const td_api::getChatAdministrators &request); void on_request(uint64 id, const td_api::replacePrimaryChatInviteLink &request); From 21c8c752be236af25039646b1719212bd64dcf3d Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Nov 2021 12:52:40 +0300 Subject: [PATCH 3/7] Add get_message_sender_dialog_ids. --- td/telegram/GroupCallManager.cpp | 17 +------------ td/telegram/MessageSender.cpp | 41 ++++++++++++++++++++++++++++++++ td/telegram/MessageSender.h | 6 +++++ td/telegram/MessagesManager.cpp | 25 ++++--------------- 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/td/telegram/GroupCallManager.cpp b/td/telegram/GroupCallManager.cpp index 82706a00f..8586daafe 100644 --- a/td/telegram/GroupCallManager.cpp +++ b/td/telegram/GroupCallManager.cpp @@ -103,22 +103,7 @@ class GetGroupCallJoinAsQuery final : public Td::ResultHandler { td_->contacts_manager_->on_get_users(std::move(ptr->users_), "GetGroupCallJoinAsQuery"); td_->contacts_manager_->on_get_chats(std::move(ptr->chats_), "GetGroupCallJoinAsQuery"); - vector> participant_aliaces; - for (auto &peer : ptr->peers_) { - DialogId dialog_id(peer); - if (!dialog_id.is_valid()) { - LOG(ERROR) << "Receive invalid " << dialog_id << " as join as peer for " << dialog_id_; - continue; - } - if (dialog_id.get_type() != DialogType::User) { - td_->messages_manager_->force_create_dialog(dialog_id, "GetGroupCallJoinAsQuery"); - } - - participant_aliaces.push_back(get_message_sender_object(td_, dialog_id, "GetGroupCallJoinAsQuery")); - } - - promise_.set_value(td_api::make_object(static_cast(participant_aliaces.size()), - std::move(participant_aliaces))); + promise_.set_value(convert_message_senders_object(td_, ptr->peers_)); } void on_error(Status status) final { diff --git a/td/telegram/MessageSender.cpp b/td/telegram/MessageSender.cpp index 1b0ddfb17..dceb8f4fb 100644 --- a/td/telegram/MessageSender.cpp +++ b/td/telegram/MessageSender.cpp @@ -11,6 +11,9 @@ #include "td/telegram/MessagesManager.h" #include "td/telegram/Td.h" +#include "td/utils/algorithm.h" +#include "td/utils/misc.h" + namespace td { td_api::object_ptr get_message_sender_object_const(Td *td, UserId user_id, DialogId dialog_id, @@ -54,6 +57,44 @@ td_api::object_ptr get_message_sender_object(Td *td, Dial return get_message_sender_object(td, UserId(), dialog_id, source); } +vector get_message_sender_dialog_ids(Td *td, + const vector> &peers) { + vector message_sender_dialog_ids; + message_sender_dialog_ids.reserve(peers.size()); + for (auto &peer : peers) { + DialogId dialog_id(peer); + if (!dialog_id.is_valid()) { + LOG(ERROR) << "Receive invalid " << dialog_id << " as message sender"; + continue; + } + if (dialog_id.get_type() == DialogType::User) { + if (!td->contacts_manager_->have_user(dialog_id.get_user_id())) { + LOG(ERROR) << "Have no info about " << dialog_id.get_user_id(); + continue; + } + } else { + if (!td->messages_manager_->have_dialog_info(dialog_id)) { + continue; + } + td->messages_manager_->force_create_dialog(dialog_id, "get_message_sender_dialog_ids"); + if (!td->messages_manager_->have_dialog(dialog_id)) { + continue; + } + } + message_sender_dialog_ids.push_back(dialog_id); + } + return message_sender_dialog_ids; +} + +td_api::object_ptr convert_message_senders_object( + Td *td, const vector> &peers) { + auto dialog_ids = get_message_sender_dialog_ids(td, peers); + auto message_senders = transform(dialog_ids, [td](DialogId dialog_id) { + return get_message_sender_object(td, dialog_id, "convert_message_senders_object"); + }); + return td_api::make_object(narrow_cast(dialog_ids.size()), std::move(message_senders)); +} + Result get_message_sender_dialog_id(const td_api::object_ptr &message_sender_id) { if (message_sender_id == nullptr) { return Status::Error(400, "Member identifier is not specified"); diff --git a/td/telegram/MessageSender.h b/td/telegram/MessageSender.h index abf7b8074..74a6c82bf 100644 --- a/td/telegram/MessageSender.h +++ b/td/telegram/MessageSender.h @@ -28,6 +28,12 @@ td_api::object_ptr get_message_sender_object(Td *td, User td_api::object_ptr get_message_sender_object(Td *td, DialogId dialog_id, const char *source); +vector get_message_sender_dialog_ids(Td *td, + const vector> &peers); + +td_api::object_ptr convert_message_senders_object( + Td *td, const vector> &peers); + Result get_message_sender_dialog_id(const td_api::object_ptr &message_sender_id); } // namespace td diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 9fb676ce6..05c489f78 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -16872,27 +16872,10 @@ void MessagesManager::on_get_blocked_dialogs(int32 offset, int32 limit, int32 to Promise> &&promise) { LOG(INFO) << "Receive " << blocked_peers.size() << " blocked chats from offset " << offset << " out of " << total_count; - vector dialog_ids; - for (auto &blocked_peer : blocked_peers) { - CHECK(blocked_peer != nullptr); - DialogId dialog_id(blocked_peer->peer_id_); - if (dialog_id.get_type() == DialogType::User) { - if (td_->contacts_manager_->have_user(dialog_id.get_user_id())) { - dialog_ids.push_back(dialog_id); - } else { - LOG(ERROR) << "Have no info about " << dialog_id.get_user_id(); - } - } else { - if (have_dialog_info(dialog_id)) { - force_create_dialog(dialog_id, "on_get_blocked_dialogs"); - if (have_dialog(dialog_id)) { - dialog_ids.push_back(dialog_id); - } - } else { - LOG(ERROR) << "Have no info about " << dialog_id; - } - } - } + auto peers = transform(std::move(blocked_peers), [](tl_object_ptr &&blocked_peer) { + return std::move(blocked_peer->peer_id_); + }); + auto dialog_ids = get_message_sender_dialog_ids(td_, std::move(peers)); if (!dialog_ids.empty() && offset + dialog_ids.size() > static_cast(total_count)) { LOG(ERROR) << "Fix total count of blocked chats from " << total_count << " to " << offset + dialog_ids.size(); total_count = offset + narrow_cast(dialog_ids.size()); From 1a291847d7ac237bfe1e69da0113910d36fbe06e Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Nov 2021 13:31:07 +0300 Subject: [PATCH 4/7] Improve get_message_sender_dialog_id. --- td/telegram/GroupCallManager.cpp | 26 --------------------- td/telegram/GroupCallManager.h | 2 -- td/telegram/MessageSender.cpp | 19 ++++++++++++++- td/telegram/MessageSender.h | 4 +++- td/telegram/Td.cpp | 40 +++++++++++++++++++------------- 5 files changed, 45 insertions(+), 46 deletions(-) diff --git a/td/telegram/GroupCallManager.cpp b/td/telegram/GroupCallManager.cpp index 8586daafe..9beec6c29 100644 --- a/td/telegram/GroupCallManager.cpp +++ b/td/telegram/GroupCallManager.cpp @@ -1069,32 +1069,6 @@ void GroupCallManager::on_sync_participants_timeout(GroupCallId group_call_id) { sync_group_call_participants(input_group_call_id); } -DialogId GroupCallManager::get_group_call_participant_id( - const td_api::object_ptr &message_sender) { - if (message_sender == nullptr) { - return DialogId(); - } - switch (message_sender->get_id()) { - case td_api::messageSenderUser::ID: { - UserId user_id(static_cast(message_sender.get())->user_id_); - if (td_->contacts_manager_->have_user_force(user_id)) { - return DialogId(user_id); - } - break; - } - case td_api::messageSenderChat::ID: { - DialogId dialog_id(static_cast(message_sender.get())->chat_id_); - if (td_->messages_manager_->have_dialog_force(dialog_id, "get_group_call_participant_id")) { - return dialog_id; - } - break; - } - default: - UNREACHABLE(); - } - return DialogId(); -} - bool GroupCallManager::is_group_call_being_joined(InputGroupCallId input_group_call_id) const { return pending_join_requests_.count(input_group_call_id) != 0; } diff --git a/td/telegram/GroupCallManager.h b/td/telegram/GroupCallManager.h index 398028733..f2a8ed821 100644 --- a/td/telegram/GroupCallManager.h +++ b/td/telegram/GroupCallManager.h @@ -39,8 +39,6 @@ class GroupCallManager final : public Actor { GroupCallManager &operator=(GroupCallManager &&) = delete; ~GroupCallManager() final; - DialogId get_group_call_participant_id(const td_api::object_ptr &message_sender); - bool is_group_call_being_joined(InputGroupCallId input_group_call_id) const; bool is_group_call_joined(InputGroupCallId input_group_call_id) const; diff --git a/td/telegram/MessageSender.cpp b/td/telegram/MessageSender.cpp index dceb8f4fb..8ff7ed527 100644 --- a/td/telegram/MessageSender.cpp +++ b/td/telegram/MessageSender.cpp @@ -95,23 +95,40 @@ td_api::object_ptr convert_message_senders_object( return td_api::make_object(narrow_cast(dialog_ids.size()), std::move(message_senders)); } -Result get_message_sender_dialog_id(const td_api::object_ptr &message_sender_id) { +Result get_message_sender_dialog_id(Td *td, + const td_api::object_ptr &message_sender_id, + bool check_access, bool allow_empty) { if (message_sender_id == nullptr) { + if (allow_empty) { + return DialogId(); + } return Status::Error(400, "Member identifier is not specified"); } switch (message_sender_id->get_id()) { case td_api::messageSenderUser::ID: { auto user_id = UserId(static_cast(message_sender_id.get())->user_id_); if (!user_id.is_valid()) { + if (allow_empty && user_id == UserId()) { + return DialogId(); + } return Status::Error(400, "Invalid user identifier specified"); } + if (check_access && !td->contacts_manager_->have_user_force(user_id)) { + return Status::Error(400, "Unknown user identifier specified"); + } return DialogId(user_id); } case td_api::messageSenderChat::ID: { auto dialog_id = DialogId(static_cast(message_sender_id.get())->chat_id_); if (!dialog_id.is_valid()) { + if (allow_empty && dialog_id == DialogId()) { + return DialogId(); + } return Status::Error(400, "Invalid chat identifier specified"); } + if (check_access && !td->messages_manager_->have_dialog_force(dialog_id, "get_message_sender_dialog_id")) { + return Status::Error(400, "Unknown chat identifier specified"); + } return dialog_id; } default: diff --git a/td/telegram/MessageSender.h b/td/telegram/MessageSender.h index 74a6c82bf..3c066353a 100644 --- a/td/telegram/MessageSender.h +++ b/td/telegram/MessageSender.h @@ -34,6 +34,8 @@ vector get_message_sender_dialog_ids(Td *td, td_api::object_ptr convert_message_senders_object( Td *td, const vector> &peers); -Result get_message_sender_dialog_id(const td_api::object_ptr &message_sender_id); +Result get_message_sender_dialog_id(Td *td, + const td_api::object_ptr &message_sender_id, + bool check_access, bool allow_empty); } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 86fc2196a..e8c0fadfd 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5800,9 +5800,10 @@ void Td::on_request(uint64 id, const td_api::getVideoChatAvailableParticipants & void Td::on_request(uint64 id, const td_api::setVideoChatDefaultParticipant &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - group_call_manager_->set_group_call_default_join_as( - DialogId(request.chat_id_), group_call_manager_->get_group_call_participant_id(request.default_participant_id_), - std::move(promise)); + TRY_RESULT_PROMISE(promise, default_join_as_dialog_id, + get_message_sender_dialog_id(this, request.default_participant_id_, true, false)); + group_call_manager_->set_group_call_default_join_as(DialogId(request.chat_id_), default_join_as_dialog_id, + std::move(promise)); } void Td::on_request(uint64 id, td_api::createVideoChat &request) { @@ -5844,6 +5845,8 @@ void Td::on_request(uint64 id, td_api::joinGroupCall &request) { CLEAN_INPUT_STRING(request.invite_hash_); CLEAN_INPUT_STRING(request.payload_); CREATE_REQUEST_PROMISE(); + TRY_RESULT_PROMISE(promise, join_as_dialog_id, + get_message_sender_dialog_id(this, request.participant_id_, true, true)); auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result result) mutable { if (result.is_error()) { promise.set_error(result.move_as_error()); @@ -5851,10 +5854,9 @@ void Td::on_request(uint64 id, td_api::joinGroupCall &request) { promise.set_value(make_tl_object(result.move_as_ok())); } }); - group_call_manager_->join_group_call(GroupCallId(request.group_call_id_), - group_call_manager_->get_group_call_participant_id(request.participant_id_), - request.audio_source_id_, std::move(request.payload_), request.is_muted_, - request.is_my_video_enabled_, request.invite_hash_, std::move(query_promise)); + group_call_manager_->join_group_call(GroupCallId(request.group_call_id_), join_as_dialog_id, request.audio_source_id_, + std::move(request.payload_), request.is_muted_, request.is_my_video_enabled_, + request.invite_hash_, std::move(query_promise)); } void Td::on_request(uint64 id, td_api::startGroupCallScreenSharing &request) { @@ -5967,25 +5969,28 @@ void Td::on_request(uint64 id, const td_api::setGroupCallParticipantIsSpeaking & void Td::on_request(uint64 id, const td_api::toggleGroupCallParticipantIsMuted &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); + TRY_RESULT_PROMISE(promise, participant_dialog_id, + get_message_sender_dialog_id(this, request.participant_id_, true, false)); group_call_manager_->toggle_group_call_participant_is_muted( - GroupCallId(request.group_call_id_), group_call_manager_->get_group_call_participant_id(request.participant_id_), - request.is_muted_, std::move(promise)); + GroupCallId(request.group_call_id_), participant_dialog_id, request.is_muted_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::setGroupCallParticipantVolumeLevel &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); + TRY_RESULT_PROMISE(promise, participant_dialog_id, + get_message_sender_dialog_id(this, request.participant_id_, true, false)); group_call_manager_->set_group_call_participant_volume_level( - GroupCallId(request.group_call_id_), group_call_manager_->get_group_call_participant_id(request.participant_id_), - request.volume_level_, std::move(promise)); + GroupCallId(request.group_call_id_), participant_dialog_id, request.volume_level_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::toggleGroupCallParticipantIsHandRaised &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); + TRY_RESULT_PROMISE(promise, participant_dialog_id, + get_message_sender_dialog_id(this, request.participant_id_, true, false)); group_call_manager_->toggle_group_call_participant_is_hand_raised( - GroupCallId(request.group_call_id_), group_call_manager_->get_group_call_participant_id(request.participant_id_), - request.is_hand_raised_, std::move(promise)); + GroupCallId(request.group_call_id_), participant_dialog_id, request.is_hand_raised_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::loadGroupCallParticipants &request) { @@ -6248,14 +6253,16 @@ void Td::on_request(uint64 id, const td_api::addChatMembers &request) { void Td::on_request(uint64 id, const td_api::setChatMemberStatus &request) { CREATE_OK_REQUEST_PROMISE(); - TRY_RESULT_PROMISE(promise, participant_dialog_id, get_message_sender_dialog_id(request.member_id_)); + TRY_RESULT_PROMISE(promise, participant_dialog_id, + get_message_sender_dialog_id(this, request.member_id_, false, false)); contacts_manager_->set_dialog_participant_status(DialogId(request.chat_id_), participant_dialog_id, request.status_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::banChatMember &request) { CREATE_OK_REQUEST_PROMISE(); - TRY_RESULT_PROMISE(promise, participant_dialog_id, get_message_sender_dialog_id(request.member_id_)); + TRY_RESULT_PROMISE(promise, participant_dialog_id, + get_message_sender_dialog_id(this, request.member_id_, false, false)); contacts_manager_->ban_dialog_participant(DialogId(request.chat_id_), participant_dialog_id, request.banned_until_date_, request.revoke_messages_, std::move(promise)); } @@ -6284,7 +6291,8 @@ void Td::on_request(uint64 id, td_api::transferChatOwnership &request) { void Td::on_request(uint64 id, const td_api::getChatMember &request) { CREATE_REQUEST_PROMISE(); - TRY_RESULT_PROMISE(promise, participant_dialog_id, get_message_sender_dialog_id(request.member_id_)); + TRY_RESULT_PROMISE(promise, participant_dialog_id, + get_message_sender_dialog_id(this, request.member_id_, false, false)); contacts_manager_->get_dialog_participant(DialogId(request.chat_id_), participant_dialog_id, std::move(promise)); } From b75d190e81b3e9db169f57b951982178eebe770d Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Nov 2021 14:00:30 +0300 Subject: [PATCH 5/7] Use get_message_sender_dialog_id in toggle_message_sender_is_blocked. --- td/telegram/MessageSender.cpp | 10 ++++-- td/telegram/MessagesManager.cpp | 56 ++++++++++----------------------- 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/td/telegram/MessageSender.cpp b/td/telegram/MessageSender.cpp index 8ff7ed527..7ff83f39e 100644 --- a/td/telegram/MessageSender.cpp +++ b/td/telegram/MessageSender.cpp @@ -102,7 +102,7 @@ Result get_message_sender_dialog_id(Td *td, if (allow_empty) { return DialogId(); } - return Status::Error(400, "Member identifier is not specified"); + return Status::Error(400, "Message sender must be non-empty"); } switch (message_sender_id->get_id()) { case td_api::messageSenderUser::ID: { @@ -126,8 +126,12 @@ Result get_message_sender_dialog_id(Td *td, } return Status::Error(400, "Invalid chat identifier specified"); } - if (check_access && !td->messages_manager_->have_dialog_force(dialog_id, "get_message_sender_dialog_id")) { - return Status::Error(400, "Unknown chat identifier specified"); + if (check_access) { + bool is_user = dialog_id.get_type() == DialogType::User; + if (is_user ? !td->contacts_manager_->have_user_force(dialog_id.get_user_id()) + : !td->messages_manager_->have_dialog_force(dialog_id, "get_message_sender_dialog_id")) { + return Status::Error(400, "Unknown chat identifier specified"); + } } return dialog_id; } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 05c489f78..50b745215 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -19202,52 +19202,30 @@ void MessagesManager::toggle_dialog_is_marked_as_unread_on_server(DialogId dialo Status MessagesManager::toggle_message_sender_is_blocked(const td_api::object_ptr &sender, bool is_blocked) { - if (sender == nullptr) { - return Status::Error(400, "Message sender must be non-empty"); - } - - UserId sender_user_id; - DialogId dialog_id; - switch (sender->get_id()) { - case td_api::messageSenderUser::ID: - sender_user_id = UserId(static_cast(sender.get())->user_id_); + TRY_RESULT(dialog_id, get_message_sender_dialog_id(td_, sender, true, false)); + switch (dialog_id.get_type()) { + case DialogType::User: + if (dialog_id == get_my_dialog_id()) { + return Status::Error(400, is_blocked ? Slice("Can't block self") : Slice("Can't unblock self")); + } break; - case td_api::messageSenderChat::ID: { - auto sender_dialog_id = DialogId(static_cast(sender.get())->chat_id_); - if (!have_dialog_force(sender_dialog_id, "toggle_message_sender_is_blocked")) { - return Status::Error(400, "Sender chat not found"); - } - - switch (sender_dialog_id.get_type()) { - case DialogType::User: - sender_user_id = sender_dialog_id.get_user_id(); - break; - case DialogType::Chat: - return Status::Error(400, "Basic group chats can't be blocked"); - case DialogType::Channel: - dialog_id = sender_dialog_id; - break; - case DialogType::SecretChat: - sender_user_id = td_->contacts_manager_->get_secret_chat_user_id(sender_dialog_id.get_secret_chat_id()); - break; - case DialogType::None: - default: - UNREACHABLE(); + case DialogType::Chat: + return Status::Error(400, "Basic group chats can't be blocked"); + case DialogType::Channel: + // ok + break; + case DialogType::SecretChat: { + auto user_id = td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id()); + if (!user_id.is_valid() || !td_->contacts_manager_->have_user_force(user_id)) { + return Status::Error(400, "The secret chat can't be blocked"); } + dialog_id = DialogId(user_id); break; } + case DialogType::None: default: UNREACHABLE(); } - if (!dialog_id.is_valid()) { - if (!td_->contacts_manager_->have_user_force(sender_user_id)) { - return Status::Error(400, "Sender user not found"); - } - dialog_id = DialogId(sender_user_id); - } - if (dialog_id == get_my_dialog_id()) { - return Status::Error(400, is_blocked ? Slice("Can't block self") : Slice("Can't unblock self")); - } Dialog *d = get_dialog_force(dialog_id, "toggle_message_sender_is_blocked"); if (!have_input_peer(dialog_id, AccessRights::Know)) { From 76e1466abd4b9baefdd764530c1df0d28ed7eeb1 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Nov 2021 15:04:48 +0300 Subject: [PATCH 6/7] Use get_message_sender_dialog_id in search_dialog_messages. --- td/telegram/MessagesManager.cpp | 76 ++++++++++----------------------- 1 file changed, 22 insertions(+), 54 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 50b745215..9261d1067 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -21401,44 +21401,18 @@ std::pair> MessagesManager::search_dialog_messages( } if (!have_input_peer(dialog_id, AccessRights::Read)) { promise.set_error(Status::Error(400, "Can't access the chat")); - return {}; + return result; } - DialogId sender_dialog_id; - if (sender != nullptr) { - switch (sender->get_id()) { - case td_api::messageSenderUser::ID: - sender_dialog_id = DialogId(UserId(static_cast(sender.get())->user_id_)); - break; - case td_api::messageSenderChat::ID: - sender_dialog_id = DialogId(static_cast(sender.get())->chat_id_); - switch (sender_dialog_id.get_type()) { - case DialogType::User: - case DialogType::Chat: - case DialogType::Channel: - // ok - break; - case DialogType::SecretChat: - promise.set_value(Unit()); - return result; - case DialogType::None: - if (sender_dialog_id == DialogId()) { - break; - } - promise.set_error(Status::Error(400, "Invalid sender chat identifier specified")); - return result; - default: - UNREACHABLE(); - return result; - } - break; - default: - UNREACHABLE(); - } - if (sender_dialog_id != DialogId() && !have_input_peer(sender_dialog_id, AccessRights::Read)) { - promise.set_error(Status::Error(400, "Invalid message sender specified")); - return result; - } + auto r_sender_dialog_id = get_message_sender_dialog_id(td_, sender, true, true); + if (r_sender_dialog_id.is_error()) { + promise.set_error(r_sender_dialog_id.move_as_error()); + return result; + } + auto sender_dialog_id = r_sender_dialog_id.move_as_ok(); + if (sender_dialog_id != DialogId() && !have_input_peer(sender_dialog_id, AccessRights::Read)) { + promise.set_error(Status::Error(400, "Invalid message sender specified")); + return result; } if (sender_dialog_id == dialog_id && is_broadcast_channel(dialog_id)) { sender_dialog_id = DialogId(); @@ -21463,6 +21437,11 @@ std::pair> MessagesManager::search_dialog_messages( } } + if (sender_dialog_id.get_type() == DialogType::SecretChat) { + promise.set_value(Unit()); + return result; + } + do { random_id = Random::secure_int64(); } while (random_id == 0 || found_dialog_messages_.find(random_id) != found_dialog_messages_.end()); @@ -27201,24 +27180,13 @@ Result MessagesManager::add_local_message( UserId sender_user_id; DialogId sender_dialog_id; if (sender != nullptr) { - switch (sender->get_id()) { - case td_api::messageSenderUser::ID: - sender_user_id = UserId(static_cast(sender.get())->user_id_); - if (!td_->contacts_manager_->have_user_force(sender_user_id)) { - return Status::Error(400, "Sender user not found"); - } - break; - case td_api::messageSenderChat::ID: - sender_dialog_id = DialogId(static_cast(sender.get())->chat_id_); - if (sender_dialog_id.get_type() != DialogType::Channel) { - return Status::Error(400, "Sender chat must be a supergroup or channel"); - } - if (!have_dialog_force(sender_dialog_id, "add_local_message")) { - return Status::Error(400, "Sender chat not found"); - } - break; - default: - UNREACHABLE(); + TRY_RESULT_ASSIGN(sender_dialog_id, get_message_sender_dialog_id(td_, sender, true, false)); + auto sender_dialog_type = sender_dialog_id.get_type(); + if (sender_dialog_type == DialogType::User) { + sender_user_id = sender_dialog_id.get_user_id(); + sender_dialog_id = DialogId(); + } else if (sender_dialog_type != DialogType::Channel) { + return Status::Error(400, "Sender chat must be a supergroup or channel"); } } else if (is_channel_post) { sender_dialog_id = dialog_id; From 922fd9b17be324f13196459a06ee60bd5b9c2fae Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Nov 2021 17:57:00 +0300 Subject: [PATCH 7/7] Pass chat member status as DialogParticipantStatus. --- td/telegram/ContactsManager.cpp | 4 +--- td/telegram/ContactsManager.h | 3 +-- td/telegram/Td.cpp | 13 ++++++------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index d801e0164..ea2966ab4 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -14890,9 +14890,7 @@ void ContactsManager::add_dialog_participants(DialogId dialog_id, const vector &chat_member_status, - Promise &&promise) { - auto status = get_dialog_participant_status(chat_member_status); + DialogParticipantStatus &&status, Promise &&promise) { if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_dialog_participant_status")) { return promise.set_error(Status::Error(400, "Chat not found")); } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 0d86b2aad..5abd303dd 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -527,8 +527,7 @@ class ContactsManager final : public Actor { void add_dialog_participants(DialogId dialog_id, const vector &user_ids, Promise &&promise); void set_dialog_participant_status(DialogId dialog_id, DialogId participant_dialog_id, - const tl_object_ptr &chat_member_status, - Promise &&promise); + DialogParticipantStatus &&status, Promise &&promise); void ban_dialog_participant(DialogId dialog_id, DialogId participant_dialog_id, int32 banned_until_date, bool revoke_messages, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index e8c0fadfd..8d315b595 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6221,7 +6221,7 @@ void Td::on_request(uint64 id, const td_api::joinChat &request) { void Td::on_request(uint64 id, const td_api::leaveChat &request) { CREATE_OK_REQUEST_PROMISE(); DialogId dialog_id(request.chat_id_); - td_api::object_ptr new_status = td_api::make_object(); + auto new_status = DialogParticipantStatus::Left(); if (dialog_id.get_type() == DialogType::Channel && messages_manager_->have_dialog_force(dialog_id, "leaveChat")) { auto status = contacts_manager_->get_channel_status(dialog_id.get_channel_id()); if (status.is_creator()) { @@ -6229,12 +6229,11 @@ void Td::on_request(uint64 id, const td_api::leaveChat &request) { return promise.set_value(Unit()); } - new_status = - td_api::make_object(status.get_rank(), status.is_anonymous(), false); + new_status = DialogParticipantStatus::Creator(false, status.is_anonymous(), status.get_rank()); } } - contacts_manager_->set_dialog_participant_status(dialog_id, DialogId(contacts_manager_->get_my_id()), new_status, - std::move(promise)); + contacts_manager_->set_dialog_participant_status(dialog_id, DialogId(contacts_manager_->get_my_id()), + std::move(new_status), std::move(promise)); } void Td::on_request(uint64 id, const td_api::addChatMember &request) { @@ -6255,8 +6254,8 @@ void Td::on_request(uint64 id, const td_api::setChatMemberStatus &request) { CREATE_OK_REQUEST_PROMISE(); TRY_RESULT_PROMISE(promise, participant_dialog_id, get_message_sender_dialog_id(this, request.member_id_, false, false)); - contacts_manager_->set_dialog_participant_status(DialogId(request.chat_id_), participant_dialog_id, request.status_, - std::move(promise)); + contacts_manager_->set_dialog_participant_status(DialogId(request.chat_id_), participant_dialog_id, + get_dialog_participant_status(request.status_), std::move(promise)); } void Td::on_request(uint64 id, const td_api::banChatMember &request) {