Chat Moderation: use explicit `sender` and `recipient` fields
authorAlex Gleason <alex@alexgleason.me>
Thu, 10 Sep 2020 06:44:32 +0000 (01:44 -0500)
committerAlex Gleason <alex@alexgleason.me>
Fri, 11 Sep 2020 19:11:00 +0000 (14:11 -0500)
docs/API/admin_api.md
lib/pleroma/web/admin_api/controllers/admin_api_controller.ex
lib/pleroma/web/admin_api/controllers/chat_controller.ex
lib/pleroma/web/admin_api/views/chat_view.ex [new file with mode: 0644]
test/web/admin_api/controllers/chat_controller_test.exs

index 7bdbd17aa4a769e31fe7547c738e3de7d940be77..eadb455ee2dd240574bdbe83c2963340551ae610 100644 (file)
@@ -1346,7 +1346,12 @@ Loads json generated from `config/descriptions.exs`.
 ```json
 [
    {
-      "account": {
+      "sender": {
+        "id": "someflakeid",
+        "username": "somenick",
+        ...
+      },
+      "receiver": {
         "id": "someflakeid",
         "username": "somenick",
         ...
@@ -1369,7 +1374,12 @@ Loads json generated from `config/descriptions.exs`.
 
 ```json
 {
-  "account": {
+  "sender": {
+    "id": "someflakeid",
+    "username": "somenick",
+    ...
+  },
+  "receiver": {
     "id": "someflakeid",
     "username": "somenick",
     ...
index fccdbabb4cf1ed2f7816810baebecbaed02e895d..d5713c3dd968265b4bd2ba30b90274c00281d29f 100644 (file)
@@ -21,11 +21,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   alias Pleroma.Web.AdminAPI.ModerationLogView
   alias Pleroma.Web.AdminAPI.Search
   alias Pleroma.Web.Endpoint
-  alias Pleroma.Web.PleromaAPI
   alias Pleroma.Web.Router
 
-  require Logger
-
   @users_page_size 50
 
   plug(
@@ -270,7 +267,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
         |> Pleroma.Repo.all()
 
       conn
-      |> put_view(PleromaAPI.ChatView)
+      |> put_view(AdminAPI.ChatView)
       |> render("index.json", chats: chats)
     else
       _ -> {:error, :not_found}
index 61d45b970cd5c6629d43932125a8481d1a428626..967600d69acaef52748dbc3460c7b822090c4fdc 100644 (file)
@@ -11,9 +11,9 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
   alias Pleroma.ModerationLog
   alias Pleroma.Pagination
   alias Pleroma.Plugs.OAuthScopesPlug
+  alias Pleroma.Web.AdminAPI
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
-  alias Pleroma.Web.PleromaAPI.ChatView
 
   require Logger
 
@@ -78,7 +78,7 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
   def show(conn, %{id: id}) do
     with %Chat{} = chat <- Chat.get_by_id(id) do
       conn
-      |> put_view(ChatView)
+      |> put_view(AdminAPI.ChatView)
       |> render("show.json", chat: chat)
     end
   end
diff --git a/lib/pleroma/web/admin_api/views/chat_view.ex b/lib/pleroma/web/admin_api/views/chat_view.ex
new file mode 100644 (file)
index 0000000..847df14
--- /dev/null
@@ -0,0 +1,30 @@
+# 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.ChatView do
+  use Pleroma.Web, :view
+
+  alias Pleroma.Chat
+  alias Pleroma.User
+  alias Pleroma.Web.MastodonAPI
+  alias Pleroma.Web.PleromaAPI
+
+  def render("index.json", %{chats: chats} = opts) do
+    render_many(chats, __MODULE__, "show.json", Map.delete(opts, :chats))
+  end
+
+  def render("show.json", %{chat: %Chat{user_id: user_id}} = opts) do
+    user = User.get_by_id(user_id)
+    sender = MastodonAPI.AccountView.render("show.json", user: user, skip_visibility_check: true)
+
+    serialized_chat = PleromaAPI.ChatView.render("show.json", opts)
+
+    serialized_chat
+    |> Map.put(:sender, sender)
+    |> Map.put(:receiver, serialized_chat[:account])
+    |> Map.delete(:account)
+  end
+
+  def render(view, opts), do: PleromaAPI.ChatView.render(view, opts)
+end
index bca9d440d764dfdffa2024b3b38d3f1645a6e6ef..840f18aa2abcbbe75c7367040330745bd130f7df 100644 (file)
@@ -123,6 +123,9 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
         |> json_response_and_validate_schema(200)
 
       assert result["id"] == to_string(chat.id)
+      assert %{} = result["sender"]
+      assert %{} = result["receiver"]
+      refute result["account"]
     end
   end
 end