From f8e8bc603515e9e631cf09d4db0921ab14d0503b Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 18 Mar 2022 18:47:34 +0300 Subject: [PATCH] Move restriction flags to RestrictedRights. --- td/telegram/DialogParticipant.cpp | 27 +++++++++------- td/telegram/DialogParticipant.h | 54 ++++++++++++------------------- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/td/telegram/DialogParticipant.cpp b/td/telegram/DialogParticipant.cpp index 7a8818c56..87b295546 100644 --- a/td/telegram/DialogParticipant.cpp +++ b/td/telegram/DialogParticipant.cpp @@ -249,8 +249,9 @@ int32 DialogParticipantStatus::fix_until_date(int32 date) { DialogParticipantStatus DialogParticipantStatus::Creator(bool is_member, bool is_anonymous, string rank) { return DialogParticipantStatus(Type::Creator, - AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | ALL_RESTRICTED_RIGHTS | - (is_member ? IS_MEMBER : 0) | (is_anonymous ? IS_ANONYMOUS : 0), + AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | + RestrictedRights::ALL_RESTRICTED_RIGHTS | (is_member ? IS_MEMBER : 0) | + (is_anonymous ? IS_ANONYMOUS : 0), 0, std::move(rank)); } @@ -271,13 +272,14 @@ DialogParticipantStatus DialogParticipantStatus::Administrator(bool is_anonymous return Member(); } flags = flags | (static_cast(can_be_edited) * CAN_BE_EDITED); - return DialogParticipantStatus(Type::Administrator, - IS_MEMBER | (ALL_RESTRICTED_RIGHTS & ~ALL_ADMIN_PERMISSION_RIGHTS) | flags, 0, - std::move(rank)); + return DialogParticipantStatus( + Type::Administrator, + IS_MEMBER | (RestrictedRights::ALL_RESTRICTED_RIGHTS & ~RestrictedRights::ALL_ADMIN_PERMISSION_RIGHTS) | flags, 0, + std::move(rank)); } DialogParticipantStatus DialogParticipantStatus::Member() { - return DialogParticipantStatus(Type::Member, IS_MEMBER | ALL_RESTRICTED_RIGHTS, 0, string()); + return DialogParticipantStatus(Type::Member, IS_MEMBER | RestrictedRights::ALL_RESTRICTED_RIGHTS, 0, string()); } DialogParticipantStatus DialogParticipantStatus::Restricted( @@ -289,14 +291,14 @@ DialogParticipantStatus DialogParticipantStatus::Restricted( can_change_info_and_settings, can_invite_users, can_pin_messages) .flags_ | (static_cast(is_member) * IS_MEMBER); - if (flags == (IS_MEMBER | ALL_RESTRICTED_RIGHTS)) { + if (flags == (IS_MEMBER | RestrictedRights::ALL_RESTRICTED_RIGHTS)) { return Member(); } return DialogParticipantStatus(Type::Restricted, flags, fix_until_date(restricted_until_date), string()); } DialogParticipantStatus DialogParticipantStatus::Left() { - return DialogParticipantStatus(Type::Left, ALL_RESTRICTED_RIGHTS, 0, string()); + return DialogParticipantStatus(Type::Left, RestrictedRights::ALL_RESTRICTED_RIGHTS, 0, string()); } DialogParticipantStatus DialogParticipantStatus::Banned(int32 banned_until_date) { @@ -369,16 +371,16 @@ DialogParticipantStatus DialogParticipantStatus::apply_restrictions(RestrictedRi // administrators aren't affected by restrictions, but if everyone can invite users, // pin messages or change info, they also can do that if (!is_bot) { - flags |= default_restrictions.flags_ & ALL_ADMIN_PERMISSION_RIGHTS; + flags |= default_restrictions.flags_ & RestrictedRights::ALL_ADMIN_PERMISSION_RIGHTS; } break; case Type::Member: case Type::Restricted: case Type::Left: // members and restricted are affected by default restrictions - flags &= (~ALL_RESTRICTED_RIGHTS) | default_restrictions.flags_; + flags &= (~RestrictedRights::ALL_RESTRICTED_RIGHTS) | default_restrictions.flags_; if (is_bot) { - flags &= ~ALL_ADMIN_PERMISSION_RIGHTS; + flags &= ~RestrictedRights::ALL_ADMIN_PERMISSION_RIGHTS; } break; case Type::Banned: @@ -401,7 +403,7 @@ void DialogParticipantStatus::update_restrictions() const { } else { type_ = Type::Left; } - flags_ |= ALL_RESTRICTED_RIGHTS; + flags_ |= RestrictedRights::ALL_RESTRICTED_RIGHTS; } else if (type_ == Type::Banned) { type_ = Type::Left; } else { @@ -551,6 +553,7 @@ DialogParticipantStatus get_dialog_participant_status(bool can_be_edited, DialogParticipantStatus get_dialog_participant_status(bool is_member, tl_object_ptr &&banned_rights) { + CHECK(banned_rights != nullptr); bool can_view_messages = (banned_rights->flags_ & telegram_api::chatBannedRights::VIEW_MESSAGES_MASK) == 0; if (!can_view_messages) { return DialogParticipantStatus::Banned(banned_rights->until_date_); diff --git a/td/telegram/DialogParticipant.h b/td/telegram/DialogParticipant.h index 37af6e8b0..8ec23b296 100644 --- a/td/telegram/DialogParticipant.h +++ b/td/telegram/DialogParticipant.h @@ -131,11 +131,18 @@ class RestrictedRights { static constexpr uint32 CAN_INVITE_USERS = 1 << 25; static constexpr uint32 CAN_PIN_MESSAGES = 1 << 26; + static constexpr uint32 ALL_ADMIN_PERMISSION_RIGHTS = + CAN_CHANGE_INFO_AND_SETTINGS | CAN_INVITE_USERS | CAN_PIN_MESSAGES; + + static constexpr uint32 ALL_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 | ALL_ADMIN_PERMISSION_RIGHTS; + uint32 flags_; friend class DialogParticipantStatus; - explicit RestrictedRights(int32 flags) : flags_(flags) { + explicit RestrictedRights(int32 flags) : flags_(flags & ALL_RESTRICTED_RIGHTS) { } public: @@ -218,31 +225,12 @@ class DialogParticipantStatus { static constexpr uint32 HAS_RANK = 1 << 14; static constexpr uint32 CAN_BE_EDITED = 1 << 15; - static constexpr uint32 CAN_SEND_MESSAGES = 1 << 16; - static constexpr uint32 CAN_SEND_MEDIA = 1 << 17; - static constexpr uint32 CAN_SEND_STICKERS = 1 << 18; - static constexpr uint32 CAN_SEND_ANIMATIONS = 1 << 19; - static constexpr uint32 CAN_SEND_GAMES = 1 << 20; - static constexpr uint32 CAN_USE_INLINE_BOTS = 1 << 21; - static constexpr uint32 CAN_ADD_WEB_PAGE_PREVIEWS = 1 << 22; - static constexpr uint32 CAN_SEND_POLLS = 1 << 23; - static constexpr uint32 CAN_CHANGE_INFO_AND_SETTINGS_BANNED = 1 << 24; - static constexpr uint32 CAN_INVITE_USERS_BANNED = 1 << 25; - static constexpr uint32 CAN_PIN_MESSAGES_BANNED = 1 << 26; - static constexpr uint32 IS_MEMBER = 1 << 27; // bits 28-30 reserved for Type static constexpr int TYPE_SHIFT = 28; static constexpr uint32 HAS_UNTIL_DATE = 1u << 31; - static constexpr uint32 ALL_ADMIN_PERMISSION_RIGHTS = - CAN_CHANGE_INFO_AND_SETTINGS_BANNED | CAN_INVITE_USERS_BANNED | CAN_PIN_MESSAGES_BANNED; - - static constexpr uint32 ALL_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 | ALL_ADMIN_PERMISSION_RIGHTS; - enum class Type : int32 { Creator, Administrator, Member, Restricted, Left, Banned }; // all fields are logically const, but should be updated in update_restrictions() mutable Type type_; @@ -259,7 +247,7 @@ class DialogParticipantStatus { } RestrictedRights get_restricted_rights() const { - return RestrictedRights(flags_ & ALL_RESTRICTED_RIGHTS); + return RestrictedRights(flags_); } public: @@ -309,7 +297,7 @@ class DialogParticipantStatus { bool can_change_info_and_settings() const { return get_administrator_rights().can_change_info_and_settings() || - (flags_ & CAN_CHANGE_INFO_AND_SETTINGS_BANNED) != 0; + get_restricted_rights().can_change_info_and_settings(); } bool can_post_messages() const { @@ -325,7 +313,7 @@ class DialogParticipantStatus { } bool can_invite_users() const { - return get_administrator_rights().can_invite_users() || (flags_ & CAN_INVITE_USERS_BANNED) != 0; + return get_administrator_rights().can_invite_users() || get_restricted_rights().can_invite_users(); } bool can_manage_invite_links() const { @@ -338,7 +326,7 @@ class DialogParticipantStatus { } bool can_pin_messages() const { - return get_administrator_rights().can_pin_messages() || (flags_ & CAN_PIN_MESSAGES_BANNED) != 0; + return get_administrator_rights().can_pin_messages() || get_restricted_rights().can_pin_messages(); } bool can_promote_members() const { @@ -354,35 +342,35 @@ class DialogParticipantStatus { } bool can_send_messages() const { - return (flags_ & CAN_SEND_MESSAGES) != 0; + return get_restricted_rights().can_send_messages(); } bool can_send_media() const { - return (flags_ & CAN_SEND_MEDIA) != 0; + return get_restricted_rights().can_send_media(); } bool can_send_stickers() const { - return (flags_ & CAN_SEND_STICKERS) != 0; + return get_restricted_rights().can_send_stickers(); } bool can_send_animations() const { - return (flags_ & CAN_SEND_ANIMATIONS) != 0; + return get_restricted_rights().can_send_animations(); } bool can_send_games() const { - return (flags_ & CAN_SEND_GAMES) != 0; + return get_restricted_rights().can_send_games(); } bool can_use_inline_bots() const { - return (flags_ & CAN_USE_INLINE_BOTS) != 0; + return get_restricted_rights().can_use_inline_bots(); } bool can_add_web_page_previews() const { - return (flags_ & CAN_ADD_WEB_PAGE_PREVIEWS) != 0; + return get_restricted_rights().can_add_web_page_previews(); } bool can_send_polls() const { - return (flags_ & CAN_SEND_POLLS) != 0; + return get_restricted_rights().can_send_polls(); } void set_is_member(bool is_member) { @@ -463,7 +451,7 @@ class DialogParticipantStatus { flags_ = stored_flags & ((1 << TYPE_SHIFT) - 1); if (is_creator()) { - flags_ |= AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | ALL_RESTRICTED_RIGHTS; + flags_ |= AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | RestrictedRights::ALL_RESTRICTED_RIGHTS; } else if (is_administrator()) { flags_ |= AdministratorRights::CAN_MANAGE_DIALOG; }