From 7d8ba1525294183bddf79507614251460b4cbfab Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 10 May 2018 17:15:16 +0300 Subject: [PATCH] Allow to report messages in supergroups. --- Telegram/Resources/langs/lang.strings | 2 + Telegram/SourceFiles/boxes/report_box.cpp | 77 +++++++++++++++---- Telegram/SourceFiles/boxes/report_box.h | 14 ++-- .../history/history_inner_widget.cpp | 29 ++++++- .../history/history_inner_widget.h | 2 + Telegram/SourceFiles/history/history_item.cpp | 11 +++ Telegram/SourceFiles/history/history_item.h | 1 + 7 files changed, 116 insertions(+), 20 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 93b1f741d..68d03b67a 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -690,6 +690,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_report_title" = "Report channel"; "lng_report_group_title" = "Report group"; "lng_report_bot_title" = "Report bot"; +"lng_report_message_title" = "Report message"; "lng_report_reason_spam" = "Spam"; "lng_report_reason_violence" = "Violence"; "lng_report_reason_pornography" = "Pornography"; @@ -1105,6 +1106,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_context_forward_msg" = "Forward Message"; "lng_context_delete_msg" = "Delete Message"; "lng_context_select_msg" = "Select Message"; +"lng_context_report_msg" = "Report Message"; "lng_context_pin_msg" = "Pin Message"; "lng_context_unpin_msg" = "Unpin Message"; "lng_context_cancel_upload" = "Cancel Upload"; diff --git a/Telegram/SourceFiles/boxes/report_box.cpp b/Telegram/SourceFiles/boxes/report_box.cpp index 48a5bc60e..8d5775c6a 100644 --- a/Telegram/SourceFiles/boxes/report_box.cpp +++ b/Telegram/SourceFiles/boxes/report_box.cpp @@ -14,23 +14,54 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/checkbox.h" #include "ui/widgets/buttons.h" #include "ui/widgets/input_fields.h" +#include "ui/toast/toast.h" #include "mainwindow.h" -ReportBox::ReportBox(QWidget*, PeerData *peer) : _peer(peer) -, _reasonGroup(std::make_shared>(Reason::Spam)) -, _reasonSpam(this, _reasonGroup, Reason::Spam, lang(lng_report_reason_spam), st::defaultBoxCheckbox) -, _reasonViolence(this, _reasonGroup, Reason::Violence, lang(lng_report_reason_violence), st::defaultBoxCheckbox) -, _reasonPornography(this, _reasonGroup, Reason::Pornography, lang(lng_report_reason_pornography), st::defaultBoxCheckbox) -, _reasonOther(this, _reasonGroup, Reason::Other, lang(lng_report_reason_other), st::defaultBoxCheckbox) { +ReportBox::ReportBox(QWidget*, not_null peer) +: _peer(peer) { +} + +ReportBox::ReportBox(QWidget*, not_null peer, MessageIdsList ids) +: _peer(peer) +, _ids(std::move(ids)) { } void ReportBox::prepare() { - setTitle(langFactory(_peer->isUser() ? lng_report_bot_title : (_peer->isMegagroup() ? lng_report_group_title : lng_report_title))); + setTitle(langFactory([&] { + if (_ids) { + return lng_report_message_title; + } else if (_peer->isUser()) { + return lng_report_bot_title; + } else if (_peer->isMegagroup()) { + return lng_report_group_title; + } else { + return lng_report_title; + } + }())); addButton(langFactory(lng_report_button), [this] { onReport(); }); addButton(langFactory(lng_cancel), [this] { closeBox(); }); - _reasonGroup->setChangedCallback([this](Reason value) { reasonChanged(value); }); + _reasonGroup = std::make_shared>( + Reason::Spam); + const auto createButton = [&]( + object_ptr> &button, + Reason reason, + LangKey key) { + button.create( + this, + _reasonGroup, + reason, + lang(key), + st::defaultBoxCheckbox); + }; + createButton(_reasonSpam, Reason::Spam, lng_report_reason_spam); + createButton(_reasonViolence, Reason::Violence, lng_report_reason_violence); + createButton(_reasonPornography, Reason::Pornography, lng_report_reason_pornography); + createButton(_reasonOther, Reason::Other, lng_report_reason_other); + _reasonGroup->setChangedCallback([=](Reason value) { + reasonChanged(value); + }); updateMaxHeight(); } @@ -90,7 +121,7 @@ void ReportBox::onReport() { return; } - auto getReason = [this]() { + const auto reason = [&] { switch (_reasonGroup->value()) { case Reason::Spam: return MTP_inputReportReasonSpam(); case Reason::Violence: return MTP_inputReportReasonViolence(); @@ -98,17 +129,37 @@ void ReportBox::onReport() { case Reason::Other: return MTP_inputReportReasonOther(MTP_string(_reasonOtherText->getLastText())); } Unexpected("Bad reason group value."); - }; - _requestId = MTP::send(MTPaccount_ReportPeer(_peer->input, getReason()), rpcDone(&ReportBox::reportDone), rpcFail(&ReportBox::reportFail)); + }(); + if (_ids) { + auto ids = QVector(); + for (const auto &fullId : *_ids) { + ids.push_back(MTP_int(fullId.msg)); + } + _requestId = MTP::send( + MTPmessages_Report( + _peer->input, + MTP_vector(ids), + reason), + rpcDone(&ReportBox::reportDone), + rpcFail(&ReportBox::reportFail)); + } else { + _requestId = MTP::send( + MTPaccount_ReportPeer(_peer->input, reason), + rpcDone(&ReportBox::reportDone), + rpcFail(&ReportBox::reportFail)); + } } void ReportBox::reportDone(const MTPBool &result) { _requestId = 0; - Ui::show(Box(lang(lng_report_thanks))); + Ui::Toast::Show(lang(lng_report_thanks)); + closeBox(); } bool ReportBox::reportFail(const RPCError &error) { - if (MTP::isDefaultHandledError(error)) return false; + if (MTP::isDefaultHandledError(error)) { + return false; + } _requestId = 0; if (_reasonOtherText) { diff --git a/Telegram/SourceFiles/boxes/report_box.h b/Telegram/SourceFiles/boxes/report_box.h index 3a34aad9b..d6e80328b 100644 --- a/Telegram/SourceFiles/boxes/report_box.h +++ b/Telegram/SourceFiles/boxes/report_box.h @@ -21,7 +21,8 @@ class ReportBox : public BoxContent, public RPCSender { Q_OBJECT public: - ReportBox(QWidget*, PeerData *peer); + ReportBox(QWidget*, not_null peer); + ReportBox(QWidget*, not_null peer, MessageIdsList ids); private slots: void onReport(); @@ -49,13 +50,14 @@ private: void reportDone(const MTPBool &result); bool reportFail(const RPCError &error); - PeerData *_peer; + not_null _peer; + base::optional _ids; std::shared_ptr> _reasonGroup; - object_ptr> _reasonSpam; - object_ptr> _reasonViolence; - object_ptr> _reasonPornography; - object_ptr> _reasonOther; + object_ptr> _reasonSpam = { nullptr }; + object_ptr> _reasonViolence = { nullptr }; + object_ptr> _reasonPornography = { nullptr }; + object_ptr> _reasonOther = { nullptr }; object_ptr _reasonOtherText = { nullptr }; mtpRequestId _requestId = 0; diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 6ab1278fa..2ea4f4dfe 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -24,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_controller.h" #include "window/window_peer_menu.h" #include "boxes/confirm_box.h" +#include "boxes/report_box.h" #include "chat_helpers/message_field.h" #include "chat_helpers/stickers.h" #include "history/history_widget.h" @@ -1564,6 +1565,11 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { deleteItem(itemId); }); } + if (item->suggestReport()) { + _menu->addAction(lang(lng_context_report_msg), [=] { + reportItem(itemId); + }); + } } if (IsServerMsgId(item->id) && !item->serviceMsg()) { _menu->addAction(lang(lng_context_select_msg), [=] { @@ -1596,6 +1602,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { && item->canDelete() && (item->id > 0 || !item->serviceMsg()); const auto canForward = item && item->allowsForward(); + const auto canReport = item && item->suggestReport(); const auto view = item ? item->mainView() : nullptr; const auto msg = dynamic_cast(item); @@ -1674,12 +1681,16 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { forwardAsGroup(itemId); }); } - if (canDelete) { _menu->addAction(lang((msg && msg->uploading()) ? lng_context_cancel_upload : lng_context_delete_msg), [=] { deleteAsGroup(itemId); }); } + if (canReport) { + _menu->addAction(lang(lng_context_report_msg), [=] { + reportAsGroup(itemId); + }); + } } if (item->id > 0 && !item->serviceMsg()) { _menu->addAction(lang(lng_context_select_msg), [=] { @@ -2899,6 +2910,22 @@ void HistoryInner::deleteAsGroup(FullMsgId itemId) { } } +void HistoryInner::reportItem(FullMsgId itemId) { + Ui::show(Box(_peer, MessageIdsList(1, itemId))); +} + +void HistoryInner::reportAsGroup(FullMsgId itemId) { + if (const auto item = App::histItemById(itemId)) { + const auto group = Auth().data().groups().find(item); + if (!group) { + return reportItem(itemId); + } + Ui::show(Box( + _peer, + Auth().data().itemsToIds(group->items))); + } +} + void HistoryInner::addSelectionRange( not_null toItems, not_null history, diff --git a/Telegram/SourceFiles/history/history_inner_widget.h b/Telegram/SourceFiles/history/history_inner_widget.h index e8eec987d..916f90054 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.h +++ b/Telegram/SourceFiles/history/history_inner_widget.h @@ -281,6 +281,8 @@ private: void deleteItem(not_null item); void deleteItem(FullMsgId itemId); void deleteAsGroup(FullMsgId itemId); + void reportItem(FullMsgId itemId); + void reportAsGroup(FullMsgId itemId); void copySelectedText(); // Does any of the shown histories has this flag set. diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 1e2751d05..09b48813b 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -407,6 +407,17 @@ bool HistoryItem::canDeleteForEveryone(TimeId now) const { return true; } +bool HistoryItem::suggestReport() const { + if (out() || serviceMsg() || !IsServerMsgId(id)) { + return false; + } else if (const auto channel = history()->peer->asChannel()) { + return true; + } else if (const auto user = history()->peer->asUser()) { + return user->botInfo != nullptr; + } + return false; +} + bool HistoryItem::suggestBanReport() const { auto channel = history()->peer->asChannel(); auto fromUser = from()->asUser(); diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 39f0fc1e9..d61e41bdd 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -215,6 +215,7 @@ public: virtual bool allowsEdit(TimeId now) const; bool canDelete() const; bool canDeleteForEveryone(TimeId now) const; + bool suggestReport() const; bool suggestBanReport() const; bool suggestDeleteAllReport() const;