diff --git a/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp b/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp index 6a60fe641..73ff07e32 100644 --- a/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp @@ -329,15 +329,21 @@ Main::Session &AddSpecialBoxController::session() const { } void AddSpecialBoxController::subscribeToMigration() { + const auto chat = _peer->asChat(); + if (!chat) { + return; + } SubscribeToMigration( - _peer, + chat, lifetime(), - [=](not_null channel) { migrate(channel); }); + [=](not_null channel) { migrate(chat, channel); }); } -void AddSpecialBoxController::migrate(not_null channel) { +void AddSpecialBoxController::migrate( + not_null chat, + not_null channel) { _peer = channel; - _additional.migrate(channel); + _additional.migrate(chat, channel); } std::unique_ptr AddSpecialBoxController::createSearchRow( diff --git a/Telegram/SourceFiles/boxes/peers/add_participants_box.h b/Telegram/SourceFiles/boxes/peers/add_participants_box.h index 8098134b7..ee626f161 100644 --- a/Telegram/SourceFiles/boxes/peers/add_participants_box.h +++ b/Telegram/SourceFiles/boxes/peers/add_participants_box.h @@ -121,7 +121,7 @@ private: std::unique_ptr createRow(not_null user) const; void subscribeToMigration(); - void migrate(not_null channel); + void migrate(not_null chat, not_null channel); not_null _peer; MTP::Sender _api; diff --git a/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp index df67a6255..1ea91afc5 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_participant_box.cpp @@ -208,10 +208,10 @@ EditAdminBox::EditAdminBox( , _oldRank(rank) { } -MTPChatAdminRights EditAdminBox::Defaults(not_null peer) { - const auto defaultRights = peer->isChat() - ? ChatData::DefaultAdminRights() - : peer->isMegagroup() +MTPChatAdminRights EditAdminBox::defaultRights() const { + const auto flags = peer()->isChat() + ? peer()->asChat()->defaultAdminRights(user()) + : peer()->isMegagroup() ? (Flag::f_change_info | Flag::f_delete_messages | Flag::f_ban_users @@ -223,7 +223,7 @@ MTPChatAdminRights EditAdminBox::Defaults(not_null peer) { | Flag::f_edit_messages | Flag::f_delete_messages | Flag::f_invite_users); - return MTP_chatAdminRights(MTP_flags(defaultRights)); + return MTP_chatAdminRights(MTP_flags(flags)); } void EditAdminBox::prepare() { @@ -242,7 +242,7 @@ void EditAdminBox::prepare() { const auto chat = peer()->asChat(); const auto channel = peer()->asChannel(); - const auto prepareRights = hadRights ? _oldRights : Defaults(peer()); + const auto prepareRights = hadRights ? _oldRights : defaultRights(); const auto disabledByDefaults = (channel && !channel->isMegagroup()) ? MTPDchatAdminRights::Flags(0) : DisabledByDefaultRestrictions(peer()); @@ -264,12 +264,12 @@ void EditAdminBox::prepare() { result.emplace( disabledByDefaults, tr::lng_rights_permission_for_all(tr::now)); - if (const auto channel = peer()->asChannel()) { - if (amCreator() && user()->isSelf()) { - result.emplace( - ~Flag::f_anonymous, - tr::lng_rights_permission_cant_edit(tr::now)); - } else if (!channel->amCreator()) { + if (amCreator() && user()->isSelf()) { + result.emplace( + ~Flag::f_anonymous, + tr::lng_rights_permission_cant_edit(tr::now)); + } else if (const auto channel = peer()->asChannel()) { + if (!channel->amCreator()) { result.emplace( ~channel->adminRights(), tr::lng_rights_permission_cant_edit(tr::now)); @@ -611,9 +611,9 @@ void EditRestrictedBox::prepare() { const auto defaultRestrictions = chat ? chat->defaultRestrictions() : channel->defaultRestrictions(); - const auto prepareRights = (_oldRights.c_chatBannedRights().vflags().v + const auto prepareRights = _oldRights.c_chatBannedRights().vflags().v ? _oldRights - : Defaults(peer())); + : defaultRights(); const auto prepareFlags = FixDependentRestrictions( prepareRights.c_chatBannedRights().vflags().v | defaultRestrictions @@ -680,7 +680,7 @@ void EditRestrictedBox::prepare() { } } -MTPChatBannedRights EditRestrictedBox::Defaults(not_null peer) { +MTPChatBannedRights EditRestrictedBox::defaultRights() const { return MTP_chatBannedRights(MTP_flags(0), MTP_int(0)); } diff --git a/Telegram/SourceFiles/boxes/peers/edit_participant_box.h b/Telegram/SourceFiles/boxes/peers/edit_participant_box.h index 371d7b98b..474c166c1 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_participant_box.h +++ b/Telegram/SourceFiles/boxes/peers/edit_participant_box.h @@ -89,7 +89,7 @@ private: using Flag = MTPDchatAdminRights::Flag; using Flags = MTPDchatAdminRights::Flags; - static MTPChatAdminRights Defaults(not_null peer); + [[nodiscard]] MTPChatAdminRights defaultRights() const; not_null addRankInput(); void transferOwnership(); @@ -144,7 +144,7 @@ private: using Flag = MTPDchatBannedRights::Flag; using Flags = MTPDchatBannedRights::Flags; - static MTPChatBannedRights Defaults(not_null peer); + [[nodiscard]] MTPChatBannedRights defaultRights() const; bool canSave() const { return !!_saveCallback; diff --git a/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp index 4adf4993f..f777344c0 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp @@ -224,7 +224,7 @@ FndefaultAdminRights(user) && rank.isEmpty()) { saveChatAdmin(true); } else if (!flags) { saveChatAdmin(false); @@ -370,7 +370,7 @@ auto ParticipantsAdditionalData::adminRights( if (const auto chat = _peer->asChat()) { return _admins.contains(user) ? std::make_optional(MTPChatAdminRights(MTP_chatAdminRights( - MTP_flags(ChatData::DefaultAdminRights())))) + MTP_flags(chat->defaultAdminRights(user))))) : std::nullopt; } const auto i = _adminRights.find(user); @@ -671,14 +671,16 @@ UserData *ParticipantsAdditionalData::applyBanned( return user; } -void ParticipantsAdditionalData::migrate(not_null channel) { +void ParticipantsAdditionalData::migrate( + not_null chat, + not_null channel) { _peer = channel; fillFromChannel(channel); for (const auto user : _admins) { _adminRights.emplace( user, - MTP_chatAdminRights(MTP_flags(ChatData::DefaultAdminRights()))); + MTP_chatAdminRights(MTP_flags(chat->defaultAdminRights(user)))); if (channel->amCreator()) { _adminCanEdit.emplace(user); } @@ -1889,15 +1891,21 @@ void ParticipantsBoxController::refreshCustomStatus( } void ParticipantsBoxController::subscribeToMigration() { + const auto chat = _peer->asChat(); + if (!chat) { + return; + } SubscribeToMigration( - _peer, + chat, lifetime(), - [=](not_null channel) { migrate(channel); }); + [=](not_null channel) { migrate(chat, channel); }); } -void ParticipantsBoxController::migrate(not_null channel) { +void ParticipantsBoxController::migrate( + not_null chat, + not_null channel) { _peer = channel; - _additional.migrate(channel); + _additional.migrate(chat, channel); subscribeToCreatorChange(channel); } diff --git a/Telegram/SourceFiles/boxes/peers/edit_participants_box.h b/Telegram/SourceFiles/boxes/peers/edit_participants_box.h index dfcb1ae6d..cff017b31 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_participants_box.h +++ b/Telegram/SourceFiles/boxes/peers/edit_participants_box.h @@ -101,7 +101,7 @@ public: [[nodiscard]] UserData *adminPromotedBy(not_null user) const; [[nodiscard]] UserData *restrictedBy(not_null user) const; - void migrate(not_null channel); + void migrate(not_null chat, not_null channel); private: UserData *applyCreator(const MTPDchannelParticipantCreator &data); @@ -242,7 +242,7 @@ private: void recomputeTypeFor(not_null user); void subscribeToMigration(); - void migrate(not_null channel); + void migrate(not_null chat, not_null channel); void subscribeToCreatorChange(not_null channel); void fullListRefresh(); diff --git a/Telegram/SourceFiles/data/data_chat.cpp b/Telegram/SourceFiles/data/data_chat.cpp index 8f4f29659..b0ec13d23 100644 --- a/Telegram/SourceFiles/data/data_chat.cpp +++ b/Telegram/SourceFiles/data/data_chat.cpp @@ -48,14 +48,17 @@ void ChatData::setPhoto(PhotoId photoId, const MTPChatPhoto &photo) { }); } -auto ChatData::DefaultAdminRights() -> AdminRights { +auto ChatData::defaultAdminRights(not_null user) -> AdminRights { + const auto isCreator = (creator == user->bareId()) + || (user->isSelf() && amCreator()); using Flag = AdminRight; return Flag::f_change_info | Flag::f_delete_messages | Flag::f_ban_users | Flag::f_invite_users | Flag::f_pin_messages - | Flag::f_manage_call; + | Flag::f_manage_call + | (isCreator ? Flag::f_add_admins : Flag(0)); } bool ChatData::canWrite() const { @@ -335,7 +338,7 @@ void ApplyChatUpdate( } if (user->isSelf()) { chat->setAdminRights(MTP_chatAdminRights(mtpIsTrue(update.vis_admin()) - ? MTP_flags(ChatData::DefaultAdminRights()) + ? MTP_flags(chat->defaultAdminRights(user)) : MTP_flags(0))); } if (mtpIsTrue(update.vis_admin())) { @@ -457,7 +460,7 @@ void ApplyChatUpdate( chat->admins.emplace(user); if (user->isSelf()) { chat->setAdminRights(MTP_chatAdminRights( - MTP_flags(ChatData::DefaultAdminRights()))); + MTP_flags(chat->defaultAdminRights(user)))); } }, [](const MTPDchatParticipant &) { }); diff --git a/Telegram/SourceFiles/data/data_chat.h b/Telegram/SourceFiles/data/data_chat.h index 52cc3e1de..d1c5d4343 100644 --- a/Telegram/SourceFiles/data/data_chat.h +++ b/Telegram/SourceFiles/data/data_chat.h @@ -45,7 +45,7 @@ public: void setName(const QString &newName); void invalidateParticipants(); - bool noParticipantInfo() const { + [[nodiscard]] bool noParticipantInfo() const { return (count > 0 || amIn()) && participants.empty(); } @@ -58,10 +58,10 @@ public: void removeFlags(MTPDchat::Flags which) { _flags.remove(which); } - auto flags() const { + [[nodiscard]] auto flags() const { return _flags.current(); } - auto flagsValue() const { + [[nodiscard]] auto flagsValue() const { return _flags.value(); } @@ -74,58 +74,58 @@ public: void removeFullFlags(MTPDchatFull::Flags which) { _fullFlags.remove(which); } - auto fullFlags() const { + [[nodiscard]] auto fullFlags() const { return _fullFlags.current(); } - auto fullFlagsValue() const { + [[nodiscard]] auto fullFlagsValue() const { return _fullFlags.value(); } - auto adminRights() const { + [[nodiscard]] auto adminRights() const { return _adminRights.current(); } - auto adminRightsValue() const { + [[nodiscard]] auto adminRightsValue() const { return _adminRights.value(); } void setAdminRights(const MTPChatAdminRights &rights); - bool hasAdminRights() const { + [[nodiscard]] bool hasAdminRights() const { return (adminRights() != 0); } - auto defaultRestrictions() const { + [[nodiscard]] auto defaultRestrictions() const { return _defaultRestrictions.current(); } - auto defaultRestrictionsValue() const { + [[nodiscard]] auto defaultRestrictionsValue() const { return _defaultRestrictions.value(); } void setDefaultRestrictions(const MTPChatBannedRights &rights); - bool isForbidden() const { + [[nodiscard]] bool isForbidden() const { return flags() & MTPDchat_ClientFlag::f_forbidden; } - bool amIn() const { + [[nodiscard]] bool amIn() const { return !isForbidden() && !isDeactivated() && !haveLeft() && !wasKicked(); } - bool haveLeft() const { + [[nodiscard]] bool haveLeft() const { return flags() & MTPDchat::Flag::f_left; } - bool wasKicked() const { + [[nodiscard]] bool wasKicked() const { return flags() & MTPDchat::Flag::f_kicked; } - bool amCreator() const { + [[nodiscard]] bool amCreator() const { return flags() & MTPDchat::Flag::f_creator; } - bool isDeactivated() const { + [[nodiscard]] bool isDeactivated() const { return flags() & MTPDchat::Flag::f_deactivated; } - bool isMigrated() const { + [[nodiscard]] bool isMigrated() const { return flags() & MTPDchat::Flag::f_migrated_to; } - static AdminRights DefaultAdminRights(); + [[nodiscard]] AdminRights defaultAdminRights(not_null user); // Like in ChannelData. bool canWrite() const;