Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

108 行
2.9KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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)
  17. plug(OAuthScopesPlug, %{scopes: ["read:reports"], admin: true} when action in [:index, :show])
  18. plug(
  19. OAuthScopesPlug,
  20. %{scopes: ["write:reports"], admin: true}
  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(conn, params) do
  26. reports = Utils.get_reports(params, params.page, params.page_size)
  27. render(conn, "index.json", reports: reports)
  28. end
  29. def show(conn, %{id: id}) do
  30. with %Activity{} = report <- Activity.get_by_id(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(%{assigns: %{user: admin}, body_params: %{reports: reports}} = conn, _) do
  37. result =
  38. Enum.map(reports, fn report ->
  39. case CommonAPI.update_report_state(report.id, report.state) do
  40. {:ok, activity} ->
  41. ModerationLog.insert_log(%{
  42. action: "report_update",
  43. actor: admin,
  44. subject: activity
  45. })
  46. activity
  47. {:error, message} ->
  48. %{id: report.id, error: message}
  49. end
  50. end)
  51. if Enum.any?(result, &Map.has_key?(&1, :error)) do
  52. json_response(conn, :bad_request, result)
  53. else
  54. json_response(conn, :no_content, "")
  55. end
  56. end
  57. def notes_create(%{assigns: %{user: user}, body_params: %{content: content}} = conn, %{
  58. id: report_id
  59. }) do
  60. with {:ok, _} <- ReportNote.create(user.id, report_id, content) do
  61. ModerationLog.insert_log(%{
  62. action: "report_note",
  63. actor: user,
  64. subject: Activity.get_by_id(report_id),
  65. text: content
  66. })
  67. json_response(conn, :no_content, "")
  68. else
  69. _ -> json_response(conn, :bad_request, "")
  70. end
  71. end
  72. def notes_delete(%{assigns: %{user: user}} = conn, %{
  73. id: note_id,
  74. report_id: report_id
  75. }) do
  76. with {:ok, note} <- ReportNote.destroy(note_id) do
  77. ModerationLog.insert_log(%{
  78. action: "report_note_delete",
  79. actor: user,
  80. subject: Activity.get_by_id(report_id),
  81. text: note.content
  82. })
  83. json_response(conn, :no_content, "")
  84. else
  85. _ -> json_response(conn, :bad_request, "")
  86. end
  87. end
  88. end