Allow to report messages in supergroups.

This commit is contained in:
John Preston 2018-05-10 17:15:16 +03:00
parent 96c0c30f7c
commit 7d8ba15252
7 changed files with 116 additions and 20 deletions

View File

@ -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";

View File

@ -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<Ui::RadioenumGroup<Reason>>(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<PeerData*> peer)
: _peer(peer) {
}
ReportBox::ReportBox(QWidget*, not_null<PeerData*> 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<Ui::RadioenumGroup<Reason>>(
Reason::Spam);
const auto createButton = [&](
object_ptr<Ui::Radioenum<Reason>> &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<MTPint>();
for (const auto &fullId : *_ids) {
ids.push_back(MTP_int(fullId.msg));
}
_requestId = MTP::send(
MTPmessages_Report(
_peer->input,
MTP_vector<MTPint>(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<InformBox>(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) {

View File

@ -21,7 +21,8 @@ class ReportBox : public BoxContent, public RPCSender {
Q_OBJECT
public:
ReportBox(QWidget*, PeerData *peer);
ReportBox(QWidget*, not_null<PeerData*> peer);
ReportBox(QWidget*, not_null<PeerData*> 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<PeerData*> _peer;
base::optional<MessageIdsList> _ids;
std::shared_ptr<Ui::RadioenumGroup<Reason>> _reasonGroup;
object_ptr<Ui::Radioenum<Reason>> _reasonSpam;
object_ptr<Ui::Radioenum<Reason>> _reasonViolence;
object_ptr<Ui::Radioenum<Reason>> _reasonPornography;
object_ptr<Ui::Radioenum<Reason>> _reasonOther;
object_ptr<Ui::Radioenum<Reason>> _reasonSpam = { nullptr };
object_ptr<Ui::Radioenum<Reason>> _reasonViolence = { nullptr };
object_ptr<Ui::Radioenum<Reason>> _reasonPornography = { nullptr };
object_ptr<Ui::Radioenum<Reason>> _reasonOther = { nullptr };
object_ptr<Ui::InputArea> _reasonOtherText = { nullptr };
mtpRequestId _requestId = 0;

View File

@ -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<HistoryMessage*>(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<ReportBox>(_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<ReportBox>(
_peer,
Auth().data().itemsToIds(group->items)));
}
}
void HistoryInner::addSelectionRange(
not_null<SelectedItems*> toItems,
not_null<History*> history,

View File

@ -281,6 +281,8 @@ private:
void deleteItem(not_null<HistoryItem*> 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.

View File

@ -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();

View File

@ -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;