Add API endpoint to remove a conversation
authorEgor Kislitsyn <egor@kislitsyn.com>
Mon, 15 Feb 2021 17:48:13 +0000 (21:48 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Mon, 15 Feb 2021 17:48:13 +0000 (21:48 +0400)
CHANGELOG.md
lib/pleroma/conversation.ex
lib/pleroma/conversation/participation.ex
lib/pleroma/web/api_spec/operations/conversation_operation.ex
lib/pleroma/web/mastodon_api/controllers/conversation_controller.ex
lib/pleroma/web/router.ex
test/pleroma/conversation/participation_test.exs
test/pleroma/web/mastodon_api/controllers/conversation_controller_test.exs

index bbd898bdfc978fdfdfbbb25b3876ba63de47204c..03655043059f3987471c9e243bc43085c636dbac 100644 (file)
@@ -66,6 +66,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Mastodon API: Add monthly active users to `/api/v1/instance` (`pleroma.stats.mau`).
 - Mastodon API: Home, public, hashtag & list timelines accept `only_media`, `remote` & `local` parameters for filtration.
 - Mastodon API: `/api/v1/accounts/:id` & `/api/v1/mutes` endpoints accept `with_relationships` parameter and return filled `pleroma.relationship` field.
+- Mastodon API: Endpoint to remove a conversation (`DELETE /api/v1/conversations/:id`).
 </details>
 
 ### Fixed
index 8812b456ddd54a2eb7f7b899bf122f91fb0cfc2e..828e274504a2d6b394a520ece4bff61e6cc06b9b 100644 (file)
@@ -61,9 +61,8 @@ defmodule Pleroma.Conversation do
          "Create" <- activity.data["type"],
          %Object{} = object <- Object.normalize(activity, fetch: false),
          true <- object.data["type"] in ["Note", "Question"],
-         ap_id when is_binary(ap_id) and byte_size(ap_id) > 0 <- object.data["context"] do
-      {:ok, conversation} = create_for_ap_id(ap_id)
-
+         ap_id when is_binary(ap_id) and byte_size(ap_id) > 0 <- object.data["context"],
+         {:ok, conversation} <- create_for_ap_id(ap_id) do
       users = User.get_users_from_set(activity.recipients, local_only: false)
 
       participations =
index da5e57714d60e6758d515326fa15f32c2537a73e..e0a3af28b9b62fdeaa2b0a31bf2ac663578b3eb0 100644 (file)
@@ -220,4 +220,8 @@ defmodule Pleroma.Conversation.Participation do
       select: %{count: count(p.id)}
     )
   end
+
+  def delete(%__MODULE__{} = participation) do
+    Repo.delete(participation)
+  end
 end
index 367f4125a4a3f72c3f76becdcc726bbe72d6f352..17ed1af5e5ca861329e74d1624b4297a9b3e519a 100644 (file)
@@ -46,16 +46,31 @@ defmodule Pleroma.Web.ApiSpec.ConversationOperation do
       tags: ["Conversations"],
       summary: "Mark conversation as read",
       operationId: "ConversationController.mark_as_read",
-      parameters: [
-        Operation.parameter(:id, :path, :string, "Conversation ID",
-          example: "123",
-          required: true
-        )
-      ],
+      parameters: [id_param()],
       security: [%{"oAuth" => ["write:conversations"]}],
       responses: %{
         200 => Operation.response("Conversation", "application/json", Conversation)
       }
     }
   end
+
+  def delete_operation do
+    %Operation{
+      tags: ["Conversations"],
+      summary: "Remove conversation",
+      operationId: "ConversationController.delete",
+      parameters: [id_param()],
+      security: [%{"oAuth" => ["write:conversations"]}],
+      responses: %{
+        200 => empty_object_response()
+      }
+    }
+  end
+
+  def id_param do
+    Operation.parameter(:id, :path, :string, "Conversation ID",
+      example: "123",
+      required: true
+    )
+  end
 end
index 4526d3c7acbaa10dd32e3927f372acbed3ef10aa..f2a0949e853c62475af264f8300a1702a2d5ab20 100644 (file)
@@ -36,4 +36,13 @@ defmodule Pleroma.Web.MastodonAPI.ConversationController do
       render(conn, "participation.json", participation: participation, for: user)
     end
   end
+
+  @doc "DELETE /api/v1/conversations/:id"
+  def delete(%{assigns: %{user: user}} = conn, %{id: participation_id}) do
+    with %Participation{} = participation <-
+           Repo.get_by(Participation, id: participation_id, user_id: user.id),
+         {:ok, _} <- Participation.delete(participation) do
+      json(conn, %{})
+    end
+  end
 end
index 2105d7e9ed2bbf842a97032d4a81e693c0ae2921..b8aa8c67cd9beb681311da2dbb6c86dd212678e5 100644 (file)
@@ -436,6 +436,7 @@ defmodule Pleroma.Web.Router do
 
     get("/conversations", ConversationController, :index)
     post("/conversations/:id/read", ConversationController, :mark_as_read)
+    delete("/conversations/:id", ConversationController, :delete)
 
     get("/domain_blocks", DomainBlockController, :index)
     post("/domain_blocks", DomainBlockController, :create)
index 8b039cd78de8cb7d30376491e6711006c8bf93a7..a25e17c957f1206b8b510cc75e67184d0b14535d 100644 (file)
@@ -359,4 +359,16 @@ defmodule Pleroma.Conversation.ParticipationTest do
       assert Participation.unread_count(blocked) == 1
     end
   end
+
+  test "deletes a conversation" do
+    user = insert(:user)
+    other_user = insert(:user)
+
+    {:ok, _activity} =
+      CommonAPI.post(user, %{status: "Hey @#{other_user.nickname}.", visibility: "direct"})
+
+    assert [participation] = Participation.for_user(other_user)
+    assert {:ok, _} = Participation.delete(participation)
+    assert [] == Participation.for_user(other_user)
+  end
 end
index 29bc4fd1784c5979078153b72940e52426fecd38..3176f12968069aac77bbdb8d15e321589dbabc52 100644 (file)
@@ -217,6 +217,32 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
     assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
   end
 
+  test "Removes a conversation", %{user: user_one, conn: conn} do
+    user_two = insert(:user)
+    token = insert(:oauth_token, user: user_one, scopes: ["read:statuses", "write:conversations"])
+
+    {:ok, _direct} = create_direct_message(user_one, [user_two])
+    {:ok, _direct} = create_direct_message(user_one, [user_two])
+
+    assert [%{"id" => conv1_id}, %{"id" => conv2_id}] =
+             conn
+             |> assign(:token, token)
+             |> get("/api/v1/conversations")
+             |> json_response_and_validate_schema(200)
+
+    assert %{} =
+             conn
+             |> assign(:token, token)
+             |> delete("/api/v1/conversations/#{conv1_id}")
+             |> json_response_and_validate_schema(200)
+
+    assert [%{"id" => ^conv2_id}] =
+             conn
+             |> assign(:token, token)
+             |> get("/api/v1/conversations")
+             |> json_response_and_validate_schema(200)
+  end
+
   defp create_direct_message(sender, recips) do
     hellos =
       recips