ChatController: Support deletion of chat messages.

This commit is contained in:
lain 2020-05-12 13:13:03 +02:00
parent f28ed36b4d
commit b5aa204eb8
5 changed files with 75 additions and 2 deletions

View file

@ -46,12 +46,13 @@ def add_deleted_activity_id(cng) do
Answer
Article
Audio
ChatMessage
Event
Note
Page
Question
Video
Tombstone
Video
}
def validate_data(cng) do
cng

View file

@ -166,6 +166,31 @@ def post_chat_message_operation 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",

View 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,

View 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

View 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 @@ test "it works with an attachment", %{conn: conn, user: user} 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"])