Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

86 строки
2.3KB

  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.ChatController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Activity
  7. alias Pleroma.Chat
  8. alias Pleroma.Chat.MessageReference
  9. alias Pleroma.ModerationLog
  10. alias Pleroma.Pagination
  11. alias Pleroma.Web.AdminAPI
  12. alias Pleroma.Web.CommonAPI
  13. alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
  14. alias Pleroma.Web.Plugs.OAuthScopesPlug
  15. require Logger
  16. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  17. plug(
  18. OAuthScopesPlug,
  19. %{scopes: ["read:chats"], admin: true} when action in [:show, :messages]
  20. )
  21. plug(
  22. OAuthScopesPlug,
  23. %{scopes: ["write:chats"], admin: true} when action in [:delete_message]
  24. )
  25. action_fallback(Pleroma.Web.AdminAPI.FallbackController)
  26. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ChatOperation
  27. def delete_message(%{assigns: %{user: user}} = conn, %{
  28. message_id: message_id,
  29. id: chat_id
  30. }) do
  31. with %MessageReference{object: %{data: %{"id" => object_ap_id}}} = cm_ref <-
  32. MessageReference.get_by_id(message_id),
  33. ^chat_id <- to_string(cm_ref.chat_id),
  34. %Activity{id: activity_id} <- Activity.get_create_by_object_ap_id(object_ap_id),
  35. {:ok, _} <- CommonAPI.delete(activity_id, user) do
  36. ModerationLog.insert_log(%{
  37. action: "chat_message_delete",
  38. actor: user,
  39. subject_id: message_id
  40. })
  41. conn
  42. |> put_view(MessageReferenceView)
  43. |> render("show.json", chat_message_reference: cm_ref)
  44. else
  45. _e ->
  46. {:error, :could_not_delete}
  47. end
  48. end
  49. def messages(conn, %{id: id} = params) do
  50. with %Chat{} = chat <- Chat.get_by_id(id) do
  51. cm_refs =
  52. chat
  53. |> MessageReference.for_chat_query()
  54. |> Pagination.fetch_paginated(params)
  55. conn
  56. |> put_view(MessageReferenceView)
  57. |> render("index.json", chat_message_references: cm_refs)
  58. else
  59. _ ->
  60. conn
  61. |> put_status(:not_found)
  62. |> json(%{error: "not found"})
  63. end
  64. end
  65. def show(conn, %{id: id}) do
  66. with %Chat{} = chat <- Chat.get_by_id(id) do
  67. conn
  68. |> put_view(AdminAPI.ChatView)
  69. |> render("show.json", chat: chat)
  70. end
  71. end
  72. end