ChatController: Support deletion of chat messages.
authorlain <lain@soykaf.club>
Tue, 12 May 2020 11:13:03 +0000 (13:13 +0200)
committerlain <lain@soykaf.club>
Tue, 12 May 2020 11:13:03 +0000 (13:13 +0200)
lib/pleroma/web/activity_pub/object_validators/delete_validator.ex
lib/pleroma/web/api_spec/operations/chat_operation.ex
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
lib/pleroma/web/router.ex
test/web/pleroma_api/controllers/chat_controller_test.exs

index f42c035105444a8b48eefa53389bdff4c3ab7d0a..e5d08eb5c7f0b380fa8733b990db348ed4e27cbc 100644 (file)
@@ -46,12 +46,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator do
     Answer
     Article
     Audio
+    ChatMessage
     Event
     Note
     Page
     Question
-    Video
     Tombstone
+    Video
   }
   def validate_data(cng) do
     cng
index fe6c2f52f3f568aa53dad02f269adeafeb2743fe..8ba10c60338eddb338960f2da2c9e64fbe426d57 100644 (file)
@@ -166,6 +166,31 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
     }
   end
 
+  def delete_message_operation do
+    %Operation{
+      tags: ["chat"],
+      summary: "delete_message",
+      operationId: "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 =>
+          Operation.response(
+            "The deleted ChatMessage",
+            "application/json",
+            ChatMessage
+          )
+      },
+      security: [
+        %{
+          "oAuth" => ["write"]
+        }
+      ]
+    }
+  end
+
   def chats_response do
     %Schema{
       title: "ChatsResponse",
index 04f136dcd77009bd92d312157966c71c735a5c48..8eed8875285cf2eb6ff7d16063ae60ab74bde250 100644 (file)
@@ -4,6 +4,7 @@
 defmodule Pleroma.Web.PleromaAPI.ChatController do
   use Pleroma.Web, :controller
 
+  alias Pleroma.Activity
   alias Pleroma.Chat
   alias Pleroma.Object
   alias Pleroma.Pagination
@@ -22,7 +23,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
 
   plug(
     OAuthScopesPlug,
-    %{scopes: ["write:statuses"]} when action in [:post_chat_message, :create, :mark_as_read]
+    %{scopes: ["write:statuses"]}
+    when action in [:post_chat_message, :create, :mark_as_read, :delete_message]
   )
 
   plug(
@@ -34,6 +36,26 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
 
   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
 
+  def delete_message(%{assigns: %{user: %{ap_id: actor} = user}} = conn, %{
+        message_id: id
+      }) do
+    with %Object{
+           data: %{
+             "actor" => ^actor,
+             "id" => object,
+             "to" => [recipient],
+             "type" => "ChatMessage"
+           }
+         } = message <- Object.get_by_id(id),
+         %Chat{} = chat <- Chat.get(user.id, recipient),
+         %Activity{} = activity <- Activity.get_create_by_object_ap_id(object),
+         {:ok, _delete} <- CommonAPI.delete(activity.id, user) do
+      conn
+      |> put_view(ChatMessageView)
+      |> render("show.json", for: user, object: message, chat: chat)
+    end
+  end
+
   def post_chat_message(
         %{body_params: %{content: content} = params, assigns: %{user: %{id: user_id} = user}} =
           conn,
index 3b1834d97ff2e1c0c0390a841faf805b67e93b46..0e4f45869aff082642eb7298aab96afa8379643b 100644 (file)
@@ -310,6 +310,7 @@ defmodule Pleroma.Web.Router do
       get("/chats/:id", ChatController, :show)
       get("/chats/:id/messages", ChatController, :messages)
       post("/chats/:id/messages", ChatController, :post_chat_message)
+      delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
       post("/chats/:id/read", ChatController, :mark_as_read)
     end
 
index dda4f9e5b212d7a1005f7ead920c00bb559af883..86ccbb117f332598623bdcfced4ba16cf1a8d9c1 100644 (file)
@@ -4,6 +4,7 @@
 defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   use Pleroma.Web.ConnCase, async: true
 
+  alias Pleroma.Object
   alias Pleroma.Chat
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.CommonAPI
@@ -78,6 +79,29 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
     end
   end
 
+  describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
+    setup do: oauth_access(["write:statuses"])
+
+    test "it deletes a message for the author of the message", %{conn: conn, user: user} do
+      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)
+
+      result =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
+        |> json_response_and_validate_schema(200)
+
+      assert result["id"] == to_string(object.id)
+    end
+  end
+
   describe "GET /api/v1/pleroma/chats/:id/messages" do
     setup do: oauth_access(["read:statuses"])