Move restriction flags to RestrictedRights.

This commit is contained in:
levlam 2022-03-18 18:47:34 +03:00
parent 2c38e261d2
commit f8e8bc6035
2 changed files with 36 additions and 45 deletions

View File

@ -249,8 +249,9 @@ int32 DialogParticipantStatus::fix_until_date(int32 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, return DialogParticipantStatus(Type::Creator,
AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | ALL_RESTRICTED_RIGHTS | AdministratorRights::ALL_ADMINISTRATOR_RIGHTS |
(is_member ? IS_MEMBER : 0) | (is_anonymous ? IS_ANONYMOUS : 0), RestrictedRights::ALL_RESTRICTED_RIGHTS | (is_member ? IS_MEMBER : 0) |
(is_anonymous ? IS_ANONYMOUS : 0),
0, std::move(rank)); 0, std::move(rank));
} }
@ -271,13 +272,14 @@ DialogParticipantStatus DialogParticipantStatus::Administrator(bool is_anonymous
return Member(); return Member();
} }
flags = flags | (static_cast<uint32>(can_be_edited) * CAN_BE_EDITED); flags = flags | (static_cast<uint32>(can_be_edited) * CAN_BE_EDITED);
return DialogParticipantStatus(Type::Administrator, return DialogParticipantStatus(
IS_MEMBER | (ALL_RESTRICTED_RIGHTS & ~ALL_ADMIN_PERMISSION_RIGHTS) | flags, 0, Type::Administrator,
IS_MEMBER | (RestrictedRights::ALL_RESTRICTED_RIGHTS & ~RestrictedRights::ALL_ADMIN_PERMISSION_RIGHTS) | flags, 0,
std::move(rank)); std::move(rank));
} }
DialogParticipantStatus DialogParticipantStatus::Member() { 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( DialogParticipantStatus DialogParticipantStatus::Restricted(
@ -289,14 +291,14 @@ DialogParticipantStatus DialogParticipantStatus::Restricted(
can_change_info_and_settings, can_invite_users, can_pin_messages) can_change_info_and_settings, can_invite_users, can_pin_messages)
.flags_ | .flags_ |
(static_cast<uint32>(is_member) * IS_MEMBER); (static_cast<uint32>(is_member) * IS_MEMBER);
if (flags == (IS_MEMBER | ALL_RESTRICTED_RIGHTS)) { if (flags == (IS_MEMBER | RestrictedRights::ALL_RESTRICTED_RIGHTS)) {
return Member(); return Member();
} }
return DialogParticipantStatus(Type::Restricted, flags, fix_until_date(restricted_until_date), string()); return DialogParticipantStatus(Type::Restricted, flags, fix_until_date(restricted_until_date), string());
} }
DialogParticipantStatus DialogParticipantStatus::Left() { 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) { 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, // administrators aren't affected by restrictions, but if everyone can invite users,
// pin messages or change info, they also can do that // pin messages or change info, they also can do that
if (!is_bot) { if (!is_bot) {
flags |= default_restrictions.flags_ & ALL_ADMIN_PERMISSION_RIGHTS; flags |= default_restrictions.flags_ & RestrictedRights::ALL_ADMIN_PERMISSION_RIGHTS;
} }
break; break;
case Type::Member: case Type::Member:
case Type::Restricted: case Type::Restricted:
case Type::Left: case Type::Left:
// members and restricted are affected by default restrictions // 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) { if (is_bot) {
flags &= ~ALL_ADMIN_PERMISSION_RIGHTS; flags &= ~RestrictedRights::ALL_ADMIN_PERMISSION_RIGHTS;
} }
break; break;
case Type::Banned: case Type::Banned:
@ -401,7 +403,7 @@ void DialogParticipantStatus::update_restrictions() const {
} else { } else {
type_ = Type::Left; type_ = Type::Left;
} }
flags_ |= ALL_RESTRICTED_RIGHTS; flags_ |= RestrictedRights::ALL_RESTRICTED_RIGHTS;
} else if (type_ == Type::Banned) { } else if (type_ == Type::Banned) {
type_ = Type::Left; type_ = Type::Left;
} else { } else {
@ -551,6 +553,7 @@ DialogParticipantStatus get_dialog_participant_status(bool can_be_edited,
DialogParticipantStatus get_dialog_participant_status(bool is_member, DialogParticipantStatus get_dialog_participant_status(bool is_member,
tl_object_ptr<telegram_api::chatBannedRights> &&banned_rights) { tl_object_ptr<telegram_api::chatBannedRights> &&banned_rights) {
CHECK(banned_rights != nullptr);
bool can_view_messages = (banned_rights->flags_ & telegram_api::chatBannedRights::VIEW_MESSAGES_MASK) == 0; bool can_view_messages = (banned_rights->flags_ & telegram_api::chatBannedRights::VIEW_MESSAGES_MASK) == 0;
if (!can_view_messages) { if (!can_view_messages) {
return DialogParticipantStatus::Banned(banned_rights->until_date_); return DialogParticipantStatus::Banned(banned_rights->until_date_);

View File

@ -131,11 +131,18 @@ class RestrictedRights {
static constexpr uint32 CAN_INVITE_USERS = 1 << 25; static constexpr uint32 CAN_INVITE_USERS = 1 << 25;
static constexpr uint32 CAN_PIN_MESSAGES = 1 << 26; 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_; uint32 flags_;
friend class DialogParticipantStatus; friend class DialogParticipantStatus;
explicit RestrictedRights(int32 flags) : flags_(flags) { explicit RestrictedRights(int32 flags) : flags_(flags & ALL_RESTRICTED_RIGHTS) {
} }
public: public:
@ -218,31 +225,12 @@ class DialogParticipantStatus {
static constexpr uint32 HAS_RANK = 1 << 14; static constexpr uint32 HAS_RANK = 1 << 14;
static constexpr uint32 CAN_BE_EDITED = 1 << 15; 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; static constexpr uint32 IS_MEMBER = 1 << 27;
// bits 28-30 reserved for Type // bits 28-30 reserved for Type
static constexpr int TYPE_SHIFT = 28; static constexpr int TYPE_SHIFT = 28;
static constexpr uint32 HAS_UNTIL_DATE = 1u << 31; 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 }; enum class Type : int32 { Creator, Administrator, Member, Restricted, Left, Banned };
// all fields are logically const, but should be updated in update_restrictions() // all fields are logically const, but should be updated in update_restrictions()
mutable Type type_; mutable Type type_;
@ -259,7 +247,7 @@ class DialogParticipantStatus {
} }
RestrictedRights get_restricted_rights() const { RestrictedRights get_restricted_rights() const {
return RestrictedRights(flags_ & ALL_RESTRICTED_RIGHTS); return RestrictedRights(flags_);
} }
public: public:
@ -309,7 +297,7 @@ class DialogParticipantStatus {
bool can_change_info_and_settings() const { bool can_change_info_and_settings() const {
return get_administrator_rights().can_change_info_and_settings() || 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 { bool can_post_messages() const {
@ -325,7 +313,7 @@ class DialogParticipantStatus {
} }
bool can_invite_users() const { 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 { bool can_manage_invite_links() const {
@ -338,7 +326,7 @@ class DialogParticipantStatus {
} }
bool can_pin_messages() const { 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 { bool can_promote_members() const {
@ -354,35 +342,35 @@ class DialogParticipantStatus {
} }
bool can_send_messages() const { bool can_send_messages() const {
return (flags_ & CAN_SEND_MESSAGES) != 0; return get_restricted_rights().can_send_messages();
} }
bool can_send_media() const { bool can_send_media() const {
return (flags_ & CAN_SEND_MEDIA) != 0; return get_restricted_rights().can_send_media();
} }
bool can_send_stickers() const { bool can_send_stickers() const {
return (flags_ & CAN_SEND_STICKERS) != 0; return get_restricted_rights().can_send_stickers();
} }
bool can_send_animations() const { bool can_send_animations() const {
return (flags_ & CAN_SEND_ANIMATIONS) != 0; return get_restricted_rights().can_send_animations();
} }
bool can_send_games() const { bool can_send_games() const {
return (flags_ & CAN_SEND_GAMES) != 0; return get_restricted_rights().can_send_games();
} }
bool can_use_inline_bots() const { 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 { 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 { 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) { void set_is_member(bool is_member) {
@ -463,7 +451,7 @@ class DialogParticipantStatus {
flags_ = stored_flags & ((1 << TYPE_SHIFT) - 1); flags_ = stored_flags & ((1 << TYPE_SHIFT) - 1);
if (is_creator()) { if (is_creator()) {
flags_ |= AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | ALL_RESTRICTED_RIGHTS; flags_ |= AdministratorRights::ALL_ADMINISTRATOR_RIGHTS | RestrictedRights::ALL_RESTRICTED_RIGHTS;
} else if (is_administrator()) { } else if (is_administrator()) {
flags_ |= AdministratorRights::CAN_MANAGE_DIALOG; flags_ |= AdministratorRights::CAN_MANAGE_DIALOG;
} }