tdesktop/Telegram/SourceFiles/boxes/peer_list_controllers.cpp

592 lines
16 KiB
C++
Raw Normal View History

/*
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/peer_list_controllers.h"
#include "boxes/confirm_box.h"
#include "ui/widgets/checkbox.h"
2019-09-13 12:22:54 +00:00
#include "ui/ui_utility.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
#include "data/data_session.h"
#include "data/data_channel.h"
#include "data/data_chat.h"
#include "data/data_user.h"
2019-04-22 18:18:57 +00:00
#include "data/data_folder.h"
#include "data/data_histories.h"
#include "apiwrap.h"
#include "mainwidget.h"
#include "lang/lang_keys.h"
#include "history/history.h"
2019-04-22 14:22:39 +00:00
#include "dialogs/dialogs_main_list.h"
2019-07-25 18:55:11 +00:00
#include "window/window_session_controller.h"
#include "facades.h"
#include "styles/style_boxes.h"
#include "styles/style_profile.h"
namespace {
2018-11-26 11:00:31 +00:00
void ShareBotGame(not_null<UserData*> bot, not_null<PeerData*> chat) {
const auto history = chat->owner().history(chat);
auto &histories = history->owner().histories();
const auto requestType = Data::Histories::RequestType::Send;
histories.sendRequest(history, requestType, [=](Fn<void()> finish) {
const auto randomId = rand_value<uint64>();
const auto api = &chat->session().api();
history->sendRequestId = api->request(MTPmessages_SendMedia(
MTP_flags(0),
chat->input,
MTP_int(0),
MTP_inputMediaGame(
MTP_inputGameShortName(
bot->inputUser,
MTP_string(bot->botInfo->shareGameShortName))),
MTP_string(),
MTP_long(randomId),
MTPReplyMarkup(),
MTPVector<MTPMessageEntity>(),
MTP_int(0) // schedule_date
)).done([=](const MTPUpdates &result) {
api->applyUpdates(result, randomId);
finish();
}).fail([=](const RPCError &error) {
api->sendMessageFail(error, chat);
finish();
}).afterRequest(
history->sendRequestId
).send();
return history->sendRequestId;
});
2018-11-26 11:00:31 +00:00
Ui::hideLayer();
Ui::showPeerHistory(chat, ShowAtUnreadMsgId);
}
void AddBotToGroup(not_null<UserData*> bot, not_null<PeerData*> chat) {
2019-07-18 08:51:11 +00:00
if (bot->isBot() && !bot->botInfo->startGroupToken.isEmpty()) {
chat->session().api().sendBotStart(bot, chat);
2018-11-26 11:00:31 +00:00
} else {
2019-07-18 08:51:11 +00:00
chat->session().api().addChatParticipants(chat, { 1, bot });
2018-11-26 11:00:31 +00:00
}
Ui::hideLayer();
Ui::showPeerHistory(chat, ShowAtUnreadMsgId);
}
} // namespace
// Not used for now.
//
//MembersAddButton::MembersAddButton(QWidget *parent, const style::TwoIconButton &st) : RippleButton(parent, st.ripple)
//, _st(st) {
// resize(_st.width, _st.height);
// setCursor(style::cur_pointer);
//}
//
//void MembersAddButton::paintEvent(QPaintEvent *e) {
// Painter p(this);
//
// auto ms = crl::now();
// auto over = isOver();
// auto down = isDown();
//
// ((over || down) ? _st.iconBelowOver : _st.iconBelow).paint(p, _st.iconPosition, width());
// paintRipple(p, _st.rippleAreaPosition.x(), _st.rippleAreaPosition.y(), ms);
// ((over || down) ? _st.iconAboveOver : _st.iconAbove).paint(p, _st.iconPosition, width());
//}
//
//QImage MembersAddButton::prepareRippleMask() const {
// return Ui::RippleAnimation::ellipseMask(QSize(_st.rippleAreaSize, _st.rippleAreaSize));
//}
//
//QPoint MembersAddButton::prepareRippleStartPosition() const {
// return mapFromGlobal(QCursor::pos()) - _st.rippleAreaPosition;
//}
void PeerListRowWithLink::setActionLink(const QString &action) {
_action = action;
refreshActionLink();
}
void PeerListRowWithLink::refreshActionLink() {
if (!isInitialized()) return;
_actionWidth = _action.isEmpty() ? 0 : st::normalFont->width(_action);
}
void PeerListRowWithLink::lazyInitialize(const style::PeerListItem &st) {
PeerListRow::lazyInitialize(st);
refreshActionLink();
}
QSize PeerListRowWithLink::actionSize() const {
return QSize(_actionWidth, st::normalFont->height);
}
QMargins PeerListRowWithLink::actionMargins() const {
return QMargins(
st::contactsCheckPosition.x(),
(st::contactsPadding.top() + st::contactsPhotoSize + st::contactsPadding.bottom() - st::normalFont->height) / 2,
st::defaultPeerListItem.photoPosition.x() + st::contactsCheckPosition.x(),
0);
}
void PeerListRowWithLink::paintAction(
Painter &p,
int x,
int y,
int outerWidth,
bool selected,
bool actionSelected) {
p.setFont(actionSelected ? st::linkOverFont : st::linkFont);
p.setPen(actionSelected ? st::defaultLinkButton.overColor : st::defaultLinkButton.color);
p.drawTextLeft(x, y, outerWidth, _action, _actionWidth);
}
2019-07-25 18:55:11 +00:00
PeerListGlobalSearchController::PeerListGlobalSearchController(
not_null<Window::SessionNavigation*> navigation)
2019-11-27 08:02:56 +00:00
: _navigation(navigation)
, _api(_navigation->session().mtp()) {
_timer.setCallback([this] { searchOnServer(); });
}
void PeerListGlobalSearchController::searchQuery(const QString &query) {
if (_query != query) {
_query = query;
_requestId = 0;
if (!_query.isEmpty() && !searchInCache()) {
_timer.callOnce(AutoSearchTimeout);
} else {
_timer.cancel();
}
}
}
bool PeerListGlobalSearchController::searchInCache() {
auto it = _cache.find(_query);
if (it != _cache.cend()) {
_requestId = 0;
searchDone(it->second, _requestId);
return true;
}
return false;
}
void PeerListGlobalSearchController::searchOnServer() {
2019-11-27 08:02:56 +00:00
_requestId = _api.request(MTPcontacts_Search(
MTP_string(_query),
MTP_int(SearchPeopleLimit)
)).done([=](const MTPcontacts_Found &result, mtpRequestId requestId) {
searchDone(result, requestId);
}).fail([=](const RPCError &error, mtpRequestId requestId) {
if (_requestId == requestId) {
_requestId = 0;
delegate()->peerListSearchRefreshRows();
}
}).send();
_queries.emplace(_requestId, _query);
}
void PeerListGlobalSearchController::searchDone(
const MTPcontacts_Found &result,
mtpRequestId requestId) {
Expects(result.type() == mtpc_contacts_found);
auto &contacts = result.c_contacts_found();
auto query = _query;
if (requestId) {
2019-07-25 18:55:11 +00:00
_navigation->session().data().processUsers(contacts.vusers());
_navigation->session().data().processChats(contacts.vchats());
auto it = _queries.find(requestId);
if (it != _queries.cend()) {
query = it->second;
_cache[query] = result;
_queries.erase(it);
}
}
const auto feedList = [&](const MTPVector<MTPPeer> &list) {
for (const auto &mtpPeer : list.v) {
2019-07-25 18:55:11 +00:00
const auto peer = _navigation->session().data().peerLoaded(
peerFromMTP(mtpPeer));
if (peer) {
delegate()->peerListSearchAddRow(peer);
}
}
};
if (_requestId == requestId) {
_requestId = 0;
2019-07-05 13:38:38 +00:00
feedList(contacts.vmy_results());
feedList(contacts.vresults());
delegate()->peerListSearchRefreshRows();
}
}
bool PeerListGlobalSearchController::isLoading() {
return _timer.isActive() || _requestId;
}
ChatsListBoxController::Row::Row(not_null<History*> history)
: PeerListRow(history->peer)
, _history(history) {
}
2019-07-25 18:55:11 +00:00
ChatsListBoxController::ChatsListBoxController(
not_null<Window::SessionNavigation*> navigation)
: ChatsListBoxController(
std::make_unique<PeerListGlobalSearchController>(navigation)) {
}
ChatsListBoxController::ChatsListBoxController(
std::unique_ptr<PeerListSearchController> searchController)
: PeerListController(std::move(searchController)) {
}
void ChatsListBoxController::prepare() {
2019-06-19 15:09:03 +00:00
setSearchNoResultsText(tr::lng_blocked_list_not_found(tr::now));
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
prepareViewHook();
2019-07-25 18:55:11 +00:00
if (!session().data().chatsListLoaded()) {
session().data().chatsListLoadedEvents(
) | rpl::filter([=](Data::Folder *folder) {
return !folder;
}) | rpl::start_with_next([=] {
checkForEmptyRows();
}, lifetime());
}
2019-07-25 18:55:11 +00:00
session().data().chatsListChanges(
2019-04-22 18:18:57 +00:00
) | rpl::start_with_next([=] {
rebuildRows();
}, lifetime());
2019-07-25 18:55:11 +00:00
session().data().contactsLoaded().value(
) | rpl::start_with_next([=] {
rebuildRows();
}, lifetime());
}
void ChatsListBoxController::rebuildRows() {
auto wasEmpty = !delegate()->peerListFullRowsCount();
auto appendList = [this](auto chats) {
auto count = 0;
for (const auto row : chats->all()) {
if (const auto history = row->history()) {
if (appendRow(history)) {
++count;
}
}
}
return count;
};
auto added = 0;
if (respectSavedMessagesChat()) {
2019-07-25 18:55:11 +00:00
if (appendRow(session().data().history(session().user()))) {
++added;
}
}
2019-07-25 18:55:11 +00:00
added += appendList(session().data().chatsList()->indexed());
2019-04-22 18:18:57 +00:00
const auto id = Data::Folder::kId;
2019-07-25 18:55:11 +00:00
if (const auto folder = session().data().folderLoaded(id)) {
2019-04-22 18:18:57 +00:00
added += appendList(folder->chatsList()->indexed());
}
2019-07-25 18:55:11 +00:00
added += appendList(session().data().contactsNoChatsList());
if (!wasEmpty && added > 0) {
// Place dialogs list before contactsNoDialogs list.
delegate()->peerListPartitionRows([](const PeerListRow &a) {
const auto history = static_cast<const Row&>(a).history();
2019-04-22 14:22:39 +00:00
return history->inChatList();
});
if (respectSavedMessagesChat()) {
delegate()->peerListPartitionRows([](const PeerListRow &a) {
return a.peer()->isSelf();
});
}
}
checkForEmptyRows();
delegate()->peerListRefreshRows();
}
void ChatsListBoxController::checkForEmptyRows() {
if (delegate()->peerListFullRowsCount()) {
setDescriptionText(QString());
} else {
2019-07-25 18:55:11 +00:00
const auto loaded = session().data().contactsLoaded().current()
&& session().data().chatsListLoaded();
2019-06-19 15:09:03 +00:00
setDescriptionText(loaded ? emptyBoxText() : tr::lng_contacts_loading(tr::now));
}
}
QString ChatsListBoxController::emptyBoxText() const {
2019-06-19 15:09:03 +00:00
return tr::lng_contacts_not_found(tr::now);
}
std::unique_ptr<PeerListRow> ChatsListBoxController::createSearchRow(not_null<PeerData*> peer) {
2019-01-18 12:27:37 +00:00
return createRow(peer->owner().history(peer));
}
bool ChatsListBoxController::appendRow(not_null<History*> history) {
if (auto row = delegate()->peerListFindRow(history->peer->id)) {
updateRowHook(static_cast<Row*>(row));
return false;
}
if (auto row = createRow(history)) {
delegate()->peerListAppendRow(std::move(row));
return true;
}
return false;
}
2019-01-13 13:28:05 +00:00
ContactsBoxController::ContactsBoxController(
2019-07-25 18:55:11 +00:00
not_null<Window::SessionNavigation*> navigation)
: PeerListController(
std::make_unique<PeerListGlobalSearchController>(navigation))
, _navigation(navigation) {
}
ContactsBoxController::ContactsBoxController(
not_null<Window::SessionNavigation*> navigation,
2019-01-13 13:28:05 +00:00
std::unique_ptr<PeerListSearchController> searchController)
2019-07-25 18:55:11 +00:00
: PeerListController(std::move(searchController))
, _navigation(navigation) {
}
Main::Session &ContactsBoxController::session() const {
return _navigation->session();
}
void ContactsBoxController::prepare() {
2019-06-19 15:09:03 +00:00
setSearchNoResultsText(tr::lng_blocked_list_not_found(tr::now));
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
delegate()->peerListSetTitle(tr::lng_contacts_header());
prepareViewHook();
2019-07-25 18:55:11 +00:00
session().data().contactsLoaded().value(
) | rpl::start_with_next([=] {
rebuildRows();
}, lifetime());
}
void ContactsBoxController::rebuildRows() {
const auto appendList = [&](auto chats) {
auto count = 0;
for (const auto row : chats->all()) {
if (const auto history = row->history()) {
if (const auto user = history->peer->asUser()) {
if (appendRow(user)) {
++count;
}
}
}
}
return count;
};
2019-07-25 18:55:11 +00:00
appendList(session().data().contactsList());
checkForEmptyRows();
delegate()->peerListRefreshRows();
}
void ContactsBoxController::checkForEmptyRows() {
2019-06-19 15:09:03 +00:00
setDescriptionText(delegate()->peerListFullRowsCount()
? QString()
2019-07-25 18:55:11 +00:00
: session().data().contactsLoaded().current()
2019-06-19 15:09:03 +00:00
? tr::lng_contacts_not_found(tr::now)
: tr::lng_contacts_loading(tr::now));
}
std::unique_ptr<PeerListRow> ContactsBoxController::createSearchRow(
not_null<PeerData*> peer) {
if (const auto user = peer->asUser()) {
return createRow(user);
}
return nullptr;
}
void ContactsBoxController::rowClicked(not_null<PeerListRow*> row) {
Ui::showPeerHistory(row->peer(), ShowAtUnreadMsgId);
}
bool ContactsBoxController::appendRow(not_null<UserData*> user) {
if (auto row = delegate()->peerListFindRow(user->id)) {
updateRowHook(row);
return false;
}
if (auto row = createRow(user)) {
delegate()->peerListAppendRow(std::move(row));
return true;
}
return false;
}
std::unique_ptr<PeerListRow> ContactsBoxController::createRow(not_null<UserData*> user) {
return std::make_unique<PeerListRow>(user);
}
2019-07-25 18:55:11 +00:00
void AddBotToGroupBoxController::Start(
not_null<Window::SessionNavigation*> navigation,
not_null<UserData*> bot) {
auto initBox = [=](not_null<PeerListBox*> box) {
box->addButton(tr::lng_cancel(), [box] { box->closeBox(); });
};
2019-07-25 18:55:11 +00:00
Ui::show(Box<PeerListBox>(
std::make_unique<AddBotToGroupBoxController>(navigation, bot),
std::move(initBox)));
}
2019-07-25 18:55:11 +00:00
AddBotToGroupBoxController::AddBotToGroupBoxController(
not_null<Window::SessionNavigation*> navigation,
not_null<UserData*> bot)
: ChatsListBoxController(SharingBotGame(bot)
2019-07-25 18:55:11 +00:00
? std::make_unique<PeerListGlobalSearchController>(navigation)
: nullptr)
, _bot(bot) {
}
2019-07-25 18:55:11 +00:00
Main::Session &AddBotToGroupBoxController::session() const {
return _bot->session();
}
void AddBotToGroupBoxController::rowClicked(not_null<PeerListRow*> row) {
if (sharingBotGame()) {
shareBotGame(row->peer());
} else {
addBotToGroup(row->peer());
}
}
void AddBotToGroupBoxController::shareBotGame(not_null<PeerData*> chat) {
2018-11-26 11:00:31 +00:00
auto send = crl::guard(this, [bot = _bot, chat] {
ShareBotGame(bot, chat);
});
auto confirmText = [chat] {
if (chat->isUser()) {
return tr::lng_bot_sure_share_game(tr::now, lt_user, chat->name);
}
return tr::lng_bot_sure_share_game_group(tr::now, lt_group, chat->name);
}();
Ui::show(
Box<ConfirmBox>(confirmText, std::move(send)),
2019-09-18 11:19:05 +00:00
Ui::LayerOption::KeepOther);
}
void AddBotToGroupBoxController::addBotToGroup(not_null<PeerData*> chat) {
if (const auto megagroup = chat->asMegagroup()) {
if (!megagroup->canAddMembers()) {
Ui::show(
2019-06-19 15:09:03 +00:00
Box<InformBox>(tr::lng_error_cant_add_member(tr::now)),
2019-09-18 11:19:05 +00:00
Ui::LayerOption::KeepOther);
return;
}
}
2018-11-26 11:00:31 +00:00
auto send = crl::guard(this, [bot = _bot, chat] {
AddBotToGroup(bot, chat);
});
auto confirmText = tr::lng_bot_sure_invite(tr::now, lt_group, chat->name);
Ui::show(
Box<ConfirmBox>(confirmText, send),
2019-09-18 11:19:05 +00:00
Ui::LayerOption::KeepOther);
}
auto AddBotToGroupBoxController::createRow(not_null<History*> history)
-> std::unique_ptr<ChatsListBoxController::Row> {
if (!needToCreateRow(history->peer)) {
return nullptr;
}
return std::make_unique<Row>(history);
}
bool AddBotToGroupBoxController::needToCreateRow(
not_null<PeerData*> peer) const {
if (sharingBotGame()) {
if (!peer->canWrite()
|| peer->amRestricted(ChatRestriction::f_send_games)) {
return false;
}
return true;
}
if (const auto chat = peer->asChat()) {
return chat->canAddMembers();
} else if (const auto group = peer->asMegagroup()) {
return group->canAddMembers();
}
return false;
}
bool AddBotToGroupBoxController::SharingBotGame(not_null<UserData*> bot) {
2019-07-18 08:51:11 +00:00
const auto &info = bot->botInfo;
return (info && !info->shareGameShortName.isEmpty());
}
bool AddBotToGroupBoxController::sharingBotGame() const {
return SharingBotGame(_bot);
}
QString AddBotToGroupBoxController::emptyBoxText() const {
2019-07-25 18:55:11 +00:00
return !session().data().chatsListLoaded()
2019-06-19 15:09:03 +00:00
? tr::lng_contacts_loading(tr::now)
: sharingBotGame()
? tr::lng_bot_no_chats(tr::now)
: tr::lng_bot_no_groups(tr::now);
}
QString AddBotToGroupBoxController::noResultsText() const {
2019-07-25 18:55:11 +00:00
return !session().data().chatsListLoaded()
2019-06-19 15:09:03 +00:00
? tr::lng_contacts_loading(tr::now)
: sharingBotGame()
? tr::lng_bot_chats_not_found(tr::now)
: tr::lng_bot_groups_not_found(tr::now);
}
void AddBotToGroupBoxController::updateLabels() {
setSearchNoResultsText(noResultsText());
}
void AddBotToGroupBoxController::prepareViewHook() {
delegate()->peerListSetTitle(sharingBotGame()
? tr::lng_bot_choose_chat()
: tr::lng_bot_choose_group());
updateLabels();
2019-07-25 18:55:11 +00:00
session().data().chatsListLoadedEvents(
) | rpl::filter([=](Data::Folder *folder) {
return !folder;
}) | rpl::start_with_next([=] {
updateLabels();
}, lifetime());
}
ChooseRecipientBoxController::ChooseRecipientBoxController(
2019-07-25 18:55:11 +00:00
not_null<Window::SessionNavigation*> navigation,
FnMut<void(not_null<PeerData*>)> callback)
2019-07-25 18:55:11 +00:00
: ChatsListBoxController(navigation)
, _navigation(navigation)
, _callback(std::move(callback)) {
}
Main::Session &ChooseRecipientBoxController::session() const {
return _navigation->session();
}
void ChooseRecipientBoxController::prepareViewHook() {
delegate()->peerListSetTitle(tr::lng_forward_choose());
}
void ChooseRecipientBoxController::rowClicked(not_null<PeerListRow*> row) {
2018-11-30 05:45:22 +00:00
auto weak = base::make_weak(this);
auto callback = std::move(_callback);
callback(row->peer());
if (weak) {
_callback = std::move(callback);
}
}
auto ChooseRecipientBoxController::createRow(
not_null<History*> history) -> std::unique_ptr<Row> {
const auto peer = history->peer;
const auto skip = peer->isChannel()
&& !peer->isMegagroup()
&& !peer->canWrite();
return skip ? nullptr : std::make_unique<Row>(history);
}