update mastofe paths (#95)
[akkoma] / lib / pleroma / web / admin_api / controllers / chat_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.Web.AdminAPI
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
16 alias Pleroma.Web.Plugs.OAuthScopesPlug
17
18 require Logger
19
20 plug(Pleroma.Web.ApiSpec.CastAndValidate)
21
22 plug(
23 OAuthScopesPlug,
24 %{scopes: ["admin:read:chats"]} when action in [:show, :messages]
25 )
26
27 plug(
28 OAuthScopesPlug,
29 %{scopes: ["admin:write:chats"]} 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, %{
37 message_id: message_id,
38 id: chat_id
39 }) do
40 with %MessageReference{object: %{data: %{"id" => object_ap_id}}} = cm_ref <-
41 MessageReference.get_by_id(message_id),
42 ^chat_id <- to_string(cm_ref.chat_id),
43 %Activity{id: activity_id} <- Activity.get_create_by_object_ap_id(object_ap_id),
44 {:ok, _} <- CommonAPI.delete(activity_id, user) do
45 ModerationLog.insert_log(%{
46 action: "chat_message_delete",
47 actor: user,
48 subject_id: message_id
49 })
50
51 conn
52 |> put_view(MessageReferenceView)
53 |> render("show.json", chat_message_reference: cm_ref)
54 else
55 _e ->
56 {:error, :could_not_delete}
57 end
58 end
59
60 def messages(conn, %{id: id} = params) do
61 with %Chat{} = chat <- Chat.get_by_id(id) do
62 cm_refs =
63 chat
64 |> MessageReference.for_chat_query()
65 |> Pagination.fetch_paginated(params)
66
67 conn
68 |> put_view(MessageReferenceView)
69 |> render("index.json", chat_message_references: cm_refs)
70 else
71 _ ->
72 conn
73 |> put_status(:not_found)
74 |> json(%{error: "not found"})
75 end
76 end
77
78 def show(conn, %{id: id}) do
79 with %Chat{} = chat <- Chat.get_by_id(id) do
80 conn
81 |> put_view(AdminAPI.ChatView)
82 |> render("show.json", chat: chat)
83 end
84 end
85 end