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 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ChatOperation
def delete_message(%{assigns: %{user: user}} = conn, %{message_id: id}) do def delete_message(%{assigns: %{user: user}} = conn, %{
with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do 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(%{ ModerationLog.insert_log(%{
action: "chat_message_delete", action: "chat_message_delete",
actor: user, 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
end end

View File

@ -4,9 +4,8 @@
defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
alias OpenApiSpex.Operation alias OpenApiSpex.Operation
alias Pleroma.Web.ApiSpec.Schemas.ApiError
alias Pleroma.Web.ApiSpec.Schemas.Chat alias Pleroma.Web.ApiSpec.Schemas.Chat
alias Pleroma.Web.ApiSpec.Schemas.FlakeID alias Pleroma.Web.ApiSpec.Schemas.ChatMessage
import Pleroma.Web.ApiSpec.Helpers import Pleroma.Web.ApiSpec.Helpers
@ -19,13 +18,24 @@ def delete_message_operation do
%Operation{ %Operation{
tags: ["admin", "chat"], tags: ["admin", "chat"],
summary: "Delete an individual chat message", summary: "Delete an individual chat message",
operationId: "AdminAPI.ChatController.delete", operationId: "AdminAPI.ChatController.delete_message",
parameters: [id_param(), message_id_param()] ++ admin_api_params(), parameters: [
security: [%{"oAuth" => ["write:chats"]}], Operation.parameter(:id, :path, :string, "The ID of the Chat"),
Operation.parameter(:message_id, :path, :string, "The ID of the message")
],
responses: %{ responses: %{
200 => empty_object_response(), 200 =>
404 => Operation.response("Not Found", "application/json", ApiError) Operation.response(
"The deleted ChatMessage",
"application/json",
ChatMessage
)
},
security: [
%{
"oAuth" => ["write:chats"]
} }
]
} }
end end
@ -83,18 +93,4 @@ def show_operation do
] ]
} }
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 end

View File

@ -7,9 +7,10 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Activity
alias Pleroma.Chat alias Pleroma.Chat
alias Pleroma.Chat.MessageReference
alias Pleroma.Config alias Pleroma.Config
alias Pleroma.Object
alias Pleroma.ModerationLog alias Pleroma.ModerationLog
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -27,29 +28,33 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
end end
describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do
setup do test "it deletes a message from the chat", %{conn: conn, admin: admin} do
chat = insert(:chat) user = insert(:user)
message = insert(:chat_message_activity, chat: chat) recipient = insert(:user)
%{chat: chat, message: message}
end
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 conn
|> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{message.id}") |> put_req_header("content-type", "application/json")
|> json_response_and_validate_schema(:ok) |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
|> json_response_and_validate_schema(200)
refute Activity.get_by_id(message.id)
log_entry = Repo.one(ModerationLog) log_entry = Repo.one(ModerationLog)
assert ModerationLog.get_log_entry_message(log_entry) == assert ModerationLog.get_log_entry_message(log_entry) ==
"@#{admin.nickname} deleted chat message ##{message.id}" "@#{admin.nickname} deleted chat message ##{cm_ref.id}"
end
test "returns 404 when the chat message does not exist", %{conn: conn} do assert result["id"] == cm_ref.id
conn = delete(conn, "/api/pleroma/admin/chats/test/messages/test") refute MessageReference.get_by_id(cm_ref.id)
assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
end end
end end