Restart instead of Unblock for bots.

This commit is contained in:
John Preston 2018-12-04 15:46:07 +04:00
parent 6562a1f6af
commit 61add763ae
9 changed files with 84 additions and 95 deletions

View File

@ -557,6 +557,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_blocked_list_title" = "Blocked users";
"lng_blocked_list_unknown_phone" = "unknown phone number";
"lng_blocked_list_unblock" = "Unblock";
"lng_blocked_list_restart" = "Restart";
"lng_blocked_list_add" = "Block user";
"lng_blocked_list_add_title" = "Select user to block";
"lng_blocked_list_already_blocked" = "blocked already";
@ -667,7 +668,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_profile_report" = "Report";
"lng_profile_search_messages" = "Search for messages";
"lng_profile_block_bot" = "Stop and block bot";
"lng_profile_unblock_bot" = "Unblock bot";
"lng_profile_restart_bot" = "Restart bot";
"lng_profile_invite_to_group" = "Add to Group";
"lng_profile_delete_contact" = "Delete";
"lng_profile_set_group_photo" = "Set Photo";
@ -1095,6 +1096,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_from_draft" = "Draft";
"lng_bot_description" = "What can this bot do?";
"lng_unblock_button" = "Unblock";
"lng_restart_button" = "Restart";
"lng_channel_mute" = "Mute";
"lng_channel_unmute" = "Unmute";
"lng_saved_messages" = "Saved Messages";

View File

