2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Improve peer context menu for info.

This commit is contained in:
John Preston
2017-11-06 22:03:20 +04:00
parent 3fe12f1249
commit 8191ebfc49
9 changed files with 424 additions and 152 deletions

View File

@@ -2309,148 +2309,6 @@ void MainWidget::scheduleViewIncrement(HistoryItem *item) {
j.value().insert(item->id, true);
}
void MainWidget::fillPeerMenu(PeerData *peer, base::lambda<QAction*(const QString &text, base::lambda<void()> handler)> callback, bool pinToggle) {
if (pinToggle) {
auto isPinned = false;
if (auto history = App::historyLoaded(peer)) {
isPinned = history->isPinnedDialog();
}
auto pinSubscription = MakeShared<base::Subscription>();
auto pinAction = callback(lang(isPinned ? lng_context_unpin_from_top : lng_context_pin_to_top), [peer, pinSubscription] {
auto history = App::history(peer);
auto isPinned = !history->isPinnedDialog();
if (isPinned && App::histories().pinnedCount() >= Global::PinnedDialogsCountMax()) {
// Some old chat, that was converted to supergroup, maybe is still pinned.
auto findWastedPin = []() -> History* {
auto order = App::histories().getPinnedOrder();
for_const (auto pinned, order) {
if (pinned->peer->isChat()
&& pinned->peer->asChat()->isDeactivated()
&& !pinned->inChatList(Dialogs::Mode::All)) {
return pinned;
}
}
return nullptr;
};
if (auto wasted = findWastedPin()) {
wasted->setPinnedDialog(false);
history->setPinnedDialog(isPinned);
App::histories().savePinnedToServer();
} else {
Ui::show(Box<InformBox>(lng_error_pinned_max(lt_count, Global::PinnedDialogsCountMax())));
}
return;
}
history->setPinnedDialog(isPinned);
auto flags = MTPmessages_ToggleDialogPin::Flags(0);
if (isPinned) {
flags |= MTPmessages_ToggleDialogPin::Flag::f_pinned;
}
MTP::send(MTPmessages_ToggleDialogPin(MTP_flags(flags), peer->input));
if (isPinned) {
if (auto main = App::main()) {
main->dialogsToUp();
}
}
});
auto pinChangedHandler = Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::PinnedChanged, [pinAction, peer](const Notify::PeerUpdate &update) {
if (update.peer != peer) return;
pinAction->setText(lang(App::history(peer)->isPinnedDialog() ? lng_context_unpin_from_top : lng_context_pin_to_top));
});
*pinSubscription = Notify::PeerUpdated().add_subscription(std::move(pinChangedHandler));
}
callback(lang((peer->isChat() || peer->isMegagroup()) ? lng_context_view_group : (peer->isUser() ? lng_context_view_profile : lng_context_view_channel)), [peer] {
Ui::showPeerProfile(peer);
});
auto muteSubscription = MakeShared<base::Subscription>();
auto muteAction = callback(!peer->isMuted() ? lang(lng_disable_notifications_from_tray) : lang(lng_enable_notifications_from_tray), [peer, muteSubscription] {
// We have to capture the muteSubscription pointer in this lambda for
// real time updates of the action when an user changes mute status of
// the peer on his (her) another device. Otherwise, the subscription
// will be destroyed ahead of time.
if (!peer->isMuted()) {
Ui::show(Box<MuteSettingsBox>(peer));
} else {
App::main()->updateNotifySetting(peer, NotifySettingSetNotify);
}
});
auto muteChangedHandler = Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::NotificationsEnabled, [muteAction, peer](const Notify::PeerUpdate &update) {
if (update.peer != peer) return;
muteAction->setText(lang(peer->isMuted() ? lng_enable_notifications_from_tray : lng_disable_notifications_from_tray));
});
*muteSubscription = Notify::PeerUpdated().add_subscription(std::move(muteChangedHandler));
callback(lang(lng_profile_search_messages), [peer] {
App::main()->searchInPeer(peer);
});
auto clearHistoryHandler = [peer] {
auto text = peer->isUser() ? lng_sure_delete_history(lt_contact, peer->name) : lng_sure_delete_group_history(lt_group, peer->name);
Ui::show(Box<ConfirmBox>(text, lang(lng_box_delete), st::attentionBoxButton, [peer] {
if (!App::main()) return;
Ui::hideLayer();
App::main()->clearHistory(peer);
}));
};
auto deleteAndLeaveHandler = [peer] {
auto warningText = peer->isUser() ? lng_sure_delete_history(lt_contact, peer->name) :
peer->isChat() ? lng_sure_delete_and_exit(lt_group, peer->name) :
lang(peer->isMegagroup() ? lng_sure_leave_group : lng_sure_leave_channel);
auto confirmText = lang(peer->isUser() ? lng_box_delete : lng_box_leave);
auto &confirmStyle = peer->isChannel() ? st::defaultBoxButton : st::attentionBoxButton;
Ui::show(Box<ConfirmBox>(warningText, confirmText, confirmStyle, [peer] {
if (!App::main()) return;
Ui::hideLayer();
Ui::showChatsList();
if (peer->isUser()) {
App::main()->deleteConversation(peer);
} else if (peer->isChat()) {
MTP::send(MTPmessages_DeleteChatUser(peer->asChat()->inputChat, App::self()->inputUser), App::main()->rpcDone(&MainWidget::deleteHistoryAfterLeave, peer), App::main()->rpcFail(&MainWidget::leaveChatFailed, peer));
} else if (peer->isChannel()) {
if (peer->migrateFrom()) {
App::main()->deleteConversation(peer->migrateFrom());
}
MTP::send(MTPchannels_LeaveChannel(peer->asChannel()->inputChannel), App::main()->rpcDone(&MainWidget::sentUpdatesReceived));
}
}));
};
if (auto user = peer->asUser()) {
callback(lang(lng_profile_delete_conversation), std::move(deleteAndLeaveHandler));
callback(lang(lng_profile_clear_history), std::move(clearHistoryHandler));
if (!user->isInaccessible() && user != App::self()) {
auto blockSubscription = MakeShared<base::Subscription>();
auto blockAction = callback(lang(user->isBlocked() ? (user->botInfo ? lng_profile_unblock_bot : lng_profile_unblock_user) : (user->botInfo ? lng_profile_block_bot : lng_profile_block_user)), [user, blockSubscription] {
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));
} else {
MTP::send(MTPcontacts_Unblock(user->inputUser), std::move(handler));
}
});
auto blockChangedHandler = Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [blockAction, peer](const Notify::PeerUpdate &update) {
if (update.peer != peer) return;
blockAction->setText(lang(peer->asUser()->isBlocked() ? (peer->asUser()->botInfo ? lng_profile_unblock_bot : lng_profile_unblock_user) : (peer->asUser()->botInfo ? lng_profile_block_bot : lng_profile_block_user)));
});
*blockSubscription = Notify::PeerUpdated().add_subscription(std::move(blockChangedHandler));
if (user->blockStatus() == UserData::BlockStatus::Unknown) {
Auth().api().requestFullPeer(user);
}
}
} else if (peer->isChat()) {
callback(lang(lng_profile_clear_and_exit), std::move(deleteAndLeaveHandler));
callback(lang(lng_profile_clear_history), std::move(clearHistoryHandler));
} else if (peer->isChannel() && peer->asChannel()->amIn() && !peer->asChannel()->amCreator()) {
callback(lang(peer->isMegagroup() ? lng_profile_leave_group : lng_profile_leave_channel), std::move(deleteAndLeaveHandler));
}
}
void MainWidget::onViewsIncrement() {
for (ViewsIncrement::iterator i = _viewsToIncrement.begin(); i != _viewsToIncrement.cend();) {
if (_viewsIncrementRequests.contains(i.key())) {