From 1f1bfbc25a32e2d8d337c541221c9abac4745d63 Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 24 Sep 2020 13:20:09 +0200 Subject: [PATCH 1/4] ChatController: Add chat deletion. --- .../web/api_spec/operations/chat_operation.ex | 24 +++++++++++++++++++ .../controllers/chat_controller.ex | 15 +++++++++++- lib/pleroma/web/router.ex | 1 + .../controllers/chat_controller_test.exs | 18 ++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/api_spec/operations/chat_operation.ex b/lib/pleroma/web/api_spec/operations/chat_operation.ex index 0dcfdb3546..1883db7fcd 100644 --- a/lib/pleroma/web/api_spec/operations/chat_operation.ex +++ b/lib/pleroma/web/api_spec/operations/chat_operation.ex @@ -196,6 +196,30 @@ def post_chat_message_operation do } end + def delete_operation do + %Operation{ + tags: ["chat"], + summary: "delete", + operationId: "ChatController.delete", + parameters: [ + Operation.parameter(:id, :path, :string, "The ID of the Chat") + ], + responses: %{ + 200 => + Operation.response( + "The deleted Chat", + "application/json", + Chat + ) + }, + security: [ + %{ + "oAuth" => ["write:chats"] + } + ] + } + end + def delete_message_operation do %Operation{ tags: ["chat"], diff --git a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex index e667831c59..231d42c196 100644 --- a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex @@ -30,7 +30,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do :create, :mark_as_read, :mark_message_as_read, - :delete_message + :delete_message, + :delete ] ) @@ -43,6 +44,18 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation + def delete(%{assigns: %{user: user}} = conn, %{id: chat_id}) do + with {:ok, chat} <- Chat.get_by_user_and_id(user, chat_id), + {:ok, chat} <- Pleroma.Repo.delete(chat) do + conn + |> put_view(ChatView) + |> render("show.json", chat: chat) + else + _e -> + {:error, :could_not_delete} + end + end + def delete_message(%{assigns: %{user: %{id: user_id} = user}} = conn, %{ message_id: message_id, id: chat_id diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 42a9db21da..330506c4db 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -324,6 +324,7 @@ defmodule Pleroma.Web.Router do post("/chats/by-account-id/:id", ChatController, :create) get("/chats", ChatController, :index) get("/chats/:id", ChatController, :show) + delete("/chats/:id", ChatController, :delete) get("/chats/:id/messages", ChatController, :messages) post("/chats/:id/messages", ChatController, :post_chat_message) delete("/chats/:id/messages/:message_id", ChatController, :delete_message) diff --git a/test/web/pleroma_api/controllers/chat_controller_test.exs b/test/web/pleroma_api/controllers/chat_controller_test.exs index 11d5ba3739..2b3845097c 100644 --- a/test/web/pleroma_api/controllers/chat_controller_test.exs +++ b/test/web/pleroma_api/controllers/chat_controller_test.exs @@ -301,6 +301,24 @@ test "it returns a chat", %{conn: conn, user: user} do end end + describe "DELETE /api/v1/pleroma/chats/:id" do + setup do: oauth_access(["write:chats"]) + + test "it deletes a chat", %{conn: conn, user: user} do + other_user = insert(:user) + {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id) + + result = + conn + |> delete("/api/v1/pleroma/chats/#{chat.id}") + |> json_response_and_validate_schema(200) + + assert result["id"] == to_string(chat.id) + + refute Chat.get_by_id(chat.id) + end + end + describe "GET /api/v1/pleroma/chats" do setup do: oauth_access(["read:chats"]) From 9c581a31a2f3a45e58725c241d92c2582b659668 Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 24 Sep 2020 13:34:34 +0200 Subject: [PATCH 2/4] Docs: Add chat deletion. --- docs/API/chats.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/API/chats.md b/docs/API/chats.md index aa61196704..ea65ec7a31 100644 --- a/docs/API/chats.md +++ b/docs/API/chats.md @@ -211,6 +211,14 @@ Returned data: } ``` +### Deleting a chat + +Deleting a chat works like this: + +`DELETE /api/v1/pleroma/chats/:chat_id` + +Returned data is the deleted chat. + ### Deleting a chat message Deleting a chat message for given Chat id works like this: From c13c924ccb877d91068982f3a0ca9690c948a29c Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 24 Sep 2020 13:35:44 +0200 Subject: [PATCH 3/4] Changelog: Add info about chat deletion --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f5d01af34..47aa8e56bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Minimum lifetime for ephmeral activities changed to 10 minutes and made configurable (`:min_lifetime` option). ### Added +- Pleroma API: Add chat deletion. - Media preview proxy (requires media proxy be enabled; see `:media_preview_proxy` config for more details). - Pleroma API: Importing the mutes users from CSV files. - Experimental websocket-based federation between Pleroma instances. From 65eaada53db93c37f8d175fca12325fcdc346cfb Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 23 Dec 2021 18:43:09 -0600 Subject: [PATCH 4/4] Chat deletion: fix missing `ChatView` alias --- lib/pleroma/web/pleroma_api/controllers/chat_controller.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex index 7d634a154e..2cfc9a89e5 100644 --- a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex @@ -15,6 +15,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do alias Pleroma.User alias Pleroma.Web.CommonAPI alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView + alias Pleroma.Web.PleromaAPI.ChatView alias Pleroma.Web.Plugs.OAuthScopesPlug import Ecto.Query