@ -1907,15 +1907,21 @@ void ApiWrap::blockUser(not_null<UserData*> user) {
void ApiWrap::unblockUser(not_null<UserData*> user) {
if (!user->isBlocked()) {
Notify::peerUpdatedDelayed(user, Notify::PeerUpdate::Flag::UserIsBlocked);
Notify::peerUpdatedDelayed(
user,
Notify::PeerUpdate::Flag::UserIsBlocked);
} else if (_blockRequests.find(user) == end(_blockRequests)) {
auto requestId = request(MTPcontacts_Unblock(user->inputUser)).done([this, user](const MTPBool &result) {
const auto requestId = request(MTPcontacts_Unblock(
user->inputUser
)).done([=](const MTPBool &result) {
_blockRequests.erase(user);
user->setBlockStatus(UserData::BlockStatus::NotBlocked);
}).fail([this, user](const RPCError &error) {
if (user->botInfo) {
sendBotStart(user);
}
}).fail([=](const RPCError &error) {
_blockRequests.erase(user);
}).send();
_blockRequests.emplace(user, requestId);
}
}
@ -4544,6 +4550,28 @@ void ApiWrap::sendMessage(MessageToSend &&message) {
}
}
void ApiWrap::sendBotStart(not_null<UserData*> bot) {
Expects(bot->botInfo != nullptr);
const auto token = bot->botInfo->startToken;
if (token.isEmpty()) {
auto message = ApiWrap::MessageToSend(App::history(bot));
message.textWithTags = { qsl("/start"), TextWithTags::Tags() };
sendMessage(std::move(message));
} else {
bot->botInfo->startToken = QString();
const auto randomId = rand_value<uint64>();
request(MTPmessages_StartBot(
bot->inputUser,
MTP_inputPeerEmpty(),
MTP_long(randomId),
MTP_string(token)
)).done([=](const MTPUpdates &result) {
applyUpdates(result);
}).send();
}
}
void ApiWrap::sendInlineResult(
not_null<UserData*> bot,
not_null<InlineBots::Result*> data,

View File

@ -329,6 +329,7 @@ public:
bool handleSupportSwitch = false;
};
void sendMessage(MessageToSend &&message);
void sendBotStart(not_null<UserData*> bot);
void sendInlineResult(
not_null<UserData*> bot,
not_null<InlineBots::Result*> data,

View File

@ -1642,14 +1642,19 @@ void HistoryWidget::showHistory(const PeerId &peerId, MsgId showAtMsgId, bool re
_tabbedSelector->setCurrentPeer(_peer);
}
if (_peer && _peer->isChannel()) {
_peer->asChannel()->updateFull();
_joinChannel->setText(lang(_peer->isMegagroup()
? lng_profile_join_group
: lng_profile_join_channel).toUpper());
if (_peer) {
_unblock->setText(lang(_peer->isUser() && _peer->asUser()->botInfo
? lng_restart_button
: lng_unblock_button).toUpper());
if (const auto channel = _peer->asChannel()) {
channel->updateFull();
_joinChannel->setText(lang(channel->isMegagroup()
? lng_profile_join_group
: lng_profile_join_channel).toUpper());
}
}
_unblockRequest = _reportSpamRequest = 0;
_reportSpamRequest = 0;
if (_reportSpamSettingRequestId > 0) {
MTP::cancel(_reportSpamSettingRequestId);
}
@ -2873,64 +2878,22 @@ void HistoryWidget::send(Qt::KeyboardModifiers modifiers) {
}
void HistoryWidget::onUnblock() {
if (_unblockRequest) return;
if (!_peer || !_peer->isUser() || !_peer->asUser()->isBlocked()) {
if (!_peer || !_peer->isUser()) {
updateControlsVisibility();
return;
}
_unblockRequest = MTP::send(MTPcontacts_Unblock(_peer->asUser()->inputUser), rpcDone(&HistoryWidget::unblockDone, _peer), rpcFail(&HistoryWidget::unblockFail));
}
void HistoryWidget::unblockDone(PeerData *peer, const MTPBool &result, mtpRequestId req) {
if (!peer->isUser()) return;
if (_unblockRequest == req) _unblockRequest = 0;
peer->asUser()->setBlockStatus(UserData::BlockStatus::NotBlocked);
}
bool HistoryWidget::unblockFail(const RPCError &error, mtpRequestId req) {
if (MTP::isDefaultHandledError(error)) return false;
if (_unblockRequest == req) _unblockRequest = 0;
return false;
}
void HistoryWidget::blockDone(PeerData *peer, const MTPBool &result) {
if (!peer->isUser()) return;
peer->asUser()->setBlockStatus(UserData::BlockStatus::Blocked);
Auth().api().unblockUser(_peer->asUser());
}
void HistoryWidget::onBotStart() {
if (!_peer || !_peer->isUser() || !_peer->asUser()->botInfo || !_canSendMessages) {
if (!_peer
|| !_peer->isUser()
|| !_peer->asUser()->botInfo
|| !_canSendMessages) {
updateControlsVisibility();
return;
}
QString token = _peer->asUser()->botInfo->startToken;
if (token.isEmpty()) {
sendBotCommand(_peer, _peer->asUser(), qsl("/start"), 0);
} else {
uint64 randomId = rand_value<uint64>();
MTP::send(
MTPmessages_StartBot(
_peer->asUser()->inputUser,
MTP_inputPeerEmpty(),
MTP_long(randomId),
MTP_string(token)),
App::main()->rpcDone(&MainWidget::sentUpdatesReceived),
App::main()->rpcFail(
&MainWidget::addParticipantFail,
{ _peer->asUser(), (PeerData*)nullptr }));
_peer->asUser()->botInfo->startToken = QString();
if (_keyboard->hasMarkup()) {
if (_keyboard->singleUse() && _keyboard->forMsgId() == FullMsgId(_channel, _history->lastKeyboardId) && _history->lastKeyboardUsed) {
_history->lastKeyboardHiddenId = _history->lastKeyboardId;
}
if (!kbWasHidden()) _kbShown = _keyboard->hasMarkup();
}
}
Auth().api().sendBotStart(_peer->asUser());
updateControlsVisibility();
updateControlsGeometry();
}
@ -4486,15 +4449,19 @@ void HistoryWidget::onReportSpamClicked() {
if (_reportSpamRequest) return;
Ui::hideLayer();
if (auto user = peer->asUser()) {
MTP::send(MTPcontacts_Block(user->inputUser), rpcDone(&HistoryWidget::blockDone, peer), RPCFailHandlerPtr(), 0, 5);
_reportSpamRequest = MTP::send(
MTPmessages_ReportSpam(peer->input),
rpcDone(&HistoryWidget::reportSpamDone, peer),
rpcFail(&HistoryWidget::reportSpamFail), 0, 5);
if (const auto user = peer->asUser()) {
Auth().api().blockUser(user);
}
_reportSpamRequest = MTP::send(MTPmessages_ReportSpam(peer->input), rpcDone(&HistoryWidget::reportSpamDone, peer), rpcFail(&HistoryWidget::reportSpamFail));
})));
}
void HistoryWidget::reportSpamDone(PeerData *peer, const MTPBool &result, mtpRequestId req) {
Expects(peer != nullptr);
if (req == _reportSpamRequest) {
_reportSpamRequest = 0;
}

View File

@ -634,10 +634,6 @@ private:
void reportSpamDone(PeerData *peer, const MTPBool &result, mtpRequestId request);
bool reportSpamFail(const RPCError &error, mtpRequestId request);
void unblockDone(PeerData *peer, const MTPBool &result, mtpRequestId req);
bool unblockFail(const RPCError &error, mtpRequestId req);
void blockDone(PeerData *peer, const MTPBool &result);
void countHistoryShowFrom();
enum class TextUpdateEvent {
@ -747,7 +743,6 @@ private:
object_ptr<Ui::FlatButton> _joinChannel;
object_ptr<Ui::FlatButton> _muteUnmute;
object_ptr<Ui::RpWidget> _aboutProxyPromotion = { nullptr };
mtpRequestId _unblockRequest = 0;
mtpRequestId _reportSpamRequest = 0;
object_ptr<Ui::IconButton> _attachToggle;
object_ptr<Ui::EmojiButton> _tabbedSelectorToggle;

View File

@ -574,7 +574,7 @@ void ActionsFiller::addBlockAction(not_null<UserData*> user) {
switch (user->blockStatus()) {
case UserData::BlockStatus::Blocked:
return Lang::Viewer(user->botInfo
? lng_profile_unblock_bot
? lng_profile_restart_bot
: lng_profile_unblock_user);
case UserData::BlockStatus::NotBlocked:
default:
@ -590,9 +590,12 @@ void ActionsFiller::addBlockAction(not_null<UserData*> user) {
) | rpl::map([](const QString &text) {
return !text.isEmpty();
});
auto callback = [user] {
auto callback = [=] {
if (user->isBlocked()) {
Auth().api().unblockUser(user);
if (user->botInfo) {
Ui::showPeerHistory(user, ShowAtUnreadMsgId);
}
} else {
Auth().api().blockUser(user);
}

View File

@ -97,7 +97,7 @@ void BlockedBoxController::prepare() {
delegate()->peerListRefreshRows();
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [this](const Notify::PeerUpdate &update) {
if (auto user = update.peer->asUser()) {
if (const auto user = update.peer->asUser()) {
handleBlockedEvent(user);
}
}));
@ -169,7 +169,7 @@ void BlockedBoxController::receivedUsers(const QVector<MTPContactBlocked> &resul
delegate()->peerListRefreshRows();
}
void BlockedBoxController::handleBlockedEvent(UserData *user) {
void BlockedBoxController::handleBlockedEvent(not_null<UserData*> user) {
if (user->isBlocked()) {
if (prependRow(user)) {
delegate()->peerListRefreshRows();
@ -195,7 +195,7 @@ void BlockedBoxController::BlockNewUser() {
LayerOption::KeepOther);
}
bool BlockedBoxController::appendRow(UserData *user) {
bool BlockedBoxController::appendRow(not_null<UserData*> user) {
if (delegate()->peerListFindRow(user->id)) {
return false;
}
@ -203,7 +203,7 @@ bool BlockedBoxController::appendRow(UserData *user) {
return true;
}
bool BlockedBoxController::prependRow(UserData *user) {
bool BlockedBoxController::prependRow(not_null<UserData*> user) {
if (delegate()->peerListFindRow(user->id)) {
return false;
}
@ -211,9 +211,12 @@ bool BlockedBoxController::prependRow(UserData *user) {
return true;
}
std::unique_ptr<PeerListRow> BlockedBoxController::createRow(UserData *user) const {
std::unique_ptr<PeerListRow> BlockedBoxController::createRow(
not_null<UserData*> user) const {
auto row = std::make_unique<PeerListRowWithLink>(user);
row->setActionLink(lang(lng_blocked_list_unblock));
row->setActionLink(lang(user->botInfo
? lng_blocked_list_restart
: lng_blocked_list_unblock));
const auto status = [&] {
if (!user->phone().isEmpty()) {
return App::formatPhone(user->phone());

View File

@ -24,11 +24,11 @@ public:
private:
void receivedUsers(const QVector<MTPContactBlocked> &result);
void handleBlockedEvent(UserData *user);
void handleBlockedEvent(not_null<UserData*> user);
bool appendRow(UserData *user);
bool prependRow(UserData *user);
std::unique_ptr<PeerListRow> createRow(UserData *user) const;
bool appendRow(not_null<UserData*> user);
bool prependRow(not_null<UserData*> user);
std::unique_ptr<PeerListRow> createRow(not_null<UserData*> user) const;
int _offset = 0;
mtpRequestId _loadRequestId = 0;

View File

@ -275,27 +275,17 @@ void Filler::addBlockUser(not_null<UserData*> user) {
auto blockText = [](not_null<UserData*> user) {
return lang(user->isBlocked()
? (user->botInfo
? lng_profile_unblock_bot
? lng_profile_restart_bot
: lng_profile_unblock_user)
: (user->botInfo
? lng_profile_block_bot
: lng_profile_block_user));
};
auto blockAction = _addAction(blockText(user), [user] {
auto willBeBlocked = !user->isBlocked();
auto handler = ::rpcDone([user, willBeBlocked](const MTPBool &result) {
user->setBlockStatus(willBeBlocked
? UserData::BlockStatus::Blocked
: UserData::BlockStatus::NotBlocked);
});
if (willBeBlocked) {
MTP::send(
MTPcontacts_Block(user->inputUser),
std::move(handler));
auto blockAction = _addAction(blockText(user), [=] {
if (user->isBlocked()) {
Auth().api().unblockUser(user);
} else {
MTP::send(
MTPcontacts_Unblock(user->inputUser),
std::move(handler));
Auth().api().blockUser(user);
}
});