Merge branch 'delete-chat' into 'develop'
Add ability to delete a chat See merge request soapbox-pub/rebased!225
This commit is contained in:
commit
f435f364d7
5 changed files with 66 additions and 1 deletions
|
@ -218,6 +218,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
|
||||||
|
|
||||||
Deleting a chat message for given Chat id works like this:
|
Deleting a chat message for given Chat id works like this:
|
||||||
|
|
|
@ -222,6 +222,30 @@ def post_chat_message_operation do
|
||||||
}
|
}
|
||||||
end
|
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
|
def delete_message_operation do
|
||||||
%Operation{
|
%Operation{
|
||||||
tags: ["Chats"],
|
tags: ["Chats"],
|
||||||
|
|
|
@ -15,6 +15,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
alias Pleroma.Web.CommonAPI
|
alias Pleroma.Web.CommonAPI
|
||||||
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
|
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
|
||||||
|
alias Pleroma.Web.PleromaAPI.ChatView
|
||||||
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
||||||
|
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
@ -29,7 +30,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|
||||||
:create,
|
:create,
|
||||||
:mark_as_read,
|
:mark_as_read,
|
||||||
:mark_message_as_read,
|
:mark_message_as_read,
|
||||||
:delete_message
|
:delete_message,
|
||||||
|
:delete
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -42,6 +44,18 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|
||||||
|
|
||||||
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
|
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, %{
|
def delete_message(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
|
||||||
message_id: message_id,
|
message_id: message_id,
|
||||||
id: chat_id
|
id: chat_id
|
||||||
|
|
|
@ -464,6 +464,7 @@ defmodule Pleroma.Web.Router do
|
||||||
post("/chats/by-account-id/:id", ChatController, :create)
|
post("/chats/by-account-id/:id", ChatController, :create)
|
||||||
get("/chats", ChatController, :index)
|
get("/chats", ChatController, :index)
|
||||||
get("/chats/:id", ChatController, :show)
|
get("/chats/:id", ChatController, :show)
|
||||||
|
delete("/chats/:id", ChatController, :delete)
|
||||||
get("/chats/:id/messages", ChatController, :messages)
|
get("/chats/:id/messages", ChatController, :messages)
|
||||||
post("/chats/:id/messages", ChatController, :post_chat_message)
|
post("/chats/:id/messages", ChatController, :post_chat_message)
|
||||||
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
|
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
|
||||||
|
|
|
@ -304,6 +304,24 @@ test "it returns a chat", %{conn: conn, user: user} do
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
||||||
for tested_endpoint <- ["/api/v1/pleroma/chats", "/api/v2/pleroma/chats"] do
|
for tested_endpoint <- ["/api/v1/pleroma/chats", "/api/v2/pleroma/chats"] do
|
||||||
describe "GET #{tested_endpoint}" do
|
describe "GET #{tested_endpoint}" do
|
||||||
setup do: oauth_access(["read:chats"])
|
setup do: oauth_access(["read:chats"])
|
||||||
|
|
Loading…
Reference in a new issue