Answer
Article
Audio
+ ChatMessage
Event
Note
Page
Question
- Video
Tombstone
+ Video
}
def validate_data(cng) do
cng
}
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",
defmodule Pleroma.Web.PleromaAPI.ChatController do
use Pleroma.Web, :controller
+ alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.Object
alias Pleroma.Pagination
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(
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,
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
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
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"])