defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ChatOperation
- def delete_message(%{assigns: %{user: user}} = conn, %{message_id: id}) do
- with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
+ def delete_message(%{assigns: %{user: user}} = conn, %{
+ message_id: message_id,
+ id: chat_id
+ }) do
+ with %MessageReference{object: %{data: %{"id" => object_ap_id}}} = cm_ref <-
+ MessageReference.get_by_id(message_id),
+ ^chat_id <- to_string(cm_ref.chat_id),
+ %Activity{id: activity_id} <- Activity.get_create_by_object_ap_id(object_ap_id),
+ {:ok, _} <- CommonAPI.delete(activity_id, user) do
ModerationLog.insert_log(%{
action: "chat_message_delete",
actor: user,
- subject_id: id
+ subject_id: message_id
})
- json(conn, %{})
+ conn
+ |> put_view(MessageReferenceView)
+ |> render("show.json", chat_message_reference: cm_ref)
+ else
+ _e ->
+ {:error, :could_not_delete}
end
end
defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
alias OpenApiSpex.Operation
- alias Pleroma.Web.ApiSpec.Schemas.ApiError
alias Pleroma.Web.ApiSpec.Schemas.Chat
- alias Pleroma.Web.ApiSpec.Schemas.FlakeID
+ alias Pleroma.Web.ApiSpec.Schemas.ChatMessage
import Pleroma.Web.ApiSpec.Helpers
%Operation{
tags: ["admin", "chat"],
summary: "Delete an individual chat message",
- operationId: "AdminAPI.ChatController.delete",
- parameters: [id_param(), message_id_param()] ++ admin_api_params(),
- security: [%{"oAuth" => ["write:chats"]}],
+ operationId: "AdminAPI.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 => empty_object_response(),
- 404 => Operation.response("Not Found", "application/json", ApiError)
- }
+ 200 =>
+ Operation.response(
+ "The deleted ChatMessage",
+ "application/json",
+ ChatMessage
+ )
+ },
+ security: [
+ %{
+ "oAuth" => ["write:chats"]
+ }
+ ]
}
end
]
}
end
-
- def id_param do
- Operation.parameter(:id, :path, FlakeID, "Chat ID",
- example: "9umDrYheeY451cQnEe",
- required: true
- )
- end
-
- def message_id_param do
- Operation.parameter(:message_id, :path, FlakeID, "Chat message ID",
- example: "9umDrYheeY451cQnEe",
- required: true
- )
- end
end
import Pleroma.Factory
- alias Pleroma.Activity
alias Pleroma.Chat
+ alias Pleroma.Chat.MessageReference
alias Pleroma.Config
+ alias Pleroma.Object
alias Pleroma.ModerationLog
alias Pleroma.Repo
alias Pleroma.Web.CommonAPI
end
describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do
- setup do
- chat = insert(:chat)
- message = insert(:chat_message_activity, chat: chat)
- %{chat: chat, message: message}
- end
+ test "it deletes a message from the chat", %{conn: conn, admin: admin} do
+ user = insert(:user)
+ 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)
- test "deletes chat message", %{conn: conn, chat: chat, message: message, admin: admin} do
- conn
- |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{message.id}")
- |> json_response_and_validate_schema(:ok)
+ cm_ref = MessageReference.for_chat_and_object(chat, object)
- refute Activity.get_by_id(message.id)
+ result =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
+ |> json_response_and_validate_schema(200)
log_entry = Repo.one(ModerationLog)
assert ModerationLog.get_log_entry_message(log_entry) ==
- "@#{admin.nickname} deleted chat message ##{message.id}"
- end
-
- test "returns 404 when the chat message does not exist", %{conn: conn} do
- conn = delete(conn, "/api/pleroma/admin/chats/test/messages/test")
+ "@#{admin.nickname} deleted chat message ##{cm_ref.id}"
- assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
+ assert result["id"] == cm_ref.id
+ refute MessageReference.get_by_id(cm_ref.id)
+ assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
end
end