Replace get_restricted_rights with RestrictedRights constructors.

This commit is contained in:
levlam 2022-04-04 14:13:51 +03:00
parent 4a50db52d8
commit 7f587c24d9
6 changed files with 48 additions and 50 deletions

View File

@ -15737,8 +15737,7 @@ void ContactsManager::on_chat_update(telegram_api::chat &chat, const char *sourc
c->need_save_to_database = true;
}
on_update_chat_status(c, chat_id, std::move(status));
on_update_chat_default_permissions(c, chat_id, get_restricted_rights(std::move(chat.default_banned_rights_)),
chat.version_);
on_update_chat_default_permissions(c, chat_id, RestrictedRights(chat.default_banned_rights_), chat.version_);
on_update_chat_photo(c, chat_id, std::move(chat.photo_));
on_update_chat_active(c, chat_id, is_active);
on_update_chat_noforwards(c, chat_id, chat.noforwards_);
@ -15874,8 +15873,7 @@ void ContactsManager::on_chat_update(telegram_api::channel &channel, const char
on_update_channel_title(c, channel_id, std::move(channel.title_));
on_update_channel_username(c, channel_id, std::move(channel.username_));
on_update_channel_photo(c, channel_id, std::move(channel.photo_));
on_update_channel_default_permissions(c, channel_id,
get_restricted_rights(std::move(channel.default_banned_rights_)));
on_update_channel_default_permissions(c, channel_id, RestrictedRights(channel.default_banned_rights_));
on_update_channel_has_location(c, channel_id, channel.has_geo_);
on_update_channel_noforwards(c, channel_id, channel.noforwards_);
@ -15941,8 +15939,7 @@ void ContactsManager::on_chat_update(telegram_api::channel &channel, const char
on_update_channel_photo(c, channel_id, std::move(channel.photo_));
on_update_channel_status(c, channel_id, std::move(status));
on_update_channel_username(c, channel_id, std::move(channel.username_)); // uses status, must be called after
on_update_channel_default_permissions(c, channel_id,
get_restricted_rights(std::move(channel.default_banned_rights_)));
on_update_channel_default_permissions(c, channel_id, RestrictedRights(channel.default_banned_rights_));
on_update_channel_has_location(c, channel_id, channel.has_geo_);
on_update_channel_noforwards(c, channel_id, channel.noforwards_);
@ -16035,7 +16032,7 @@ void ContactsManager::on_chat_update(telegram_api::channelForbidden &channel, co
on_update_channel_status(c, channel_id, DialogParticipantStatus::Banned(unban_date));
// on_update_channel_username(c, channel_id, ""); // don't know if channel username is empty, so don't update it
tl_object_ptr<telegram_api::chatBannedRights> banned_rights; // == nullptr
on_update_channel_default_permissions(c, channel_id, get_restricted_rights(std::move(banned_rights)));
on_update_channel_default_permissions(c, channel_id, RestrictedRights(banned_rights));
// on_update_channel_has_location(c, channel_id, false);
on_update_channel_noforwards(c, channel_id, false);
td_->messages_manager_->on_update_dialog_group_call(DialogId(channel_id), false, false, "receive channelForbidden");

View File

@ -136,8 +136,8 @@ static td_api::object_ptr<td_api::ChatEventAction> get_chat_event_action_object(
}
case telegram_api::channelAdminLogEventActionDefaultBannedRights::ID: {
auto action = move_tl_object_as<telegram_api::channelAdminLogEventActionDefaultBannedRights>(action_ptr);
auto old_permissions = get_restricted_rights(std::move(action->prev_banned_rights_));
auto new_permissions = get_restricted_rights(std::move(action->new_banned_rights_));
auto old_permissions = RestrictedRights(action->prev_banned_rights_);
auto new_permissions = RestrictedRights(action->new_banned_rights_);
return td_api::make_object<td_api::chatEventPermissionsChanged>(old_permissions.get_chat_permissions_object(),
new_permissions.get_chat_permissions_object());
}

View File

@ -157,6 +157,40 @@ StringBuilder &operator<<(StringBuilder &string_builder, const AdministratorRigh
return string_builder;
}
RestrictedRights::RestrictedRights(const tl_object_ptr<telegram_api::chatBannedRights> &rights) {
if (rights == nullptr) {
flags_ = 0;
return;
}
if (rights->view_messages_) {
LOG(ERROR) << "Can't view messages in banned rights " << to_string(rights);
}
LOG_IF(ERROR, rights->until_date_ != std::numeric_limits<int32>::max())
<< "Have until date " << rights->until_date_ << " in restricted rights";
*this = RestrictedRights(!rights->send_messages_, !rights->send_media_, !rights->send_stickers_,
!rights->send_gifs_, !rights->send_games_, !rights->send_inline_,
!rights->embed_links_, !rights->send_polls_, !rights->change_info_,
!rights->invite_users_, !rights->pin_messages_);
}
RestrictedRights::RestrictedRights(const td_api::object_ptr<td_api::chatPermissions> &rights) {
if (rights == nullptr) {
flags_ = 0;
return;
}
bool can_send_polls = rights->can_send_polls_;
bool can_send_media = rights->can_send_media_messages_;
bool can_send_messages = rights->can_send_messages_ || can_send_media || can_send_polls ||
rights->can_send_other_messages_ || rights->can_add_web_page_previews_;
*this =
RestrictedRights(can_send_messages, can_send_media, rights->can_send_other_messages_,
rights->can_send_other_messages_, rights->can_send_other_messages_,
rights->can_send_other_messages_, rights->can_add_web_page_previews_, can_send_polls,
rights->can_change_info_, rights->can_invite_users_, rights->can_pin_messages_);
}
RestrictedRights::RestrictedRights(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,
@ -370,8 +404,7 @@ DialogParticipantStatus::DialogParticipantStatus(bool is_member,
auto until_date = fix_until_date(banned_rights->until_date_);
banned_rights->until_date_ = std::numeric_limits<int32>::max();
uint32 flags =
::td::get_restricted_rights(std::move(banned_rights)).flags_ | (static_cast<uint32>(is_member) * IS_MEMBER);
uint32 flags = RestrictedRights(banned_rights).flags_ | (static_cast<uint32>(is_member) * IS_MEMBER);
*this = DialogParticipantStatus(Type::Restricted, flags, until_date, string());
}
@ -551,7 +584,7 @@ DialogParticipantStatus get_dialog_participant_status(const tl_object_ptr<td_api
return DialogParticipantStatus::Member();
case td_api::chatMemberStatusRestricted::ID: {
auto st = static_cast<const td_api::chatMemberStatusRestricted *>(status.get());
return DialogParticipantStatus::Restricted(::td::get_restricted_rights(st->permissions_), st->is_member_,
return DialogParticipantStatus::Restricted(RestrictedRights(st->permissions_), st->is_member_,
st->restricted_until_date_);
}
case td_api::chatMemberStatusLeft::ID:
@ -566,38 +599,6 @@ DialogParticipantStatus get_dialog_participant_status(const tl_object_ptr<td_api
}
}
RestrictedRights get_restricted_rights(tl_object_ptr<telegram_api::chatBannedRights> &&banned_rights) {
if (banned_rights == nullptr) {
return RestrictedRights(false, false, false, false, false, false, false, false, false, false, false);
}
if (banned_rights->view_messages_) {
LOG(ERROR) << "Can't view messages in banned rights " << to_string(banned_rights);
}
LOG_IF(ERROR, banned_rights->until_date_ != std::numeric_limits<int32>::max())
<< "Have until date " << banned_rights->until_date_ << " in restricted rights";
return RestrictedRights(!banned_rights->send_messages_, !banned_rights->send_media_, !banned_rights->send_stickers_,
!banned_rights->send_gifs_, !banned_rights->send_games_, !banned_rights->send_inline_,
!banned_rights->embed_links_, !banned_rights->send_polls_, !banned_rights->change_info_,
!banned_rights->invite_users_, !banned_rights->pin_messages_);
}
RestrictedRights get_restricted_rights(const td_api::object_ptr<td_api::chatPermissions> &permissions) {
if (permissions == nullptr) {
return RestrictedRights(false, false, false, false, false, false, false, false, false, false, false);
}
bool can_send_polls = permissions->can_send_polls_;
bool can_send_media = permissions->can_send_media_messages_;
bool can_send_messages = permissions->can_send_messages_ || can_send_media || can_send_polls ||
permissions->can_send_other_messages_ || permissions->can_add_web_page_previews_;
return RestrictedRights(can_send_messages, can_send_media, permissions->can_send_other_messages_,
permissions->can_send_other_messages_, permissions->can_send_other_messages_,
permissions->can_send_other_messages_, permissions->can_add_web_page_previews_,
can_send_polls, permissions->can_change_info_, permissions->can_invite_users_,
permissions->can_pin_messages_);
}
DialogParticipant::DialogParticipant(DialogId dialog_id, UserId inviter_user_id, int32 joined_date,
DialogParticipantStatus status)
: dialog_id_(dialog_id), inviter_user_id_(inviter_user_id), joined_date_(joined_date), status_(std::move(status)) {

View File

@ -161,6 +161,10 @@ class RestrictedRights {
}
public:
explicit RestrictedRights(const tl_object_ptr<telegram_api::chatBannedRights> &rights);
explicit RestrictedRights(const td_api::object_ptr<td_api::chatPermissions> &rights);
RestrictedRights(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);
@ -550,8 +554,4 @@ struct DialogParticipants {
DialogParticipantStatus get_dialog_participant_status(const tl_object_ptr<td_api::ChatMemberStatus> &status);
RestrictedRights get_restricted_rights(tl_object_ptr<telegram_api::chatBannedRights> &&banned_rights);
RestrictedRights get_restricted_rights(const td_api::object_ptr<td_api::chatPermissions> &permissions);
} // namespace td

View File

@ -33790,7 +33790,7 @@ void MessagesManager::set_dialog_permissions(DialogId dialog_id,
UNREACHABLE();
}
auto new_permissions = get_restricted_rights(permissions);
RestrictedRights new_permissions(permissions);
// TODO this can be wrong if there were previous change permissions requests
if (get_dialog_default_permissions(dialog_id) == new_permissions) {

View File

@ -2986,7 +2986,7 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateChatParticipant
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateChatDefaultBannedRights> update,
Promise<Unit> &&promise) {
DialogId dialog_id(update->peer_);
RestrictedRights permissions = get_restricted_rights(std::move(update->default_banned_rights_));
RestrictedRights permissions(update->default_banned_rights_);
auto version = update->version_;
switch (dialog_id.get_type()) {
case DialogType::Chat: