Unify permissions checks in participants boxes.

This commit is contained in:
John Preston 2019-01-10 15:01:59 +04:00
parent e1a2ab0d7e
commit 9a60e744d3
6 changed files with 950 additions and 764 deletions

View File

@ -87,7 +87,7 @@ void ShowAddParticipantsError(
const auto makeAdmin = [=] {
const auto user = users.front();
const auto weak = std::make_shared<QPointer<EditAdminBox>>();
const auto close = [=] {
const auto close = [=](auto&&...) {
if (*weak) {
(*weak)->closeBox();
}
@ -95,7 +95,7 @@ void ShowAddParticipantsError(
const auto saveCallback = SaveAdminCallback(
channel,
user,
[=](auto&&...) { close(); },
close,
close);
auto box = Box<EditAdminBox>(
channel,

View File

@ -62,7 +62,7 @@ bool InviteSelectedUsers(
if (users.empty()) {
return false;
}
Auth().api().addChatParticipants(chat, users);
chat->session().api().addChatParticipants(chat, users);
return true;
}
@ -167,7 +167,11 @@ void AddParticipantsBoxController::Start(not_null<ChatData*> chat) {
});
box->addButton(langFactory(lng_cancel), [box] { box->closeBox(); });
};
Ui::show(Box<PeerListBox>(std::make_unique<AddParticipantsBoxController>(chat), std::move(initBox)));
Ui::show(
Box<PeerListBox>(
std::make_unique<AddParticipantsBoxController>(chat),
std::move(initBox)),
LayerOption::KeepOther);
}
void AddParticipantsBoxController::Start(
@ -192,7 +196,13 @@ void AddParticipantsBoxController::Start(
}, *subscription);
}
};
Ui::show(Box<PeerListBox>(std::make_unique<AddParticipantsBoxController>(channel, std::move(alreadyIn)), std::move(initBox)));
Ui::show(
Box<PeerListBox>(
std::make_unique<AddParticipantsBoxController>(
channel,
std::move(alreadyIn)),
std::move(initBox)),
LayerOption::KeepOther);
}
void AddParticipantsBoxController::Start(
@ -215,9 +225,9 @@ AddSpecialBoxController::AddSpecialBoxController(
&_additional))
, _peer(peer)
, _role(role)
, _additional(peer, Role::Members)
, _adminDoneCallback(std::move(adminDoneCallback))
, _bannedDoneCallback(std::move(bannedDoneCallback)) {
_additional.fillCreator(_peer);
}
std::unique_ptr<PeerListRow> AddSpecialBoxController::createSearchRow(not_null<PeerData*> peer) {
@ -305,6 +315,7 @@ void AddSpecialBoxController::rebuildChatRows(not_null<ChatData*> chat) {
_onlineSorter->sort();
delegate()->peerListRefreshRows();
setDescriptionText(QString());
}
void AddSpecialBoxController::loadMoreRows() {
@ -329,17 +340,16 @@ void AddSpecialBoxController::loadMoreRows() {
MTP_int(participantsHash)
)).done([=](const MTPchannels_ChannelParticipants &result) {
_loadRequestId = 0;
Auth().api().parseChannelParticipants(channel, result, [&](
auto &session = channel->session();
session.api().parseChannelParticipants(channel, result, [&](
int availableCount,
const QVector<MTPChannelParticipant> &list) {
for (auto &participant : list) {
HandleParticipant(
participant,
&_additional,
[&](auto user) { appendRow(user); });
for (const auto &data : list) {
if (const auto user = _additional.applyParticipant(data)) {
appendRow(user);
}
}
if (auto size = list.size()) {
if (const auto size = list.size()) {
_offset += size;
} else {
// To be sure - wait for a whole empty result list.
@ -372,9 +382,7 @@ template <typename Callback>
bool AddSpecialBoxController::checkInfoLoaded(
not_null<UserData*> user,
Callback callback) {
if (_peer->isChat()
|| (_additional.infoNotLoaded.find(user)
== _additional.infoNotLoaded.end())) {
if (_additional.infoLoaded(user)) {
return true;
}
@ -388,15 +396,10 @@ bool AddSpecialBoxController::checkInfoLoaded(
const auto &participant = result.c_channels_channelParticipant();
App::feedUsers(participant.vusers);
HandleParticipant(
participant.vparticipant,
&_additional,
[](not_null<UserData*>) {});
_additional.infoNotLoaded.erase(user);
_additional.applyParticipant(participant.vparticipant);
callback();
}).fail([this, user, callback](const RPCError &error) {
_additional.infoNotLoaded.erase(user);
_additional.external.emplace(user);
}).fail([=](const RPCError &error) {
_additional.setExternal(user);
callback();
}).send();
return false;
@ -416,6 +419,9 @@ void AddSpecialBoxController::showAdmin(
const auto chat = _peer->asChat();
const auto channel = _peer->asChannel();
const auto showAdminSure = crl::guard(this, [=] {
showAdmin(user, true);
});
// Check restrictions.
const auto canAddMembers = chat
@ -424,24 +430,19 @@ void AddSpecialBoxController::showAdmin(
const auto canBanMembers = chat
? chat->canBanMembers()
: channel->canBanMembers();
const auto weak = base::make_weak(this);
const auto alreadyIt = _additional.adminRights.find(user);
auto currentRights = (_additional.creator == user)
? MTP_chatAdminRights(MTP_flags(~MTPDchatAdminRights::Flag::f_add_admins | MTPDchatAdminRights::Flag::f_add_admins))
: MTP_chatAdminRights(MTP_flags(0));
if (alreadyIt != _additional.adminRights.end()) {
const auto adminRights = _additional.adminRights(user);
if (adminRights.has_value()) {
// The user is already an admin.
currentRights = alreadyIt->second;
} else if (_additional.kicked.find(user) != _additional.kicked.end()) {
} else if (_additional.isKicked(user)) {
// The user is banned.
if (canAddMembers) {
if (canBanMembers) {
if (!sure) {
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_add_admin_unban), [weak, user] {
if (weak) {
weak->showAdmin(user, true);
}
}), LayerOption::KeepOther);
_editBox = Ui::show(
Box<ConfirmBox>(
lang(lng_sure_add_admin_unban),
showAdminSure),
LayerOption::KeepOther);
return;
}
} else {
@ -456,15 +457,15 @@ void AddSpecialBoxController::showAdmin(
LayerOption::KeepOther);
return;
}
} else if (_additional.restrictedRights.find(user) != _additional.restrictedRights.end()) {
} else if (_additional.restrictedRights(user).has_value()) {
// The user is restricted.
if (canBanMembers) {
if (!sure) {
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_add_admin_unban), [weak, user] {
if (weak) {
weak->showAdmin(user, true);
}
}), LayerOption::KeepOther);
_editBox = Ui::show(
Box<ConfirmBox>(
lang(lng_sure_add_admin_unban),
showAdminSure),
LayerOption::KeepOther);
return;
}
} else {
@ -473,7 +474,7 @@ void AddSpecialBoxController::showAdmin(
LayerOption::KeepOther);
return;
}
} else if (_additional.external.find(user) != _additional.external.end()) {
} else if (_additional.isExternal(user)) {
// The user is not in the group yet.
if (canAddMembers) {
if (!sure) {
@ -481,11 +482,11 @@ void AddSpecialBoxController::showAdmin(
((_peer->isChat() || _peer->isMegagroup())
? lng_sure_add_admin_invite
: lng_sure_add_admin_invite_channel));
_editBox = Ui::show(Box<ConfirmBox>(text, [=] {
if (weak) {
weak->showAdmin(user, true);
}
}), LayerOption::KeepOther);
_editBox = Ui::show(
Box<ConfirmBox>(
text,
showAdminSure),
LayerOption::KeepOther);
return;
}
} else {
@ -497,24 +498,29 @@ void AddSpecialBoxController::showAdmin(
}
// Finally show the admin.
auto canNotEdit = (_additional.creator == user)
|| ((alreadyIt != _additional.adminRights.end())
&& (_additional.adminCanEdit.find(user) == _additional.adminCanEdit.end()));
const auto currentRights = _additional.isCreator(user)
? MTPChatAdminRights(MTP_chatAdminRights(
MTP_flags(~MTPDchatAdminRights::Flag::f_add_admins
| MTPDchatAdminRights::Flag::f_add_admins)))
: adminRights
? *adminRights
: MTPChatAdminRights(MTP_chatAdminRights(MTP_flags(0)));
auto box = Box<EditAdminBox>(_peer, user, currentRights);
if (!canNotEdit) {
if (_additional.canAddOrEditAdmin(user)) {
if (chat) {
// #TODO groups autoconv
} else {
box->setSaveCallback(SaveAdminCallback(channel, user, [=](
const auto done = crl::guard(this, [=](
const MTPChatAdminRights &newRights) {
if (weak) {
weak->editAdminDone(user, newRights);
editAdminDone(user, newRights);
});
const auto fail = crl::guard(this, [=] {
if (_editBox) {
_editBox->closeBox();
}
}, [=] {
if (weak && weak->_editBox) {
weak->_editBox->closeBox();
}
}));
});
box->setSaveCallback(
SaveAdminCallback(channel, user, done, fail));
}
}
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
@ -524,24 +530,26 @@ void AddSpecialBoxController::editAdminDone(
not_null<UserData*> user,
const MTPChatAdminRights &rights) {
if (_editBox) _editBox->closeBox();
_additional.restrictedRights.erase(user);
_additional.restrictedBy.erase(user);
_additional.kicked.erase(user);
_additional.external.erase(user);
const auto date = unixtime(); // Incorrect, but ignored.
if (rights.c_chatAdminRights().vflags.v == 0) {
_additional.adminRights.erase(user);
_additional.adminPromotedBy.erase(user);
_additional.adminCanEdit.erase(user);
_additional.applyParticipant(MTP_channelParticipant(
MTP_int(user->bareId()),
MTP_int(date)));
} else {
_additional.adminRights[user] = rights;
_additional.adminCanEdit.emplace(user);
auto it = _additional.adminPromotedBy.find(user);
if (it == _additional.adminPromotedBy.end()) {
_additional.adminPromotedBy.emplace(user, Auth().user());
}
const auto alreadyPromotedBy = _additional.adminPromotedBy(user);
_additional.applyParticipant(MTP_channelParticipantAdmin(
MTP_flags(MTPDchannelParticipantAdmin::Flag::f_can_edit),
MTP_int(user->bareId()),
MTPint(), // inviter_id
MTP_int(alreadyPromotedBy
? alreadyPromotedBy->bareId()
: user->session().userId()),
MTP_int(date),
rights));
}
if (_adminDoneCallback) {
_adminDoneCallback(user, rights);
if (const auto callback = _adminDoneCallback) {
callback(user, rights);
}
}
@ -559,25 +567,24 @@ void AddSpecialBoxController::showRestricted(
const auto chat = _peer->asChat();
const auto channel = _peer->asChannel();
const auto showRestrictedSure = crl::guard(this, [=] {
showRestricted(user, true);
});
// Check restrictions.
const auto weak = base::make_weak(this);
const auto alreadyIt = _additional.restrictedRights.find(user);
auto currentRights = MTP_chatBannedRights(MTP_flags(0), MTP_int(0));
auto hasAdminRights = false;
if (alreadyIt != _additional.restrictedRights.end()) {
const auto restrictedRights = _additional.restrictedRights(user);
if (restrictedRights.has_value()) {
// The user is already banned or restricted.
currentRights = alreadyIt->second;
} else if (_additional.adminRights.find(user) != _additional.adminRights.end() || _additional.creator == user) {
} else if (_additional.adminRights(user).has_value()
|| _additional.isCreator(user)) {
// The user is an admin or creator.
if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()) {
hasAdminRights = true;
if (_additional.canEditAdmin(user)) {
if (!sure) {
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_ban_admin), [weak, user] {
if (weak) {
weak->showRestricted(user, true);
}
}), LayerOption::KeepOther);
_editBox = Ui::show(
Box<ConfirmBox>(
lang(lng_sure_ban_admin),
showRestrictedSure),
LayerOption::KeepOther);
return;
}
} else {
@ -589,20 +596,30 @@ void AddSpecialBoxController::showRestricted(
}
// Finally edit the restricted.
auto box = Box<EditRestrictedBox>(_peer, user, hasAdminRights, currentRights);
const auto currentRights = restrictedRights
? *restrictedRights
: MTPChatBannedRights(MTP_chatBannedRights(
MTP_flags(0),
MTP_int(0)));
auto box = Box<EditRestrictedBox>(
_peer,
user,
_additional.adminRights(user).has_value(),
currentRights);
if (chat) {
// #TODO groups autoconv
} else {
box->setSaveCallback(SaveRestrictedCallback(channel, user, [=](
const auto done = crl::guard(this, [=](
const MTPChatBannedRights &newRights) {
if (const auto strong = weak.get()) {
strong->editRestrictedDone(user, newRights);
editRestrictedDone(user, newRights);
});
const auto fail = crl::guard(this, [=] {
if (_editBox) {
_editBox->closeBox();
}
}, [=] {
if (weak && weak->_editBox) {
weak->_editBox->closeBox();
}
}));
});
box->setSaveCallback(
SaveRestrictedCallback(channel, user, done, fail));
}
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
}
@ -611,24 +628,28 @@ void AddSpecialBoxController::editRestrictedDone(
not_null<UserData*> user,
const MTPChatBannedRights &rights) {
if (_editBox) _editBox->closeBox();
_additional.adminRights.erase(user);
_additional.adminCanEdit.erase(user);
_additional.adminPromotedBy.erase(user);
const auto date = unixtime(); // Incorrect, but ignored.
if (rights.c_chatBannedRights().vflags.v == 0) {
_additional.restrictedRights.erase(user);
_additional.restrictedBy.erase(user);
_additional.kicked.erase(user);
_additional.applyParticipant(MTP_channelParticipant(
MTP_int(user->bareId()),
MTP_int(date)));
} else {
_additional.restrictedRights[user] = rights;
if (rights.c_chatBannedRights().vflags.v & MTPDchatBannedRights::Flag::f_view_messages) {
_additional.kicked.emplace(user);
} else {
_additional.kicked.erase(user);
}
_additional.restrictedBy.emplace(user, Auth().user());
const auto kicked = rights.c_chatBannedRights().is_view_messages();
const auto alreadyRestrictedBy = _additional.restrictedBy(user);
_additional.applyParticipant(MTP_channelParticipantBanned(
MTP_flags(kicked
? MTPDchannelParticipantBanned::Flag::f_left
: MTPDchannelParticipantBanned::Flag(0)),
MTP_int(user->bareId()),
MTP_int(alreadyRestrictedBy
? alreadyRestrictedBy->bareId()
: user->session().userId()),
MTP_int(date),
rights));
}
if (_bannedDoneCallback) {
_bannedDoneCallback(user, rights);
if (const auto callback = _bannedDoneCallback) {
callback(user, rights);
}
}
@ -639,17 +660,21 @@ void AddSpecialBoxController::kickUser(
return;
}
const auto kickUserSure = crl::guard(this, [=] {
kickUser(user, true);
});
// Check restrictions.
auto weak = base::make_weak(this);
if (_additional.adminRights.find(user) != _additional.adminRights.end() || _additional.creator == user) {
if (_additional.adminRights(user).has_value()
|| _additional.isCreator(user)) {
// The user is an admin or creator.
if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()) {
if (_additional.canEditAdmin(user)) {
if (!sure) {
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_ban_admin), [weak, user] {
if (weak) {
weak->kickUser(user, true);
}
}), LayerOption::KeepOther);
_editBox = Ui::show(
Box<ConfirmBox>(
lang(lng_sure_ban_admin),
kickUserSure),
LayerOption::KeepOther);
return;
}
} else {
@ -665,25 +690,22 @@ void AddSpecialBoxController::kickUser(
const auto text = ((_peer->isChat() || _peer->isMegagroup())
? lng_sure_ban_user_group
: lng_sure_ban_user_channel)(lt_user, App::peerName(user));
_editBox = Ui::show(Box<ConfirmBox>(text, [=] {
if (weak) {
weak->kickUser(user, true);
}
}), LayerOption::KeepOther);
_editBox = Ui::show(
Box<ConfirmBox>(text, kickUserSure),
LayerOption::KeepOther);
return;
}
auto currentRights = MTP_chatBannedRights(MTP_flags(0), MTP_int(0));
auto alreadyIt = _additional.restrictedRights.find(user);
if (alreadyIt != _additional.restrictedRights.end()) {
// The user is already banned or restricted.
currentRights = alreadyIt->second;
}
const auto restrictedRights = _additional.restrictedRights(user);
const auto currentRights = restrictedRights
? *restrictedRights
: MTPChatBannedRights(MTP_chatBannedRights(
MTP_flags(0),
MTP_int(0)));
auto &session = _peer->session();
if (const auto chat = _peer->asChat()) {
// #TODO groups
session.api().kickParticipant(chat, user);
} else if (const auto channel = _peer->asChannel()) {
const auto callback = SaveRestrictedCallback(channel, user, [](
const MTPChatBannedRights &newRights) {}, [] {});
callback(currentRights, ChannelData::KickedRestrictedRights());
session.api().kickParticipant(channel, user, currentRights);
}
}
@ -708,90 +730,9 @@ std::unique_ptr<PeerListRow> AddSpecialBoxController::createRow(
return std::make_unique<PeerListRow>(user);
}
template <typename Callback>
void AddSpecialBoxController::HandleParticipant(
const MTPChannelParticipant &participant,
not_null<Additional*> additional,
Callback callback) {
switch (participant.type()) {
case mtpc_channelParticipantAdmin: {
auto &admin = participant.c_channelParticipantAdmin();
if (auto user = App::userLoaded(admin.vuser_id.v)) {
additional->infoNotLoaded.erase(user);
additional->restrictedRights.erase(user);
additional->kicked.erase(user);
additional->restrictedBy.erase(user);
additional->adminRights[user] = admin.vadmin_rights;
if (admin.is_can_edit()) {
additional->adminCanEdit.emplace(user);
} else {
additional->adminCanEdit.erase(user);
}
if (auto promoted = App::userLoaded(admin.vpromoted_by.v)) {
auto it = additional->adminPromotedBy.find(user);
if (it == additional->adminPromotedBy.end()) {
additional->adminPromotedBy.emplace(user, promoted);
} else {
it->second = promoted;
}
} else {
LOG(("API Error: No user %1 for admin promoted by.").arg(admin.vpromoted_by.v));
}
callback(user);
}
} break;
case mtpc_channelParticipantCreator: {
auto &creator = participant.c_channelParticipantCreator();
if (auto user = App::userLoaded(creator.vuser_id.v)) {
additional->infoNotLoaded.erase(user);
additional->creator = user;
callback(user);
}
} break;
case mtpc_channelParticipantBanned: {
auto &banned = participant.c_channelParticipantBanned();
if (auto user = App::userLoaded(banned.vuser_id.v)) {
additional->infoNotLoaded.erase(user);
additional->adminRights.erase(user);
additional->adminCanEdit.erase(user);
additional->adminPromotedBy.erase(user);
if (banned.is_left()) {
additional->kicked.emplace(user);
} else {
additional->kicked.erase(user);
}
additional->restrictedRights[user] = banned.vbanned_rights;
if (auto kickedby = App::userLoaded(banned.vkicked_by.v)) {
auto it = additional->restrictedBy.find(user);
if (it == additional->restrictedBy.end()) {
additional->restrictedBy.emplace(user, kickedby);
} else {
it->second = kickedby;
}
}
callback(user);
}
} break;
case mtpc_channelParticipant: {
auto &data = participant.c_channelParticipant();
if (auto user = App::userLoaded(data.vuser_id.v)) {
additional->infoNotLoaded.erase(user);
additional->adminRights.erase(user);
additional->adminCanEdit.erase(user);
additional->adminPromotedBy.erase(user);
additional->restrictedRights.erase(user);
additional->kicked.erase(user);
additional->restrictedBy.erase(user);
callback(user);
}
} break;
default: Unexpected("Participant type in AddSpecialBoxController::HandleParticipant()");
}
}
AddSpecialBoxSearchController::AddSpecialBoxSearchController(
not_null<PeerData*> peer,
not_null<Additional*> additional)
not_null<ParticipantsAdditionalData*> additional)
: _peer(peer)
, _additional(additional)
, _timer([=] { searchOnServer(); }) {
@ -914,7 +855,8 @@ void AddSpecialBoxSearchController::searchParticipantsDone(
const auto channel = _peer->asChannel();
auto query = _query;
if (requestId) {
Auth().api().parseChannelParticipants(channel, result, [&](auto&&...) {
auto &session = channel->session();
session.api().parseChannelParticipants(channel, result, [&](auto&&...) {
auto it = _participantsQueries.find(requestId);
if (it != _participantsQueries.cend()) {
query = it->second.text;
@ -945,14 +887,10 @@ void AddSpecialBoxSearchController::searchParticipantsDone(
loadMoreRows();
}
}
auto addUser = [&](auto user) {
delegate()->peerListSearchAddRow(user);
};
for (auto &participant : list) {
AddSpecialBoxController::HandleParticipant(
participant,
_additional,
addUser);
for (const auto &data : list) {
if (const auto user = _additional->applyParticipant(data)) {
delegate()->peerListSearchAddRow(user);
}
}
_offset += list.size();
}, [&](mtpTypeId type) {
@ -984,7 +922,9 @@ void AddSpecialBoxSearchController::requestGlobal() {
_globalQueries.emplace(_requestId, _query);
}
void AddSpecialBoxSearchController::searchGlobalDone(mtpRequestId requestId, const MTPcontacts_Found &result) {
void AddSpecialBoxSearchController::searchGlobalDone(
mtpRequestId requestId,
const MTPcontacts_Found &result) {
Expects(result.type() == mtpc_contacts_found);
auto &found = result.c_contacts_found();
@ -1001,20 +941,11 @@ void AddSpecialBoxSearchController::searchGlobalDone(mtpRequestId requestId, con
}
const auto feedList = [&](const MTPVector<MTPPeer> &list) {
const auto contains = [](const auto &map, const auto &value) {
return map.find(value) != map.end();
};
for (const auto &mtpPeer : list.v) {
const auto peerId = peerFromMTP(mtpPeer);
if (const auto peer = App::peerLoaded(peerId)) {
if (const auto user = peer->asUser()) {
if (_additional->creator != user
&& !contains(_additional->adminRights, user)
&& !contains(_additional->restrictedRights, user)
&& !contains(_additional->external, user)
&& !contains(_additional->kicked, user)) {
_additional->infoNotLoaded.emplace(user);
}
_additional->checkForLoaded(user);
delegate()->peerListSearchAddRow(user);
}
}

View File

@ -59,7 +59,6 @@ class AddSpecialBoxController
, public base::has_weak_ptr {
public:
using Role = ParticipantsBoxController::Role;
using Additional = ParticipantsBoxController::Additional;
using AdminDoneCallback = Fn<void(
not_null<UserData*> user,
@ -80,13 +79,6 @@ public:
std::unique_ptr<PeerListRow> createSearchRow(
not_null<PeerData*> peer) override;
// Callback(not_null<UserData*>)
template <typename Callback>
static void HandleParticipant(
const MTPChannelParticipant &participant,
not_null<Additional*> additional,
Callback callback);
private:
template <typename Callback>
bool checkInfoLoaded(not_null<UserData*> user, Callback callback);
@ -116,7 +108,7 @@ private:
int _offset = 0;
mtpRequestId _loadRequestId = 0;
bool _allLoaded = false;
Additional _additional;
ParticipantsAdditionalData _additional;
std::unique_ptr<ParticipantsOnlineSorter> _onlineSorter;
QPointer<BoxContent> _editBox;
AdminDoneCallback _adminDoneCallback;
@ -130,11 +122,10 @@ class AddSpecialBoxSearchController
, private MTP::Sender {
public:
using Role = ParticipantsBoxController::Role;
using Additional = ParticipantsBoxController::Additional;
AddSpecialBoxSearchController(
not_null<PeerData*> peer,
not_null<Additional*> additional);
not_null<ParticipantsAdditionalData*> additional);
void searchQuery(const QString &query) override;
bool isLoading() override;
@ -166,7 +157,7 @@ private:
void requestGlobal();
not_null<PeerData*> _peer;
not_null<Additional*> _additional;
not_null<ParticipantsAdditionalData*> _additional;
base::Timer _timer;
QString _query;

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/weak_ptr.h"
#include "info/profile/info_profile_members_controllers.h"
struct MegagroupInfo;
namespace Window {
class Navigation;
} // namespace Window
@ -34,6 +36,14 @@ Fn<void(
Fn<void(const MTPChatBannedRights &newRights)> onDone,
Fn<void()> onFail);
enum class ParticipantsRole {
Profile,
Members,
Admins,
Restricted,
Kicked,
};
class ParticipantsOnlineSorter : private base::Subscriber {
public:
ParticipantsOnlineSorter(
@ -54,6 +64,62 @@ private:
};
class ParticipantsAdditionalData {
public:
using Role = ParticipantsRole;
ParticipantsAdditionalData(not_null<PeerData*> peer, Role role);
UserData *applyParticipant(const MTPChannelParticipant &data);
UserData *applyParticipant(
const MTPChannelParticipant &data,
Role overrideRole);
void setExternal(not_null<UserData*> user);
void checkForLoaded(not_null<UserData*> user);
void fillFromPeer();
[[nodiscard]] bool infoLoaded(not_null<UserData*> user) const;
[[nodiscard]] bool canEditAdmin(not_null<UserData*> user) const;
[[nodiscard]] bool canAddOrEditAdmin(not_null<UserData*> user) const;
[[nodiscard]] bool canRestrictUser(not_null<UserData*> user) const;
[[nodiscard]] std::optional<MTPChatAdminRights> adminRights(
not_null<UserData*> user) const;
[[nodiscard]] std::optional<MTPChatBannedRights> restrictedRights(
not_null<UserData*> user) const;
[[nodiscard]] bool isCreator(not_null<UserData*> user) const;
[[nodiscard]] bool isExternal(not_null<UserData*> user) const;
[[nodiscard]] bool isKicked(not_null<UserData*> user) const;
[[nodiscard]] UserData *adminPromotedBy(not_null<UserData*> user) const;
[[nodiscard]] UserData *restrictedBy(not_null<UserData*> user) const;
private:
UserData *applyCreator(const MTPDchannelParticipantCreator &data);
UserData *applyAdmin(const MTPDchannelParticipantAdmin &data);
UserData *applyRegular(MTPint userId);
UserData *applyBanned(const MTPDchannelParticipantBanned &data);
void fillFromChat(not_null<ChatData*> chat);
void fillFromChannel(not_null<ChannelData*> channel);
not_null<PeerData*> _peer;
Role _role = Role::Members;
UserData *_creator = nullptr;
// Data for chats.
base::flat_set<not_null<UserData*>> _members;
base::flat_set<not_null<UserData*>> _admins;
// Data for channels.
base::flat_map<not_null<UserData*>, MTPChatAdminRights> _adminRights;
base::flat_set<not_null<UserData*>> _adminCanEdit;
base::flat_map<not_null<UserData*>, not_null<UserData*>> _adminPromotedBy;
std::map<not_null<UserData*>, MTPChatBannedRights> _restrictedRights;
std::set<not_null<UserData*>> _kicked;
std::map<not_null<UserData*>, not_null<UserData*>> _restrictedBy;
std::set<not_null<UserData*>> _external;
std::set<not_null<UserData*>> _infoNotLoaded;
};
// Viewing admins, banned or restricted users list with search.
class ParticipantsBoxController
: public PeerListController
@ -61,32 +127,13 @@ class ParticipantsBoxController
, private MTP::Sender
, public base::has_weak_ptr {
public:
enum class Role {
Profile,
Members,
Admins,
Restricted,
Kicked,
};
using Role = ParticipantsRole;
static void Start(
not_null<Window::Navigation*> navigation,
not_null<PeerData*> peer,
Role role);
struct Additional {
void fillCreator(not_null<PeerData*> peer);
std::map<not_null<UserData*>, MTPChatAdminRights> adminRights;
std::set<not_null<UserData*>> adminCanEdit;
std::map<not_null<UserData*>, not_null<UserData*>> adminPromotedBy;
std::map<not_null<UserData*>, MTPChatBannedRights> restrictedRights;
std::set<not_null<UserData*>> kicked;
std::map<not_null<UserData*>, not_null<UserData*>> restrictedBy;
std::set<not_null<UserData*>> external;
std::set<not_null<UserData*>> infoNotLoaded;
UserData *creator = nullptr;
};
ParticipantsBoxController(
not_null<Window::Navigation*> navigation,
not_null<PeerData*> peer,
@ -109,14 +156,6 @@ public:
std::unique_ptr<PeerListState> saveState() const override;
void restoreState(std::unique_ptr<PeerListState> state) override;
// Callback(not_null<UserData*>)
template <typename Callback>
static void HandleParticipant(
const MTPChannelParticipant &participant,
Role role,
not_null<Additional*> additional,
Callback callback);
rpl::producer<int> onlineCountValue() const override;
protected:
@ -128,22 +167,26 @@ private:
using Type = Row::Type;
using Rights = Row::Rights;
struct SavedState : SavedStateBase {
explicit SavedState(const ParticipantsAdditionalData &additional);
using SearchStateBase = PeerListSearchController::SavedStateBase;
std::unique_ptr<SearchStateBase> searchState;
int offset = 0;
bool allLoaded = false;
bool wasLoading = false;
Additional additional;
ParticipantsAdditionalData additional;
rpl::lifetime lifetime;
};
static std::unique_ptr<PeerListSearchController> CreateSearchController(
not_null<PeerData*> peer,
Role role,
not_null<Additional*> additional);
not_null<ParticipantsAdditionalData*> additional);
void prepareChatRows(not_null<ChatData*> chat);
void rebuildChatRows(not_null<ChatData*> chat);
void rebuildChatParticipants(not_null<ChatData*> chat);
void rebuildChatAdmins(not_null<ChatData*> chat);
void rebuildRowTypes();
void addNewItem();
@ -169,12 +212,8 @@ private:
bool removeRow(not_null<UserData*> user);
void refreshCustomStatus(not_null<PeerListRow*> row) const;
bool feedMegagroupLastParticipants();
void refreshOnlineCount();
Type computeType(not_null<UserData*> user) const;
void recomputeTypeFor(not_null<UserData*> user);
bool canEditAdmin(not_null<UserData*> user) const;
bool canRestrictUser(not_null<UserData*> user) const;
bool canEditAdminByRights(not_null<UserData*> user) const;
not_null<Window::Navigation*> _navigation;
not_null<PeerData*> _peer;
@ -182,7 +221,7 @@ private:
int _offset = 0;
mtpRequestId _loadRequestId = 0;
bool _allLoaded = false;
Additional _additional;
ParticipantsAdditionalData _additional;
std::unique_ptr<ParticipantsOnlineSorter> _onlineSorter;
QPointer<BoxContent> _editBox;
QPointer<PeerListBox> _addBox;
@ -195,12 +234,11 @@ class ParticipantsBoxSearchController
, private MTP::Sender {
public:
using Role = ParticipantsBoxController::Role;
using Additional = ParticipantsBoxController::Additional;
ParticipantsBoxSearchController(
not_null<ChannelData*> channel,
Role role,
not_null<Additional*> additional);
not_null<ParticipantsAdditionalData*> additional);
void searchQuery(const QString &query) override;
bool isLoading() override;
@ -234,7 +272,7 @@ private:
not_null<ChannelData*> _channel;
Role _role = Role::Restricted;
not_null<Additional*> _additional;
not_null<ParticipantsAdditionalData*> _additional;
base::Timer _timer;
QString _query;

View File

@ -60,25 +60,25 @@ protected:
public:
virtual ~PeerData();
Data::Session &owner() const;
AuthSession &session() const;
[[nodiscard]] Data::Session &owner() const;
[[nodiscard]] AuthSession &session() const;
bool isUser() const {
[[nodiscard]] bool isUser() const {
return peerIsUser(id);
}
bool isChat() const {
[[nodiscard]] bool isChat() const {
return peerIsChat(id);
}
bool isChannel() const {
[[nodiscard]] bool isChannel() const {
return peerIsChannel(id);
}
bool isSelf() const {
[[nodiscard]] bool isSelf() const {
return (input.type() == mtpc_inputPeerSelf);
}
bool isVerified() const;
bool isMegagroup() const;
[[nodiscard]] bool isVerified() const;
[[nodiscard]] bool isMegagroup() const;
std::optional<TimeId> notifyMuteUntil() const {
[[nodiscard]] std::optional<TimeId> notifyMuteUntil() const {
return _notify.muteUntil();
}
bool notifyChange(const MTPPeerNotifySettings &settings) {
@ -89,66 +89,54 @@ public:
std::optional<bool> silentPosts) {
return _notify.change(muteForSeconds, silentPosts);
}
bool notifySettingsUnknown() const {
[[nodiscard]] bool notifySettingsUnknown() const {
return _notify.settingsUnknown();
}
std::optional<bool> notifySilentPosts() const {
[[nodiscard]] std::optional<bool> notifySilentPosts() const {
return _notify.silentPosts();
}
MTPinputPeerNotifySettings notifySerialize() const {
[[nodiscard]] MTPinputPeerNotifySettings notifySerialize() const {
return _notify.serialize();
}
bool canWrite() const;
bool amRestricted(ChatRestriction right) const;
[[nodiscard]] bool canWrite() const;
[[nodiscard]] bool amRestricted(ChatRestriction right) const;
UserData *asUser();
const UserData *asUser() const;
ChatData *asChat();
const ChatData *asChat() const;
ChannelData *asChannel();
const ChannelData *asChannel() const;
ChannelData *asMegagroup();
const ChannelData *asMegagroup() const;
[[nodiscard]] UserData *asUser();
[[nodiscard]] const UserData *asUser() const;
[[nodiscard]] ChatData *asChat();
[[nodiscard]] const ChatData *asChat() const;
[[nodiscard]] ChannelData *asChannel();
[[nodiscard]] const ChannelData *asChannel() const;
[[nodiscard]] ChannelData *asMegagroup();
[[nodiscard]] const ChannelData *asMegagroup() const;
ChatData *migrateFrom() const;
ChannelData *migrateTo() const;
Data::Feed *feed() const;
[[nodiscard]] ChatData *migrateFrom() const;
[[nodiscard]] ChannelData *migrateTo() const;
[[nodiscard]] Data::Feed *feed() const;
void updateFull();
void updateFullForced();
void fullUpdated();
bool wasFullUpdated() const {
[[nodiscard]] bool wasFullUpdated() const {
return (_lastFullUpdate != 0);
}
const Text &dialogName() const;
const QString &shortName() const;
QString userName() const;
[[nodiscard]] const Text &dialogName() const;
[[nodiscard]] const QString &shortName() const;
[[nodiscard]] QString userName() const;
const PeerId id;
int32 bareId() const {
[[nodiscard]] int32 bareId() const {
return int32(uint32(id & 0xFFFFFFFFULL));
}
QString name;
Text nameText;
const base::flat_set<QString> &nameWords() const {
[[nodiscard]] const base::flat_set<QString> &nameWords() const {
return _nameWords;
}
const base::flat_set<QChar> &nameFirstLetters() const {
[[nodiscard]] const base::flat_set<QChar> &nameFirstLetters() const {
return _nameFirstLetters;
}
enum LoadedStatus {
NotLoaded = 0x00,
MinimalLoaded = 0x01,
FullLoaded = 0x02,
};
LoadedStatus loadedStatus = NotLoaded;
MTPinputPeer input;
void setUserpic(
PhotoId photoId,
const StorageImageLocation &location,
@ -178,45 +166,43 @@ public:
int y,
int size) const;
void loadUserpic(bool loadFirst = false, bool prior = true);
bool userpicLoaded() const;
bool useEmptyUserpic() const;
StorageKey userpicUniqueKey() const;
[[nodiscard]] bool userpicLoaded() const;
[[nodiscard]] bool useEmptyUserpic() const;
[[nodiscard]] StorageKey userpicUniqueKey() const;
void saveUserpic(const QString &path, int size) const;
void saveUserpicRounded(const QString &path, int size) const;
QPixmap genUserpic(int size) const;
QPixmap genUserpicRounded(int size) const;
StorageImageLocation userpicLocation() const {
[[nodiscard]] QPixmap genUserpic(int size) const;
[[nodiscard]] QPixmap genUserpicRounded(int size) const;
[[nodiscard]] StorageImageLocation userpicLocation() const {
return _userpicLocation;
}
bool userpicPhotoUnknown() const {
[[nodiscard]] bool userpicPhotoUnknown() const {
return (_userpicPhotoId == kUnknownPhotoId);
}
PhotoId userpicPhotoId() const {
[[nodiscard]] PhotoId userpicPhotoId() const {
return userpicPhotoUnknown() ? 0 : _userpicPhotoId;
}
Data::FileOrigin userpicOrigin() const;
Data::FileOrigin userpicPhotoOrigin() const;
int nameVersion = 1;
[[nodiscard]] Data::FileOrigin userpicOrigin() const;
[[nodiscard]] Data::FileOrigin userpicPhotoOrigin() const;
// If this string is not empty we must not allow to open the
// conversation and we must show this string instead.
virtual QString unavailableReason() const {
[[nodiscard]] virtual QString unavailableReason() const {
return QString();
}
ClickHandlerPtr createOpenLink();
const ClickHandlerPtr &openLink() {
[[nodiscard]] ClickHandlerPtr createOpenLink();
[[nodiscard]] const ClickHandlerPtr &openLink() {
if (!_openLink) {
_openLink = createOpenLink();
}
return _openLink;
}
ImagePtr currentUserpic() const;
[[nodiscard]] ImagePtr currentUserpic() const;
bool canPinMessages() const;
MsgId pinnedMessageId() const {
[[nodiscard]] bool canPinMessages() const;
[[nodiscard]] MsgId pinnedMessageId() const {
return _pinnedMessageId;
}
void setPinnedMessageId(MsgId messageId);
@ -230,6 +216,20 @@ public:
return _about;
}
enum LoadedStatus {
NotLoaded = 0x00,
MinimalLoaded = 0x01,
FullLoaded = 0x02,
};
const PeerId id;
QString name;
Text nameText;
LoadedStatus loadedStatus = NotLoaded;
MTPinputPeer input;
int nameVersion = 1;
protected:
void updateNameDelayed(
const QString &newName,