2019-01-10 06:26:08 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "boxes/peers/add_participants_box.h"
|
|
|
|
|
|
|
|
#include "boxes/peers/edit_participant_box.h"
|
|
|
|
#include "boxes/confirm_box.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_user.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2019-01-10 06:26:08 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "dialogs/dialogs_indexed_list.h"
|
|
|
|
#include "auth_session.h"
|
|
|
|
#include "mainwidget.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "observer_peer.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kParticipantsFirstPageCount = 16;
|
|
|
|
constexpr auto kParticipantsPerPage = 200;
|
|
|
|
|
|
|
|
base::flat_set<not_null<UserData*>> GetAlreadyInFromPeer(PeerData *peer) {
|
|
|
|
if (!peer) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (const auto chat = peer->asChat()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
return chat->participants;
|
2019-01-10 06:26:08 +00:00
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
if (channel->isMegagroup()) {
|
|
|
|
const auto &participants = channel->mgInfo->lastParticipants;
|
|
|
|
return { participants.cbegin(), participants.cend() };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
} // namespace
|
2019-01-10 06:26:08 +00:00
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
AddParticipantsBoxController::AddParticipantsBoxController()
|
|
|
|
: ContactsBoxController(
|
|
|
|
std::make_unique<PeerListGlobalSearchController>()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
AddParticipantsBoxController::AddParticipantsBoxController(
|
|
|
|
not_null<PeerData*> peer)
|
|
|
|
: AddParticipantsBoxController(peer, GetAlreadyInFromPeer(peer)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddParticipantsBoxController::AddParticipantsBoxController(
|
2019-01-13 13:28:05 +00:00
|
|
|
not_null<PeerData*> peer,
|
2019-01-10 06:26:08 +00:00
|
|
|
base::flat_set<not_null<UserData*>> &&alreadyIn)
|
|
|
|
: ContactsBoxController(std::make_unique<PeerListGlobalSearchController>())
|
2019-01-13 13:28:05 +00:00
|
|
|
, _peer(peer)
|
2019-01-10 06:26:08 +00:00
|
|
|
, _alreadyIn(std::move(alreadyIn)) {
|
2019-01-13 13:28:05 +00:00
|
|
|
subscribeToMigration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::subscribeToMigration() {
|
|
|
|
Expects(_peer != nullptr);
|
|
|
|
|
|
|
|
SubscribeToMigration(
|
|
|
|
_peer,
|
|
|
|
lifetime(),
|
|
|
|
[=](not_null<ChannelData*> channel) { _peer = channel; });
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::rowClicked(not_null<PeerListRow*> row) {
|
|
|
|
auto count = fullCount();
|
2019-01-13 13:28:05 +00:00
|
|
|
auto limit = _peer && (_peer->isChat() || _peer->isMegagroup())
|
|
|
|
? Global::MegagroupSizeMax()
|
|
|
|
: Global::ChatSizeMax();
|
2019-01-10 06:26:08 +00:00
|
|
|
if (count < limit || row->checked()) {
|
|
|
|
delegate()->peerListSetRowChecked(row, !row->checked());
|
|
|
|
updateTitle();
|
2019-01-13 13:28:05 +00:00
|
|
|
} else if (const auto channel = _peer ? _peer->asChannel() : nullptr) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!_peer->isMegagroup()) {
|
|
|
|
Ui::show(
|
|
|
|
Box<MaxInviteBox>(_peer->asChannel()),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
}
|
2019-01-13 13:28:05 +00:00
|
|
|
} else if (count >= Global::ChatSizeMax()
|
|
|
|
&& count < Global::MegagroupSizeMax()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
Ui::show(
|
2019-01-14 06:34:51 +00:00
|
|
|
Box<InformBox>(lang(lng_profile_add_more_after_create)),
|
2019-01-10 06:26:08 +00:00
|
|
|
LayerOption::KeepOther);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
void AddParticipantsBoxController::itemDeselectedHook(
|
|
|
|
not_null<PeerData*> peer) {
|
2019-01-10 06:26:08 +00:00
|
|
|
updateTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::prepareViewHook() {
|
|
|
|
updateTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
int AddParticipantsBoxController::alreadyInCount() const {
|
|
|
|
if (!_peer) {
|
|
|
|
return 1; // self
|
|
|
|
}
|
2019-01-13 13:28:05 +00:00
|
|
|
if (const auto chat = _peer->asChat()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return qMax(chat->count, 1);
|
2019-01-13 13:28:05 +00:00
|
|
|
} else if (const auto channel = _peer->asChannel()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return qMax(channel->membersCount(), int(_alreadyIn.size()));
|
|
|
|
}
|
|
|
|
Unexpected("User in AddParticipantsBoxController::alreadyInCount");
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
bool AddParticipantsBoxController::isAlreadyIn(
|
|
|
|
not_null<UserData*> user) const {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!_peer) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-13 13:28:05 +00:00
|
|
|
if (const auto chat = _peer->asChat()) {
|
|
|
|
return _alreadyIn.contains(user)
|
|
|
|
|| chat->participants.contains(user);
|
|
|
|
} else if (const auto channel = _peer->asChannel()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return _alreadyIn.contains(user)
|
2019-01-13 13:28:05 +00:00
|
|
|
|| (channel->isMegagroup()
|
|
|
|
&& base::contains(channel->mgInfo->lastParticipants, user));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
Unexpected("User in AddParticipantsBoxController::isAlreadyIn");
|
|
|
|
}
|
|
|
|
|
|
|
|
int AddParticipantsBoxController::fullCount() const {
|
|
|
|
return alreadyInCount() + delegate()->peerListSelectedRowsCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PeerListRow> AddParticipantsBoxController::createRow(
|
|
|
|
not_null<UserData*> user) {
|
|
|
|
if (user->isSelf()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto result = std::make_unique<PeerListRow>(user);
|
|
|
|
if (isAlreadyIn(user)) {
|
|
|
|
result->setDisabledState(PeerListRow::State::DisabledChecked);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::updateTitle() {
|
2019-01-13 13:28:05 +00:00
|
|
|
const auto additional = (_peer
|
|
|
|
&& _peer->isChannel()
|
|
|
|
&& !_peer->isMegagroup())
|
2019-01-10 06:26:08 +00:00
|
|
|
? QString() :
|
|
|
|
QString("%1 / %2").arg(fullCount()).arg(Global::MegagroupSizeMax());
|
|
|
|
delegate()->peerListSetTitle(langFactory(lng_profile_add_participant));
|
2019-01-13 13:28:05 +00:00
|
|
|
delegate()->peerListSetAdditionalTitle([=] { return additional; });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddParticipantsBoxController::inviteSelectedUsers(
|
|
|
|
not_null<PeerListBox*> box) const {
|
|
|
|
Expects(_peer != nullptr);
|
|
|
|
|
|
|
|
const auto rows = box->peerListCollectSelectedRows();
|
|
|
|
const auto users = ranges::view::all(
|
|
|
|
rows
|
|
|
|
) | ranges::view::transform([](not_null<PeerData*> peer) {
|
|
|
|
Expects(peer->isUser());
|
|
|
|
Expects(!peer->isSelf());
|
|
|
|
|
|
|
|
return not_null<UserData*>(peer->asUser());
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
if (users.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_peer->session().api().addChatParticipants(_peer, users);
|
|
|
|
return true;
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::Start(not_null<ChatData*> chat) {
|
2019-01-13 13:28:05 +00:00
|
|
|
auto controller = std::make_unique<AddParticipantsBoxController>(chat);
|
|
|
|
const auto weak = controller.get();
|
2019-01-10 06:26:08 +00:00
|
|
|
auto initBox = [=](not_null<PeerListBox*> box) {
|
|
|
|
box->addButton(langFactory(lng_participant_invite), [=] {
|
2019-01-13 13:28:05 +00:00
|
|
|
if (weak->inviteSelectedUsers(box)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
Ui::showPeerHistory(chat, ShowAtTheEndMsgId);
|
|
|
|
}
|
|
|
|
});
|
2019-01-13 13:28:05 +00:00
|
|
|
box->addButton(langFactory(lng_cancel), [=] { box->closeBox(); });
|
2019-01-10 06:26:08 +00:00
|
|
|
};
|
2019-01-10 11:01:59 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<PeerListBox>(
|
2019-01-13 13:28:05 +00:00
|
|
|
std::move(controller),
|
2019-01-10 11:01:59 +00:00
|
|
|
std::move(initBox)),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::Start(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
base::flat_set<not_null<UserData*>> &&alreadyIn,
|
|
|
|
bool justCreated) {
|
2019-01-13 13:28:05 +00:00
|
|
|
auto controller = std::make_unique<AddParticipantsBoxController>(
|
|
|
|
channel,
|
|
|
|
std::move(alreadyIn));
|
|
|
|
const auto weak = controller.get();
|
|
|
|
auto initBox = [=](not_null<PeerListBox*> box) {
|
|
|
|
box->addButton(langFactory(lng_participant_invite), [=] {
|
|
|
|
if (weak->inviteSelectedUsers(box)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (channel->isMegagroup()) {
|
|
|
|
Ui::showPeerHistory(channel, ShowAtTheEndMsgId);
|
|
|
|
} else {
|
|
|
|
box->closeBox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-01-13 13:28:05 +00:00
|
|
|
box->addButton(
|
|
|
|
langFactory(justCreated ? lng_create_group_skip : lng_cancel),
|
|
|
|
[=] { box->closeBox(); });
|
2019-01-10 06:26:08 +00:00
|
|
|
if (justCreated) {
|
|
|
|
box->boxClosing() | rpl::start_with_next([=] {
|
|
|
|
Ui::showPeerHistory(channel, ShowAtTheEndMsgId);
|
2019-01-13 13:28:05 +00:00
|
|
|
}, box->lifetime());
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
};
|
2019-01-10 11:01:59 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<PeerListBox>(
|
2019-01-13 13:28:05 +00:00
|
|
|
std::move(controller),
|
2019-01-10 11:01:59 +00:00
|
|
|
std::move(initBox)),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::Start(
|
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
base::flat_set<not_null<UserData*>> &&alreadyIn) {
|
|
|
|
Start(channel, std::move(alreadyIn), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::Start(not_null<ChannelData*> channel) {
|
|
|
|
Start(channel, {}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddSpecialBoxController::AddSpecialBoxController(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
Role role,
|
|
|
|
AdminDoneCallback adminDoneCallback,
|
|
|
|
BannedDoneCallback bannedDoneCallback)
|
|
|
|
: PeerListController(std::make_unique<AddSpecialBoxSearchController>(
|
|
|
|
peer,
|
|
|
|
&_additional))
|
|
|
|
, _peer(peer)
|
|
|
|
, _role(role)
|
2019-01-10 11:01:59 +00:00
|
|
|
, _additional(peer, Role::Members)
|
2019-01-10 06:26:08 +00:00
|
|
|
, _adminDoneCallback(std::move(adminDoneCallback))
|
|
|
|
, _bannedDoneCallback(std::move(bannedDoneCallback)) {
|
2019-01-13 13:28:05 +00:00
|
|
|
subscribeToMigration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::subscribeToMigration() {
|
|
|
|
SubscribeToMigration(
|
|
|
|
_peer,
|
|
|
|
lifetime(),
|
|
|
|
[=](not_null<ChannelData*> channel) { migrate(channel); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::migrate(not_null<ChannelData*> channel) {
|
|
|
|
_peer = channel;
|
|
|
|
_additional.migrate(channel);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
std::unique_ptr<PeerListRow> AddSpecialBoxController::createSearchRow(
|
|
|
|
not_null<PeerData*> peer) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (peer->isSelf()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
return createRow(user);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::prepare() {
|
|
|
|
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
|
|
|
|
const auto title = [&] {
|
|
|
|
switch (_role) {
|
2019-01-13 13:28:05 +00:00
|
|
|
case Role::Admins:
|
|
|
|
return langFactory(lng_channel_add_admin);
|
|
|
|
case Role::Restricted:
|
2019-01-15 07:15:38 +00:00
|
|
|
return langFactory(lng_channel_add_exception);
|
2019-01-13 13:28:05 +00:00
|
|
|
case Role::Kicked:
|
2019-01-15 07:15:38 +00:00
|
|
|
return langFactory(lng_channel_add_removed);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
Unexpected("Role in AddSpecialBoxController::prepare()");
|
|
|
|
}();
|
|
|
|
delegate()->peerListSetTitle(title);
|
|
|
|
setDescriptionText(lang(lng_contacts_loading));
|
|
|
|
setSearchNoResultsText(lang(lng_blocked_list_not_found));
|
|
|
|
|
|
|
|
if (const auto chat = _peer->asChat()) {
|
|
|
|
prepareChatRows(chat);
|
|
|
|
} else {
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::prepareChatRows(not_null<ChatData*> chat) {
|
|
|
|
_onlineSorter = std::make_unique<ParticipantsOnlineSorter>(
|
|
|
|
chat,
|
|
|
|
delegate());
|
|
|
|
|
|
|
|
rebuildChatRows(chat);
|
|
|
|
if (!delegate()->peerListFullRowsCount()) {
|
|
|
|
chat->updateFullForced();
|
|
|
|
}
|
|
|
|
|
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(
|
2019-01-10 12:38:57 +00:00
|
|
|
UpdateFlag::MembersChanged | UpdateFlag::AdminsChanged,
|
2019-01-10 06:26:08 +00:00
|
|
|
[=](const Notify::PeerUpdate &update) {
|
2019-01-10 12:38:57 +00:00
|
|
|
if (update.peer == chat) {
|
|
|
|
_additional.fillFromPeer();
|
|
|
|
if (update.flags & UpdateFlag::MembersChanged) {
|
2019-01-10 06:26:08 +00:00
|
|
|
rebuildChatRows(chat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::rebuildChatRows(not_null<ChatData*> chat) {
|
|
|
|
if (chat->participants.empty()) {
|
|
|
|
// We get such updates often
|
|
|
|
// (when participants list was invalidated).
|
|
|
|
//while (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
// delegate()->peerListRemoveRow(
|
|
|
|
// delegate()->peerListRowAt(0));
|
|
|
|
//}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &participants = chat->participants;
|
|
|
|
auto count = delegate()->peerListFullRowsCount();
|
|
|
|
for (auto i = 0; i != count;) {
|
|
|
|
auto row = delegate()->peerListRowAt(i);
|
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
if (participants.contains(user)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 08:03:34 +00:00
|
|
|
for (const auto user : participants) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (auto row = createRow(user)) {
|
|
|
|
delegate()->peerListAppendRow(std::move(row));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_onlineSorter->sort();
|
|
|
|
|
|
|
|
delegate()->peerListRefreshRows();
|
2019-01-10 11:01:59 +00:00
|
|
|
setDescriptionText(QString());
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::loadMoreRows() {
|
|
|
|
if (searchController() && searchController()->loadMoreRows()) {
|
|
|
|
return;
|
|
|
|
} else if (!_peer->isChannel() || _loadRequestId || _allLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First query is small and fast, next loads a lot of rows.
|
|
|
|
const auto perPage = (_offset > 0)
|
|
|
|
? kParticipantsPerPage
|
|
|
|
: kParticipantsFirstPageCount;
|
|
|
|
const auto participantsHash = 0;
|
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
|
|
|
|
_loadRequestId = request(MTPchannels_GetParticipants(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsRecent(),
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([=](const MTPchannels_ChannelParticipants &result) {
|
|
|
|
_loadRequestId = 0;
|
2019-01-10 11:01:59 +00:00
|
|
|
auto &session = channel->session();
|
|
|
|
session.api().parseChannelParticipants(channel, result, [&](
|
2019-01-10 06:26:08 +00:00
|
|
|
int availableCount,
|
|
|
|
const QVector<MTPChannelParticipant> &list) {
|
2019-01-10 11:01:59 +00:00
|
|
|
for (const auto &data : list) {
|
|
|
|
if (const auto user = _additional.applyParticipant(data)) {
|
|
|
|
appendRow(user);
|
|
|
|
}
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
if (const auto size = list.size()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
_offset += size;
|
|
|
|
} else {
|
|
|
|
// To be sure - wait for a whole empty result list.
|
|
|
|
_allLoaded = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
setDescriptionText(QString());
|
|
|
|
} else if (_allLoaded) {
|
|
|
|
setDescriptionText(lang(lng_blocked_list_not_found));
|
|
|
|
}
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
_loadRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::rowClicked(not_null<PeerListRow*> row) {
|
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
switch (_role) {
|
|
|
|
case Role::Admins: return showAdmin(user);
|
|
|
|
case Role::Restricted: return showRestricted(user);
|
|
|
|
case Role::Kicked: return kickUser(user);
|
|
|
|
}
|
|
|
|
Unexpected("Role in AddSpecialBoxController::rowClicked()");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback>
|
|
|
|
bool AddSpecialBoxController::checkInfoLoaded(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
Callback callback) {
|
2019-01-10 11:01:59 +00:00
|
|
|
if (_additional.infoLoaded(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't know what this user status is in the group.
|
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
request(MTPchannels_GetParticipant(
|
|
|
|
channel->inputChannel,
|
|
|
|
user->inputUser
|
|
|
|
)).done([=](const MTPchannels_ChannelParticipant &result) {
|
|
|
|
Expects(result.type() == mtpc_channels_channelParticipant);
|
|
|
|
|
|
|
|
const auto &participant = result.c_channels_channelParticipant();
|
2019-01-18 12:27:37 +00:00
|
|
|
channel->owner().processUsers(participant.vusers);
|
2019-01-10 11:01:59 +00:00
|
|
|
_additional.applyParticipant(participant.vparticipant);
|
2019-01-10 06:26:08 +00:00
|
|
|
callback();
|
2019-01-10 11:01:59 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_additional.setExternal(user);
|
2019-01-10 06:26:08 +00:00
|
|
|
callback();
|
|
|
|
}).send();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::showAdmin(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
bool sure) {
|
|
|
|
if (!checkInfoLoaded(user, [=] { showAdmin(user); })) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 06:34:51 +00:00
|
|
|
_editBox = nullptr;
|
2019-01-10 06:26:08 +00:00
|
|
|
|
|
|
|
const auto chat = _peer->asChat();
|
|
|
|
const auto channel = _peer->asChannel();
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto showAdminSure = crl::guard(this, [=] {
|
|
|
|
showAdmin(user, true);
|
|
|
|
});
|
2019-01-10 06:26:08 +00:00
|
|
|
|
|
|
|
// Check restrictions.
|
|
|
|
const auto canAddMembers = chat
|
|
|
|
? chat->canAddMembers()
|
|
|
|
: channel->canAddMembers();
|
|
|
|
const auto canBanMembers = chat
|
|
|
|
? chat->canBanMembers()
|
|
|
|
: channel->canBanMembers();
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto adminRights = _additional.adminRights(user);
|
|
|
|
if (adminRights.has_value()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is already an admin.
|
2019-01-10 11:01:59 +00:00
|
|
|
} else if (_additional.isKicked(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is banned.
|
|
|
|
if (canAddMembers) {
|
|
|
|
if (canBanMembers) {
|
|
|
|
if (!sure) {
|
2019-01-10 11:01:59 +00:00
|
|
|
_editBox = Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
2019-01-15 07:15:38 +00:00
|
|
|
lang(lng_sure_add_admin_unremove),
|
2019-01-10 11:01:59 +00:00
|
|
|
showAdminSure),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
lang(lng_error_cant_add_admin_unban)),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
lang(lng_error_cant_add_admin_invite)),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
} else if (_additional.restrictedRights(user).has_value()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is restricted.
|
|
|
|
if (canBanMembers) {
|
|
|
|
if (!sure) {
|
2019-01-10 11:01:59 +00:00
|
|
|
_editBox = Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
2019-01-15 07:15:38 +00:00
|
|
|
lang(lng_sure_add_admin_unremove),
|
2019-01-10 11:01:59 +00:00
|
|
|
showAdminSure),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
lang(lng_error_cant_add_admin_unban)),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
} else if (_additional.isExternal(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is not in the group yet.
|
|
|
|
if (canAddMembers) {
|
|
|
|
if (!sure) {
|
|
|
|
const auto text = lang(
|
|
|
|
((_peer->isChat() || _peer->isMegagroup())
|
|
|
|
? lng_sure_add_admin_invite
|
|
|
|
: lng_sure_add_admin_invite_channel));
|
2019-01-10 11:01:59 +00:00
|
|
|
_editBox = Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
|
|
|
text,
|
|
|
|
showAdminSure),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_error_cant_add_admin_invite)),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally show the admin.
|
2019-01-10 11:01:59 +00:00
|
|
|
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)));
|
2019-01-10 06:26:08 +00:00
|
|
|
auto box = Box<EditAdminBox>(_peer, user, currentRights);
|
2019-01-10 11:01:59 +00:00
|
|
|
if (_additional.canAddOrEditAdmin(user)) {
|
2019-01-10 12:38:57 +00:00
|
|
|
const auto done = crl::guard(this, [=](
|
|
|
|
const MTPChatAdminRights &newRights) {
|
|
|
|
editAdminDone(user, newRights);
|
|
|
|
});
|
|
|
|
const auto fail = crl::guard(this, [=] {
|
2019-01-14 06:34:51 +00:00
|
|
|
_editBox = nullptr;
|
2019-01-10 12:38:57 +00:00
|
|
|
});
|
|
|
|
box->setSaveCallback(SaveAdminCallback(_peer, user, done, fail));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::editAdminDone(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPChatAdminRights &rights) {
|
2019-01-14 06:34:51 +00:00
|
|
|
_editBox = nullptr;
|
2019-01-10 11:01:59 +00:00
|
|
|
|
|
|
|
const auto date = unixtime(); // Incorrect, but ignored.
|
2019-01-10 06:26:08 +00:00
|
|
|
if (rights.c_chatAdminRights().vflags.v == 0) {
|
2019-01-10 11:01:59 +00:00
|
|
|
_additional.applyParticipant(MTP_channelParticipant(
|
|
|
|
MTP_int(user->bareId()),
|
|
|
|
MTP_int(date)));
|
2019-01-10 06:26:08 +00:00
|
|
|
} else {
|
2019-01-10 11:01:59 +00:00
|
|
|
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));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
if (const auto callback = _adminDoneCallback) {
|
|
|
|
callback(user, rights);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::showRestricted(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
bool sure) {
|
|
|
|
if (!checkInfoLoaded(user, [=] { showRestricted(user); })) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 06:34:51 +00:00
|
|
|
_editBox = nullptr;
|
2019-01-10 06:26:08 +00:00
|
|
|
|
|
|
|
const auto chat = _peer->asChat();
|
|
|
|
const auto channel = _peer->asChannel();
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto showRestrictedSure = crl::guard(this, [=] {
|
|
|
|
showRestricted(user, true);
|
|
|
|
});
|
2019-01-10 06:26:08 +00:00
|
|
|
|
|
|
|
// Check restrictions.
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto restrictedRights = _additional.restrictedRights(user);
|
|
|
|
if (restrictedRights.has_value()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is already banned or restricted.
|
2019-01-10 11:01:59 +00:00
|
|
|
} else if (_additional.adminRights(user).has_value()
|
|
|
|
|| _additional.isCreator(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is an admin or creator.
|
2019-01-10 11:01:59 +00:00
|
|
|
if (_additional.canEditAdmin(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!sure) {
|
2019-01-10 11:01:59 +00:00
|
|
|
_editBox = Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
|
|
|
lang(lng_sure_ban_admin),
|
|
|
|
showRestrictedSure),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_error_cant_ban_admin)),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally edit the restricted.
|
2019-01-10 11:01:59 +00:00
|
|
|
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);
|
2019-01-10 12:38:57 +00:00
|
|
|
if (_additional.canRestrictUser(user)) {
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto done = crl::guard(this, [=](
|
2019-01-10 06:26:08 +00:00
|
|
|
const MTPChatBannedRights &newRights) {
|
2019-01-10 11:01:59 +00:00
|
|
|
editRestrictedDone(user, newRights);
|
|
|
|
});
|
|
|
|
const auto fail = crl::guard(this, [=] {
|
2019-01-14 06:34:51 +00:00
|
|
|
_editBox = nullptr;
|
2019-01-10 11:01:59 +00:00
|
|
|
});
|
|
|
|
box->setSaveCallback(
|
2019-01-10 12:38:57 +00:00
|
|
|
SaveRestrictedCallback(_peer, user, done, fail));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
_editBox = Ui::show(std::move(box), LayerOption::KeepOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::editRestrictedDone(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPChatBannedRights &rights) {
|
2019-01-14 06:34:51 +00:00
|
|
|
_editBox = nullptr;
|
2019-01-10 11:01:59 +00:00
|
|
|
|
|
|
|
const auto date = unixtime(); // Incorrect, but ignored.
|
2019-01-10 06:26:08 +00:00
|
|
|
if (rights.c_chatBannedRights().vflags.v == 0) {
|
2019-01-10 11:01:59 +00:00
|
|
|
_additional.applyParticipant(MTP_channelParticipant(
|
|
|
|
MTP_int(user->bareId()),
|
|
|
|
MTP_int(date)));
|
2019-01-10 06:26:08 +00:00
|
|
|
} else {
|
2019-01-10 11:01:59 +00:00
|
|
|
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));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
if (const auto callback = _bannedDoneCallback) {
|
|
|
|
callback(user, rights);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::kickUser(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
bool sure) {
|
|
|
|
if (!checkInfoLoaded(user, [=] { kickUser(user); })) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto kickUserSure = crl::guard(this, [=] {
|
|
|
|
kickUser(user, true);
|
|
|
|
});
|
|
|
|
|
2019-01-10 06:26:08 +00:00
|
|
|
// Check restrictions.
|
2019-01-10 11:01:59 +00:00
|
|
|
if (_additional.adminRights(user).has_value()
|
|
|
|
|| _additional.isCreator(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is an admin or creator.
|
2019-01-10 11:01:59 +00:00
|
|
|
if (_additional.canEditAdmin(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!sure) {
|
2019-01-10 11:01:59 +00:00
|
|
|
_editBox = Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
|
|
|
lang(lng_sure_ban_admin),
|
|
|
|
kickUserSure),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_error_cant_ban_admin)),
|
|
|
|
LayerOption::KeepOther);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally kick him.
|
|
|
|
if (!sure) {
|
|
|
|
const auto text = ((_peer->isChat() || _peer->isMegagroup())
|
2019-01-31 17:31:44 +00:00
|
|
|
? lng_profile_sure_kick
|
|
|
|
: lng_profile_sure_kick_channel)(lt_user, App::peerName(user));
|
2019-01-10 11:01:59 +00:00
|
|
|
_editBox = Ui::show(
|
|
|
|
Box<ConfirmBox>(text, kickUserSure),
|
|
|
|
LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-14 06:34:51 +00:00
|
|
|
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto restrictedRights = _additional.restrictedRights(user);
|
|
|
|
const auto currentRights = restrictedRights
|
|
|
|
? *restrictedRights
|
|
|
|
: MTPChatBannedRights(MTP_chatBannedRights(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTP_int(0)));
|
2019-01-31 17:31:44 +00:00
|
|
|
|
|
|
|
const auto done = crl::guard(this, [=](
|
|
|
|
const MTPChatBannedRights &newRights) {
|
|
|
|
editRestrictedDone(user, newRights);
|
|
|
|
});
|
|
|
|
const auto fail = crl::guard(this, [=] {
|
|
|
|
_editBox = nullptr;
|
|
|
|
});
|
|
|
|
const auto callback = SaveRestrictedCallback(_peer, user, done, fail);
|
|
|
|
callback(currentRights, ChannelData::KickedRestrictedRights());
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AddSpecialBoxController::appendRow(not_null<UserData*> user) {
|
|
|
|
if (delegate()->peerListFindRow(user->id) || user->isSelf()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delegate()->peerListAppendRow(createRow(user));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddSpecialBoxController::prependRow(not_null<UserData*> user) {
|
|
|
|
if (delegate()->peerListFindRow(user->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delegate()->peerListPrependRow(createRow(user));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PeerListRow> AddSpecialBoxController::createRow(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
return std::make_unique<PeerListRow>(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddSpecialBoxSearchController::AddSpecialBoxSearchController(
|
|
|
|
not_null<PeerData*> peer,
|
2019-01-10 11:01:59 +00:00
|
|
|
not_null<ParticipantsAdditionalData*> additional)
|
2019-01-10 06:26:08 +00:00
|
|
|
: _peer(peer)
|
|
|
|
, _additional(additional)
|
|
|
|
, _timer([=] { searchOnServer(); }) {
|
2019-01-13 13:28:05 +00:00
|
|
|
subscribeToMigration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxSearchController::subscribeToMigration() {
|
|
|
|
SubscribeToMigration(
|
|
|
|
_peer,
|
|
|
|
lifetime(),
|
|
|
|
[=](not_null<ChannelData*> channel) { _peer = channel; });
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxSearchController::searchQuery(const QString &query) {
|
|
|
|
if (_query != query) {
|
|
|
|
_query = query;
|
|
|
|
_offset = 0;
|
|
|
|
_requestId = 0;
|
|
|
|
_participantsLoaded = false;
|
|
|
|
_chatsContactsAdded = false;
|
2019-01-14 13:01:00 +00:00
|
|
|
_chatMembersAdded = false;
|
2019-01-10 06:26:08 +00:00
|
|
|
_globalLoaded = false;
|
|
|
|
if (!_query.isEmpty() && !searchParticipantsInCache()) {
|
|
|
|
_timer.callOnce(AutoSearchTimeout);
|
|
|
|
} else {
|
|
|
|
_timer.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxSearchController::searchOnServer() {
|
|
|
|
Expects(!_query.isEmpty());
|
|
|
|
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddSpecialBoxSearchController::isLoading() {
|
|
|
|
return _timer.isActive() || _requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddSpecialBoxSearchController::searchParticipantsInCache() {
|
2019-01-13 13:28:05 +00:00
|
|
|
const auto i = _participantsCache.find(_query);
|
|
|
|
if (i != _participantsCache.cend()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
_requestId = 0;
|
2019-01-13 13:28:05 +00:00
|
|
|
searchParticipantsDone(
|
|
|
|
_requestId,
|
|
|
|
i->second.result,
|
|
|
|
i->second.requestedCount);
|
2019-01-10 06:26:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddSpecialBoxSearchController::searchGlobalInCache() {
|
|
|
|
auto it = _globalCache.find(_query);
|
|
|
|
if (it != _globalCache.cend()) {
|
|
|
|
_requestId = 0;
|
|
|
|
searchGlobalDone(_requestId, it->second);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddSpecialBoxSearchController::loadMoreRows() {
|
|
|
|
if (_query.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_globalLoaded) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-14 13:01:00 +00:00
|
|
|
if (_participantsLoaded || _chatMembersAdded) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!_chatsContactsAdded) {
|
|
|
|
addChatsContacts();
|
|
|
|
}
|
|
|
|
if (!isLoading() && !searchGlobalInCache()) {
|
|
|
|
requestGlobal();
|
|
|
|
}
|
|
|
|
} else if (const auto chat = _peer->asChat()) {
|
2019-01-14 13:01:00 +00:00
|
|
|
if (!_chatMembersAdded) {
|
|
|
|
addChatMembers(chat);
|
|
|
|
}
|
2019-01-10 06:26:08 +00:00
|
|
|
} else if (!isLoading()) {
|
|
|
|
requestParticipants();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxSearchController::requestParticipants() {
|
|
|
|
Expects(_peer->isChannel());
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
const auto perPage = kParticipantsPerPage;
|
|
|
|
const auto participantsHash = 0;
|
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
|
|
|
|
_requestId = request(MTPchannels_GetParticipants(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsSearch(MTP_string(_query)),
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
|
|
|
MTP_int(participantsHash)
|
|
|
|
)).done([=](
|
|
|
|
const MTPchannels_ChannelParticipants &result,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
searchParticipantsDone(requestId, result, perPage);
|
|
|
|
}).fail([=](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 AddSpecialBoxSearchController::searchParticipantsDone(
|
|
|
|
mtpRequestId requestId,
|
|
|
|
const MTPchannels_ChannelParticipants &result,
|
|
|
|
int requestedCount) {
|
|
|
|
Expects(_peer->isChannel());
|
|
|
|
|
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
auto query = _query;
|
|
|
|
if (requestId) {
|
2019-01-13 13:28:05 +00:00
|
|
|
const auto addToCache = [&](auto&&...) {
|
2019-01-10 06:26:08 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-01-13 13:28:05 +00:00
|
|
|
};
|
|
|
|
channel->session().api().parseChannelParticipants(
|
|
|
|
channel,
|
|
|
|
result,
|
|
|
|
addToCache);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_requestId != requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_requestId = 0;
|
2019-01-10 11:15:13 +00:00
|
|
|
result.match([&](const MTPDchannels_channelParticipants &data) {
|
2019-01-13 13:28:05 +00:00
|
|
|
const auto &list = data.vparticipants.v;
|
2019-01-10 06:26:08 +00:00
|
|
|
if (list.size() < requestedCount) {
|
2019-01-13 13:28:05 +00:00
|
|
|
// We want cache to have full information about a query with
|
|
|
|
// small results count (that we don't need the second request).
|
|
|
|
// So we don't wait for empty list unlike the non-search case.
|
2019-01-10 06:26:08 +00:00
|
|
|
_participantsLoaded = true;
|
|
|
|
if (list.empty() && _offset == 0) {
|
2019-01-13 13:28:05 +00:00
|
|
|
// No results, request global search immediately.
|
2019-01-10 06:26:08 +00:00
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
for (const auto &data : list) {
|
|
|
|
if (const auto user = _additional->applyParticipant(data)) {
|
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
}
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
_offset += list.size();
|
2019-01-10 11:15:13 +00:00
|
|
|
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
|
2019-01-10 06:26:08 +00:00
|
|
|
_participantsLoaded = true;
|
2019-01-10 11:15:13 +00:00
|
|
|
});
|
2019-01-10 06:26:08 +00:00
|
|
|
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxSearchController::requestGlobal() {
|
|
|
|
if (_query.isEmpty()) {
|
|
|
|
_globalLoaded = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto perPage = SearchPeopleLimit;
|
|
|
|
_requestId = request(MTPcontacts_Search(
|
|
|
|
MTP_string(_query),
|
|
|
|
MTP_int(perPage)
|
|
|
|
)).done([=](const MTPcontacts_Found &result, mtpRequestId requestId) {
|
|
|
|
searchGlobalDone(requestId, result);
|
|
|
|
}).fail([=](const RPCError &error, mtpRequestId requestId) {
|
|
|
|
if (_requestId == requestId) {
|
|
|
|
_requestId = 0;
|
|
|
|
_globalLoaded = true;
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
_globalQueries.emplace(_requestId, _query);
|
|
|
|
}
|
|
|
|
|
2019-01-10 11:01:59 +00:00
|
|
|
void AddSpecialBoxSearchController::searchGlobalDone(
|
|
|
|
mtpRequestId requestId,
|
|
|
|
const MTPcontacts_Found &result) {
|
2019-01-10 06:26:08 +00:00
|
|
|
Expects(result.type() == mtpc_contacts_found);
|
|
|
|
|
|
|
|
auto &found = result.c_contacts_found();
|
|
|
|
auto query = _query;
|
|
|
|
if (requestId) {
|
2019-01-18 12:27:37 +00:00
|
|
|
_peer->owner().processUsers(found.vusers);
|
|
|
|
_peer->owner().processChats(found.vchats);
|
2019-01-10 06:26:08 +00:00
|
|
|
auto it = _globalQueries.find(requestId);
|
|
|
|
if (it != _globalQueries.cend()) {
|
|
|
|
query = it->second;
|
|
|
|
_globalCache[query] = result;
|
|
|
|
_globalQueries.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto feedList = [&](const MTPVector<MTPPeer> &list) {
|
|
|
|
for (const auto &mtpPeer : list.v) {
|
|
|
|
const auto peerId = peerFromMTP(mtpPeer);
|
2019-01-18 12:27:37 +00:00
|
|
|
if (const auto peer = _peer->owner().peerLoaded(peerId)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (const auto user = peer->asUser()) {
|
2019-01-10 11:01:59 +00:00
|
|
|
_additional->checkForLoaded(user);
|
2019-01-10 06:26:08 +00:00
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (_requestId == requestId) {
|
|
|
|
_requestId = 0;
|
|
|
|
_globalLoaded = true;
|
|
|
|
feedList(found.vmy_results);
|
|
|
|
feedList(found.vresults);
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 13:21:24 +00:00
|
|
|
void AddSpecialBoxSearchController::addChatMembers(
|
|
|
|
not_null<ChatData*> chat) {
|
|
|
|
if (chat->participants.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-14 13:01:00 +00:00
|
|
|
_chatMembersAdded = true;
|
2019-01-10 13:21:24 +00:00
|
|
|
const auto wordList = TextUtilities::PrepareSearchWords(_query);
|
|
|
|
if (wordList.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto allWordsAreFound = [&](
|
|
|
|
const base::flat_set<QString> &nameWords) {
|
|
|
|
const auto hasNamePartStartingWith = [&](const QString &word) {
|
|
|
|
for (const auto &nameWord : nameWords) {
|
|
|
|
if (nameWord.startsWith(word)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &word : wordList) {
|
|
|
|
if (!hasNamePartStartingWith(word)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-01-13 08:03:34 +00:00
|
|
|
for (const auto user : chat->participants) {
|
2019-01-10 13:21:24 +00:00
|
|
|
if (allWordsAreFound(user->nameWords())) {
|
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxSearchController::addChatsContacts() {
|
|
|
|
_chatsContactsAdded = true;
|
2019-01-10 13:21:24 +00:00
|
|
|
const auto wordList = TextUtilities::PrepareSearchWords(_query);
|
2019-01-10 06:26:08 +00:00
|
|
|
if (wordList.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 13:21:24 +00:00
|
|
|
const auto allWordsAreFound = [&](
|
|
|
|
const base::flat_set<QString> &nameWords) {
|
|
|
|
const auto hasNamePartStartingWith = [&](const QString &word) {
|
|
|
|
for (const auto &nameWord : nameWords) {
|
|
|
|
if (nameWord.startsWith(word)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &word : wordList) {
|
|
|
|
if (!hasNamePartStartingWith(word)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2019-01-10 06:26:08 +00:00
|
|
|
|
2019-01-10 13:21:24 +00:00
|
|
|
const auto getSmallestIndex = [&](
|
|
|
|
Dialogs::IndexedList *list) -> const Dialogs::List* {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (list->isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = (const Dialogs::List*)nullptr;
|
2019-01-10 13:21:24 +00:00
|
|
|
for (const auto &word : wordList) {
|
|
|
|
const auto found = list->filtered(word[0]);
|
2019-01-10 06:26:08 +00:00
|
|
|
if (found->isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!result || result->size() > found->size()) {
|
|
|
|
result = found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2019-01-10 13:21:24 +00:00
|
|
|
const auto dialogsIndex = getSmallestIndex(App::main()->dialogsList());
|
2019-01-13 13:28:05 +00:00
|
|
|
const auto contactsIndex = getSmallestIndex(
|
|
|
|
App::main()->contactsNoDialogsList());
|
2019-01-10 06:26:08 +00:00
|
|
|
|
2019-01-10 13:21:24 +00:00
|
|
|
const auto filterAndAppend = [&](const Dialogs::List *list) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!list) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto row : *list) {
|
|
|
|
if (const auto history = row->history()) {
|
|
|
|
if (const auto user = history->peer->asUser()) {
|
|
|
|
if (allWordsAreFound(user->nameWords())) {
|
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
filterAndAppend(dialogsIndex);
|
|
|
|
filterAndAppend(contactsIndex);
|
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|