Better apply_restrictions for administrators.

GitOrigin-RevId: 3ea51eefe47c54cddf4d6880fd62869f94ac5434
This commit is contained in:
levlam 2019-03-22 17:04:01 +03:00
parent 5e69f957f3
commit 046be9b785
3 changed files with 36 additions and 10 deletions

View File

@ -9161,9 +9161,6 @@ DialogParticipantStatus ContactsManager::get_chat_permissions(const Chat *c) {
if (!c->is_active) {
return DialogParticipantStatus::Banned(0);
}
if (c->status.is_administrator() || c->status.is_banned()) {
return c->status;
}
return c->status.apply_restrictions(c->default_permissions);
}
@ -9230,7 +9227,8 @@ DialogParticipantStatus ContactsManager::get_channel_permissions(ChannelId chann
DialogParticipantStatus ContactsManager::get_channel_permissions(const Channel *c) {
c->status.update_restrictions();
if (!c->is_megagroup || c->status.is_administrator() || c->status.is_banned()) {
if (!c->is_megagroup) {
// there is no restrictions in broadcast channels
return c->status;
}
return c->status.apply_restrictions(c->default_permissions);

View File

@ -201,6 +201,34 @@ tl_object_ptr<telegram_api::chatBannedRights> DialogParticipantStatus::get_chat_
false /*ignored*/, until_date_);
}
DialogParticipantStatus DialogParticipantStatus::apply_restrictions(RestrictedRights default_restrictions) const {
auto flags = flags_;
switch (type_) {
case Type::Creator:
// creator can do anything and isn't affected by restrictions
break;
case Type::Administrator:
// administrators aren't affected by restrictions, but if everyone can invite users,
// pin messages or change info, they also can do that
flags &= ~ALL_ADMIN_RESTRICTED_RIGHTS | default_restrictions.flags_;
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_;
break;
case Type::Banned:
// banned can do nothing, even restirctions allows them to do that
break;
default:
UNREACHABLE();
break;
}
return DialogParticipantStatus(type_, flags, 0);
}
void DialogParticipantStatus::update_restrictions() const {
if (until_date_ != 0 && G()->unix_time() > until_date_) {
until_date_ = 0;
@ -520,7 +548,7 @@ StringBuilder &operator<<(StringBuilder &string_builder, const RestrictedRights
RestrictedRights get_restricted_rights(const tl_object_ptr<telegram_api::chatBannedRights> &banned_rights) {
if (banned_rights == nullptr) {
return RestrictedRights(false, false, false, false, false, false, false, false, false, true, false);
return RestrictedRights(false, false, false, false, false, false, false, false, false, false, false);
}
bool can_view_messages = (banned_rights->flags_ & telegram_api::chatBannedRights::VIEW_MESSAGES_MASK) == 0;
if (!can_view_messages) {

View File

@ -143,10 +143,12 @@ class DialogParticipantStatus {
CAN_CHANGE_INFO_AND_SETTINGS_ADMIN | CAN_POST_MESSAGES | CAN_EDIT_MESSAGES | CAN_DELETE_MESSAGES |
CAN_INVITE_USERS_ADMIN | CAN_RESTRICT_MEMBERS | CAN_PIN_MESSAGES_ADMIN | CAN_PROMOTE_MEMBERS;
static constexpr uint32 ALL_ADMIN_RESTRICTED_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 | CAN_CHANGE_INFO_AND_SETTINGS_BANNED |
CAN_INVITE_USERS_BANNED | CAN_PIN_MESSAGES_BANNED;
CAN_USE_INLINE_BOTS | CAN_ADD_WEB_PAGE_PREVIEWS | CAN_SEND_POLLS | ALL_ADMIN_RESTRICTED_RIGHTS;
enum class Type : int32 { Creator, Administrator, Member, Restricted, Left, Banned };
// all fields are logically const, but should be updated in update_restrictions()
@ -189,9 +191,7 @@ class DialogParticipantStatus {
RestrictedRights get_restricted_rights() const;
DialogParticipantStatus apply_restrictions(RestrictedRights default_restrictions) const {
return DialogParticipantStatus(type_, flags_ & (~ALL_RESTRICTED_RIGHTS | default_restrictions.flags_), 0);
}
DialogParticipantStatus apply_restrictions(RestrictedRights default_restrictions) const;
tl_object_ptr<td_api::ChatMemberStatus> get_chat_member_status_object() const;