AdminAPI: show chat
[akkoma] / lib / pleroma / web / admin_api / controllers / chat_controller.ex
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
5 defmodule Pleroma.Web.AdminAPI.ChatController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Activity
9 alias Pleroma.Chat
10 alias Pleroma.Chat.MessageReference
11 alias Pleroma.ModerationLog
12 alias Pleroma.Pagination
13 alias Pleroma.Plugs.OAuthScopesPlug
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
16 alias Pleroma.Web.PleromaAPI.ChatView
17
18 require Logger
19
20 plug(Pleroma.Web.ApiSpec.CastAndValidate)
21
22 plug(
23 OAuthScopesPlug,
24 %{scopes: ["read:chats"], admin: true} when action in [:show, :messages]
25 )
26
27 plug(
28 OAuthScopesPlug,
29 %{scopes: ["write:chats"], admin: true} when action in [:delete_message]
30 )
31
32 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
33
34 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ChatOperation
35
36 def delete_message(%{assigns: %{user: user}} = conn, %{message_id: id}) do
37 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
38 ModerationLog.insert_log(%{
39 action: "chat_message_delete",
40 actor: user,
41 subject_id: id
42 })
43
44 json(conn, %{})
45 end
46 end
47
48 def messages(conn, %{id: id} = params) do
49 with %Chat{} = chat <- Chat.get_by_id(id) do
50 cm_refs =
51 chat
52 |> MessageReference.for_chat_query()
53 |> Pagination.fetch_paginated(params)
54
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
66 def show(conn, %{id: id}) do
67 with %Chat{} = chat <- Chat.get_by_id(id) do
68 conn
69 |> put_view(ChatView)
70 |> render("show.json", chat: chat)
71 end
72 end
73 end