logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

report_controller.ex (3726B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.AdminAPI.ReportController do
  5. use Pleroma.Web, :controller
  6. import Pleroma.Web.ControllerHelper, only: [json_response: 3]
  7. alias Pleroma.Activity
  8. alias Pleroma.ModerationLog
  9. alias Pleroma.ReportNote
  10. alias Pleroma.Web.ActivityPub.Utils
  11. alias Pleroma.Web.AdminAPI
  12. alias Pleroma.Web.AdminAPI.Report
  13. alias Pleroma.Web.CommonAPI
  14. alias Pleroma.Web.Plugs.OAuthScopesPlug
  15. require Logger
  16. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  17. plug(OAuthScopesPlug, %{scopes: ["admin:read:reports"]} when action in [:index, :show])
  18. plug(
  19. OAuthScopesPlug,
  20. %{scopes: ["admin:write:reports"]}
  21. when action in [:update, :notes_create, :notes_delete]
  22. )
  23. action_fallback(AdminAPI.FallbackController)
  24. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ReportOperation
  25. def index(%{private: %{open_api_spex: %{params: params}}} = conn, _) do
  26. reports = Utils.get_reports(params, params.page, params.page_size)
  27. render(conn, "index.json", reports: reports)
  28. end
  29. def show(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
  30. with %Activity{} = report <- Activity.get_report(id) do
  31. render(conn, "show.json", Report.extract_report_info(report))
  32. else
  33. _ -> {:error, :not_found}
  34. end
  35. end
  36. def update(
  37. %{
  38. assigns: %{user: admin},
  39. private: %{open_api_spex: %{body_params: %{reports: reports}}}
  40. } = conn,
  41. _
  42. ) do
  43. result =
  44. Enum.map(reports, fn report ->
  45. case CommonAPI.update_report_state(report.id, report.state) do
  46. {:ok, activity} ->
  47. report = Activity.get_by_id_with_user_actor(activity.id)
  48. ModerationLog.insert_log(%{
  49. action: "report_update",
  50. actor: admin,
  51. subject: activity,
  52. subject_actor: report.user_actor
  53. })
  54. activity
  55. {:error, message} ->
  56. %{id: report.id, error: message}
  57. end
  58. end)
  59. if Enum.any?(result, &Map.has_key?(&1, :error)) do
  60. json_response(conn, :bad_request, result)
  61. else
  62. json_response(conn, :no_content, "")
  63. end
  64. end
  65. def notes_create(
  66. %{
  67. assigns: %{user: user},
  68. private: %{open_api_spex: %{body_params: %{content: content}, params: %{id: report_id}}}
  69. } = conn,
  70. _
  71. ) do
  72. with {:ok, _} <- ReportNote.create(user.id, report_id, content),
  73. report <- Activity.get_by_id_with_user_actor(report_id) do
  74. ModerationLog.insert_log(%{
  75. action: "report_note",
  76. actor: user,
  77. subject: report,
  78. subject_actor: report.user_actor,
  79. text: content
  80. })
  81. json_response(conn, :no_content, "")
  82. else
  83. _ -> json_response(conn, :bad_request, "")
  84. end
  85. end
  86. def notes_delete(
  87. %{
  88. assigns: %{user: user},
  89. private: %{
  90. open_api_spex: %{
  91. params: %{
  92. id: note_id,
  93. report_id: report_id
  94. }
  95. }
  96. }
  97. } = conn,
  98. _
  99. ) do
  100. with {:ok, note} <- ReportNote.destroy(note_id),
  101. report <- Activity.get_by_id_with_user_actor(report_id) do
  102. ModerationLog.insert_log(%{
  103. action: "report_note_delete",
  104. actor: user,
  105. subject: report,
  106. subject_actor: report.user_actor,
  107. text: note.content
  108. })
  109. json_response(conn, :no_content, "")
  110. else
  111. _ -> json_response(conn, :bad_request, "")
  112. end
  113. end
  114. end