AdminAPI: delete a chat message
authorAlex Gleason <alex@alexgleason.me>
Tue, 1 Sep 2020 00:56:05 +0000 (19:56 -0500)
committerAlex Gleason <alex@alexgleason.me>
Fri, 11 Sep 2020 19:09:58 +0000 (14:09 -0500)
lib/pleroma/moderation_log.ex
lib/pleroma/web/admin_api/controllers/chat_controller.ex [new file with mode: 0644]
lib/pleroma/web/api_spec/operations/admin/chat_operation.ex [new file with mode: 0644]
lib/pleroma/web/router.ex
test/support/factory.ex
test/web/admin_api/controllers/chat_controller_test.exs [new file with mode: 0644]

index 31c9afe2a293a0f46ae944eee0479aa51c5aac57..47036a6f624052174543b4bec9704f49c3d4c615 100644 (file)
@@ -320,6 +320,19 @@ defmodule Pleroma.ModerationLog do
     |> insert_log_entry_with_message()
   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}
   defp insert_log_entry_with_message(entry) do
     entry.data["message"]
@@ -627,6 +640,17 @@ defmodule Pleroma.ModerationLog do
     "@#{actor_nickname} updated users: #{users_to_nicknames_string(subjects)}"
   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
     nicknames
     |> Enum.map(&"@#{&1}")
diff --git a/lib/pleroma/web/admin_api/controllers/chat_controller.ex b/lib/pleroma/web/admin_api/controllers/chat_controller.ex
new file mode 100644 (file)
index 0000000..bcce824
--- /dev/null
@@ -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
diff --git a/lib/pleroma/web/api_spec/operations/admin/chat_operation.ex b/lib/pleroma/web/api_spec/operations/admin/chat_operation.ex
new file mode 100644 (file)
index 0000000..7045fd7
--- /dev/null
@@ -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
index c6433cc5325f3e923dc9150632c82ec5945d7d5a..e438768ed18ffb9a3d0a7f9a03fd591911db1e9e 100644 (file)
@@ -214,6 +214,8 @@ defmodule Pleroma.Web.Router do
     get("/media_proxy_caches", MediaProxyCacheController, :index)
     post("/media_proxy_caches/delete", MediaProxyCacheController, :delete)
     post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
+
+    delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
   end
 
   scope "/api/pleroma/emoji", Pleroma.Web.PleromaAPI do
index 486eda8da5676fc0b86dc39ce16a7b093ec32016..61ca4587ca46fb728216df4993edc7e2fb47705e 100644 (file)
@@ -460,4 +460,58 @@ defmodule Pleroma.Factory do
       phrase: "cofe"
     }
   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
diff --git a/test/web/admin_api/controllers/chat_controller_test.exs b/test/web/admin_api/controllers/chat_controller_test.exs
new file mode 100644 (file)
index 0000000..4527437
--- /dev/null
@@ -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