AdminAPI: fix delete chat message
authorAlex Gleason <alex@alexgleason.me>
Wed, 2 Sep 2020 01:40:36 +0000 (20:40 -0500)
committerAlex Gleason <alex@alexgleason.me>
Fri, 11 Sep 2020 19:10:35 +0000 (14:10 -0500)
lib/pleroma/web/admin_api/controllers/chat_controller.ex
lib/pleroma/web/api_spec/operations/admin/chat_operation.ex
test/web/admin_api/controllers/chat_controller_test.exs

index ac362c430f3034cdb128a4a09b50e918e62e226a..61d45b970cd5c6629d43932125a8481d1a428626 100644 (file)
@@ -33,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
 
index 3550d531e9c5eec0537bd3074c4ad68ae1f39896..d3e5dfc1cac3b4fff9221c2751b86a0475c8d1c0 100644 (file)
@@ -4,9 +4,8 @@
 
 defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
   alias OpenApiSpex.Operation
-  alias Pleroma.Web.ApiSpec.Schemas.ApiError
   alias Pleroma.Web.ApiSpec.Schemas.Chat
-  alias Pleroma.Web.ApiSpec.Schemas.FlakeID
+  alias Pleroma.Web.ApiSpec.Schemas.ChatMessage
 
   import Pleroma.Web.ApiSpec.Helpers
 
@@ -19,13 +18,24 @@ defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
     %Operation{
       tags: ["admin", "chat"],
       summary: "Delete an individual chat message",
-      operationId: "AdminAPI.ChatController.delete",
-      parameters: [id_param(), message_id_param()] ++ admin_api_params(),
-      security: [%{"oAuth" => ["write:chats"]}],
+      operationId: "AdminAPI.ChatController.delete_message",
+      parameters: [
+        Operation.parameter(:id, :path, :string, "The ID of the Chat"),
+        Operation.parameter(:message_id, :path, :string, "The ID of the message")
+      ],
       responses: %{
-        200 => empty_object_response(),
-        404 => Operation.response("Not Found", "application/json", ApiError)
-      }
+        200 =>
+          Operation.response(
+            "The deleted ChatMessage",
+            "application/json",
+            ChatMessage
+          )
+      },
+      security: [
+        %{
+          "oAuth" => ["write:chats"]
+        }
+      ]
     }
   end
 
@@ -83,18 +93,4 @@ defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
       ]
     }
   end
-
-  def id_param do
-    Operation.parameter(:id, :path, FlakeID, "Chat ID",
-      example: "9umDrYheeY451cQnEe",
-      required: true
-    )
-  end
-
-  def message_id_param do
-    Operation.parameter(:message_id, :path, FlakeID, "Chat message ID",
-      example: "9umDrYheeY451cQnEe",
-      required: true
-    )
-  end
 end
index 63c195b99afc99b945ca9661bdce672ac3d3ac99..9393dd49bbdc59500f1a62e23f06478741080df1 100644 (file)
@@ -7,9 +7,10 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
 
   import Pleroma.Factory
 
-  alias Pleroma.Activity
   alias Pleroma.Chat
+  alias Pleroma.Chat.MessageReference
   alias Pleroma.Config
+  alias Pleroma.Object
   alias Pleroma.ModerationLog
   alias Pleroma.Repo
   alias Pleroma.Web.CommonAPI
@@ -27,29 +28,33 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
   end
 
   describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do
-    setup do
-      chat = insert(:chat)
-      message = insert(:chat_message_activity, chat: chat)
-      %{chat: chat, message: message}
-    end
+    test "it deletes a message from the chat", %{conn: conn, admin: admin} do
+      user = insert(:user)
+      recipient = insert(:user)
+
+      {:ok, message} =
+        CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
+
+      object = Object.normalize(message, false)
+
+      chat = Chat.get(user.id, recipient.ap_id)
 
-    test "deletes chat message", %{conn: conn, chat: chat, message: message, admin: admin} do
-      conn
-      |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{message.id}")
-      |> json_response_and_validate_schema(:ok)
+      cm_ref = MessageReference.for_chat_and_object(chat, object)
 
-      refute Activity.get_by_id(message.id)
+      result =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
+        |> json_response_and_validate_schema(200)
 
       log_entry = Repo.one(ModerationLog)
 
       assert ModerationLog.get_log_entry_message(log_entry) ==
-               "@#{admin.nickname} deleted chat message ##{message.id}"
-    end
-
-    test "returns 404 when the chat message does not exist", %{conn: conn} do
-      conn = delete(conn, "/api/pleroma/admin/chats/test/messages/test")
+               "@#{admin.nickname} deleted chat message ##{cm_ref.id}"
 
-      assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
+      assert result["id"] == cm_ref.id
+      refute MessageReference.get_by_id(cm_ref.id)
+      assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
     end
   end