AdminAPI: fix delete chat message

This commit is contained in:
Alex Gleason 2020-09-01 20:40:36 -05:00
parent 9dd0b23da4
commit 02d70228b5
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 56 additions and 43 deletions

View File

@ -33,15 +33,27 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
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

View File

@ -4,9 +4,8 @@
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
@ -19,13 +18,24 @@ def delete_message_operation do
%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
@ -83,18 +93,4 @@ def show_operation do
]
}
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

View File

@ -7,9 +7,10 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
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
@ -27,29 +28,33 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
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)
test "deletes chat message", %{conn: conn, chat: chat, message: message, admin: admin} do
{: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)
cm_ref = MessageReference.for_chat_and_object(chat, object)
result =
conn
|> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{message.id}")
|> json_response_and_validate_schema(:ok)
refute Activity.get_by_id(message.id)
|> 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
"@#{admin.nickname} deleted chat message ##{cm_ref.id}"
test "returns 404 when the chat message does not exist", %{conn: conn} do
conn = delete(conn, "/api/pleroma/admin/chats/test/messages/test")
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