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"
|
|
|
|
|
2021-11-24 04:25:05 +00:00
|
|
|
#include "api/api_chat_participants.h"
|
2019-01-10 06:26:08 +00:00
|
|
|
#include "boxes/peers/edit_participant_box.h"
|
2021-02-01 18:30:00 +00:00
|
|
|
#include "boxes/peers/edit_peer_type_box.h"
|
2021-10-18 21:36:55 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2021-10-18 21:12:11 +00:00
|
|
|
#include "boxes/max_invite_box.h"
|
2019-01-10 06:26:08 +00:00
|
|
|
#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-04-22 18:18:57 +00:00
|
|
|
#include "data/data_folder.h"
|
2020-06-12 12:12:34 +00:00
|
|
|
#include "data/data_changes.h"
|
2019-01-10 06:26:08 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "dialogs/dialogs_indexed_list.h"
|
2022-07-07 12:51:15 +00:00
|
|
|
#include "ui/text/text_utilities.h" // Ui::Text::RichLangValue
|
2021-02-01 18:30:00 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2022-07-07 12:51:15 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
2021-02-01 18:30:00 +00:00
|
|
|
#include "ui/wrap/padding_wrap.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2021-02-01 18:30:00 +00:00
|
|
|
#include "info/profile/info_profile_icon.h"
|
2019-01-10 06:26:08 +00:00
|
|
|
#include "apiwrap.h"
|
2021-02-01 18:30:00 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2022-07-07 12:51:15 +00:00
|
|
|
#include "styles/style_layers.h"
|
2019-01-10 06:26:08 +00:00
|
|
|
|
|
|
|
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()) {
|
2022-12-23 13:13:32 +00:00
|
|
|
if (channel->isMegagroup() && channel->canViewMembers()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
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-07-25 18:55:11 +00:00
|
|
|
AddParticipantsBoxController::AddParticipantsBoxController(
|
2020-12-21 19:43:23 +00:00
|
|
|
not_null<Main::Session*> session)
|
|
|
|
: ContactsBoxController(session) {
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
AddParticipantsBoxController::AddParticipantsBoxController(
|
|
|
|
not_null<PeerData*> peer)
|
2019-07-25 18:55:11 +00:00
|
|
|
: 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)
|
2020-12-21 19:43:23 +00:00
|
|
|
: ContactsBoxController(&peer->session())
|
2019-01-13 13:28:05 +00:00
|
|
|
, _peer(peer)
|
2019-01-10 06:26:08 +00:00
|
|
|
, _alreadyIn(std::move(alreadyIn)) {
|
2021-02-01 18:30:00 +00:00
|
|
|
if (needsInviteLinkButton()) {
|
|
|
|
setStyleOverrides(&st::peerListWithInviteViaLink);
|
|
|
|
}
|
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) {
|
2020-06-22 07:30:17 +00:00
|
|
|
const auto &serverConfig = session().serverConfig();
|
2019-01-10 06:26:08 +00:00
|
|
|
auto count = fullCount();
|
2019-01-13 13:28:05 +00:00
|
|
|
auto limit = _peer && (_peer->isChat() || _peer->isMegagroup())
|
2020-06-17 09:36:25 +00:00
|
|
|
? serverConfig.megagroupSizeMax
|
|
|
|
: serverConfig.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()) {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Box<MaxInviteBox>(_peer->asChannel()));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
} else if (count >= serverConfig.chatSizeMax
|
|
|
|
&& count < serverConfig.megagroupSizeMax) {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Ui::MakeInformBox(tr::lng_profile_add_more_after_create()));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2022-12-23 13:13:32 +00:00
|
|
|
&& channel->canViewMembers()
|
2019-01-13 13:28:05 +00:00
|
|
|
&& 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-06-18 16:53:27 +00:00
|
|
|
? QString()
|
2022-11-29 21:46:36 +00:00
|
|
|
: (u"%1 / %2"_q
|
2020-06-17 09:36:25 +00:00
|
|
|
).arg(fullCount()
|
|
|
|
).arg(session().serverConfig().megagroupSizeMax);
|
2019-06-18 15:00:55 +00:00
|
|
|
delegate()->peerListSetTitle(tr::lng_profile_add_participant());
|
2019-06-18 16:53:27 +00:00
|
|
|
delegate()->peerListSetAdditionalTitle(rpl::single(additional));
|
2021-02-01 18:30:00 +00:00
|
|
|
|
|
|
|
addInviteLinkButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddParticipantsBoxController::needsInviteLinkButton() {
|
|
|
|
if (!_peer) {
|
|
|
|
return false;
|
|
|
|
} else if (const auto channel = _peer->asChannel()) {
|
|
|
|
return channel->canHaveInviteLink();
|
|
|
|
}
|
|
|
|
return _peer->asChat()->canHaveInviteLink();
|
|
|
|
}
|
|
|
|
|
2022-03-03 07:22:26 +00:00
|
|
|
QPointer<Ui::BoxContent> AddParticipantsBoxController::showBox(
|
|
|
|
object_ptr<Ui::BoxContent> box) const {
|
|
|
|
const auto weak = Ui::MakeWeak(box.data());
|
|
|
|
delegate()->peerListShowBox(std::move(box), Ui::LayerOption::KeepOther);
|
|
|
|
return weak;
|
|
|
|
}
|
|
|
|
|
2021-02-01 18:30:00 +00:00
|
|
|
void AddParticipantsBoxController::addInviteLinkButton() {
|
|
|
|
if (!needsInviteLinkButton()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto button = object_ptr<Ui::PaddingWrap<Ui::SettingsButton>>(
|
|
|
|
nullptr,
|
|
|
|
object_ptr<Ui::SettingsButton>(
|
|
|
|
nullptr,
|
|
|
|
tr::lng_profile_add_via_link(),
|
|
|
|
st::inviteViaLinkButton),
|
|
|
|
style::margins(0, st::membersMarginTop, 0, 0));
|
2022-06-09 05:05:11 +00:00
|
|
|
|
|
|
|
const auto icon = Ui::CreateChild<Info::Profile::FloatingIcon>(
|
2021-02-01 18:30:00 +00:00
|
|
|
button->entity(),
|
|
|
|
st::inviteViaLinkIcon,
|
2022-06-09 05:05:11 +00:00
|
|
|
QPoint());
|
|
|
|
button->entity()->heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
icon->moveToLeft(
|
|
|
|
st::inviteViaLinkIconPosition.x(),
|
|
|
|
(height - st::inviteViaLinkIcon.height()) / 2);
|
|
|
|
}, icon->lifetime());
|
|
|
|
|
2021-02-01 18:30:00 +00:00
|
|
|
button->entity()->setClickedCallback([=] {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Box<EditPeerTypeBox>(_peer));
|
2021-02-01 18:30:00 +00:00
|
|
|
});
|
|
|
|
button->entity()->events(
|
|
|
|
) | rpl::filter([=](not_null<QEvent*> e) {
|
|
|
|
return (e->type() == QEvent::Enter);
|
|
|
|
}) | rpl::start_with_next([=] {
|
|
|
|
delegate()->peerListMouseLeftGeometry();
|
|
|
|
}, button->lifetime());
|
|
|
|
delegate()->peerListSetAboveWidget(std::move(button));
|
2019-01-13 13:28:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:51:15 +00:00
|
|
|
void AddParticipantsBoxController::inviteSelectedUsers(
|
|
|
|
not_null<PeerListBox*> box,
|
|
|
|
Fn<void()> done) const {
|
2019-01-13 13:28:05 +00:00
|
|
|
Expects(_peer != nullptr);
|
|
|
|
|
2020-12-21 19:43:23 +00:00
|
|
|
const auto rows = box->collectSelectedRows();
|
2021-03-13 12:12:08 +00:00
|
|
|
const auto users = ranges::views::all(
|
2019-01-13 13:28:05 +00:00
|
|
|
rows
|
2021-03-13 12:12:08 +00:00
|
|
|
) | ranges::views::transform([](not_null<PeerData*> peer) {
|
2019-01-13 13:28:05 +00:00
|
|
|
Expects(peer->isUser());
|
|
|
|
Expects(!peer->isSelf());
|
|
|
|
|
|
|
|
return not_null<UserData*>(peer->asUser());
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
if (users.empty()) {
|
2022-07-07 12:51:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto request = [=](bool checked) {
|
|
|
|
_peer->session().api().chatParticipants().add(_peer, users, checked);
|
|
|
|
};
|
|
|
|
if (_peer->isChannel()) {
|
|
|
|
request(false);
|
|
|
|
return done();
|
2019-01-13 13:28:05 +00:00
|
|
|
}
|
2022-07-07 12:51:15 +00:00
|
|
|
Ui::BoxShow(box).showBox(Box([=](not_null<Ui::GenericBox*> box) {
|
|
|
|
auto checkbox = object_ptr<Ui::Checkbox>(
|
|
|
|
box.get(),
|
|
|
|
tr::lng_participant_invite_history(),
|
|
|
|
true,
|
|
|
|
st::defaultBoxCheckbox);
|
|
|
|
const auto weak = Ui::MakeWeak(checkbox.data());
|
|
|
|
|
|
|
|
auto text = (users.size() == 1)
|
|
|
|
? tr::lng_participant_invite_sure(
|
|
|
|
tr::now,
|
|
|
|
lt_user,
|
2022-08-09 11:12:19 +00:00
|
|
|
{ users.front()->name()},
|
2022-07-07 12:51:15 +00:00
|
|
|
lt_group,
|
2022-08-09 11:12:19 +00:00
|
|
|
{ _peer->name()},
|
2022-07-07 12:51:15 +00:00
|
|
|
Ui::Text::RichLangValue)
|
|
|
|
: tr::lng_participant_invite_sure_many(
|
|
|
|
tr::now,
|
|
|
|
lt_count,
|
|
|
|
int(users.size()),
|
|
|
|
lt_group,
|
2022-08-09 11:12:19 +00:00
|
|
|
{ _peer->name() },
|
2022-07-07 12:51:15 +00:00
|
|
|
Ui::Text::RichLangValue);
|
|
|
|
Ui::ConfirmBox(box, {
|
|
|
|
.text = std::move(text),
|
|
|
|
.confirmed = crl::guard(weak, [=](Fn<void()> &&close) {
|
|
|
|
request(weak->checked());
|
|
|
|
done();
|
|
|
|
close();
|
|
|
|
}),
|
|
|
|
.confirmText = tr::lng_participant_invite(),
|
|
|
|
});
|
|
|
|
|
|
|
|
auto padding = st::boxPadding;
|
|
|
|
padding.setTop(padding.bottom());
|
|
|
|
box->addRow(std::move(checkbox), std::move(padding));
|
|
|
|
}));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 18:55:11 +00:00
|
|
|
void AddParticipantsBoxController::Start(
|
|
|
|
not_null<Window::SessionNavigation*> navigation,
|
|
|
|
not_null<ChatData*> chat) {
|
2020-12-21 19:43:23 +00:00
|
|
|
auto controller = std::make_unique<AddParticipantsBoxController>(chat);
|
2019-01-13 13:28:05 +00:00
|
|
|
const auto weak = controller.get();
|
2019-01-10 06:26:08 +00:00
|
|
|
auto initBox = [=](not_null<PeerListBox*> box) {
|
2019-06-18 16:53:27 +00:00
|
|
|
box->addButton(tr::lng_participant_invite(), [=] {
|
2022-07-07 12:51:15 +00:00
|
|
|
weak->inviteSelectedUsers(box, [=] {
|
2022-03-03 07:22:26 +00:00
|
|
|
navigation->parentController()->showPeerHistory(
|
|
|
|
chat,
|
|
|
|
Window::SectionShow::Way::ClearStack,
|
|
|
|
ShowAtTheEndMsgId);
|
2022-07-07 12:51:15 +00:00
|
|
|
});
|
2019-01-10 06:26:08 +00:00
|
|
|
});
|
2019-06-18 16:53:27 +00:00
|
|
|
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
2019-01-10 06:26:08 +00:00
|
|
|
};
|
2022-03-03 07:22:26 +00:00
|
|
|
Window::Show(navigation).showBox(
|
|
|
|
Box<PeerListBox>(std::move(controller), std::move(initBox)),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::Start(
|
2019-07-25 18:55:11 +00:00
|
|
|
not_null<Window::SessionNavigation*> navigation,
|
2019-01-10 06:26:08 +00:00
|
|
|
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) {
|
2019-06-18 16:53:27 +00:00
|
|
|
box->addButton(tr::lng_participant_invite(), [=] {
|
2022-07-07 12:51:15 +00:00
|
|
|
weak->inviteSelectedUsers(box, [=] {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (channel->isMegagroup()) {
|
2022-03-03 07:22:26 +00:00
|
|
|
navigation->parentController()->showPeerHistory(
|
|
|
|
channel,
|
|
|
|
Window::SectionShow::Way::ClearStack,
|
|
|
|
ShowAtTheEndMsgId);
|
2019-01-10 06:26:08 +00:00
|
|
|
} else {
|
|
|
|
box->closeBox();
|
|
|
|
}
|
2022-07-07 12:51:15 +00:00
|
|
|
});
|
2019-01-10 06:26:08 +00:00
|
|
|
});
|
2019-01-13 13:28:05 +00:00
|
|
|
box->addButton(
|
2019-06-18 16:53:27 +00:00
|
|
|
justCreated ? tr::lng_create_group_skip() : tr::lng_cancel(),
|
2019-01-13 13:28:05 +00:00
|
|
|
[=] { box->closeBox(); });
|
2019-01-10 06:26:08 +00:00
|
|
|
if (justCreated) {
|
|
|
|
box->boxClosing() | rpl::start_with_next([=] {
|
2019-06-05 17:15:17 +00:00
|
|
|
auto params = Window::SectionShow();
|
|
|
|
params.activation = anim::activation::background;
|
2020-06-23 17:21:58 +00:00
|
|
|
navigation->parentController()->showPeerHistory(
|
2019-06-05 17:15:17 +00:00
|
|
|
channel,
|
|
|
|
params,
|
|
|
|
ShowAtTheEndMsgId);
|
2019-01-13 13:28:05 +00:00
|
|
|
}, box->lifetime());
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
};
|
2022-03-03 07:22:26 +00:00
|
|
|
Window::Show(navigation).showBox(
|
|
|
|
Box<PeerListBox>(std::move(controller), std::move(initBox)),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddParticipantsBoxController::Start(
|
2019-07-25 18:55:11 +00:00
|
|
|
not_null<Window::SessionNavigation*> navigation,
|
2019-01-10 06:26:08 +00:00
|
|
|
not_null<ChannelData*> channel,
|
|
|
|
base::flat_set<not_null<UserData*>> &&alreadyIn) {
|
2019-07-25 18:55:11 +00:00
|
|
|
Start(navigation, channel, std::move(alreadyIn), false);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 18:55:11 +00:00
|
|
|
void AddParticipantsBoxController::Start(
|
|
|
|
not_null<Window::SessionNavigation*> navigation,
|
|
|
|
not_null<ChannelData*> channel) {
|
|
|
|
Start(navigation, channel, {}, true);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddSpecialBoxController::AddSpecialBoxController(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
Role role,
|
|
|
|
AdminDoneCallback adminDoneCallback,
|
|
|
|
BannedDoneCallback bannedDoneCallback)
|
|
|
|
: PeerListController(std::make_unique<AddSpecialBoxSearchController>(
|
|
|
|
peer,
|
|
|
|
&_additional))
|
|
|
|
, _peer(peer)
|
2020-06-17 09:36:25 +00:00
|
|
|
, _api(&_peer->session().mtp())
|
2019-01-10 06:26:08 +00:00
|
|
|
, _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();
|
|
|
|
}
|
|
|
|
|
2019-07-25 18:55:11 +00:00
|
|
|
Main::Session &AddSpecialBoxController::session() const {
|
|
|
|
return _peer->session();
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
void AddSpecialBoxController::subscribeToMigration() {
|
2021-02-12 10:40:55 +00:00
|
|
|
const auto chat = _peer->asChat();
|
|
|
|
if (!chat) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-13 13:28:05 +00:00
|
|
|
SubscribeToMigration(
|
2021-02-12 10:40:55 +00:00
|
|
|
chat,
|
2019-01-13 13:28:05 +00:00
|
|
|
lifetime(),
|
2021-02-12 10:40:55 +00:00
|
|
|
[=](not_null<ChannelData*> channel) { migrate(chat, channel); });
|
2019-01-13 13:28:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 10:40:55 +00:00
|
|
|
void AddSpecialBoxController::migrate(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
not_null<ChannelData*> channel) {
|
2019-01-13 13:28:05 +00:00
|
|
|
_peer = channel;
|
2021-02-12 10:40:55 +00:00
|
|
|
_additional.migrate(chat, channel);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 07:22:26 +00:00
|
|
|
QPointer<Ui::BoxContent> AddSpecialBoxController::showBox(
|
|
|
|
object_ptr<Ui::BoxContent> box) const {
|
|
|
|
const auto weak = Ui::MakeWeak(box.data());
|
|
|
|
delegate()->peerListShowBox(std::move(box), Ui::LayerOption::KeepOther);
|
|
|
|
return weak;
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
std::unique_ptr<PeerListRow> AddSpecialBoxController::createSearchRow(
|
|
|
|
not_null<PeerData*> peer) {
|
2019-09-09 07:05:29 +00:00
|
|
|
if (_excludeSelf && peer->isSelf()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
return createRow(user);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::prepare() {
|
|
|
|
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
|
2019-06-18 15:00:55 +00:00
|
|
|
auto title = [&] {
|
2019-01-10 06:26:08 +00:00
|
|
|
switch (_role) {
|
2019-09-09 07:05:29 +00:00
|
|
|
case Role::Members:
|
|
|
|
return tr::lng_profile_participants_section();
|
2019-01-13 13:28:05 +00:00
|
|
|
case Role::Admins:
|
2019-06-18 15:00:55 +00:00
|
|
|
return tr::lng_channel_add_admin();
|
2019-01-13 13:28:05 +00:00
|
|
|
case Role::Restricted:
|
2019-06-18 15:00:55 +00:00
|
|
|
return tr::lng_channel_add_exception();
|
2019-01-13 13:28:05 +00:00
|
|
|
case Role::Kicked:
|
2019-06-18 15:00:55 +00:00
|
|
|
return tr::lng_channel_add_removed();
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
Unexpected("Role in AddSpecialBoxController::prepare()");
|
|
|
|
}();
|
2019-06-18 15:00:55 +00:00
|
|
|
delegate()->peerListSetTitle(std::move(title));
|
2019-06-19 15:09:03 +00:00
|
|
|
setDescriptionText(tr::lng_contacts_loading(tr::now));
|
|
|
|
setSearchNoResultsText(tr::lng_blocked_list_not_found(tr::now));
|
2019-01-10 06:26:08 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-06-12 12:12:34 +00:00
|
|
|
using UpdateFlag = Data::PeerUpdate::Flag;
|
|
|
|
chat->session().changes().peerUpdates(
|
|
|
|
chat,
|
|
|
|
UpdateFlag::Members | UpdateFlag::Admins
|
|
|
|
) | rpl::start_with_next([=](const Data::PeerUpdate &update) {
|
|
|
|
_additional.fillFromPeer();
|
|
|
|
if (update.flags & UpdateFlag::Members) {
|
|
|
|
rebuildChatRows(chat);
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-03-19 15:10:44 +00:00
|
|
|
Assert(row->peer()->isUser());
|
2019-01-10 06:26:08 +00:00
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
if (participants.contains(user)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 10:53:54 +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;
|
2021-08-25 08:15:05 +00:00
|
|
|
const auto participantsHash = uint64(0);
|
2019-01-10 06:26:08 +00:00
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
_loadRequestId = _api.request(MTPchannels_GetParticipants(
|
2019-01-10 06:26:08 +00:00
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsRecent(),
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_long(participantsHash)
|
2019-01-10 06:26:08 +00:00
|
|
|
)).done([=](const MTPchannels_ChannelParticipants &result) {
|
|
|
|
_loadRequestId = 0;
|
2021-11-24 04:25:05 +00:00
|
|
|
result.match([&](const MTPDchannels_channelParticipants &data) {
|
|
|
|
const auto &[availableCount, list] = Api::ChatParticipants::Parse(
|
|
|
|
channel,
|
|
|
|
data);
|
2019-01-10 11:01:59 +00:00
|
|
|
for (const auto &data : list) {
|
2021-03-19 14:01:21 +00:00
|
|
|
if (const auto participant = _additional.applyParticipant(
|
|
|
|
data)) {
|
|
|
|
appendRow(participant);
|
2019-01-10 11:01:59 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2021-11-24 04:25:05 +00:00
|
|
|
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
|
|
|
|
LOG(("API Error: channels.channelParticipantsNotModified received!"));
|
2019-01-10 06:26:08 +00:00
|
|
|
});
|
|
|
|
if (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
setDescriptionText(QString());
|
|
|
|
} else if (_allLoaded) {
|
2019-06-19 15:09:03 +00:00
|
|
|
setDescriptionText(tr::lng_blocked_list_not_found(tr::now));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
delegate()->peerListRefreshRows();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([this] {
|
2019-01-10 06:26:08 +00:00
|
|
|
_loadRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::rowClicked(not_null<PeerListRow*> row) {
|
2021-03-19 15:10:44 +00:00
|
|
|
const auto participant = row->peer();
|
|
|
|
const auto user = participant->asUser();
|
2019-01-10 06:26:08 +00:00
|
|
|
switch (_role) {
|
2021-03-19 15:10:44 +00:00
|
|
|
case Role::Admins:
|
|
|
|
Assert(user != nullptr);
|
|
|
|
return showAdmin(user);
|
|
|
|
case Role::Restricted:
|
|
|
|
Assert(user != nullptr);
|
|
|
|
return showRestricted(user);
|
|
|
|
case Role::Kicked: return kickUser(participant);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
Unexpected("Role in AddSpecialBoxController::rowClicked()");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback>
|
|
|
|
bool AddSpecialBoxController::checkInfoLoaded(
|
2021-03-19 14:01:21 +00:00
|
|
|
not_null<PeerData*> participant,
|
2019-01-10 06:26:08 +00:00
|
|
|
Callback callback) {
|
2021-03-19 14:01:21 +00:00
|
|
|
if (_additional.infoLoaded(participant)) {
|
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();
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPchannels_GetParticipant(
|
2019-01-10 06:26:08 +00:00
|
|
|
channel->inputChannel,
|
2021-03-19 14:01:21 +00:00
|
|
|
participant->input
|
2019-01-10 06:26:08 +00:00
|
|
|
)).done([=](const MTPchannels_ChannelParticipant &result) {
|
2021-03-19 14:01:21 +00:00
|
|
|
result.match([&](const MTPDchannels_channelParticipant &data) {
|
|
|
|
channel->owner().processUsers(data.vusers());
|
2021-11-24 04:25:05 +00:00
|
|
|
_additional.applyParticipant(
|
|
|
|
Api::ChatParticipant(data.vparticipant(), channel));
|
2021-03-19 14:01:21 +00:00
|
|
|
});
|
2019-01-10 06:26:08 +00:00
|
|
|
callback();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-03-19 14:01:21 +00:00
|
|
|
_additional.setExternal(participant);
|
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-06-14 14:56:06 +00:00
|
|
|
if (_editParticipantBox) {
|
|
|
|
_editParticipantBox->closeBox();
|
|
|
|
}
|
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) {
|
2022-03-03 07:22:26 +00:00
|
|
|
_editBox = showBox(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeConfirmBox({
|
|
|
|
tr::lng_sure_add_admin_unremove(),
|
|
|
|
showAdminSure
|
2022-03-03 07:22:26 +00:00
|
|
|
}));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(
|
|
|
|
Ui::MakeInformBox(tr::lng_error_cant_add_admin_unban()));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Ui::MakeInformBox(tr::lng_error_cant_add_admin_invite()));
|
2019-01-10 06:26:08 +00:00
|
|
|
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) {
|
2022-03-03 07:22:26 +00:00
|
|
|
_editBox = showBox(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeConfirmBox({
|
|
|
|
tr::lng_sure_add_admin_unremove(),
|
|
|
|
showAdminSure
|
2022-03-03 07:22:26 +00:00
|
|
|
}));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Ui::MakeInformBox(tr::lng_error_cant_add_admin_unban()));
|
2019-01-10 06:26:08 +00:00
|
|
|
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) {
|
2022-02-27 08:23:20 +00:00
|
|
|
auto text = ((_peer->isChat() || _peer->isMegagroup())
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_sure_add_admin_invite
|
2022-02-27 08:23:20 +00:00
|
|
|
: tr::lng_sure_add_admin_invite_channel)();
|
2022-03-03 07:22:26 +00:00
|
|
|
_editBox = showBox(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeConfirmBox({
|
|
|
|
std::move(text),
|
|
|
|
showAdminSure
|
2022-03-03 07:22:26 +00:00
|
|
|
}));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Ui::MakeInformBox(tr::lng_error_cant_add_admin_invite()));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally show the admin.
|
2020-09-10 11:19:48 +00:00
|
|
|
const auto currentRights = adminRights
|
2019-01-10 11:01:59 +00:00
|
|
|
? *adminRights
|
2021-07-08 10:34:06 +00:00
|
|
|
: ChatAdminRightsInfo();
|
2019-07-19 13:34:09 +00:00
|
|
|
auto box = Box<EditAdminBox>(
|
|
|
|
_peer,
|
|
|
|
user,
|
|
|
|
currentRights,
|
|
|
|
_additional.adminRank(user));
|
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, [=](
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRightsInfo newRights,
|
2019-07-19 13:34:09 +00:00
|
|
|
const QString &rank) {
|
|
|
|
editAdminDone(user, newRights, rank);
|
2019-01-10 12:38:57 +00:00
|
|
|
});
|
|
|
|
const auto fail = crl::guard(this, [=] {
|
2019-06-14 14:56:06 +00:00
|
|
|
if (_editParticipantBox) {
|
|
|
|
_editParticipantBox->closeBox();
|
|
|
|
}
|
2019-01-10 12:38:57 +00:00
|
|
|
});
|
|
|
|
box->setSaveCallback(SaveAdminCallback(_peer, user, done, fail));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
2022-03-03 07:22:26 +00:00
|
|
|
_editParticipantBox = showBox(std::move(box));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::editAdminDone(
|
|
|
|
not_null<UserData*> user,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRightsInfo rights,
|
2019-07-19 13:34:09 +00:00
|
|
|
const QString &rank) {
|
2019-06-14 14:56:06 +00:00
|
|
|
if (_editParticipantBox) {
|
|
|
|
_editParticipantBox->closeBox();
|
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
|
2021-11-21 21:16:44 +00:00
|
|
|
_additional.applyAdminLocally(user, rights, rank);
|
2019-01-10 11:01:59 +00:00
|
|
|
if (const auto callback = _adminDoneCallback) {
|
2019-07-19 13:34:09 +00:00
|
|
|
callback(user, rights, rank);
|
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-06-14 14:56:06 +00:00
|
|
|
if (_editParticipantBox) {
|
|
|
|
_editParticipantBox->closeBox();
|
|
|
|
}
|
2019-01-10 06:26:08 +00:00
|
|
|
|
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.
|
2020-09-10 11:19:48 +00:00
|
|
|
if (!_additional.isCreator(user) && _additional.canEditAdmin(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!sure) {
|
2022-03-03 07:22:26 +00:00
|
|
|
_editBox = showBox(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeConfirmBox({
|
|
|
|
tr::lng_sure_ban_admin(),
|
|
|
|
showRestrictedSure
|
2022-03-03 07:22:26 +00:00
|
|
|
}));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Ui::MakeInformBox(tr::lng_error_cant_ban_admin()));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally edit the restricted.
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto currentRights = restrictedRights
|
|
|
|
? *restrictedRights
|
2021-07-08 10:34:06 +00:00
|
|
|
: ChatRestrictionsInfo();
|
2019-01-10 11:01:59 +00:00
|
|
|
auto box = Box<EditRestrictedBox>(
|
|
|
|
_peer,
|
|
|
|
user,
|
|
|
|
_additional.adminRights(user).has_value(),
|
|
|
|
currentRights);
|
2021-03-19 14:01:21 +00:00
|
|
|
if (_additional.canRestrictParticipant(user)) {
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto done = crl::guard(this, [=](
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestrictionsInfo newRights) {
|
2019-01-10 11:01:59 +00:00
|
|
|
editRestrictedDone(user, newRights);
|
|
|
|
});
|
|
|
|
const auto fail = crl::guard(this, [=] {
|
2019-06-14 14:56:06 +00:00
|
|
|
if (_editParticipantBox) {
|
|
|
|
_editParticipantBox->closeBox();
|
|
|
|
}
|
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
|
|
|
}
|
2022-03-03 07:22:26 +00:00
|
|
|
_editParticipantBox = showBox(std::move(box));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::editRestrictedDone(
|
2021-03-19 14:01:21 +00:00
|
|
|
not_null<PeerData*> participant,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestrictionsInfo rights) {
|
2019-06-14 14:56:06 +00:00
|
|
|
if (_editParticipantBox) {
|
|
|
|
_editParticipantBox->closeBox();
|
|
|
|
}
|
2019-01-10 11:01:59 +00:00
|
|
|
|
2021-11-21 21:16:44 +00:00
|
|
|
_additional.applyBannedLocally(participant, rights);
|
2019-01-10 11:01:59 +00:00
|
|
|
if (const auto callback = _bannedDoneCallback) {
|
2021-03-19 14:01:21 +00:00
|
|
|
callback(participant, rights);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSpecialBoxController::kickUser(
|
2021-03-19 14:01:21 +00:00
|
|
|
not_null<PeerData*> participant,
|
2019-01-10 06:26:08 +00:00
|
|
|
bool sure) {
|
2021-03-19 14:01:21 +00:00
|
|
|
if (!checkInfoLoaded(participant, [=] { kickUser(participant); })) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto kickUserSure = crl::guard(this, [=] {
|
2021-03-19 14:01:21 +00:00
|
|
|
kickUser(participant, true);
|
2019-01-10 11:01:59 +00:00
|
|
|
});
|
|
|
|
|
2019-01-10 06:26:08 +00:00
|
|
|
// Check restrictions.
|
2021-03-19 14:01:21 +00:00
|
|
|
const auto user = participant->asUser();
|
|
|
|
if (user && (_additional.adminRights(user).has_value()
|
|
|
|
|| (_additional.isCreator(user)))) {
|
2019-01-10 06:26:08 +00:00
|
|
|
// The user is an admin or creator.
|
2020-09-10 11:19:48 +00:00
|
|
|
if (!_additional.isCreator(user) && _additional.canEditAdmin(user)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!sure) {
|
2022-03-03 07:22:26 +00:00
|
|
|
_editBox = showBox(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeConfirmBox({
|
|
|
|
tr::lng_sure_ban_admin(),
|
|
|
|
kickUserSure
|
2022-03-03 07:22:26 +00:00
|
|
|
}));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-03 07:22:26 +00:00
|
|
|
showBox(Ui::MakeInformBox(tr::lng_error_cant_ban_admin()));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally kick him.
|
|
|
|
if (!sure) {
|
|
|
|
const auto text = ((_peer->isChat() || _peer->isMegagroup())
|
2019-06-19 16:39:25 +00:00
|
|
|
? tr::lng_profile_sure_kick
|
|
|
|
: tr::lng_profile_sure_kick_channel)(
|
|
|
|
tr::now,
|
|
|
|
lt_user,
|
2022-08-09 11:12:19 +00:00
|
|
|
participant->name());
|
2022-03-03 07:22:26 +00:00
|
|
|
_editBox = showBox(Ui::MakeConfirmBox({ text, kickUserSure }));
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-14 06:34:51 +00:00
|
|
|
|
2021-03-19 14:01:21 +00:00
|
|
|
const auto restrictedRights = _additional.restrictedRights(participant);
|
2019-01-10 11:01:59 +00:00
|
|
|
const auto currentRights = restrictedRights
|
|
|
|
? *restrictedRights
|
2021-07-08 10:34:06 +00:00
|
|
|
: ChatRestrictionsInfo();
|
2019-01-31 17:31:44 +00:00
|
|
|
|
|
|
|
const auto done = crl::guard(this, [=](
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestrictionsInfo newRights) {
|
2021-03-19 14:01:21 +00:00
|
|
|
editRestrictedDone(participant, newRights);
|
2019-01-31 17:31:44 +00:00
|
|
|
});
|
|
|
|
const auto fail = crl::guard(this, [=] {
|
|
|
|
_editBox = nullptr;
|
|
|
|
});
|
2021-03-19 14:01:21 +00:00
|
|
|
const auto callback = SaveRestrictedCallback(
|
|
|
|
_peer,
|
|
|
|
participant,
|
|
|
|
done,
|
|
|
|
fail);
|
2021-03-19 15:10:44 +00:00
|
|
|
callback(currentRights, ChannelData::KickedRestrictedRights(participant));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 14:01:21 +00:00
|
|
|
bool AddSpecialBoxController::appendRow(not_null<PeerData*> participant) {
|
2021-04-01 21:04:10 +00:00
|
|
|
if (delegate()->peerListFindRow(participant->id.value)
|
2021-03-19 14:01:21 +00:00
|
|
|
|| (_excludeSelf && participant->isSelf())) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-19 14:01:21 +00:00
|
|
|
delegate()->peerListAppendRow(createRow(participant));
|
2019-01-10 06:26:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddSpecialBoxController::prependRow(not_null<UserData*> user) {
|
2021-04-01 21:04:10 +00:00
|
|
|
if (delegate()->peerListFindRow(user->id.value)) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delegate()->peerListPrependRow(createRow(user));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PeerListRow> AddSpecialBoxController::createRow(
|
2021-03-19 14:01:21 +00:00
|
|
|
not_null<PeerData*> participant) const {
|
|
|
|
return std::make_unique<PeerListRow>(participant);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-06-17 09:36:25 +00:00
|
|
|
, _api(&_peer->session().mtp())
|
2019-01-10 06:26:08 +00:00
|
|
|
, _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;
|
2021-08-25 08:15:05 +00:00
|
|
|
const auto participantsHash = uint64(0);
|
2019-01-10 06:26:08 +00:00
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
_requestId = _api.request(MTPchannels_GetParticipants(
|
2019-01-10 06:26:08 +00:00
|
|
|
channel->inputChannel,
|
|
|
|
MTP_channelParticipantsSearch(MTP_string(_query)),
|
|
|
|
MTP_int(_offset),
|
|
|
|
MTP_int(perPage),
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_long(participantsHash)
|
2019-01-10 06:26:08 +00:00
|
|
|
)).done([=](
|
|
|
|
const MTPchannels_ChannelParticipants &result,
|
|
|
|
mtpRequestId requestId) {
|
|
|
|
searchParticipantsDone(requestId, result, perPage);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error, mtpRequestId requestId) {
|
2019-01-10 06:26:08 +00:00
|
|
|
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) {
|
2021-11-24 04:25:05 +00:00
|
|
|
const auto addToCache = [&] {
|
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
|
|
|
};
|
2021-11-24 04:25:05 +00:00
|
|
|
result.match([&](const MTPDchannels_channelParticipants &data) {
|
|
|
|
Api::ChatParticipants::Parse(channel, data);
|
|
|
|
addToCache();
|
|
|
|
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"channels.channelParticipantsNotModified received!"));
|
|
|
|
});
|
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-07-05 13:38:38 +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) {
|
2021-11-24 04:25:05 +00:00
|
|
|
if (const auto user = _additional->applyParticipant(
|
|
|
|
Api::ChatParticipant(data, channel))) {
|
2019-01-10 11:01:59 +00:00
|
|
|
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;
|
2019-11-27 08:02:56 +00:00
|
|
|
_requestId = _api.request(MTPcontacts_Search(
|
2019-01-10 06:26:08 +00:00
|
|
|
MTP_string(_query),
|
|
|
|
MTP_int(perPage)
|
|
|
|
)).done([=](const MTPcontacts_Found &result, mtpRequestId requestId) {
|
|
|
|
searchGlobalDone(requestId, result);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error, mtpRequestId requestId) {
|
2019-01-10 06:26:08 +00:00
|
|
|
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-07-05 13:38:38 +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;
|
2019-07-05 13:38:38 +00:00
|
|
|
feedList(found.vmy_results());
|
|
|
|
feedList(found.vresults());
|
2019-01-10 06:26:08 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-09-08 10:53:54 +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-04-16 14:05:56 +00:00
|
|
|
const auto getSmallestIndex = [&](not_null<Dialogs::IndexedList*> list)
|
|
|
|
-> const Dialogs::List* {
|
|
|
|
if (list->empty()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
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-04-16 14:05:56 +00:00
|
|
|
if (!found || found->empty()) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!result || result->size() > found->size()) {
|
|
|
|
result = found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2019-04-22 18:18:57 +00:00
|
|
|
const auto filterAndAppend = [&](not_null<Dialogs::IndexedList*> list) {
|
|
|
|
const auto index = getSmallestIndex(list);
|
|
|
|
if (!index) {
|
2019-01-10 06:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-08 10:53:54 +00:00
|
|
|
for (const auto &row : *index) {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (const auto history = row->history()) {
|
|
|
|
if (const auto user = history->peer->asUser()) {
|
|
|
|
if (allWordsAreFound(user->nameWords())) {
|
|
|
|
delegate()->peerListSearchAddRow(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-22 18:18:57 +00:00
|
|
|
filterAndAppend(_peer->owner().chatsList()->indexed());
|
|
|
|
const auto id = Data::Folder::kId;
|
|
|
|
if (const auto folder = _peer->owner().folderLoaded(id)) {
|
|
|
|
filterAndAppend(folder->chatsList()->indexed());
|
|
|
|
}
|
|
|
|
filterAndAppend(_peer->owner().contactsNoChatsList());
|
2019-01-10 06:26:08 +00:00
|
|
|
delegate()->peerListSearchRefreshRows();
|
|
|
|
}
|