Unblock bots without restarting from Settings.

This commit is contained in:
John Preston 2019-07-04 16:02:22 +02:00
parent 0e964b06dc
commit 9d09cee1cc
8 changed files with 46 additions and 40 deletions

View File

@ -592,7 +592,6 @@ 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";

View File

@ -2224,38 +2224,40 @@ void ApiWrap::blockUser(not_null<UserData*> user) {
}
}
void ApiWrap::unblockUser(not_null<UserData*> user) {
void ApiWrap::unblockUser(not_null<UserData*> user, Fn<void()> onDone) {
if (!user->isBlocked()) {
Notify::peerUpdatedDelayed(
user,
Notify::PeerUpdate::Flag::UserIsBlocked);
} else if (_blockRequests.find(user) == end(_blockRequests)) {
const auto requestId = request(MTPcontacts_Unblock(
user->inputUser
)).done([=](const MTPBool &result) {
_blockRequests.erase(user);
user->setIsBlocked(false);
if (_blockedUsersSlice) {
auto &list = _blockedUsersSlice->list;
for (auto i = list.begin(); i != list.end(); ++i) {
if (i->user == user) {
list.erase(i);
break;
}
}
if (_blockedUsersSlice->total > list.size()) {
--_blockedUsersSlice->total;
}
_blockedUsersChanges.fire_copy(*_blockedUsersSlice);
}
if (user->isBot() && !user->isSupport()) {
sendBotStart(user);
}
}).fail([=](const RPCError &error) {
_blockRequests.erase(user);
}).send();
_blockRequests.emplace(user, requestId);
return;
} else if (_blockRequests.find(user) != end(_blockRequests)) {
return;
}
const auto requestId = request(MTPcontacts_Unblock(
user->inputUser
)).done([=](const MTPBool &result) {
_blockRequests.erase(user);
user->setIsBlocked(false);
if (_blockedUsersSlice) {
auto &list = _blockedUsersSlice->list;
for (auto i = list.begin(); i != list.end(); ++i) {
if (i->user == user) {
list.erase(i);
break;
}
}
if (_blockedUsersSlice->total > list.size()) {
--_blockedUsersSlice->total;
}
_blockedUsersChanges.fire_copy(*_blockedUsersSlice);
}
if (onDone) {
onDone();
}
}).fail([=](const RPCError &error) {
_blockRequests.erase(user);
}).send();
_blockRequests.emplace(user, requestId);
}
void ApiWrap::exportInviteLink(not_null<PeerData*> peer) {

View File

@ -249,7 +249,7 @@ public:
void leaveChannel(not_null<ChannelData*> channel);
void blockUser(not_null<UserData*> user);
void unblockUser(not_null<UserData*> user);
void unblockUser(not_null<UserData*> user, Fn<void()> onDone = nullptr);
void exportInviteLink(not_null<PeerData*> peer);
void requestNotifySettings(const MTPInputNotifyPeer &peer);

View File

@ -2831,11 +2831,11 @@ void HistoryWidget::send(Qt::KeyboardModifiers modifiers) {
}
void HistoryWidget::unblockUser() {
if (!_peer || !_peer->isUser()) {
if (const auto user = _peer ? _peer->asUser() : nullptr) {
Window::PeerMenuUnblockUserWithBotRestart(user);
} else {
updateControlsVisibility();
return;
}
session().api().unblockUser(_peer->asUser());
}
void HistoryWidget::sendBotStartCommand() {

View File

@ -615,14 +615,12 @@ void ActionsFiller::addBlockAction(not_null<UserData*> user) {
});
auto callback = [=] {
if (user->isBlocked()) {
user->session().api().unblockUser(user);
Window::PeerMenuUnblockUserWithBotRestart(user);
if (user->botInfo) {
Ui::showPeerHistory(user, ShowAtUnreadMsgId);
}
} else if (user->isBot()) {
user->session().api().blockUser(user);
} else {
window->show(Box(Window::PeerMenuBlockUserBox, window, user));
user->session().api().blockUser(user);
}
};
AddActionButton(

View File

@ -286,9 +286,7 @@ bool BlockedBoxController::prependRow(not_null<UserData*> user) {
std::unique_ptr<PeerListRow> BlockedBoxController::createRow(
not_null<UserData*> user) const {
auto row = std::make_unique<PeerListRowWithLink>(user);
row->setActionLink((user->isBot() && !user->isSupport())
? tr::lng_blocked_list_restart(tr::now)
: tr::lng_blocked_list_unblock(tr::now));
row->setActionLink(tr::lng_blocked_list_unblock(tr::now));
const auto status = [&] {
if (!user->phone().isEmpty()) {
return App::formatPhone(user->phone());

View File

@ -344,7 +344,7 @@ void Filler::addBlockUser(not_null<UserData*> user) {
};
const auto blockAction = _addAction(blockText(user), [=] {
if (user->isBlocked()) {
user->session().api().unblockUser(user);
PeerMenuUnblockUserWithBotRestart(user);
} else if (user->isBot()) {
user->session().api().blockUser(user);
} else {
@ -778,6 +778,14 @@ void PeerMenuBlockUserBox(
});
}
void PeerMenuUnblockUserWithBotRestart(not_null<UserData*> user) {
user->session().api().unblockUser(user, [=] {
if (user->isBot() && !user->isSupport()) {
user->session().api().sendBotStart(user);
}
});
}
QPointer<Ui::RpWidget> ShowForwardMessagesBox(
MessageIdsList &&items,
FnMut<void()> &&successCallback) {

View File

@ -57,6 +57,7 @@ void PeerMenuBlockUserBox(
not_null<GenericBox*> box,
not_null<Window::Controller*> window,
not_null<UserData*> user);
void PeerMenuUnblockUserWithBotRestart(not_null<UserData*> user);
void ToggleHistoryArchived(not_null<History*> history, bool archived);
Fn<void()> ClearHistoryHandler(not_null<PeerData*> peer);