AdminAPI: list messages in a chat
authorAlex Gleason <alex@alexgleason.me>
Wed, 2 Sep 2020 00:39:34 +0000 (19:39 -0500)
committerAlex Gleason <alex@alexgleason.me>
Fri, 11 Sep 2020 19:10:19 +0000 (14:10 -0500)
lib/pleroma/web/admin_api/controllers/chat_controller.ex
lib/pleroma/web/api_spec/operations/admin/chat_operation.ex
lib/pleroma/web/router.ex
test/web/admin_api/controllers/chat_controller_test.exs

index bcce824d26446e9ffa6e5f0ca13c4bc30332d5fb..b423188d756514fb0800c93970432bc7a42b2721 100644 (file)
@@ -6,14 +6,23 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
   use Pleroma.Web, :controller
 
   alias Pleroma.Activity
+  alias Pleroma.Chat
+  alias Pleroma.Chat.MessageReference
   alias Pleroma.ModerationLog
+  alias Pleroma.Pagination
   alias Pleroma.Plugs.OAuthScopesPlug
   alias Pleroma.Web.CommonAPI
+  alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
 
   require Logger
 
   plug(Pleroma.Web.ApiSpec.CastAndValidate)
 
+  plug(
+    OAuthScopesPlug,
+    %{scopes: ["read:chats"], admin: true} when action in [:messages]
+  )
+
   plug(
     OAuthScopesPlug,
     %{scopes: ["write:chats"], admin: true} when action in [:delete_message]
@@ -34,4 +43,22 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
       json(conn, %{})
     end
   end
+
+  def messages(conn, %{id: id} = params) do
+    with %Chat{} = chat <- Chat.get_by_id(id) do
+      cm_refs =
+        chat
+        |> MessageReference.for_chat_query()
+        |> Pagination.fetch_paginated(params)
+
+      conn
+      |> put_view(MessageReferenceView)
+      |> render("index.json", chat_message_references: cm_refs)
+    else
+      _ ->
+        conn
+        |> put_status(:not_found)
+        |> json(%{error: "not found"})
+    end
+  end
 end
index 7045fd7cec25a958fc7146f084742267292b188c..a382bd35aba876d1a5cb832436e5d0639217ed0a 100644 (file)
@@ -16,7 +16,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
 
   def delete_message_operation do
     %Operation{
-      tags: ["Admin", "Chats"],
+      tags: ["admin", "chat"],
       summary: "Delete an individual chat message",
       operationId: "AdminAPI.ChatController.delete",
       parameters: [id_param(), message_id_param()] ++ admin_api_params(),
@@ -28,6 +28,30 @@ defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do
     }
   end
 
+  def messages_operation do
+    %Operation{
+      tags: ["admin", "chat"],
+      summary: "Get the most recent messages of the chat",
+      operationId: "AdminAPI.ChatController.messages",
+      parameters:
+        [Operation.parameter(:id, :path, :string, "The ID of the Chat")] ++
+          pagination_params(),
+      responses: %{
+        200 =>
+          Operation.response(
+            "The messages in the chat",
+            "application/json",
+            Pleroma.Web.ApiSpec.ChatOperation.chat_messages_response()
+          )
+      },
+      security: [
+        %{
+          "oAuth" => ["read:chats"]
+        }
+      ]
+    }
+  end
+
   def id_param do
     Operation.parameter(:id, :path, FlakeID, "Chat ID",
       example: "9umDrYheeY451cQnEe",
index ad3282df45bcec157a362df6c1ef18248bf052eb..02836114a09d6c794355f6b1a98d3f38efb4c2b0 100644 (file)
@@ -217,7 +217,7 @@ defmodule Pleroma.Web.Router do
     post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
 
     # get("/chats/:id", ChatController, :show)
-    get("/chats/:id/messages", ChatController, :messages)
+    get("/chats/:id/messages", ChatController, :messages)
     delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
   end
 
index 4527437afdc7e9e6e71e2570eb42d826d0a183a0..f61e2a1fa01f4e6a79d927fa580bf725ecde355d 100644 (file)
@@ -8,9 +8,11 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
   import Pleroma.Factory
 
   alias Pleroma.Activity
+  alias Pleroma.Chat
   alias Pleroma.Config
   alias Pleroma.ModerationLog
   alias Pleroma.Repo
+  alias Pleroma.Web.CommonAPI
 
   setup do
     admin = insert(:user, is_admin: true)
@@ -50,4 +52,56 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
       assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
     end
   end
+
+  describe "GET /api/pleroma/admin/chats/:id/messages" do
+    test "it paginates", %{conn: conn} do
+      user = insert(:user)
+      recipient = insert(:user)
+
+      Enum.each(1..30, fn _ ->
+        {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
+      end)
+
+      chat = Chat.get(user.id, recipient.ap_id)
+
+      result =
+        conn
+        |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 20
+
+      result =
+        conn
+        |> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 10
+    end
+
+    test "it returns the messages for a given chat", %{conn: conn} do
+      user = insert(:user)
+      other_user = insert(:user)
+      third_user = insert(:user)
+
+      {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
+      {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
+      {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
+      {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
+
+      chat = Chat.get(user.id, other_user.ap_id)
+
+      result =
+        conn
+        |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
+        |> json_response_and_validate_schema(200)
+
+      result
+      |> Enum.each(fn message ->
+        assert message["chat_id"] == chat.id |> to_string()
+      end)
+
+      assert length(result) == 3
+    end
+  end
 end