api_report.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "api/api_report.h"
  8. #include "apiwrap.h"
  9. #include "data/data_peer.h"
  10. #include "data/data_photo.h"
  11. #include "data/data_report.h"
  12. #include "data/data_user.h"
  13. #include "lang/lang_keys.h"
  14. #include "main/main_session.h"
  15. #include "ui/boxes/report_box_graphics.h"
  16. #include "ui/layers/show.h"
  17. namespace Api {
  18. namespace {
  19. MTPreportReason ReasonToTL(const Ui::ReportReason &reason) {
  20. using Reason = Ui::ReportReason;
  21. switch (reason) {
  22. case Reason::Spam: return MTP_inputReportReasonSpam();
  23. case Reason::Fake: return MTP_inputReportReasonFake();
  24. case Reason::Violence: return MTP_inputReportReasonViolence();
  25. case Reason::ChildAbuse: return MTP_inputReportReasonChildAbuse();
  26. case Reason::Pornography: return MTP_inputReportReasonPornography();
  27. case Reason::Copyright: return MTP_inputReportReasonCopyright();
  28. case Reason::IllegalDrugs: return MTP_inputReportReasonIllegalDrugs();
  29. case Reason::PersonalDetails:
  30. return MTP_inputReportReasonPersonalDetails();
  31. case Reason::Other: return MTP_inputReportReasonOther();
  32. }
  33. Unexpected("Bad reason group value.");
  34. }
  35. } // namespace
  36. void SendPhotoReport(
  37. std::shared_ptr<Ui::Show> show,
  38. not_null<PeerData*> peer,
  39. Ui::ReportReason reason,
  40. const QString &comment,
  41. not_null<PhotoData*> photo) {
  42. peer->session().api().request(MTPaccount_ReportProfilePhoto(
  43. peer->input,
  44. photo->mtpInput(),
  45. ReasonToTL(reason),
  46. MTP_string(comment)
  47. )).done([=] {
  48. show->showToast(tr::lng_report_thanks(tr::now));
  49. }).send();
  50. }
  51. auto CreateReportMessagesOrStoriesCallback(
  52. std::shared_ptr<Ui::Show> show,
  53. not_null<PeerData*> peer)
  54. -> Fn<void(Data::ReportInput, Fn<void(ReportResult)>)> {
  55. using TLChoose = MTPDreportResultChooseOption;
  56. using TLAddComment = MTPDreportResultAddComment;
  57. using TLReported = MTPDreportResultReported;
  58. using Result = ReportResult;
  59. struct State final {
  60. #ifdef _DEBUG
  61. ~State() {
  62. qDebug() << "Messages or Stories Report ~State().";
  63. }
  64. #endif
  65. mtpRequestId requestId = 0;
  66. };
  67. const auto state = std::make_shared<State>();
  68. return [=](
  69. Data::ReportInput reportInput,
  70. Fn<void(Result)> done) {
  71. auto apiIds = QVector<MTPint>();
  72. apiIds.reserve(reportInput.ids.size() + reportInput.stories.size());
  73. for (const auto &id : reportInput.ids) {
  74. apiIds.push_back(MTP_int(id));
  75. }
  76. for (const auto &story : reportInput.stories) {
  77. apiIds.push_back(MTP_int(story));
  78. }
  79. const auto received = [=](
  80. const MTPReportResult &result,
  81. mtpRequestId requestId) {
  82. if (state->requestId != requestId) {
  83. return;
  84. }
  85. state->requestId = 0;
  86. done(result.match([&](const TLChoose &data) {
  87. const auto t = qs(data.vtitle());
  88. auto list = Result::Options();
  89. list.reserve(data.voptions().v.size());
  90. for (const auto &tl : data.voptions().v) {
  91. list.emplace_back(Result::Option{
  92. .id = tl.data().voption().v,
  93. .text = qs(tl.data().vtext()),
  94. });
  95. }
  96. return Result{ .options = std::move(list), .title = t };
  97. }, [&](const TLAddComment &data) -> Result {
  98. return {
  99. .commentOption = ReportResult::CommentOption{
  100. .optional = data.is_optional(),
  101. .id = data.voption().v,
  102. }
  103. };
  104. }, [&](const TLReported &data) -> Result {
  105. return { .successful = true };
  106. }));
  107. };
  108. const auto fail = [=](const MTP::Error &error) {
  109. state->requestId = 0;
  110. done({ .error = error.type() });
  111. };
  112. if (!reportInput.stories.empty()) {
  113. state->requestId = peer->session().api().request(
  114. MTPstories_Report(
  115. peer->input,
  116. MTP_vector<MTPint>(apiIds),
  117. MTP_bytes(reportInput.optionId),
  118. MTP_string(reportInput.comment))
  119. ).done(received).fail(fail).send();
  120. } else {
  121. state->requestId = peer->session().api().request(
  122. MTPmessages_Report(
  123. peer->input,
  124. MTP_vector<MTPint>(apiIds),
  125. MTP_bytes(reportInput.optionId),
  126. MTP_string(reportInput.comment))
  127. ).done(received).fail(fail).send();
  128. }
  129. };
  130. }
  131. } // namespace Api