Improve phrases in adding a bot as an admin.

This commit is contained in:
John Preston
2022-03-23 18:22:09 +04:00
parent a35888a07b
commit 649f2908e8
13 changed files with 596 additions and 312 deletions
@@ -25,8 +25,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mainwindow.h"
#include "lang/lang_keys.h"
#include "history/history.h"
#include "boxes/peers/edit_participant_box.h"
#include "boxes/peers/edit_participants_box.h"
#include "dialogs/dialogs_main_list.h"
#include "window/window_session_controller.h" // showAddContact()
#include "base/unixtime.h"
@@ -38,52 +36,6 @@ namespace {
constexpr auto kSortByOnlineThrottle = 3 * crl::time(1000);
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 = base::RandomValue<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
MTPInputPeer() // send_as
)).done([=](const MTPUpdates &result) {
api->applyUpdates(result, randomId);
finish();
}).fail([=](const MTP::Error &error) {
api->sendMessageFail(error, chat);
finish();
}).afterRequest(
history->sendRequestId
).send();
return history->sendRequestId;
});
Ui::hideLayer();
Ui::showPeerHistory(chat, ShowAtUnreadMsgId);
}
void AddBotToGroup(not_null<UserData*> bot, not_null<PeerData*> chat) {
if (bot->isBot() && !bot->botInfo->startGroupToken.isEmpty()) {
chat->session().api().sendBotStart(bot, chat);
} else {
chat->session().api().chatParticipants().add(chat, { 1, bot });
}
Ui::hideLayer();
Ui::showPeerHistory(chat, ShowAtUnreadMsgId);
}
} // namespace
// Not used for now.
@@ -526,168 +478,6 @@ std::unique_ptr<PeerListRow> ContactsBoxController::createRow(
return std::make_unique<PeerListRow>(user);
}
void AddBotToGroupBoxController::Start(not_null<UserData*> bot) {
auto initBox = [=](not_null<PeerListBox*> box) {
box->addButton(tr::lng_cancel(), [box] { box->closeBox(); });
};
Ui::show(Box<PeerListBox>(
std::make_unique<AddBotToGroupBoxController>(bot),
std::move(initBox)));
}
AddBotToGroupBoxController::AddBotToGroupBoxController(
not_null<UserData*> bot)
: ChatsListBoxController(SharingBotGame(bot)
? std::make_unique<PeerListGlobalSearchController>(&bot->session())
: nullptr)
, _bot(bot) {
}
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) {
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(
Ui::MakeConfirmBox({
.text = confirmText,
.confirmed = std::move(send),
}),
Ui::LayerOption::KeepOther);
}
void AddBotToGroupBoxController::addBotToGroup(not_null<PeerData*> chat) {
if (const auto megagroup = chat->asMegagroup()) {
if (!megagroup->canAddMembers()) {
Ui::show(
Ui::MakeInformBox(tr::lng_error_cant_add_member()),
Ui::LayerOption::KeepOther);
return;
}
}
const auto bot = _bot;
const auto close = [=](auto&&...) {
Ui::hideLayer();
Ui::showPeerHistory(chat, ShowAtUnreadMsgId);
};
const auto saveCallback = SaveAdminCallback(
chat,
bot,
close,
close);
auto box = object_ptr<EditAdminBox>(nullptr);
if (chat->isBroadcast()) {
if (bot->botInfo->channelAdminRights) {
box = Box<EditAdminBox>(
chat,
bot,
ChatAdminRightsInfo(bot->botInfo->channelAdminRights),
QString());
}
} else if (bot->botInfo->groupAdminRights) {
box = Box<EditAdminBox>(
chat,
bot,
ChatAdminRightsInfo(bot->botInfo->groupAdminRights),
QString());
}
if (box) {
box->setSaveCallback(saveCallback);
Ui::show(std::move(box));
return;
}
Ui::show(
Ui::MakeConfirmBox({
tr::lng_bot_sure_invite(tr::now, lt_group, chat->name),
crl::guard(this, [=] { AddBotToGroup(bot, chat); }),
}),
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::SendGames)) {
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) {
const auto &info = bot->botInfo;
return (info && !info->shareGameShortName.isEmpty());
}
bool AddBotToGroupBoxController::sharingBotGame() const {
return SharingBotGame(_bot);
}
QString AddBotToGroupBoxController::emptyBoxText() const {
return !session().data().chatsListLoaded()
? 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 {
return !session().data().chatsListLoaded()
? 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();
session().data().chatsListLoadedEvents(
) | rpl::filter([=](Data::Folder *folder) {
return !folder;
}) | rpl::start_with_next([=] {
updateLabels();
}, lifetime());
}
ChooseRecipientBoxController::ChooseRecipientBoxController(
not_null<Main::Session*> session,
FnMut<void(not_null<PeerData*>)> callback)