2017-06-16 11:05:13 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#include "profile/profile_channel_controllers.h"
|
|
|
|
|
2017-08-14 12:53:49 +00:00
|
|
|
#include "boxes/peer_list_controllers.h"
|
2017-06-16 11:05:13 +00:00
|
|
|
#include "boxes/edit_participant_box.h"
|
|
|
|
#include "boxes/confirm_box.h"
|
2017-08-14 12:53:49 +00:00
|
|
|
#include "boxes/add_contact_box.h"
|
2017-11-20 19:54:05 +00:00
|
|
|
#include "core/tl_help.h"
|
2017-11-21 11:59:18 +00:00
|
|
|
#include "base/overload.h"
|
2017-06-16 11:05:13 +00:00
|
|
|
#include "auth_session.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "mainwidget.h"
|
2017-09-26 18:44:58 +00:00
|
|
|
#include "observer_peer.h"
|
2017-06-16 11:05:13 +00:00
|
|
|
#include "dialogs/dialogs_indexed_list.h"
|
2017-10-22 17:06:57 +00:00
|
|
|
#include "ui/widgets/popup_menu.h"
|
2017-10-03 13:05:58 +00:00
|
|
|
#include "window/window_controller.h"
|
2017-06-16 11:05:13 +00:00
|
|
|
|
|
|
|
namespace Profile {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kParticipantsFirstPageCount = 16;
|
|
|
|
constexpr auto kParticipantsPerPage = 200;
|
2017-09-26 18:44:58 +00:00
|
|
|
constexpr auto kSortByOnlineDelay = TimeMs(1000);
|
2017-06-16 11:05:13 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-09-26 18:44:58 +00:00
|
|
|
ParticipantsBoxController::ParticipantsBoxController(
|
2017-10-03 13:05:58 +00:00
|
|
|
not_null<Window::Controller*> window,
|
2017-09-26 18:44:58 +00:00
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
Role role)
|
2017-09-26 17:57:01 +00:00
|
|
|
: PeerListController(CreateSearchController(channel, role, &_additional))
|
2017-10-03 13:05:58 +00:00
|
|
|
, _window(window)
|
2017-06-16 11:05:13 +00:00
|
|
|
, _channel(channel)
|
|
|
|
, _role(role) {
|
|
|
|
if (_channel->mgInfo) {
|
|
|
|
_additional.creator = _channel->mgInfo->creator;
|
|
|
|
}
|
2017-09-26 18:44:58 +00:00
|
|
|
if (_role == Role::Profile) {
|
|
|
|
setupSortByOnline();
|
2017-10-22 17:06:57 +00:00
|
|
|
setupListChangeViewers();
|
2017-09-26 18:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::setupSortByOnline() {
|
|
|
|
_sortByOnlineTimer.setCallback([this] { sortByOnline(); });
|
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(
|
|
|
|
UpdateFlag::UserOnlineChanged,
|
|
|
|
[this](const Notify::PeerUpdate &update) {
|
|
|
|
if (auto row = delegate()->peerListFindRow(
|
|
|
|
update.peer->id)) {
|
|
|
|
row->refreshStatus();
|
|
|
|
sortByOnlineDelayed();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-10-22 17:06:57 +00:00
|
|
|
void ParticipantsBoxController::setupListChangeViewers() {
|
2017-11-17 16:06:20 +00:00
|
|
|
if (!_channel->isMegagroup()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-22 17:06:57 +00:00
|
|
|
Auth().data().megagroupParticipantAdded(_channel)
|
|
|
|
| rpl::start_with_next([this](not_null<UserData*> user) {
|
|
|
|
if (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
if (delegate()->peerListRowAt(0)->peer() == user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (auto row = delegate()->peerListFindRow(user->id)) {
|
|
|
|
delegate()->peerListPartitionRows([user](const PeerListRow &row) {
|
|
|
|
return (row.peer() == user);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
delegate()->peerListPrependRow(createRow(user));
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
sortByOnline();
|
|
|
|
}
|
|
|
|
}, lifetime());
|
|
|
|
Auth().data().megagroupParticipantRemoved(_channel)
|
|
|
|
| rpl::start_with_next([this](not_null<UserData*> user) {
|
|
|
|
if (auto row = delegate()->peerListFindRow(user->id)) {
|
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
}
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:44:58 +00:00
|
|
|
void ParticipantsBoxController::sortByOnlineDelayed() {
|
|
|
|
if (!_sortByOnlineTimer.isActive()) {
|
|
|
|
_sortByOnlineTimer.callOnce(kSortByOnlineDelay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::sortByOnline() {
|
|
|
|
if (_role != Role::Profile
|
2017-11-17 16:06:20 +00:00
|
|
|
|| !_channel->isMegagroup()
|
2017-09-26 18:44:58 +00:00
|
|
|
|| _channel->membersCount() > Global::ChatSizeMax()) {
|
2017-10-22 12:07:57 +00:00
|
|
|
_onlineCount = 0;
|
2017-09-26 18:44:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto now = unixtime();
|
|
|
|
delegate()->peerListSortRows([now](
|
2017-10-22 12:07:57 +00:00
|
|
|
const PeerListRow &a,
|
|
|
|
const PeerListRow &b) {
|
2017-09-26 18:44:58 +00:00
|
|
|
return App::onlineForSort(a.peer()->asUser(), now) >
|
|
|
|
App::onlineForSort(b.peer()->asUser(), now);
|
|
|
|
});
|
2017-10-22 12:07:57 +00:00
|
|
|
refreshOnlineCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::refreshOnlineCount() {
|
|
|
|
Expects(_role == Role::Profile);
|
|
|
|
Expects(_channel->membersCount() <= Global::ChatSizeMax());
|
|
|
|
|
|
|
|
auto now = unixtime();
|
|
|
|
auto left = 0, right = delegate()->peerListFullRowsCount();
|
|
|
|
while (right > left) {
|
|
|
|
auto middle = (left + right) / 2;
|
|
|
|
auto row = delegate()->peerListRowAt(middle);
|
|
|
|
if (App::onlineColorUse(row->peer()->asUser(), now)) {
|
|
|
|
left = middle + 1;
|
|
|
|
} else {
|
|
|
|
right = middle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_onlineCount = left;
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
std::unique_ptr<PeerListSearchController>
|
|
|
|
ParticipantsBoxController::CreateSearchController(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
Role role,
|
|
|
|
not_null<Additional*> additional) {
|
2017-07-19 08:18:20 +00:00
|
|
|
// In admins box complex search is used for adding new admins.
|
|
|
|
if (role != Role::Admins || channel->canAddAdmins()) {
|
2017-09-26 17:57:01 +00:00
|
|
|
return std::make_unique<ParticipantsBoxSearchController>(
|
|
|
|
channel,
|
|
|
|
role,
|
|
|
|
additional);
|
2017-07-19 08:18:20 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:05:58 +00:00
|
|
|
void ParticipantsBoxController::Start(
|
|
|
|
not_null<Window::Controller*> window,
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
Role role) {
|
|
|
|
auto controller = std::make_unique<ParticipantsBoxController>(
|
|
|
|
window,
|
|
|
|
channel,
|
|
|
|
role);
|
2017-08-17 08:31:24 +00:00
|
|
|
auto initBox = [role, channel, controller = controller.get()](not_null<PeerListBox*> box) {
|
2017-06-16 11:05:13 +00:00
|
|
|
box->addButton(langFactory(lng_close), [box] { box->closeBox(); });
|
|
|
|
auto canAddNewItem = [role, channel] {
|
|
|
|
switch (role) {
|
2017-07-14 06:46:57 +00:00
|
|
|
case Role::Members: return !channel->isMegagroup() && channel->canAddMembers() && (channel->membersCount() < Global::ChatSizeMax());
|
2017-06-16 11:05:13 +00:00
|
|
|
case Role::Admins: return channel->canAddAdmins();
|
|
|
|
case Role::Restricted:
|
|
|
|
case Role::Kicked: return channel->canBanMembers();
|
|
|
|
}
|
|
|
|
Unexpected("Role value in ParticipantsBoxController::Start()");
|
|
|
|
};
|
|
|
|
auto addNewItemText = [role] {
|
|
|
|
switch (role) {
|
2017-07-14 06:46:57 +00:00
|
|
|
case Role::Members: return langFactory(lng_channel_add_members);
|
2017-06-16 11:05:13 +00:00
|
|
|
case Role::Admins: return langFactory(lng_channel_add_admin);
|
|
|
|
case Role::Restricted: return langFactory(lng_channel_add_restricted);
|
|
|
|
case Role::Kicked: return langFactory(lng_channel_add_banned);
|
|
|
|
}
|
|
|
|
Unexpected("Role value in ParticipantsBoxController::Start()");
|
|
|
|
};
|
|
|
|
if (canAddNewItem()) {
|
|
|
|
box->addLeftButton(addNewItemText(), [controller] { controller->addNewItem(); });
|
|
|
|
}
|
|
|
|
};
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<PeerListBox>(std::move(controller), std::move(initBox)),
|
|
|
|
LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::addNewItem() {
|
2017-09-26 17:57:01 +00:00
|
|
|
Expects(_role != Role::Profile);
|
2017-11-17 16:06:20 +00:00
|
|
|
|
2017-07-14 06:46:57 +00:00
|
|
|
if (_role == Role::Members) {
|
|
|
|
if (_channel->membersCount() >= Global::ChatSizeMax()) {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<MaxInviteBox>(_channel),
|
|
|
|
LayerOption::KeepOther);
|
2017-07-14 06:46:57 +00:00
|
|
|
} else {
|
2017-08-17 08:31:24 +00:00
|
|
|
auto already = std::vector<not_null<UserData*>>();
|
2017-08-14 12:53:49 +00:00
|
|
|
already.reserve(delegate()->peerListFullRowsCount());
|
2017-07-14 06:46:57 +00:00
|
|
|
for (auto i = 0, count = delegate()->peerListFullRowsCount(); i != count; ++i) {
|
2017-08-14 12:53:49 +00:00
|
|
|
already.push_back(delegate()->peerListRowAt(i)->peer()->asUser());
|
2017-07-14 06:46:57 +00:00
|
|
|
}
|
2017-08-14 12:48:11 +00:00
|
|
|
AddParticipantsBoxController::Start(_channel, { already.begin(), already.end() });
|
2017-07-14 06:46:57 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-08-17 08:31:24 +00:00
|
|
|
_addBox = Ui::show(Box<PeerListBox>(std::make_unique<AddParticipantBoxController>(_channel, _role, [weak](not_null<UserData*> user, const MTPChannelAdminRights &rights) {
|
2017-06-16 13:47:44 +00:00
|
|
|
if (weak) {
|
|
|
|
weak->editAdminDone(user, rights);
|
|
|
|
}
|
2017-08-17 08:31:24 +00:00
|
|
|
}, [weak](not_null<UserData*> user, const MTPChannelBannedRights &rights) {
|
2017-06-16 13:47:44 +00:00
|
|
|
if (weak) {
|
|
|
|
weak->editRestrictedDone(user, rights);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-08-17 08:31:24 +00:00
|
|
|
}), [](not_null<PeerListBox*> box) {
|
2017-06-16 11:05:13 +00:00
|
|
|
box->addButton(langFactory(lng_cancel), [box] { box->closeBox(); });
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::peerListSearchAddRow(not_null<PeerData*> peer) {
|
2017-06-16 11:05:13 +00:00
|
|
|
PeerListController::peerListSearchAddRow(peer);
|
|
|
|
if (_role == Role::Restricted && delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
setDescriptionText(QString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
std::unique_ptr<PeerListRow> ParticipantsBoxController::createSearchRow(
|
|
|
|
not_null<PeerData*> peer) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (auto user = peer->asUser()) {
|
|
|
|
return createRow(user);
|
|
|
|
}
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
std::unique_ptr<PeerListRow> ParticipantsBoxController::createRestoredRow(
|
|
|
|
not_null<PeerData*> peer) {
|
|
|
|
if (auto user = peer->asUser()) {
|
|
|
|
return createRow(user);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PeerListState> ParticipantsBoxController::saveState() {
|
|
|
|
Expects(_role == Role::Profile);
|
|
|
|
|
|
|
|
auto result = PeerListController::saveState();
|
|
|
|
|
2017-10-25 08:38:55 +00:00
|
|
|
auto my = std::make_unique<SavedState>();
|
|
|
|
my->additional = std::move(_additional);
|
|
|
|
my->offset = _offset;
|
|
|
|
my->allLoaded = _allLoaded;
|
2017-10-24 17:11:35 +00:00
|
|
|
if (auto requestId = base::take(_loadRequestId)) {
|
|
|
|
request(requestId).cancel();
|
2017-10-25 08:38:55 +00:00
|
|
|
my->wasLoading = true;
|
2017-10-24 17:11:35 +00:00
|
|
|
}
|
|
|
|
if (auto search = searchController()) {
|
2017-10-25 08:38:55 +00:00
|
|
|
my->searchState = search->saveState();
|
2017-10-24 17:11:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 16:06:20 +00:00
|
|
|
if (_channel->isMegagroup()) {
|
|
|
|
auto weak = result.get();
|
|
|
|
Auth().data().megagroupParticipantAdded(_channel)
|
|
|
|
| rpl::start_with_next([weak](not_null<UserData*> user) {
|
|
|
|
if (!weak->list.empty()) {
|
|
|
|
if (weak->list[0] == user) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-24 17:11:35 +00:00
|
|
|
}
|
2017-11-20 12:23:20 +00:00
|
|
|
auto pos = ranges::find(weak->list, user);
|
2017-11-17 16:06:20 +00:00
|
|
|
if (pos == weak->list.cend()) {
|
|
|
|
weak->list.push_back(user);
|
|
|
|
}
|
2017-11-20 12:23:20 +00:00
|
|
|
ranges::stable_partition(
|
|
|
|
weak->list,
|
|
|
|
[user](auto peer) { return (peer == user); });
|
2017-11-17 16:06:20 +00:00
|
|
|
}, my->lifetime);
|
|
|
|
Auth().data().megagroupParticipantRemoved(_channel)
|
|
|
|
| rpl::start_with_next([weak](not_null<UserData*> user) {
|
|
|
|
weak->list.erase(std::remove(
|
|
|
|
weak->list.begin(),
|
|
|
|
weak->list.end(),
|
|
|
|
user), weak->list.end());
|
|
|
|
weak->filterResults.erase(std::remove(
|
|
|
|
weak->filterResults.begin(),
|
|
|
|
weak->filterResults.end(),
|
|
|
|
user), weak->filterResults.end());
|
|
|
|
}, my->lifetime);
|
|
|
|
}
|
2017-10-24 17:11:35 +00:00
|
|
|
result->controllerState = std::move(my);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::restoreState(
|
|
|
|
std::unique_ptr<PeerListState> state) {
|
2017-10-25 08:38:55 +00:00
|
|
|
auto typeErasedState = state
|
|
|
|
? state->controllerState.get()
|
|
|
|
: nullptr;
|
|
|
|
if (auto my = dynamic_cast<SavedState*>(typeErasedState)) {
|
2017-10-24 17:11:35 +00:00
|
|
|
if (auto requestId = base::take(_loadRequestId)) {
|
|
|
|
request(requestId).cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
_additional = std::move(my->additional);
|
|
|
|
_offset = my->offset;
|
|
|
|
_allLoaded = my->allLoaded;
|
|
|
|
if (auto search = searchController()) {
|
|
|
|
search->restoreState(std::move(my->searchState));
|
|
|
|
}
|
|
|
|
if (my->wasLoading) {
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
PeerListController::restoreState(std::move(state));
|
2017-11-10 15:45:10 +00:00
|
|
|
if (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
setNonEmptyDescription();
|
|
|
|
} else if (_allLoaded) {
|
|
|
|
setDescriptionText(lang(lng_blocked_list_not_found));
|
2017-10-24 17:11:35 +00:00
|
|
|
}
|
|
|
|
sortByOnline();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<int> ParticipantsBoxController::onlineCountValue() const {
|
|
|
|
return _onlineCount.value();
|
|
|
|
}
|
|
|
|
|
2017-06-16 11:05:13 +00:00
|
|
|
template <typename Callback>
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::HandleParticipant(const MTPChannelParticipant &participant, Role role, not_null<Additional*> additional, Callback callback) {
|
2017-09-26 17:57:01 +00:00
|
|
|
if ((role == Role::Profile
|
|
|
|
|| role == Role::Members
|
|
|
|
|| role == Role::Admins)
|
|
|
|
&& participant.type() == mtpc_channelParticipantAdmin) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto &admin = participant.c_channelParticipantAdmin();
|
|
|
|
if (auto user = App::userLoaded(admin.vuser_id.v)) {
|
|
|
|
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);
|
|
|
|
}
|
2017-09-26 17:57:01 +00:00
|
|
|
} else if ((role == Role::Profile
|
|
|
|
|| role == Role::Members
|
|
|
|
|| role == Role::Admins)
|
|
|
|
&& participant.type() == mtpc_channelParticipantCreator) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto &creator = participant.c_channelParticipantCreator();
|
|
|
|
if (auto user = App::userLoaded(creator.vuser_id.v)) {
|
|
|
|
additional->creator = user;
|
|
|
|
callback(user);
|
|
|
|
}
|
2017-09-26 17:57:01 +00:00
|
|
|
} else if ((role == Role::Profile
|
|
|
|
|| role == Role::Members
|
|
|
|
|| role == Role::Restricted
|
|
|
|
|| role == Role::Kicked)
|
|
|
|
&& participant.type() == mtpc_channelParticipantBanned) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto &banned = participant.c_channelParticipantBanned();
|
|
|
|
if (auto user = App::userLoaded(banned.vuser_id.v)) {
|
|
|
|
additional->restrictedRights[user] = banned.vbanned_rights;
|
2017-08-01 17:06:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
callback(user);
|
|
|
|
}
|
2017-09-26 17:57:01 +00:00
|
|
|
} else if ((role == Role::Profile
|
|
|
|
|| role == Role::Members)
|
|
|
|
&& participant.type() == mtpc_channelParticipant) {
|
2017-07-10 16:13:40 +00:00
|
|
|
auto &member = participant.c_channelParticipant();
|
|
|
|
if (auto user = App::userLoaded(member.vuser_id.v)) {
|
|
|
|
callback(user);
|
|
|
|
}
|
2017-09-26 17:57:01 +00:00
|
|
|
} else if ((role == Role::Profile
|
|
|
|
|| role == Role::Members)
|
|
|
|
&& participant.type() == mtpc_channelParticipantSelf) {
|
2017-07-10 16:13:40 +00:00
|
|
|
auto &member = participant.c_channelParticipantSelf();
|
|
|
|
if (auto user = App::userLoaded(member.vuser_id.v)) {
|
|
|
|
callback(user);
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
} else {
|
|
|
|
LOG(("API Error: Bad participant type got while requesting for participants: %1").arg(participant.type()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::prepare() {
|
2017-07-19 08:18:20 +00:00
|
|
|
auto titleKey = [this] {
|
|
|
|
switch (_role) {
|
|
|
|
case Role::Admins: return lng_channel_admins;
|
2017-09-26 17:57:01 +00:00
|
|
|
case Role::Profile:
|
2017-07-19 08:18:20 +00:00
|
|
|
case Role::Members: return lng_profile_participants_section;
|
|
|
|
case Role::Restricted: return lng_restricted_list_title;
|
|
|
|
case Role::Kicked: return lng_banned_list_title;
|
2017-07-10 16:13:40 +00:00
|
|
|
}
|
2017-07-19 08:18:20 +00:00
|
|
|
Unexpected("Role in ParticipantsBoxController::prepare()");
|
|
|
|
};
|
|
|
|
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
|
|
|
|
delegate()->peerListSetTitle(langFactory(titleKey()));
|
2017-06-16 11:05:13 +00:00
|
|
|
setDescriptionText(lang(lng_contacts_loading));
|
|
|
|
setSearchNoResultsText(lang(lng_blocked_list_not_found));
|
|
|
|
|
|
|
|
loadMoreRows();
|
2017-07-10 16:13:40 +00:00
|
|
|
delegate()->peerListRefreshRows();
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::loadMoreRows() {
|
|
|
|
if (searchController() && searchController()->loadMoreRows()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_loadRequestId || _allLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:13:40 +00:00
|
|
|
if (feedMegagroupLastParticipants()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-16 11:05:13 +00:00
|
|
|
auto filter = [this] {
|
2017-09-26 17:57:01 +00:00
|
|
|
if (_role == Role::Members || _role == Role::Profile) {
|
2017-07-10 16:13:40 +00:00
|
|
|
return MTP_channelParticipantsRecent();
|
|
|
|
} else if (_role == Role::Admins) {
|
2017-06-16 11:05:13 +00:00
|
|
|
return MTP_channelParticipantsAdmins();
|
|
|
|
} else if (_role == Role::Restricted) {
|
|
|
|
return MTP_channelParticipantsBanned(MTP_string(QString()));
|
|
|
|
}
|
|
|
|
return MTP_channelParticipantsKicked(MTP_string(QString()));
|
2017-11-20 19:54:05 +00:00
|
|
|
}();
|
2017-06-16 11:05:13 +00:00
|
|
|
|
|
|
|
// First query is small and fast, next loads a lot of rows.
|
|
|
|
auto perPage = (_offset > 0) ? kParticipantsPerPage : kParticipantsFirstPageCount;
|
2017-11-20 19:54:05 +00:00
|
|
|
auto participantsHash = 0;
|
|
|
|
|
|
|
|
_loadRequestId = request(MTPchannels_GetParticipants(
|
|
|
|
_channel->inputChannel,
|
|
|
|
filter,
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([this](const MTPchannels_ChannelParticipants &result) {
|
2017-11-10 15:45:10 +00:00
|
|
|
auto firstLoad = !_offset;
|
2017-06-16 11:05:13 +00:00
|
|
|
_loadRequestId = 0;
|
|
|
|
|
2017-11-22 09:56:00 +00:00
|
|
|
auto wasRecentRequest = firstLoad
|
|
|
|
&& (_role == Role::Members || _role == Role::Profile);
|
|
|
|
auto parseParticipants = [&](auto &&result, auto &&callback) {
|
|
|
|
if (wasRecentRequest) {
|
|
|
|
Auth().api().parseRecentChannelParticipants(
|
|
|
|
_channel,
|
|
|
|
result,
|
|
|
|
callback);
|
|
|
|
} else {
|
|
|
|
Auth().api().parseChannelParticipants(
|
|
|
|
result,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
parseParticipants(result, [&](
|
2017-11-28 18:30:01 +00:00
|
|
|
int availableCount,
|
2017-11-20 19:54:05 +00:00
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
|
|
|
for (auto &participant : list) {
|
|
|
|
HandleParticipant(
|
|
|
|
participant,
|
|
|
|
_role,
|
|
|
|
&_additional,
|
|
|
|
[&](auto user) { appendRow(user); });
|
2017-07-10 16:13:40 +00:00
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
if (auto size = list.size()) {
|
|
|
|
_offset += size;
|
|
|
|
} else {
|
|
|
|
// To be sure - wait for a whole empty result list.
|
|
|
|
_allLoaded = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-10 15:45:10 +00:00
|
|
|
if (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
sortByOnline();
|
|
|
|
if (firstLoad) {
|
|
|
|
setNonEmptyDescription();
|
|
|
|
}
|
|
|
|
} else if (_allLoaded) {
|
|
|
|
setDescriptionText(lang(lng_blocked_list_not_found));
|
|
|
|
}
|
2017-07-10 16:13:40 +00:00
|
|
|
delegate()->peerListRefreshRows();
|
2017-06-16 11:05:13 +00:00
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
_loadRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-11-10 15:45:10 +00:00
|
|
|
void ParticipantsBoxController::setNonEmptyDescription() {
|
|
|
|
setDescriptionText((_role == Role::Kicked)
|
|
|
|
? lang(lng_group_blocked_list_about)
|
|
|
|
: QString());
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:13:40 +00:00
|
|
|
bool ParticipantsBoxController::feedMegagroupLastParticipants() {
|
2017-09-26 17:57:01 +00:00
|
|
|
if ((_role != Role::Members && _role != Role::Profile)
|
|
|
|
|| _offset > 0) {
|
2017-07-10 16:13:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto megagroup = _channel->asMegagroup();
|
|
|
|
if (!megagroup) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto info = megagroup->mgInfo.get();
|
2017-11-28 16:20:05 +00:00
|
|
|
//
|
|
|
|
// channelFull and channels_channelParticipants members count is desynced
|
|
|
|
// so we almost always have LastParticipantsCountOutdated that is set
|
|
|
|
// inside setMembersCount() and so we almost never use lastParticipants.
|
|
|
|
//
|
|
|
|
// => disable this check temporarily.
|
|
|
|
//
|
|
|
|
//if (info->lastParticipantsStatus != MegagroupInfo::LastParticipantsUpToDate) {
|
|
|
|
// _channel->updateFull();
|
|
|
|
// return false;
|
|
|
|
//}
|
2017-07-10 16:13:40 +00:00
|
|
|
if (info->lastParticipants.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->creator) {
|
|
|
|
_additional.creator = info->creator;
|
|
|
|
}
|
|
|
|
for_const (auto user, info->lastParticipants) {
|
|
|
|
auto admin = info->lastAdmins.constFind(user);
|
|
|
|
if (admin != info->lastAdmins.cend()) {
|
|
|
|
_additional.restrictedRights.erase(user);
|
|
|
|
if (admin->canEdit) {
|
|
|
|
_additional.adminCanEdit.emplace(user);
|
|
|
|
} else {
|
|
|
|
_additional.adminCanEdit.erase(user);
|
|
|
|
}
|
|
|
|
_additional.adminRights.emplace(user, admin->rights);
|
|
|
|
} else {
|
|
|
|
_additional.adminCanEdit.erase(user);
|
|
|
|
_additional.adminRights.erase(user);
|
|
|
|
auto restricted = info->lastRestricted.constFind(user);
|
|
|
|
if (restricted != info->lastRestricted.cend()) {
|
|
|
|
_additional.restrictedRights.emplace(user, restricted->rights);
|
|
|
|
} else {
|
|
|
|
_additional.restrictedRights.erase(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendRow(user);
|
|
|
|
++_offset;
|
|
|
|
}
|
2017-09-26 18:44:58 +00:00
|
|
|
sortByOnline();
|
2017-07-10 16:13:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::rowClicked(not_null<PeerListRow*> row) {
|
2017-07-10 12:29:55 +00:00
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
Expects(user != nullptr);
|
|
|
|
|
|
|
|
if (_role == Role::Admins) {
|
|
|
|
showAdmin(user);
|
|
|
|
} else if (_role == Role::Restricted || _role == Role::Kicked) {
|
|
|
|
showRestricted(user);
|
|
|
|
} else {
|
2017-10-03 13:05:58 +00:00
|
|
|
_window->showPeerInfo(row->peer());
|
2017-07-10 12:29:55 +00:00
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::rowActionClicked(not_null<PeerListRow*> row) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
Expects(user != nullptr);
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
if (_role == Role::Members || _role == Role::Profile) {
|
2017-07-10 16:13:40 +00:00
|
|
|
kickMember(user);
|
|
|
|
} else if (_role == Role::Admins) {
|
2017-07-10 12:29:55 +00:00
|
|
|
showAdmin(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
} else if (_role == Role::Restricted) {
|
2017-07-10 12:29:55 +00:00
|
|
|
showRestricted(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
} else {
|
|
|
|
removeKicked(row, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
bool ParticipantsBoxController::canEditAdminByRights(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.cend()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return (user != _additional.creator);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParticipantsBoxController::canEditAdmin(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
if (user->isSelf()) {
|
|
|
|
return false;
|
|
|
|
} else if (_channel->amCreator()) {
|
|
|
|
return true;
|
|
|
|
} else if (!canEditAdminByRights(user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _channel->adminRights() & ChannelAdminRight::f_add_admins;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParticipantsBoxController::canRestrictUser(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
if (user->isSelf()) {
|
|
|
|
return false;
|
|
|
|
} else if (_channel->amCreator()) {
|
|
|
|
return true;
|
|
|
|
} else if (!canEditAdminByRights(user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _channel->adminRights() & ChannelAdminRight::f_ban_users;
|
|
|
|
}
|
|
|
|
|
2017-10-22 17:06:57 +00:00
|
|
|
Ui::PopupMenu *ParticipantsBoxController::rowContextMenu(
|
|
|
|
not_null<PeerListRow*> row) {
|
|
|
|
Expects(row->peer()->isUser());
|
|
|
|
|
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
auto result = new Ui::PopupMenu(nullptr);
|
|
|
|
result->addAction(
|
|
|
|
lang(lng_context_view_profile),
|
|
|
|
[weak = base::make_weak_unique(this), user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->_window->showPeerInfo(user);
|
|
|
|
}
|
|
|
|
});
|
2017-11-07 17:53:00 +00:00
|
|
|
if (canEditAdmin(user)) {
|
2017-10-22 17:06:57 +00:00
|
|
|
auto it = _additional.adminRights.find(user);
|
|
|
|
auto isCreator = (user == _additional.creator);
|
|
|
|
auto notAdmin = !isCreator && (it == _additional.adminRights.cend());
|
|
|
|
auto label = lang(notAdmin
|
|
|
|
? lng_context_promote_admin
|
|
|
|
: lng_context_edit_permissions);
|
|
|
|
result->addAction(
|
|
|
|
label,
|
|
|
|
[weak = base::make_weak_unique(this), user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->showAdmin(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-07 17:53:00 +00:00
|
|
|
if (canRestrictUser(user)) {
|
2017-11-17 16:06:20 +00:00
|
|
|
auto isGroup = _channel->isMegagroup();
|
|
|
|
if (isGroup) {
|
|
|
|
result->addAction(
|
|
|
|
lang(lng_context_restrict_user),
|
|
|
|
[weak = base::make_weak_unique(this), user]{
|
|
|
|
if (weak) {
|
|
|
|
weak->showRestricted(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-10-22 17:06:57 +00:00
|
|
|
result->addAction(
|
2017-11-17 16:06:20 +00:00
|
|
|
lang(isGroup
|
|
|
|
? lng_context_remove_from_group
|
|
|
|
: lng_profile_kick),
|
2017-10-22 17:06:57 +00:00
|
|
|
[weak = base::make_weak_unique(this), user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->kickMember(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::showAdmin(not_null<UserData*> user) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto it = _additional.adminRights.find(user);
|
2017-07-19 08:18:20 +00:00
|
|
|
auto isCreator = (user == _additional.creator);
|
|
|
|
auto notAdmin = !isCreator && (it == _additional.adminRights.cend());
|
|
|
|
auto currentRights = isCreator
|
2017-07-10 12:29:55 +00:00
|
|
|
? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins))
|
2017-07-19 08:18:20 +00:00
|
|
|
: notAdmin ? MTP_channelAdminRights(MTP_flags(0)) : it->second;
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-07-10 12:29:55 +00:00
|
|
|
auto box = Box<EditAdminBox>(_channel, user, currentRights);
|
2017-07-19 08:18:20 +00:00
|
|
|
auto canEdit = (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end());
|
|
|
|
auto canSave = notAdmin ? _channel->canAddAdmins() : canEdit;
|
|
|
|
if (canSave) {
|
2017-07-10 12:29:55 +00:00
|
|
|
box->setSaveCallback([channel = _channel.get(), user, weak](const MTPChannelAdminRights &oldRights, const MTPChannelAdminRights &newRights) {
|
|
|
|
MTP::send(MTPchannels_EditAdmin(channel->inputChannel, user->inputUser, newRights), rpcDone([channel, user, weak, oldRights, newRights](const MTPUpdates &result) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().applyUpdates(result);
|
2017-07-10 12:29:55 +00:00
|
|
|
channel->applyEditAdmin(user, oldRights, newRights);
|
|
|
|
if (weak) {
|
|
|
|
weak->editAdminDone(user, newRights);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
void ParticipantsBoxController::editAdminDone(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPChannelAdminRights &rights) {
|
2017-06-16 13:47:44 +00:00
|
|
|
if (_editBox) {
|
|
|
|
_editBox->closeBox();
|
|
|
|
}
|
|
|
|
if (_addBox) {
|
|
|
|
_addBox->closeBox();
|
|
|
|
}
|
|
|
|
auto notAdmin = (rights.c_channelAdminRights().vflags.v == 0);
|
|
|
|
if (notAdmin) {
|
|
|
|
_additional.adminRights.erase(user);
|
|
|
|
_additional.adminPromotedBy.erase(user);
|
|
|
|
_additional.adminCanEdit.erase(user);
|
|
|
|
if (_role == Role::Admins) {
|
|
|
|
removeRow(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-16 13:47:44 +00:00
|
|
|
// It won't be replaced if the entry already exists.
|
|
|
|
_additional.adminPromotedBy.emplace(user, App::self());
|
|
|
|
_additional.adminCanEdit.emplace(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
_additional.adminRights[user] = rights;
|
2017-06-16 13:47:44 +00:00
|
|
|
_additional.kicked.erase(user);
|
|
|
|
_additional.restrictedRights.erase(user);
|
2017-08-01 17:06:40 +00:00
|
|
|
_additional.restrictedBy.erase(user);
|
2017-06-16 13:47:44 +00:00
|
|
|
if (_role == Role::Admins) {
|
|
|
|
prependRow(user);
|
2017-10-22 17:06:57 +00:00
|
|
|
} else if (_role == Role::Kicked || _role == Role::Restricted) {
|
2017-06-16 13:47:44 +00:00
|
|
|
removeRow(user);
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-11-07 17:53:00 +00:00
|
|
|
recomputeTypeFor(user);
|
2017-06-16 13:47:44 +00:00
|
|
|
delegate()->peerListRefreshRows();
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::showRestricted(not_null<UserData*> user) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto it = _additional.restrictedRights.find(user);
|
2017-10-22 17:06:57 +00:00
|
|
|
auto restrictedRights = (it == _additional.restrictedRights.cend())
|
|
|
|
? MTP_channelBannedRights(MTP_flags(0), MTP_int(0))
|
|
|
|
: it->second;
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-06-17 19:51:23 +00:00
|
|
|
auto hasAdminRights = false;
|
2017-10-22 17:06:57 +00:00
|
|
|
auto box = Box<EditRestrictedBox>(_channel, user, hasAdminRights, restrictedRights);
|
2017-07-10 12:29:55 +00:00
|
|
|
if (_channel->canBanMembers()) {
|
|
|
|
box->setSaveCallback([megagroup = _channel.get(), user, weak](const MTPChannelBannedRights &oldRights, const MTPChannelBannedRights &newRights) {
|
|
|
|
MTP::send(MTPchannels_EditBanned(megagroup->inputChannel, user->inputUser, newRights), rpcDone([megagroup, user, weak, oldRights, newRights](const MTPUpdates &result) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().applyUpdates(result);
|
2017-07-10 12:29:55 +00:00
|
|
|
megagroup->applyEditBanned(user, oldRights, newRights);
|
|
|
|
if (weak) {
|
|
|
|
weak->editRestrictedDone(user, newRights);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::editRestrictedDone(not_null<UserData*> user, const MTPChannelBannedRights &rights) {
|
2017-06-16 13:47:44 +00:00
|
|
|
if (_editBox) {
|
|
|
|
_editBox->closeBox();
|
|
|
|
}
|
|
|
|
if (_addBox) {
|
|
|
|
_addBox->closeBox();
|
|
|
|
}
|
|
|
|
auto notBanned = (rights.c_channelBannedRights().vflags.v == 0);
|
|
|
|
auto fullBanned = rights.c_channelBannedRights().is_view_messages();
|
|
|
|
if (notBanned) {
|
|
|
|
_additional.kicked.erase(user);
|
|
|
|
_additional.restrictedRights.erase(user);
|
2017-08-01 17:06:40 +00:00
|
|
|
_additional.restrictedBy.erase(user);
|
2017-10-22 17:06:57 +00:00
|
|
|
if (_role == Role::Kicked || _role == Role::Restricted) {
|
2017-06-16 13:47:44 +00:00
|
|
|
removeRow(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-16 13:47:44 +00:00
|
|
|
_additional.adminRights.erase(user);
|
|
|
|
_additional.adminCanEdit.erase(user);
|
|
|
|
_additional.adminPromotedBy.erase(user);
|
2017-08-01 17:06:40 +00:00
|
|
|
_additional.restrictedBy.emplace(user, App::self());
|
2017-06-16 13:47:44 +00:00
|
|
|
if (fullBanned) {
|
|
|
|
_additional.kicked.emplace(user);
|
|
|
|
_additional.restrictedRights.erase(user);
|
|
|
|
if (_role == Role::Kicked) {
|
|
|
|
prependRow(user);
|
2017-10-22 17:06:57 +00:00
|
|
|
} else if (_role == Role::Admins
|
|
|
|
|| _role == Role::Restricted
|
|
|
|
|| _role == Role::Members) {
|
2017-06-16 13:47:44 +00:00
|
|
|
removeRow(user);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_additional.restrictedRights[user] = rights;
|
|
|
|
_additional.kicked.erase(user);
|
|
|
|
if (_role == Role::Restricted) {
|
|
|
|
prependRow(user);
|
2017-10-22 17:06:57 +00:00
|
|
|
} else if (_role == Role::Kicked
|
|
|
|
|| _role == Role::Admins
|
|
|
|
|| _role == Role::Members) {
|
2017-06-16 13:47:44 +00:00
|
|
|
removeRow(user);
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-11-07 17:53:00 +00:00
|
|
|
recomputeTypeFor(user);
|
2017-06-16 13:47:44 +00:00
|
|
|
delegate()->peerListRefreshRows();
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::kickMember(not_null<UserData*> user) {
|
2017-07-10 16:13:40 +00:00
|
|
|
auto text = (_channel->isMegagroup() ? lng_profile_sure_kick : lng_profile_sure_kick_channel)(lt_user, user->firstName);
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-07-10 16:13:40 +00:00
|
|
|
_editBox = Ui::show(Box<ConfirmBox>(text, lang(lng_box_remove), [weak, user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->kickMemberSure(user);
|
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-07-10 16:13:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::kickMemberSure(not_null<UserData*> user) {
|
2017-07-10 16:13:40 +00:00
|
|
|
if (_editBox) {
|
|
|
|
_editBox->closeBox();
|
|
|
|
}
|
|
|
|
auto alreadyIt = _additional.restrictedRights.find(user);
|
|
|
|
auto currentRights = (alreadyIt == _additional.restrictedRights.cend()) ? MTP_channelBannedRights(MTP_flags(0), MTP_int(0)) : alreadyIt->second;
|
|
|
|
|
|
|
|
if (auto row = delegate()->peerListFindRow(user->id)) {
|
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
}
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().kickParticipant(_channel, user, currentRights);
|
2017-07-10 16:13:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::removeKicked(not_null<PeerListRow*> row, not_null<UserData*> user) {
|
2017-06-16 11:05:13 +00:00
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().unblockParticipant(_channel, user);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
bool ParticipantsBoxController::appendRow(not_null<UserData*> user) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (delegate()->peerListFindRow(user->id)) {
|
2017-11-07 17:53:00 +00:00
|
|
|
recomputeTypeFor(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delegate()->peerListAppendRow(createRow(user));
|
|
|
|
if (_role != Role::Kicked) {
|
|
|
|
setDescriptionText(QString());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
bool ParticipantsBoxController::prependRow(not_null<UserData*> user) {
|
2017-07-19 08:18:20 +00:00
|
|
|
if (auto row = delegate()->peerListFindRow(user->id)) {
|
2017-11-07 17:53:00 +00:00
|
|
|
recomputeTypeFor(user);
|
2017-08-01 17:06:40 +00:00
|
|
|
refreshCustomStatus(row);
|
2017-07-19 08:18:20 +00:00
|
|
|
if (_role == Role::Admins) {
|
|
|
|
// Perhaps we've added a new admin from search.
|
|
|
|
delegate()->peerListPrependRowFromSearchResult(row);
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delegate()->peerListPrependRow(createRow(user));
|
|
|
|
if (_role != Role::Kicked) {
|
|
|
|
setDescriptionText(QString());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
bool ParticipantsBoxController::removeRow(not_null<UserData*> user) {
|
2017-06-16 13:47:44 +00:00
|
|
|
if (auto row = delegate()->peerListFindRow(user->id)) {
|
2017-07-19 08:18:20 +00:00
|
|
|
if (_role == Role::Admins) {
|
|
|
|
// Perhaps we are removing an admin from search results.
|
|
|
|
row->setCustomStatus(lang(lng_channel_admin_status_not_admin));
|
|
|
|
delegate()->peerListConvertRowToSearchResult(row);
|
|
|
|
} else {
|
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
}
|
2017-11-10 15:45:10 +00:00
|
|
|
if (_role != Role::Kicked
|
|
|
|
&& !delegate()->peerListFullRowsCount()) {
|
2017-06-16 13:47:44 +00:00
|
|
|
setDescriptionText(lang(lng_blocked_list_not_found));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
std::unique_ptr<PeerListRow> ParticipantsBoxController::createRow(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
if (_role == Role::Profile) {
|
|
|
|
return std::make_unique<Row>(user, computeType(user));
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
auto row = std::make_unique<PeerListRowWithLink>(user);
|
2017-08-01 17:06:40 +00:00
|
|
|
refreshCustomStatus(row.get());
|
2017-07-10 16:13:40 +00:00
|
|
|
if (_role == Role::Restricted || (_role == Role::Admins && _additional.adminCanEdit.find(user) != _additional.adminCanEdit.cend())) {
|
2017-07-10 12:29:55 +00:00
|
|
|
// row->setActionLink(lang(lng_profile_edit_permissions));
|
2017-06-16 11:05:13 +00:00
|
|
|
} else if (_role == Role::Kicked) {
|
|
|
|
row->setActionLink(lang(lng_blocked_list_unblock));
|
2017-07-10 16:13:40 +00:00
|
|
|
} else if (_role == Role::Members) {
|
|
|
|
if (_channel->canBanMembers() && _additional.creator != user
|
|
|
|
&& (_additional.adminRights.find(user) == _additional.adminRights.cend()
|
|
|
|
|| _additional.adminCanEdit.find(user) != _additional.adminCanEdit.cend())) {
|
|
|
|
row->setActionLink(lang(lng_profile_kick));
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
return std::move(row);
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
auto ParticipantsBoxController::computeType(
|
|
|
|
not_null<UserData*> user) const -> Type {
|
|
|
|
auto isCreator = (user == _additional.creator);
|
|
|
|
auto isAdmin = (_additional.adminRights.find(user) != _additional.adminRights.cend());
|
|
|
|
|
|
|
|
auto result = Type();
|
|
|
|
result.rights = isCreator
|
|
|
|
? Rights::Creator
|
|
|
|
: isAdmin
|
|
|
|
? Rights::Admin
|
|
|
|
: Rights::Normal;
|
|
|
|
result.canRemove = canRestrictUser(user);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxController::recomputeTypeFor(
|
|
|
|
not_null<UserData*> user) {
|
|
|
|
if (_role != Role::Profile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auto row = delegate()->peerListFindRow(user->id)) {
|
|
|
|
static_cast<Row*>(row)->setType(computeType(user));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void ParticipantsBoxController::refreshCustomStatus(not_null<PeerListRow*> row) const {
|
2017-07-19 08:18:20 +00:00
|
|
|
auto user = row->peer()->asUser();
|
2017-08-01 17:06:40 +00:00
|
|
|
if (_role == Role::Admins) {
|
|
|
|
auto promotedBy = _additional.adminPromotedBy.find(user);
|
|
|
|
if (promotedBy == _additional.adminPromotedBy.cend()) {
|
|
|
|
if (user == _additional.creator) {
|
|
|
|
row->setCustomStatus(lang(lng_channel_admin_status_creator));
|
|
|
|
} else {
|
|
|
|
row->setCustomStatus(lang(lng_channel_admin_status_not_admin));
|
|
|
|
}
|
2017-07-19 08:18:20 +00:00
|
|
|
} else {
|
2017-08-01 17:06:40 +00:00
|
|
|
row->setCustomStatus(lng_channel_admin_status_promoted_by(lt_user, App::peerName(promotedBy->second)));
|
|
|
|
}
|
|
|
|
} else if (_role == Role::Kicked || _role == Role::Restricted) {
|
|
|
|
auto restrictedBy = _additional.restrictedBy.find(user);
|
|
|
|
if (restrictedBy == _additional.restrictedBy.cend()) {
|
|
|
|
row->setCustomStatus(lng_channel_banned_status_restricted_by(lt_user, "Unknown"));
|
|
|
|
} else {
|
|
|
|
row->setCustomStatus(lng_channel_banned_status_restricted_by(lt_user, App::peerName(restrictedBy->second)));
|
2017-07-19 08:18:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
ParticipantsBoxSearchController::ParticipantsBoxSearchController(not_null<ChannelData*> channel, Role role, not_null<Additional*> additional)
|
2017-06-16 11:05:13 +00:00
|
|
|
: _channel(channel)
|
|
|
|
, _role(role)
|
|
|
|
, _additional(additional) {
|
|
|
|
_timer.setCallback([this] { searchOnServer(); });
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:13:40 +00:00
|
|
|
void ParticipantsBoxSearchController::searchQuery(const QString &query) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (_query != query) {
|
|
|
|
_query = query;
|
|
|
|
_offset = 0;
|
|
|
|
_requestId = 0;
|
|
|
|
_allLoaded = false;
|
|
|
|
if (!_query.isEmpty() && !searchInCache()) {
|
|
|
|
_timer.callOnce(AutoSearchTimeout);
|
|
|
|
} else {
|
|
|
|
_timer.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 08:38:55 +00:00
|
|
|
auto ParticipantsBoxSearchController::saveState()
|
|
|
|
-> std::unique_ptr<SavedStateBase> {
|
|
|
|
auto result = std::make_unique<SavedState>();
|
|
|
|
result->query = _query;
|
|
|
|
result->offset = _offset;
|
|
|
|
result->allLoaded = _allLoaded;
|
2017-10-24 17:11:35 +00:00
|
|
|
if (auto requestId = base::take(_requestId)) {
|
|
|
|
request(requestId).cancel();
|
2017-10-25 08:38:55 +00:00
|
|
|
result->wasLoading = true;
|
2017-10-24 17:11:35 +00:00
|
|
|
}
|
2017-10-25 08:38:55 +00:00
|
|
|
return std::move(result);
|
2017-10-24 17:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticipantsBoxSearchController::restoreState(
|
2017-10-25 08:38:55 +00:00
|
|
|
std::unique_ptr<SavedStateBase> state) {
|
|
|
|
if (auto my = dynamic_cast<SavedState*>(state.get())) {
|
2017-10-24 17:11:35 +00:00
|
|
|
if (auto requestId = base::take(_requestId)) {
|
|
|
|
request(requestId).cancel();
|
|
|
|
}
|
|
|
|
_cache.clear();
|
|
|
|
_queries.clear();
|
|
|
|
|
|
|
|
_allLoaded = my->allLoaded;
|
|
|
|
_offset = my->offset;
|
|
|
|
_query = my->query;
|
|
|
|
if (my->wasLoading) {
|
|
|
|
searchOnServer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:13:40 +00:00
|
|
|
void ParticipantsBoxSearchController::searchOnServer() {
|
2017-06-16 11:05:13 +00:00
|
|
|
Expects(!_query.isEmpty());
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:13:40 +00:00
|
|
|
bool ParticipantsBoxSearchController::isLoading() {
|
2017-06-16 11:05:13 +00:00
|
|
|
return _timer.isActive() || _requestId;
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:13:40 +00:00
|
|
|
bool ParticipantsBoxSearchController::searchInCache() {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto it = _cache.find(_query);
|
|
|
|
if (it != _cache.cend()) {
|
|
|
|
_requestId = 0;
|
|
|
|
searchDone(_requestId, it->second.result, it->second.requestedCount);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:13:40 +00:00
|
|
|
bool ParticipantsBoxSearchController::loadMoreRows() {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (_query.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!_allLoaded && !isLoading()) {
|
2017-07-10 16:13:40 +00:00
|
|
|
auto filter = [this] {
|
|
|
|
switch (_role) {
|
2017-07-19 08:18:20 +00:00
|
|
|
case Role::Admins: // Search for members, appoint as admin on found.
|
2017-09-26 17:57:01 +00:00
|
|
|
case Role::Profile:
|
2017-07-10 16:13:40 +00:00
|
|
|
case Role::Members: return MTP_channelParticipantsSearch(MTP_string(_query));
|
|
|
|
case Role::Restricted: return MTP_channelParticipantsBanned(MTP_string(_query));
|
|
|
|
case Role::Kicked: return MTP_channelParticipantsKicked(MTP_string(_query));
|
|
|
|
}
|
|
|
|
Unexpected("Role in ParticipantsBoxSearchController::loadMoreRows()");
|
2017-11-20 19:54:05 +00:00
|
|
|
}();
|
2017-06-16 11:05:13 +00:00
|
|
|
|
|
|
|
// For search we request a lot of rows from the first query.
|
|
|
|
// (because we've waited for search request by timer already,
|
|
|
|
// so we don't expect it to be fast, but we want to fill cache).
|
|
|
|
auto perPage = kParticipantsPerPage;
|
2017-11-20 19:54:05 +00:00
|
|
|
auto participantsHash = 0;
|
|
|
|
|
|
|
|
_requestId = request(MTPchannels_GetParticipants(
|
|
|
|
_channel->inputChannel,
|
|
|
|
filter,
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([this, perPage](const MTPchannels_ChannelParticipants &result, mtpRequestId requestId) {
|
2017-06-16 11:05:13 +00:00
|
|
|
searchDone(requestId, result, perPage);
|
|
|
|
}).fail([this](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
if (_requestId == requestId) {
|
|
|
|
_requestId = 0;
|
|
|
|
_allLoaded = true;
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
|
|
|
|
auto entry = Query();
|
|
|
|
entry.text = _query;
|
|
|
|
entry.offset = _offset;
|
|
|
|
_queries.emplace(_requestId, entry);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
void ParticipantsBoxSearchController::searchDone(
|
|
|
|
mtpRequestId requestId,
|
|
|
|
const MTPchannels_ChannelParticipants &result,
|
|
|
|
int requestedCount) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto query = _query;
|
|
|
|
if (requestId) {
|
2017-11-20 19:54:05 +00:00
|
|
|
Auth().api().parseChannelParticipants(result, [&](auto&&...) {
|
|
|
|
auto it = _queries.find(requestId);
|
|
|
|
if (it != _queries.cend()) {
|
|
|
|
query = it->second.text;
|
|
|
|
if (it->second.offset == 0) {
|
|
|
|
auto &entry = _cache[query];
|
|
|
|
entry.result = result;
|
|
|
|
entry.requestedCount = requestedCount;
|
|
|
|
}
|
|
|
|
_queries.erase(it);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
});
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
if (_requestId != requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_requestId = 0;
|
2017-11-21 11:59:18 +00:00
|
|
|
TLHelp::VisitChannelParticipants(result, base::overload([&](
|
2017-11-20 19:54:05 +00:00
|
|
|
const MTPDchannels_channelParticipants &data) {
|
|
|
|
auto &list = data.vparticipants.v;
|
2017-06-16 11:05:13 +00:00
|
|
|
if (list.size() < requestedCount) {
|
|
|
|
// We want cache to have full information about a query with small
|
|
|
|
// results count (if we don't need the second request). So we don't
|
|
|
|
// wait for an empty results list unlike the non-search peer list.
|
|
|
|
_allLoaded = true;
|
|
|
|
}
|
2017-07-19 08:18:20 +00:00
|
|
|
auto parseRole = (_role == Role::Admins) ? Role::Members : _role;
|
2017-06-16 11:05:13 +00:00
|
|
|
for_const (auto &participant, list) {
|
2017-08-17 08:31:24 +00:00
|
|
|
ParticipantsBoxController::HandleParticipant(participant, parseRole, _additional, [this](not_null<UserData*> user) {
|
2017-06-16 11:05:13 +00:00
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_offset += list.size();
|
2017-11-20 19:54:05 +00:00
|
|
|
}, [&](mtpTypeId type) {
|
|
|
|
_allLoaded = true;
|
|
|
|
}));
|
|
|
|
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
AddParticipantBoxController::AddParticipantBoxController(not_null<ChannelData*> channel, Role role, AdminDoneCallback adminDoneCallback, BannedDoneCallback bannedDoneCallback) : PeerListController(std::make_unique<AddParticipantBoxSearchController>(channel, &_additional))
|
2017-06-16 11:05:13 +00:00
|
|
|
, _channel(channel)
|
|
|
|
, _role(role)
|
2017-06-16 13:47:44 +00:00
|
|
|
, _adminDoneCallback(std::move(adminDoneCallback))
|
|
|
|
, _bannedDoneCallback(std::move(bannedDoneCallback)) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (_channel->mgInfo) {
|
|
|
|
_additional.creator = _channel->mgInfo->creator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<PeerListRow> AddParticipantBoxController::createSearchRow(not_null<PeerData*> peer) {
|
2017-08-13 16:14:00 +00:00
|
|
|
if (peer->isSelf()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto user = peer->asUser()) {
|
|
|
|
return createRow(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxController::prepare() {
|
2017-07-19 08:18:20 +00:00
|
|
|
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
|
2017-06-16 11:05:13 +00:00
|
|
|
auto title = [this] {
|
|
|
|
switch (_role) {
|
|
|
|
case Role::Admins: return langFactory(lng_channel_add_admin);
|
|
|
|
case Role::Restricted: return langFactory(lng_channel_add_restricted);
|
|
|
|
case Role::Kicked: return langFactory(lng_channel_add_banned);
|
|
|
|
}
|
|
|
|
Unexpected("Role in AddParticipantBoxController::prepare()");
|
|
|
|
};
|
|
|
|
delegate()->peerListSetTitle(title());
|
|
|
|
setDescriptionText(lang(lng_contacts_loading));
|
|
|
|
setSearchNoResultsText(lang(lng_blocked_list_not_found));
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxController::loadMoreRows() {
|
|
|
|
if (searchController() && searchController()->loadMoreRows()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_loadRequestId || _allLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First query is small and fast, next loads a lot of rows.
|
|
|
|
auto perPage = (_offset > 0) ? kParticipantsPerPage : kParticipantsFirstPageCount;
|
2017-11-20 19:54:05 +00:00
|
|
|
auto participantsHash = 0;
|
|
|
|
|
|
|
|
_loadRequestId = request(MTPchannels_GetParticipants(
|
|
|
|
_channel->inputChannel,
|
|
|
|
MTP_channelParticipantsRecent(),
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([this](const MTPchannels_ChannelParticipants &result) {
|
2017-06-16 11:05:13 +00:00
|
|
|
_loadRequestId = 0;
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
Auth().api().parseChannelParticipants(result, [&](
|
2017-11-28 18:30:01 +00:00
|
|
|
int availableCount,
|
2017-11-20 19:54:05 +00:00
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
|
|
|
for (auto &participant : list) {
|
|
|
|
HandleParticipant(
|
|
|
|
participant,
|
|
|
|
&_additional,
|
|
|
|
[this](auto user) { appendRow(user); });
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
if (auto size = list.size()) {
|
|
|
|
_offset += size;
|
|
|
|
} else {
|
|
|
|
// To be sure - wait for a whole empty result list.
|
|
|
|
_allLoaded = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-16 11:05:13 +00:00
|
|
|
if (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
setDescriptionText(QString());
|
2017-06-29 12:27:24 +00:00
|
|
|
} else if (_allLoaded) {
|
|
|
|
setDescriptionText(lang(lng_blocked_list_not_found));
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
_loadRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void AddParticipantBoxController::rowClicked(not_null<PeerListRow*> row) {
|
2017-06-16 11:05:13 +00:00
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
switch (_role) {
|
2017-07-10 12:29:55 +00:00
|
|
|
case Role::Admins: return showAdmin(user);
|
|
|
|
case Role::Restricted: return showRestricted(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
case Role::Kicked: return kickUser(user);
|
|
|
|
}
|
|
|
|
Unexpected("Role in AddParticipantBoxController::rowClicked()");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback>
|
2017-08-17 08:31:24 +00:00
|
|
|
bool AddParticipantBoxController::checkInfoLoaded(not_null<UserData*> user, Callback callback) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (_additional.infoNotLoaded.find(user) == _additional.infoNotLoaded.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't know what this user status is in the group.
|
|
|
|
request(MTPchannels_GetParticipant(_channel->inputChannel, user->inputUser)).done([this, user, callback](const MTPchannels_ChannelParticipant &result) {
|
|
|
|
Expects(result.type() == mtpc_channels_channelParticipant);
|
|
|
|
auto &participant = result.c_channels_channelParticipant();
|
|
|
|
App::feedUsers(participant.vusers);
|
2017-08-17 08:31:24 +00:00
|
|
|
HandleParticipant(participant.vparticipant, &_additional, [](not_null<UserData*>) {});
|
2017-06-16 11:05:13 +00:00
|
|
|
_additional.infoNotLoaded.erase(user);
|
|
|
|
callback();
|
|
|
|
}).fail([this, user, callback](const RPCError &error) {
|
|
|
|
_additional.infoNotLoaded.erase(user);
|
|
|
|
_additional.external.emplace(user);
|
|
|
|
callback();
|
|
|
|
}).send();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void AddParticipantBoxController::showAdmin(not_null<UserData*> user, bool sure) {
|
2017-07-10 12:29:55 +00:00
|
|
|
if (!checkInfoLoaded(user, [this, user] { showAdmin(user); })) {
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sure && _editBox) {
|
|
|
|
// Close the confirmation box.
|
|
|
|
_editBox->closeBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check restrictions.
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-06-16 11:05:13 +00:00
|
|
|
auto alreadyIt = _additional.adminRights.find(user);
|
2017-07-10 12:29:55 +00:00
|
|
|
auto currentRights = (_additional.creator == user)
|
|
|
|
? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins))
|
|
|
|
: MTP_channelAdminRights(MTP_flags(0));
|
|
|
|
if (alreadyIt != _additional.adminRights.end()) {
|
2017-06-16 11:05:13 +00:00
|
|
|
// The user is already an admin.
|
|
|
|
currentRights = alreadyIt->second;
|
|
|
|
} else if (_additional.kicked.find(user) != _additional.kicked.end()) {
|
|
|
|
// The user is banned.
|
|
|
|
if (_channel->canAddMembers()) {
|
|
|
|
if (_channel->canBanMembers()) {
|
|
|
|
if (!sure) {
|
2017-07-05 09:37:00 +00:00
|
|
|
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_add_admin_unban), [weak, user] {
|
|
|
|
if (weak) {
|
2017-07-10 12:29:55 +00:00
|
|
|
weak->showAdmin(user, true);
|
2017-07-05 09:37:00 +00:00
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
lang(lng_error_cant_add_admin_unban)),
|
|
|
|
LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
lang(lng_error_cant_add_admin_invite)),
|
|
|
|
LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (_additional.restrictedRights.find(user) != _additional.restrictedRights.end()) {
|
|
|
|
// The user is restricted.
|
|
|
|
if (_channel->canBanMembers()) {
|
|
|
|
if (!sure) {
|
2017-07-05 09:37:00 +00:00
|
|
|
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_add_admin_unban), [weak, user] {
|
|
|
|
if (weak) {
|
2017-07-10 12:29:55 +00:00
|
|
|
weak->showAdmin(user, true);
|
2017-07-05 09:37:00 +00:00
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
lang(lng_error_cant_add_admin_unban)),
|
|
|
|
LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (_additional.external.find(user) != _additional.external.end()) {
|
|
|
|
// The user is not in the group yet.
|
|
|
|
if (_channel->canAddMembers()) {
|
|
|
|
if (!sure) {
|
2017-07-05 09:37:00 +00:00
|
|
|
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_add_admin_invite), [weak, user] {
|
|
|
|
if (weak) {
|
2017-07-10 12:29:55 +00:00
|
|
|
weak->showAdmin(user, true);
|
2017-07-05 09:37:00 +00:00
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_error_cant_add_admin_invite)),
|
|
|
|
LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:29:55 +00:00
|
|
|
// Finally show the admin.
|
|
|
|
auto canNotEdit = (_additional.creator == user)
|
|
|
|
|| ((alreadyIt != _additional.adminRights.end())
|
|
|
|
&& (_additional.adminCanEdit.find(user) == _additional.adminCanEdit.end()));
|
|
|
|
auto box = Box<EditAdminBox>(_channel, user, currentRights);
|
|
|
|
if (!canNotEdit) {
|
|
|
|
box->setSaveCallback([channel = _channel.get(), user, weak](const MTPChannelAdminRights &oldRights, const MTPChannelAdminRights &newRights) {
|
|
|
|
MTP::send(MTPchannels_EditAdmin(channel->inputChannel, user->inputUser, newRights), rpcDone([channel, user, weak, oldRights, newRights](const MTPUpdates &result) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().applyUpdates(result);
|
2017-07-10 12:29:55 +00:00
|
|
|
channel->applyEditAdmin(user, oldRights, newRights);
|
|
|
|
if (weak) {
|
|
|
|
weak->editAdminDone(user, newRights);
|
|
|
|
}
|
|
|
|
}), rpcFail([channel, weak](const RPCError &error) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (error.type() == qstr("USER_NOT_MUTUAL_CONTACT")) {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(PeerFloodErrorText(
|
|
|
|
channel->isMegagroup()
|
|
|
|
? PeerFloodType::InviteGroup
|
|
|
|
: PeerFloodType::InviteChannel)),
|
|
|
|
LayerOption::KeepOther);
|
2017-07-10 12:29:55 +00:00
|
|
|
} else if (error.type() == qstr("BOT_GROUPS_BLOCKED")) {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_error_cant_add_bot)),
|
|
|
|
LayerOption::KeepOther);
|
2017-07-10 12:29:55 +00:00
|
|
|
}
|
|
|
|
if (weak && weak->_editBox) {
|
|
|
|
weak->_editBox->closeBox();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
void AddParticipantBoxController::editAdminDone(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPChannelAdminRights &rights) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (_editBox) _editBox->closeBox();
|
|
|
|
_additional.restrictedRights.erase(user);
|
2017-08-01 17:06:40 +00:00
|
|
|
_additional.restrictedBy.erase(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
_additional.kicked.erase(user);
|
|
|
|
_additional.external.erase(user);
|
|
|
|
if (rights.c_channelAdminRights().vflags.v == 0) {
|
|
|
|
_additional.adminRights.erase(user);
|
|
|
|
_additional.adminPromotedBy.erase(user);
|
|
|
|
_additional.adminCanEdit.erase(user);
|
|
|
|
} else {
|
|
|
|
_additional.adminRights[user] = rights;
|
|
|
|
_additional.adminCanEdit.emplace(user);
|
|
|
|
auto it = _additional.adminPromotedBy.find(user);
|
|
|
|
if (it == _additional.adminPromotedBy.end()) {
|
|
|
|
_additional.adminPromotedBy.emplace(user, App::self());
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 13:47:44 +00:00
|
|
|
if (_adminDoneCallback) {
|
|
|
|
_adminDoneCallback(user, rights);
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void AddParticipantBoxController::showRestricted(not_null<UserData*> user, bool sure) {
|
2017-07-10 12:29:55 +00:00
|
|
|
if (!checkInfoLoaded(user, [this, user] { showRestricted(user); })) {
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sure && _editBox) {
|
|
|
|
// Close the confirmation box.
|
|
|
|
_editBox->closeBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check restrictions.
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-06-16 11:05:13 +00:00
|
|
|
auto alreadyIt = _additional.restrictedRights.find(user);
|
2017-07-05 21:11:49 +00:00
|
|
|
auto currentRights = MTP_channelBannedRights(MTP_flags(0), MTP_int(0));
|
2017-06-17 19:51:23 +00:00
|
|
|
auto hasAdminRights = false;
|
2017-06-16 11:05:13 +00:00
|
|
|
if (alreadyIt != _additional.restrictedRights.end()) {
|
|
|
|
// The user is already banned or restricted.
|
|
|
|
currentRights = alreadyIt->second;
|
|
|
|
} else if (_additional.adminRights.find(user) != _additional.adminRights.end() || _additional.creator == user) {
|
|
|
|
// The user is an admin or creator.
|
|
|
|
if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()) {
|
2017-06-17 19:51:23 +00:00
|
|
|
hasAdminRights = true;
|
2017-06-16 11:05:13 +00:00
|
|
|
if (!sure) {
|
2017-07-05 09:37:00 +00:00
|
|
|
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_ban_admin), [weak, user] {
|
|
|
|
if (weak) {
|
2017-07-10 12:29:55 +00:00
|
|
|
weak->showRestricted(user, true);
|
2017-07-05 09:37:00 +00:00
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_error_cant_ban_admin)),
|
|
|
|
LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally edit the restricted.
|
2017-07-10 12:29:55 +00:00
|
|
|
auto box = Box<EditRestrictedBox>(_channel, user, hasAdminRights, currentRights);
|
|
|
|
box->setSaveCallback([user, weak](const MTPChannelBannedRights &oldRights, const MTPChannelBannedRights &newRights) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (weak) {
|
2017-07-05 21:11:49 +00:00
|
|
|
weak->restrictUserSure(user, oldRights, newRights);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-07-10 12:29:55 +00:00
|
|
|
});
|
2017-09-15 17:34:41 +00:00
|
|
|
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void AddParticipantBoxController::restrictUserSure(not_null<UserData*> user, const MTPChannelBannedRights &oldRights, const MTPChannelBannedRights &newRights) {
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-07-05 21:11:49 +00:00
|
|
|
MTP::send(MTPchannels_EditBanned(_channel->inputChannel, user->inputUser, newRights), rpcDone([megagroup = _channel.get(), user, weak, oldRights, newRights](const MTPUpdates &result) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().applyUpdates(result);
|
2017-07-05 21:11:49 +00:00
|
|
|
megagroup->applyEditBanned(user, oldRights, newRights);
|
2017-06-16 11:05:13 +00:00
|
|
|
if (weak) {
|
2017-07-05 21:11:49 +00:00
|
|
|
weak->editRestrictedDone(user, newRights);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
void AddParticipantBoxController::editRestrictedDone(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPChannelBannedRights &rights) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (_editBox) _editBox->closeBox();
|
|
|
|
_additional.adminRights.erase(user);
|
|
|
|
_additional.adminCanEdit.erase(user);
|
|
|
|
_additional.adminPromotedBy.erase(user);
|
|
|
|
if (rights.c_channelBannedRights().vflags.v == 0) {
|
|
|
|
_additional.restrictedRights.erase(user);
|
2017-08-01 17:06:40 +00:00
|
|
|
_additional.restrictedBy.erase(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
_additional.kicked.erase(user);
|
|
|
|
} else {
|
|
|
|
_additional.restrictedRights[user] = rights;
|
|
|
|
if (rights.c_channelBannedRights().vflags.v & MTPDchannelBannedRights::Flag::f_view_messages) {
|
|
|
|
_additional.kicked.emplace(user);
|
|
|
|
} else {
|
|
|
|
_additional.kicked.erase(user);
|
|
|
|
}
|
2017-08-01 17:06:40 +00:00
|
|
|
_additional.restrictedBy.emplace(user, App::self());
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-06-16 13:47:44 +00:00
|
|
|
if (_bannedDoneCallback) {
|
|
|
|
_bannedDoneCallback(user, rights);
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void AddParticipantBoxController::kickUser(not_null<UserData*> user, bool sure) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (!checkInfoLoaded(user, [this, user] { kickUser(user); })) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check restrictions.
|
2017-08-15 09:35:01 +00:00
|
|
|
auto weak = base::make_weak_unique(this);
|
2017-06-16 11:05:13 +00:00
|
|
|
if (_additional.adminRights.find(user) != _additional.adminRights.end() || _additional.creator == user) {
|
|
|
|
// The user is an admin or creator.
|
|
|
|
if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()) {
|
|
|
|
if (!sure) {
|
2017-07-05 09:37:00 +00:00
|
|
|
_editBox = Ui::show(Box<ConfirmBox>(lang(lng_sure_ban_admin), [weak, user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->kickUser(user, true);
|
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_error_cant_ban_admin)),
|
|
|
|
LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally kick him.
|
|
|
|
if (!sure) {
|
|
|
|
auto text = lng_sure_ban_user_group(lt_user, App::peerName(user));
|
2017-07-05 09:37:00 +00:00
|
|
|
_editBox = Ui::show(Box<ConfirmBox>(text, [weak, user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->kickUser(user, true);
|
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-16 11:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-05 21:11:49 +00:00
|
|
|
auto currentRights = MTP_channelBannedRights(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;
|
|
|
|
}
|
|
|
|
restrictUserSure(user, currentRights, ChannelData::KickedRestrictedRights());
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
bool AddParticipantBoxController::appendRow(not_null<UserData*> user) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (delegate()->peerListFindRow(user->id) || user->isSelf()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delegate()->peerListAppendRow(createRow(user));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
bool AddParticipantBoxController::prependRow(not_null<UserData*> user) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (delegate()->peerListFindRow(user->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delegate()->peerListPrependRow(createRow(user));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<PeerListRow> AddParticipantBoxController::createRow(not_null<UserData*> user) const {
|
2017-06-17 19:52:18 +00:00
|
|
|
return std::make_unique<PeerListRow>(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback>
|
2017-11-07 17:53:00 +00:00
|
|
|
void AddParticipantBoxController::HandleParticipant(
|
|
|
|
const MTPChannelParticipant &participant,
|
|
|
|
not_null<Additional*> additional,
|
|
|
|
Callback callback) {
|
2017-06-16 11:05:13 +00:00
|
|
|
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);
|
2017-08-01 17:06:40 +00:00
|
|
|
additional->restrictedBy.erase(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
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;
|
2017-08-01 17:06:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 11:05:13 +00:00
|
|
|
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);
|
2017-08-01 17:06:40 +00:00
|
|
|
additional->restrictedBy.erase(user);
|
2017-06-16 11:05:13 +00:00
|
|
|
callback(user);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: Unexpected("Participant type in AddParticipantBoxController::HandleParticipant()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
AddParticipantBoxSearchController::AddParticipantBoxSearchController(not_null<ChannelData*> channel, not_null<Additional*> additional)
|
2017-06-16 11:05:13 +00:00
|
|
|
: _channel(channel)
|
|
|
|
, _additional(additional) {
|
|
|
|
_timer.setCallback([this] { searchOnServer(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxSearchController::searchQuery(const QString &query) {
|
|
|
|
if (_query != query) {
|
|
|
|
_query = query;
|
|
|
|
_offset = 0;
|
|
|
|
_requestId = 0;
|
|
|
|
_participantsLoaded = false;
|
|
|
|
_chatsContactsAdded = false;
|
|
|
|
_globalLoaded = false;
|
|
|
|
if (!_query.isEmpty() && !searchParticipantsInCache()) {
|
|
|
|
_timer.callOnce(AutoSearchTimeout);
|
|
|
|
} else {
|
|
|
|
_timer.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxSearchController::searchOnServer() {
|
|
|
|
Expects(!_query.isEmpty());
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddParticipantBoxSearchController::isLoading() {
|
|
|
|
return _timer.isActive() || _requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddParticipantBoxSearchController::searchParticipantsInCache() {
|
|
|
|
auto it = _participantsCache.find(_query);
|
|
|
|
if (it != _participantsCache.cend()) {
|
|
|
|
_requestId = 0;
|
|
|
|
searchParticipantsDone(_requestId, it->second.result, it->second.requestedCount);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddParticipantBoxSearchController::searchGlobalInCache() {
|
|
|
|
auto it = _globalCache.find(_query);
|
|
|
|
if (it != _globalCache.cend()) {
|
|
|
|
_requestId = 0;
|
|
|
|
searchGlobalDone(_requestId, it->second);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddParticipantBoxSearchController::loadMoreRows() {
|
|
|
|
if (_query.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_globalLoaded) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_participantsLoaded) {
|
|
|
|
if (!_chatsContactsAdded) {
|
|
|
|
addChatsContacts();
|
|
|
|
}
|
|
|
|
if (!isLoading() && !searchGlobalInCache()) {
|
|
|
|
requestGlobal();
|
|
|
|
}
|
|
|
|
} else if (!isLoading()) {
|
|
|
|
requestParticipants();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxSearchController::requestParticipants() {
|
|
|
|
// For search we request a lot of rows from the first query.
|
|
|
|
// (because we've waited for search request by timer already,
|
|
|
|
// so we don't expect it to be fast, but we want to fill cache).
|
|
|
|
auto perPage = kParticipantsPerPage;
|
2017-11-20 19:54:05 +00:00
|
|
|
auto participantsHash = 0;
|
|
|
|
|
|
|
|
_requestId = request(MTPchannels_GetParticipants(
|
|
|
|
_channel->inputChannel,
|
|
|
|
MTP_channelParticipantsSearch(MTP_string(_query)),
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([this, perPage](const MTPchannels_ChannelParticipants &result, mtpRequestId requestId) {
|
2017-06-16 11:05:13 +00:00
|
|
|
searchParticipantsDone(requestId, result, perPage);
|
|
|
|
}).fail([this](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
if (_requestId == requestId) {
|
|
|
|
_requestId = 0;
|
|
|
|
_participantsLoaded = true;
|
|
|
|
loadMoreRows();
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
auto entry = Query();
|
|
|
|
entry.text = _query;
|
|
|
|
entry.offset = _offset;
|
|
|
|
_participantsQueries.emplace(_requestId, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxSearchController::searchParticipantsDone(mtpRequestId requestId, const MTPchannels_ChannelParticipants &result, int requestedCount) {
|
|
|
|
auto query = _query;
|
|
|
|
if (requestId) {
|
2017-11-20 19:54:05 +00:00
|
|
|
Auth().api().parseChannelParticipants(result, [&](auto&&...) {
|
|
|
|
auto it = _participantsQueries.find(requestId);
|
|
|
|
if (it != _participantsQueries.cend()) {
|
|
|
|
query = it->second.text;
|
|
|
|
if (it->second.offset == 0) {
|
|
|
|
auto &entry = _participantsCache[query];
|
|
|
|
entry.result = result;
|
|
|
|
entry.requestedCount = requestedCount;
|
|
|
|
}
|
|
|
|
_participantsQueries.erase(it);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
});
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
if (_requestId != requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_requestId = 0;
|
2017-11-21 11:59:18 +00:00
|
|
|
TLHelp::VisitChannelParticipants(result, base::overload([&](
|
2017-11-20 19:54:05 +00:00
|
|
|
const MTPDchannels_channelParticipants &data) {
|
|
|
|
auto &list = data.vparticipants.v;
|
2017-06-16 11:05:13 +00:00
|
|
|
if (list.size() < requestedCount) {
|
|
|
|
// We want cache to have full information about a query with small
|
|
|
|
// results count (if we don't need the second request). So we don't
|
|
|
|
// wait for an empty results list unlike the non-search peer list.
|
|
|
|
_participantsLoaded = true;
|
|
|
|
if (list.empty() && _offset == 0) {
|
|
|
|
// No results, so we want to request global search immediately.
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
auto addUser = [&](auto user) {
|
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
};
|
|
|
|
for (auto &participant : list) {
|
|
|
|
AddParticipantBoxController::HandleParticipant(
|
|
|
|
participant,
|
|
|
|
_additional,
|
|
|
|
addUser);
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
_offset += list.size();
|
2017-11-20 19:54:05 +00:00
|
|
|
}, [&](mtpTypeId type) {
|
|
|
|
_participantsLoaded = true;
|
|
|
|
}));
|
|
|
|
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
2017-06-16 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxSearchController::requestGlobal() {
|
|
|
|
if (_query.size() < MinUsernameLength) {
|
|
|
|
_globalLoaded = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto perPage = SearchPeopleLimit;
|
|
|
|
_requestId = request(MTPcontacts_Search(MTP_string(_query), MTP_int(perPage))).done([this](const MTPcontacts_Found &result, mtpRequestId requestId) {
|
|
|
|
searchGlobalDone(requestId, result);
|
|
|
|
}).fail([this](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
if (_requestId == requestId) {
|
|
|
|
_requestId = 0;
|
|
|
|
_globalLoaded = true;
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
_globalQueries.emplace(_requestId, _query);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxSearchController::searchGlobalDone(mtpRequestId requestId, const MTPcontacts_Found &result) {
|
|
|
|
Expects(result.type() == mtpc_contacts_found);
|
|
|
|
|
|
|
|
auto &found = result.c_contacts_found();
|
|
|
|
auto query = _query;
|
|
|
|
if (requestId) {
|
|
|
|
App::feedUsers(found.vusers);
|
|
|
|
App::feedChats(found.vchats);
|
|
|
|
auto it = _globalQueries.find(requestId);
|
|
|
|
if (it != _globalQueries.cend()) {
|
|
|
|
query = it->second;
|
|
|
|
_globalCache[query] = result;
|
|
|
|
_globalQueries.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_requestId == requestId) {
|
|
|
|
_requestId = 0;
|
|
|
|
_globalLoaded = true;
|
|
|
|
for_const (auto &mtpPeer, found.vresults.v) {
|
|
|
|
auto peerId = peerFromMTP(mtpPeer);
|
|
|
|
if (auto peer = App::peerLoaded(peerId)) {
|
|
|
|
if (auto user = peer->asUser()) {
|
|
|
|
if (_additional->adminRights.find(user) == _additional->adminRights.cend()
|
|
|
|
&& _additional->restrictedRights.find(user) == _additional->restrictedRights.cend()
|
|
|
|
&& _additional->external.find(user) == _additional->external.cend()
|
|
|
|
&& _additional->kicked.find(user) == _additional->kicked.cend()
|
|
|
|
&& _additional->creator != user) {
|
|
|
|
_additional->infoNotLoaded.emplace(user);
|
|
|
|
}
|
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantBoxSearchController::addChatsContacts() {
|
|
|
|
_chatsContactsAdded = true;
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
auto wordList = TextUtilities::PrepareSearchWords(_query);
|
2017-06-16 11:05:13 +00:00
|
|
|
if (wordList.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
auto getSmallestIndex = [&](Dialogs::IndexedList *list) -> const Dialogs::List* {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (list->isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = (const Dialogs::List*)nullptr;
|
|
|
|
for_const (auto &word, wordList) {
|
|
|
|
auto found = list->filtered(word[0]);
|
|
|
|
if (found->isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!result || result->size() > found->size()) {
|
|
|
|
result = found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
auto dialogsIndex = getSmallestIndex(App::main()->dialogsList());
|
|
|
|
auto contactsIndex = getSmallestIndex(App::main()->contactsNoDialogsList());
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
auto allWordsAreFound = [&](const base::flat_set<QString> &nameWords) {
|
|
|
|
auto hasNamePartStartingWith = [&](const QString &word) {
|
|
|
|
for (auto &nameWord : nameWords) {
|
|
|
|
if (nameWord.startsWith(word)) {
|
2017-06-16 11:05:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
for_const (auto &word, wordList) {
|
|
|
|
if (!hasNamePartStartingWith(word)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2017-10-24 17:11:35 +00:00
|
|
|
auto filterAndAppend = [&](const Dialogs::List *list) {
|
2017-06-16 11:05:13 +00:00
|
|
|
if (!list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for_const (auto row, *list) {
|
|
|
|
if (auto user = row->history()->peer->asUser()) {
|
2017-10-24 17:11:35 +00:00
|
|
|
if (allWordsAreFound(user->nameWords())) {
|
2017-06-16 11:05:13 +00:00
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
filterAndAppend(dialogsIndex);
|
|
|
|
filterAndAppend(contactsIndex);
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Profile
|