logo

pleroma

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

chat_controller.ex (2201B)


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