From 213a8d888a803a82f5a9bcbd258aff5eb200a4cc Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 10 Jun 2019 16:58:45 +0200 Subject: [PATCH] Implement report spam from ContactStatus bar. --- Telegram/Resources/langs/lang.strings | 2 +- .../SourceFiles/history/history_widget.cpp | 1 + .../view/history_view_contact_status.cpp | 137 ++++++++---------- .../view/history_view_contact_status.h | 16 +- .../info/profile/info_profile_values.cpp | 10 +- .../SourceFiles/window/window_controller.cpp | 7 + .../SourceFiles/window/window_controller.h | 15 ++ .../SourceFiles/window/window_peer_menu.cpp | 39 +++-- 8 files changed, 121 insertions(+), 106 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 7cad39beb..4f9d82cfd 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1189,7 +1189,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_ban_user" = "Ban User"; "lng_delete_all_from" = "Delete all from this user"; "lng_report_spam" = "Report Spam"; -"lng_report_spam_sure" = "Are you sure you want to report spam from this user?"; +"lng_report_spam_and_leave" = "Report spam and leave"; "lng_report_spam_sure_group" = "Are you sure you want to report spam in this group?"; "lng_report_spam_sure_channel" = "Are you sure you want to report spam in this channel?"; "lng_report_spam_ok" = "Report"; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 80b45f97b..d8ab9c8a1 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1684,6 +1684,7 @@ void HistoryWidget::showHistory( _canSendMessages = _peer->canWrite(); _tabbedSelector->setCurrentPeer(_peer); _contactStatus = std::make_unique( + &controller()->window()->controller(), this, _peer); _contactStatus->heightValue() | rpl::start_with_next([=] { diff --git a/Telegram/SourceFiles/history/view/history_view_contact_status.cpp b/Telegram/SourceFiles/history/view/history_view_contact_status.cpp index 0477b5400..bb5fb0525 100644 --- a/Telegram/SourceFiles/history/view/history_view_contact_status.cpp +++ b/Telegram/SourceFiles/history/view/history_view_contact_status.cpp @@ -13,9 +13,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_user.h" #include "data/data_chat.h" #include "data/data_channel.h" +#include "window/window_peer_menu.h" +#include "window/window_controller.h" +#include "window/window_session_controller.h" #include "apiwrap.h" #include "auth_session.h" +#include "boxes/confirm_box.h" #include "styles/style_history.h" +#include "styles/style_boxes.h" namespace HistoryView { namespace { @@ -51,80 +56,13 @@ auto MapToEmpty() { } } // namespace -// -//void HistoryWidget::onReportSpamClicked() { -// auto text = lang(_peer->isUser() ? lng_report_spam_sure : ((_peer->isChat() || _peer->isMegagroup()) ? lng_report_spam_sure_group : lng_report_spam_sure_channel)); -// Ui::show(Box(text, lang(lng_report_spam_ok), st::attentionBoxButton, crl::guard(this, [this, peer = _peer] { -// if (_reportSpamRequest) return; -// -// Ui::hideLayer(); -// _reportSpamRequest = MTP::send( -// MTPmessages_ReportSpam(peer->input), -// rpcDone(&HistoryWidget::reportSpamDone, peer), -// rpcFail(&HistoryWidget::reportSpamFail), 0, 5); -// if (const auto user = peer->asUser()) { -// session().api().blockUser(user); -// } -// }))); -//} -// -//void HistoryWidget::reportSpamDone(PeerData *peer, const MTPBool &result, mtpRequestId req) { -// Expects(peer != nullptr); -// -// if (req == _reportSpamRequest) { -// _reportSpamRequest = 0; -// } -// cRefReportSpamStatuses().insert(peer->id, dbiprsReportSent); -// Local::writeReportSpamStatuses(); -// if (_peer == peer) { -// setReportSpamStatus(dbiprsReportSent); -// if (_reportSpamPanel) { -// _reportSpamPanel->setReported(_reportSpamStatus == dbiprsReportSent, peer); -// } -// } -//} -// -//bool HistoryWidget::reportSpamFail(const RPCError &error, mtpRequestId req) { -// if (MTP::isDefaultHandledError(error)) return false; -// -// if (req == _reportSpamRequest) { -// _reportSpamRequest = 0; -// } -// return false; -//} -// -//void HistoryWidget::onReportSpamHide() { -// if (_peer) { -// cRefReportSpamStatuses().insert(_peer->id, dbiprsHidden); -// Local::writeReportSpamStatuses(); -// -// MTP::send(MTPmessages_HidePeerSettingsBar(_peer->input)); -// } -// setReportSpamStatus(dbiprsHidden); -// updateControlsVisibility(); -//} -// -//void HistoryWidget::onReportSpamClear() { -// Expects(_peer != nullptr); -// -// InvokeQueued(App::main(), [peer = _peer] { -// Ui::showChatsList(); -// if (const auto from = peer->migrateFrom()) { -// peer->session().api().deleteConversation(from, false); -// } -// peer->session().api().deleteConversation(peer, false); -// }); -// -// // Invalidates _peer. -// controller()->showBackFromStack(); -//} ContactStatus::Bar::Bar(QWidget *parent, const QString &name) : RpWidget(parent) , _name(name) , _add( this, - lang(lng_new_contact_add).toUpper(), + QString(), st::historyContactStatusButton) , _block( this, @@ -136,7 +74,7 @@ ContactStatus::Bar::Bar(QWidget *parent, const QString &name) st::historyContactStatusButton) , _report( this, - lang(lng_report_spam).toUpper(), + lang(lng_report_spam_and_leave).toUpper(), st::historyContactStatusBlock) , _close(this, st::historyReplyCancel) { resize(_close->size()); @@ -152,6 +90,7 @@ void ContactStatus::Bar::showState(State state) { : lang(lng_new_contact_add).toUpper()); updateButtonsGeometry(); } + rpl::producer<> ContactStatus::Bar::addClicks() const { return _add->clicks() | MapToEmpty(); } @@ -241,9 +180,11 @@ void ContactStatus::Bar::updateButtonsGeometry() { } ContactStatus::ContactStatus( + not_null window, not_null parent, not_null peer) -: _bar(parent, object_ptr(parent, PeerFirstName(peer))) +: _window(window) +, _bar(parent, object_ptr(parent, PeerFirstName(peer))) , _shadow(parent) { setupWidgets(parent); setupState(peer); @@ -333,23 +274,67 @@ void ContactStatus::setupState(not_null peer) { } void ContactStatus::setupHandlers(not_null peer) { - setupAddHandler(peer); - setupBlockHandler(peer); - setupShareHandler(peer); + if (const auto user = peer->asUser()) { + setupAddHandler(user); + setupBlockHandler(user); + setupShareHandler(user); + } setupReportHandler(peer); setupCloseHandler(peer); } -void ContactStatus::setupAddHandler(not_null peer) { +void ContactStatus::setupAddHandler(not_null peer) { + _bar.entity()->addClicks( + ) | rpl::start_with_next([=] { + Expects(peer->isUser()); + + Window::PeerMenuAddContact(peer->asUser()); + }, _bar.lifetime()); } -void ContactStatus::setupBlockHandler(not_null peer) { +void ContactStatus::setupBlockHandler(not_null peer) { } -void ContactStatus::setupShareHandler(not_null peer) { +void ContactStatus::setupShareHandler(not_null peer) { } void ContactStatus::setupReportHandler(not_null peer) { + _bar.entity()->reportClicks( + ) | rpl::start_with_next([=] { + Expects(!peer->isUser()); + + const auto box = std::make_shared>(); + const auto callback = crl::guard(&_bar, [=] { + if (*box) { + (*box)->closeBox(); + } + + peer->session().api().request(MTPmessages_ReportSpam( + peer->input + )).send(); + + crl::on_main(&peer->session(), [=] { + if (const auto from = peer->migrateFrom()) { + peer->session().api().deleteConversation(from, false); + } + peer->session().api().deleteConversation(peer, false); + }); + + // Destroys _bar. + _window->sessionController()->showBackFromStack(); + }); + if (const auto user = peer->asUser()) { + peer->session().api().blockUser(user); + } + const auto text = lang((peer->isChat() || peer->isMegagroup()) + ? lng_report_spam_sure_group + : lng_report_spam_sure_channel); + _window->show(Box( + text, + lang(lng_report_spam_ok), + st::attentionBoxButton, + callback)); + }, _bar.lifetime()); } void ContactStatus::setupCloseHandler(not_null peer) { diff --git a/Telegram/SourceFiles/history/view/history_view_contact_status.h b/Telegram/SourceFiles/history/view/history_view_contact_status.h index 196e34e88..63f679769 100644 --- a/Telegram/SourceFiles/history/view/history_view_contact_status.h +++ b/Telegram/SourceFiles/history/view/history_view_contact_status.h @@ -11,6 +11,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/wrap/slide_wrap.h" #include "ui/widgets/shadow.h" +namespace Window { +class Controller; +} // namespace Window + namespace Ui { class FlatButton; class IconButton; @@ -20,7 +24,10 @@ namespace HistoryView { class ContactStatus final { public: - ContactStatus(not_null parent, not_null peer); + ContactStatus( + not_null window, + not_null parent, + not_null peer); void show(); void raise(); @@ -72,14 +79,15 @@ private: void setupWidgets(not_null parent); void setupState(not_null peer); void setupHandlers(not_null peer); - void setupAddHandler(not_null peer); - void setupBlockHandler(not_null peer); - void setupShareHandler(not_null peer); + void setupAddHandler(not_null peer); + void setupBlockHandler(not_null peer); + void setupShareHandler(not_null peer); void setupReportHandler(not_null peer); void setupCloseHandler(not_null peer); static rpl::producer PeerState(not_null peer); + not_null _window; State _state = State::None; Ui::SlideWrap _bar; Ui::PlainShadow _shadow; diff --git a/Telegram/SourceFiles/info/profile/info_profile_values.cpp b/Telegram/SourceFiles/info/profile/info_profile_values.cpp index 135a77f4b..7375456d6 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_values.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_values.cpp @@ -154,10 +154,12 @@ rpl::producer CanShareContactValue(not_null user) { rpl::producer CanAddContactValue(not_null user) { using namespace rpl::mappers; - return rpl::combine( - IsContactValue(user), - CanShareContactValue(user), - !_1 && _2); + if (user->isBot() || user->isSelf()) { + return rpl::single(false); + } + return IsContactValue( + user + ) | rpl::map(!_1); } rpl::producer AmInChannelValue(not_null channel) { diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp index e4b9ea166..b2a7640ec 100644 --- a/Telegram/SourceFiles/window/window_controller.cpp +++ b/Telegram/SourceFiles/window/window_controller.cpp @@ -54,6 +54,13 @@ void Controller::showSettings() { _widget.showSettings(); } +void Controller::showBox( + object_ptr content, + LayerOptions options, + anim::type animated) { + _widget.ui_showBox(std::move(content), options, animated); +} + void Controller::activate() { _widget.activate(); } diff --git a/Telegram/SourceFiles/window/window_controller.h b/Telegram/SourceFiles/window/window_controller.h index f5fb3a573..99b6888c0 100644 --- a/Telegram/SourceFiles/window/window_controller.h +++ b/Telegram/SourceFiles/window/window_controller.h @@ -42,6 +42,16 @@ public: void showSettings(); + template + QPointer show( + object_ptr content, + LayerOptions options = LayerOption::KeepOther, + anim::type animated = anim::type::normal) { + const auto result = QPointer(content.data()); + showBox(std::move(content), options, animated); + return result; + } + void activate(); void reActivate(); void updateIsActive(int timeout); @@ -53,6 +63,11 @@ public: void tempDirDelete(int task); private: + void showBox( + object_ptr content, + LayerOptions options, + anim::type animated); + not_null _account; ::MainWindow _widget; std::unique_ptr _sessionController; diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 1b70052a0..381afd21b 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -360,31 +360,28 @@ void Filler::addUserActions(not_null user) { Auth().supportHelper().editInfo(user); }); } - if (user->isContact()) { - if (!user->isSelf()) { - _addAction( - lang(lng_info_share_contact), - [user] { PeerMenuShareContactBox(user); }); - _addAction( - lang(lng_info_edit_contact), - [user] { Ui::show(Box(user)); }); - _addAction( - lang(lng_info_delete_contact), - [user] { PeerMenuDeleteContact(user); }); - } - } else if (user->canShareThisContact()) { - if (!user->isSelf()) { - _addAction( - lang(lng_info_add_as_contact), - [user] { PeerMenuAddContact(user); }); - } + if (!user->isContact() && !user->isSelf() && !user->isBot()) { + _addAction( + lang(lng_info_add_as_contact), + [=] { PeerMenuAddContact(user); }); + } + if (user->canShareThisContact()) { _addAction( lang(lng_info_share_contact), - [user] { PeerMenuShareContactBox(user); }); - } else if (user->botInfo && !user->botInfo->cantJoinGroups) { + [=] { PeerMenuShareContactBox(user); }); + } + if (user->isContact() && !user->isSelf()) { + _addAction( + lang(lng_info_edit_contact), + [=] { Ui::show(Box(user)); }); + _addAction( + lang(lng_info_delete_contact), + [=] { PeerMenuDeleteContact(user); }); + } + if (user->isBot() && !user->botInfo->cantJoinGroups) { _addAction( lang(lng_profile_invite_to_group), - [user] { AddBotToGroupBoxController::Start(user); }); + [=] { AddBotToGroupBoxController::Start(user); }); } if (user->canExportChatHistory()) { _addAction(