AdminAPI: delete a chat message
This commit is contained in:
parent
f88dc1937e
commit
b40a627ab0
6 changed files with 214 additions and 0 deletions
|
@ -320,6 +320,19 @@ defmodule Pleroma.ModerationLog do
|
||||||
|> insert_log_entry_with_message()
|
|> insert_log_entry_with_message()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec insert_log(%{actor: User, action: String.t(), subject_id: String.t()}) ::
|
||||||
|
{:ok, ModerationLog} | {:error, any}
|
||||||
|
def insert_log(%{actor: %User{} = actor, action: "chat_message_delete", subject_id: subject_id}) do
|
||||||
|
%ModerationLog{
|
||||||
|
data: %{
|
||||||
|
"actor" => %{"nickname" => actor.nickname},
|
||||||
|
"action" => "chat_message_delete",
|
||||||
|
"subject_id" => subject_id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|> insert_log_entry_with_message()
|
||||||
|
end
|
||||||
|
|
||||||
@spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
|
@spec insert_log_entry_with_message(ModerationLog) :: {:ok, ModerationLog} | {:error, any}
|
||||||
defp insert_log_entry_with_message(entry) do
|
defp insert_log_entry_with_message(entry) do
|
||||||
entry.data["message"]
|
entry.data["message"]
|
||||||
|
@ -627,6 +640,17 @@ defmodule Pleroma.ModerationLog do
|
||||||
"@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
|
"@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec get_log_entry_message(ModerationLog) :: String.t()
|
||||||
|
def get_log_entry_message(%ModerationLog{
|
||||||
|
data: %{
|
||||||
|
"actor" => %{"nickname" => actor_nickname},
|
||||||
|
"action" => "chat_message_delete",
|
||||||
|
"subject_id" => subject_id
|
||||||
|
}
|
||||||
|
}) do
|
||||||
|
"@#{actor_nickname} deleted chat message ##{subject_id}"
|
||||||
|
end
|
||||||
|
|
||||||
defp nicknames_to_string(nicknames) do
|
defp nicknames_to_string(nicknames) do
|
||||||
nicknames
|
nicknames
|
||||||
|> Enum.map(&"@#{&1}")
|
|> Enum.map(&"@#{&1}")
|
||||||
|
|
37
lib/pleroma/web/admin_api/controllers/chat_controller.ex
Normal file
37
lib/pleroma/web/admin_api/controllers/chat_controller.ex
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.AdminAPI.ChatController do
|
||||||
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
|
alias Pleroma.Activity
|
||||||
|
alias Pleroma.ModerationLog
|
||||||
|
alias Pleroma.Plugs.OAuthScopesPlug
|
||||||
|
alias Pleroma.Web.CommonAPI
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
||||||
|
|
||||||
|
plug(
|
||||||
|
OAuthScopesPlug,
|
||||||
|
%{scopes: ["write:chats"], admin: true} when action in [:delete_message]
|
||||||
|
)
|
||||||
|
|
||||||
|
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
||||||
|
|
||||||
|
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
|
||||||
|
ModerationLog.insert_log(%{
|
||||||
|
action: "chat_message_delete",
|
||||||
|
actor: user,
|
||||||
|
subject_id: id
|
||||||
|
})
|
||||||
|
|
||||||
|
json(conn, %{})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
44
lib/pleroma/web/api_spec/operations/admin/chat_operation.ex
Normal file
44
lib/pleroma/web/api_spec/operations/admin/chat_operation.ex
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
|
||||||
|
alias OpenApiSpex.Operation
|
||||||
|
alias Pleroma.Web.ApiSpec.Schemas.ApiError
|
||||||
|
alias Pleroma.Web.ApiSpec.Schemas.FlakeID
|
||||||
|
|
||||||
|
import Pleroma.Web.ApiSpec.Helpers
|
||||||
|
|
||||||
|
def open_api_operation(action) do
|
||||||
|
operation = String.to_existing_atom("#{action}_operation")
|
||||||
|
apply(__MODULE__, operation, [])
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_message_operation do
|
||||||
|
%Operation{
|
||||||
|
tags: ["Admin", "Chats"],
|
||||||
|
summary: "Delete an individual chat message",
|
||||||
|
operationId: "AdminAPI.ChatController.delete",
|
||||||
|
parameters: [id_param(), message_id_param()] ++ admin_api_params(),
|
||||||
|
security: [%{"oAuth" => ["write:chats"]}],
|
||||||
|
responses: %{
|
||||||
|
200 => empty_object_response(),
|
||||||
|
404 => Operation.response("Not Found", "application/json", ApiError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
|
|
@ -214,6 +214,8 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/media_proxy_caches", MediaProxyCacheController, :index)
|
get("/media_proxy_caches", MediaProxyCacheController, :index)
|
||||||
post("/media_proxy_caches/delete", MediaProxyCacheController, :delete)
|
post("/media_proxy_caches/delete", MediaProxyCacheController, :delete)
|
||||||
post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
|
post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
|
||||||
|
|
||||||
|
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api/pleroma/emoji", Pleroma.Web.PleromaAPI do
|
scope "/api/pleroma/emoji", Pleroma.Web.PleromaAPI do
|
||||||
|
|
|
@ -460,4 +460,58 @@ defmodule Pleroma.Factory do
|
||||||
phrase: "cofe"
|
phrase: "cofe"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def chat_factory(attrs \\ %{}) do
|
||||||
|
user = attrs[:user] || insert(:user)
|
||||||
|
recipient = attrs[:recipient] || insert(:user)
|
||||||
|
|
||||||
|
%Pleroma.Chat{
|
||||||
|
user_id: user.id,
|
||||||
|
recipient: recipient.ap_id
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def chat_message_factory(attrs \\ %{}) do
|
||||||
|
text = sequence(:text, &"This is :moominmamma: chat message #{&1}")
|
||||||
|
chat = attrs[:chat] || insert(:chat)
|
||||||
|
|
||||||
|
data = %{
|
||||||
|
"type" => "ChatMessage",
|
||||||
|
"content" => text,
|
||||||
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
|
||||||
|
"actor" => User.get_by_id(chat.user_id).ap_id,
|
||||||
|
"to" => [chat.recipient],
|
||||||
|
"published" => DateTime.utc_now() |> DateTime.to_iso8601()
|
||||||
|
}
|
||||||
|
|
||||||
|
%Pleroma.Object{
|
||||||
|
data: merge_attributes(data, Map.get(attrs, :data, %{}))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def chat_message_activity_factory(attrs \\ %{}) do
|
||||||
|
chat = attrs[:chat] || insert(:chat)
|
||||||
|
chat_message = attrs[:chat_message] || insert(:chat_message, chat: chat)
|
||||||
|
|
||||||
|
data_attrs = attrs[:data_attrs] || %{}
|
||||||
|
attrs = Map.drop(attrs, [:chat, :chat_message, :data_attrs])
|
||||||
|
|
||||||
|
data =
|
||||||
|
%{
|
||||||
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
|
||||||
|
"type" => "Create",
|
||||||
|
"actor" => chat_message.data["actor"],
|
||||||
|
"to" => chat_message.data["to"],
|
||||||
|
"object" => chat_message.data["id"],
|
||||||
|
"published" => DateTime.utc_now() |> DateTime.to_iso8601()
|
||||||
|
}
|
||||||
|
|> Map.merge(data_attrs)
|
||||||
|
|
||||||
|
%Pleroma.Activity{
|
||||||
|
data: data,
|
||||||
|
actor: data["actor"],
|
||||||
|
recipients: data["to"]
|
||||||
|
}
|
||||||
|
|> Map.merge(attrs)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
53
test/web/admin_api/controllers/chat_controller_test.exs
Normal file
53
test/web/admin_api/controllers/chat_controller_test.exs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
|
||||||
|
use Pleroma.Web.ConnCase
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
alias Pleroma.Activity
|
||||||
|
alias Pleroma.Config
|
||||||
|
alias Pleroma.ModerationLog
|
||||||
|
alias Pleroma.Repo
|
||||||
|
|
||||||
|
setup do
|
||||||
|
admin = insert(:user, is_admin: true)
|
||||||
|
token = insert(:oauth_admin_token, user: admin)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
build_conn()
|
||||||
|
|> assign(:user, admin)
|
||||||
|
|> assign(:token, token)
|
||||||
|
|
||||||
|
{:ok, %{admin: admin, token: token, conn: conn}}
|
||||||
|
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 "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)
|
||||||
|
|
||||||
|
refute Activity.get_by_id(message.id)
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue