From 180c2e34fd1bbd27ee056fd5400f38a597ddcf8a Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 22 Mar 2022 12:38:44 +0300 Subject: [PATCH] Add simpler DialogParticipantStatus constructors. --- td/telegram/DialogParticipant.cpp | 62 ++++++++++++++++++------------- td/telegram/DialogParticipant.h | 10 ++++- td/telegram/Td.cpp | 3 +- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/td/telegram/DialogParticipant.cpp b/td/telegram/DialogParticipant.cpp index 375780b3e..db3d07763 100644 --- a/td/telegram/DialogParticipant.cpp +++ b/td/telegram/DialogParticipant.cpp @@ -249,7 +249,7 @@ int32 DialogParticipantStatus::fix_until_date(int32 date) { return date; } -DialogParticipantStatus DialogParticipantStatus::Creator(bool is_member, bool is_anonymous, string rank) { +DialogParticipantStatus DialogParticipantStatus::Creator(bool is_member, bool is_anonymous, string &&rank) { return DialogParticipantStatus(Type::Creator, AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | RestrictedRights::ALL_RESTRICTED_RIGHTS | (is_member ? IS_MEMBER : 0) | @@ -257,16 +257,9 @@ DialogParticipantStatus DialogParticipantStatus::Creator(bool is_member, bool is 0, std::move(rank)); } -DialogParticipantStatus DialogParticipantStatus::Administrator(bool is_anonymous, string rank, bool can_be_edited, - bool can_manage_dialog, bool can_change_info, - bool can_post_messages, bool can_edit_messages, - bool can_delete_messages, bool can_invite_users, - bool can_restrict_members, bool can_pin_messages, - bool can_promote_members, bool can_manage_calls) { - uint32 flags = AdministratorRights(is_anonymous, can_manage_dialog, can_change_info, can_post_messages, - can_edit_messages, can_delete_messages, can_invite_users, can_restrict_members, - can_pin_messages, can_promote_members, can_manage_calls) - .flags_; +DialogParticipantStatus DialogParticipantStatus::Administrator(AdministratorRights administrator_rights, string &&rank, + bool can_be_edited) { + uint32 flags = administrator_rights.flags_; if (flags == 0) { return Member(); } @@ -277,23 +270,40 @@ DialogParticipantStatus DialogParticipantStatus::Administrator(bool is_anonymous std::move(rank)); } +DialogParticipantStatus DialogParticipantStatus::Administrator(bool is_anonymous, string &&rank, bool can_be_edited, + bool can_manage_dialog, bool can_change_info, + bool can_post_messages, bool can_edit_messages, + bool can_delete_messages, bool can_invite_users, + bool can_restrict_members, bool can_pin_messages, + bool can_promote_members, bool can_manage_calls) { + auto administrator_rights = AdministratorRights( + is_anonymous, can_manage_dialog, can_change_info, can_post_messages, can_edit_messages, can_delete_messages, + can_invite_users, can_restrict_members, can_pin_messages, can_promote_members, can_manage_calls); + return Administrator(administrator_rights, std::move(rank), can_be_edited); +} + DialogParticipantStatus DialogParticipantStatus::Member() { return DialogParticipantStatus(Type::Member, IS_MEMBER | RestrictedRights::ALL_RESTRICTED_RIGHTS, 0, string()); } +DialogParticipantStatus DialogParticipantStatus::Restricted(RestrictedRights restricted_rights, bool is_member, + int32 restricted_until_date) { + uint32 flags = restricted_rights.flags_; + if (flags == RestrictedRights::ALL_RESTRICTED_RIGHTS) { + return is_member ? Member() : Left(); + } + flags |= (static_cast(is_member) * IS_MEMBER); + return DialogParticipantStatus(Type::Restricted, flags, fix_until_date(restricted_until_date), string()); +} + DialogParticipantStatus DialogParticipantStatus::Restricted( bool is_member, int32 restricted_until_date, bool can_send_messages, bool can_send_media, bool can_send_stickers, bool can_send_animations, bool can_send_games, bool can_use_inline_bots, bool can_add_web_page_previews, bool can_send_polls, bool can_change_info_and_settings, bool can_invite_users, bool can_pin_messages) { - uint32 flags = RestrictedRights(can_send_messages, can_send_media, can_send_stickers, can_send_animations, - can_send_games, can_use_inline_bots, can_add_web_page_previews, can_send_polls, - can_change_info_and_settings, can_invite_users, can_pin_messages) - .flags_ | - (static_cast(is_member) * IS_MEMBER); - if (flags == (IS_MEMBER | RestrictedRights::ALL_RESTRICTED_RIGHTS)) { - return Member(); - } - return DialogParticipantStatus(Type::Restricted, flags, fix_until_date(restricted_until_date), string()); + RestrictedRights restricted_rights(can_send_messages, can_send_media, can_send_stickers, can_send_animations, + can_send_games, can_use_inline_bots, can_add_web_page_previews, can_send_polls, + can_change_info_and_settings, can_invite_users, can_pin_messages); + return Restricted(restricted_rights, is_member, restricted_until_date); } DialogParticipantStatus DialogParticipantStatus::Left() { @@ -508,7 +518,7 @@ DialogParticipantStatus get_dialog_participant_status(const tl_object_ptris_member_, st->is_anonymous_, custom_title); + return DialogParticipantStatus::Creator(st->is_member_, st->is_anonymous_, std::move(custom_title)); } case td_api::chatMemberStatusAdministrator::ID: { auto st = static_cast(status.get()); @@ -516,10 +526,12 @@ DialogParticipantStatus get_dialog_participant_status(const tl_object_ptris_anonymous_, custom_title, true /*st->can_be_edited_*/, st->can_manage_chat_, st->can_change_info_, - st->can_post_messages_, st->can_edit_messages_, st->can_delete_messages_, st->can_invite_users_, - st->can_restrict_members_, st->can_pin_messages_, st->can_promote_members_, st->can_manage_video_chats_); + AdministratorRights administrator_rights(st->is_anonymous_, st->can_manage_chat_, st->can_change_info_, + st->can_post_messages_, st->can_edit_messages_, st->can_delete_messages_, + st->can_invite_users_, st->can_restrict_members_, st->can_pin_messages_, + st->can_promote_members_, st->can_manage_video_chats_); + return DialogParticipantStatus::Administrator(administrator_rights, std::move(custom_title), + true /*st->can_be_edited_*/); } case td_api::chatMemberStatusMember::ID: return DialogParticipantStatus::Member(); diff --git a/td/telegram/DialogParticipant.h b/td/telegram/DialogParticipant.h index d7ca0a59d..089bae241 100644 --- a/td/telegram/DialogParticipant.h +++ b/td/telegram/DialogParticipant.h @@ -256,9 +256,12 @@ class DialogParticipantStatus { } public: - static DialogParticipantStatus Creator(bool is_member, bool is_anonymous, string rank); + static DialogParticipantStatus Creator(bool is_member, bool is_anonymous, string &&rank); - static DialogParticipantStatus Administrator(bool is_anonymous, string rank, bool can_be_edited, + static DialogParticipantStatus Administrator(AdministratorRights administrator_rights, string &&rank, + bool can_be_edited); + + static DialogParticipantStatus Administrator(bool is_anonymous, string &&rank, bool can_be_edited, bool can_manage_dialog, bool can_change_info, bool can_post_messages, bool can_edit_messages, bool can_delete_messages, bool can_invite_users, bool can_restrict_members, bool can_pin_messages, @@ -266,6 +269,9 @@ class DialogParticipantStatus { static DialogParticipantStatus Member(); + static DialogParticipantStatus Restricted(RestrictedRights restricted_rights, bool is_member, + int32 restricted_until_date); + static DialogParticipantStatus Restricted(bool is_member, int32 restricted_until_date, bool can_send_messages, bool can_send_media, bool can_send_stickers, bool can_send_animations, bool can_send_games, bool can_use_inline_bots, diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index f0e2ae651..8b16c8d5c 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -6188,7 +6188,8 @@ void Td::on_request(uint64 id, const td_api::leaveChat &request) { return promise.set_value(Unit()); } - new_status = DialogParticipantStatus::Creator(false, status.is_anonymous(), status.get_rank()); + auto rank = status.get_rank(); + new_status = DialogParticipantStatus::Creator(false, status.is_anonymous(), std::move(rank)); } } contacts_manager_->set_dialog_participant_status(dialog_id, DialogId(contacts_manager_->get_my_id()),