Merge branch 'develop' into refactor/discoverable_user_field
[akkoma] / lib / pleroma / web / admin_api / controllers / chat_controller.ex
index b423188d756514fb0800c93970432bc7a42b2721..af8ff8292d77ce1ae4db26f4b716ffd5af59a586 100644 (file)
@@ -10,9 +10,10 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
   alias Pleroma.Chat.MessageReference
   alias Pleroma.ModerationLog
   alias Pleroma.Pagination
-  alias Pleroma.Plugs.OAuthScopesPlug
+  alias Pleroma.Web.AdminAPI
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
+  alias Pleroma.Web.Plugs.OAuthScopesPlug
 
   require Logger
 
@@ -20,7 +21,7 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
 
   plug(
     OAuthScopesPlug,
-    %{scopes: ["read:chats"], admin: true} when action in [:messages]
+    %{scopes: ["read:chats"], admin: true} when action in [:show, :messages]
   )
 
   plug(
@@ -32,15 +33,27 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
 
   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ChatOperation
 
-  def delete_message(%{assigns: %{user: user}} = conn, %{message_id: id}) do
-    with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
+  def delete_message(%{assigns: %{user: user}} = conn, %{
+        message_id: message_id,
+        id: chat_id
+      }) do
+    with %MessageReference{object: %{data: %{"id" => object_ap_id}}} = cm_ref <-
+           MessageReference.get_by_id(message_id),
+         ^chat_id <- to_string(cm_ref.chat_id),
+         %Activity{id: activity_id} <- Activity.get_create_by_object_ap_id(object_ap_id),
+         {:ok, _} <- CommonAPI.delete(activity_id, user) do
       ModerationLog.insert_log(%{
         action: "chat_message_delete",
         actor: user,
-        subject_id: id
+        subject_id: message_id
       })
 
-      json(conn, %{})
+      conn
+      |> put_view(MessageReferenceView)
+      |> render("show.json", chat_message_reference: cm_ref)
+    else
+      _e ->
+        {:error, :could_not_delete}
     end
   end
 
@@ -61,4 +74,12 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
         |> json(%{error: "not found"})
     end
   end
+
+  def show(conn, %{id: id}) do
+    with %Chat{} = chat <- Chat.get_by_id(id) do
+      conn
+      |> put_view(AdminAPI.ChatView)
+      |> render("show.json", chat: chat)
+    end
+  end
 end