Admin API: Endpoint for fetching latest user's statuses
authorMaxim Filippov <colixer@gmail.com>
Sat, 13 Jul 2019 21:37:19 +0000 (00:37 +0300)
committerMaxim Filippov <colixer@gmail.com>
Sat, 13 Jul 2019 21:37:26 +0000 (00:37 +0300)
CHANGELOG.md
docs/api/admin_api.md
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/router.ex
test/support/factory.ex
test/web/admin_api/admin_api_controller_test.exs

index 942733ab6c53a17fe0892a1417d2ade9bf406761..86cbaeff7160acf1a69fe0e35b9c3b529edf1e6e 100644 (file)
@@ -30,6 +30,7 @@ Configuration: `federation_incoming_replies_max_depth` option
 - Added synchronization of following/followers counters for external users
 - Configuration: `enabled` option for `Pleroma.Emails.Mailer`, defaulting to `false`.
 - Mastodon API: Add support for categories for custom emojis by reusing the group feature. <https://github.com/tootsuite/mastodon/pull/11196>
+- Admin API: Endpoint for fetching latest user's statuses
 
 ### Changed
 - Configuration: Filter.AnonymizeFilename added ability to retain file extension with custom text
index c429da822860988db7e8bffdd518d0f729a4dc34..3880af21888c9eddff2bafb568000f06cc78b04e 100644 (file)
@@ -187,6 +187,18 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
   - On failure: `Not found`
   - On success: JSON of the user
 
+## `/api/pleroma/admin/users/:nickname_or_id/statuses`
+
+### Retrive user's latest statuses
+
+- Method: `GET`
+- Params:
+  - `nickname` or `id`
+  - *optional* `page_size`: number of statuses to return (default is `20`)
+- Response:
+  - On failure: `Not found`
+  - On success: JSON array of user's latest statuses
+
 ## `/api/pleroma/admin/relay`
 
 ### Follow a Relay
index 4a0bf482346782a5bd25010efe5b18bfb2edc949..64ad7e8e2522d5fabddcdfd61d9a75728bf710f0 100644 (file)
@@ -82,6 +82,22 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     end
   end
 
+  def list_user_statuses(conn, %{"nickname" => nickname} = params) do
+    with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
+      {_, page_size} = page_params(params)
+
+      activities =
+        ActivityPub.fetch_user_activities(user, nil, %{
+          "limit" => page_size
+        })
+
+      conn
+      |> json(StatusView.render("index.json", %{activities: activities, as: :activity}))
+    else
+      _ -> {:error, :not_found}
+    end
+  end
+
   def user_toggle_activation(conn, %{"nickname" => nickname}) do
     user = User.get_cached_by_nickname(nickname)
 
index d53fa8a350b56c1f9387af3d6a76d92c0462c1af..9315302c898bb03b62d63272d54743c22cd2b16c 100644 (file)
@@ -197,6 +197,7 @@ defmodule Pleroma.Web.Router do
 
     get("/users", AdminAPIController, :list_users)
     get("/users/:nickname", AdminAPIController, :user_show)
+    get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses)
 
     get("/reports", AdminAPIController, :list_reports)
     get("/reports/:id", AdminAPIController, :report_show)
index 531eb81e4cc553173cbbd877d48e63a8939a088f..807b34545d73ad3ffef82e8fb2f646b7aabf1492 100644 (file)
@@ -118,7 +118,10 @@ defmodule Pleroma.Factory do
   def note_activity_factory(attrs \\ %{}) do
     user = attrs[:user] || insert(:user)
     note = attrs[:note] || insert(:note, user: user)
+    published = attrs[:published] || DateTime.utc_now() |> DateTime.to_iso8601()
     attrs = Map.drop(attrs, [:user, :note])
+    require IEx
+    IEx.pry()
 
     data = %{
       "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
@@ -126,7 +129,7 @@ defmodule Pleroma.Factory do
       "actor" => note.data["actor"],
       "to" => note.data["to"],
       "object" => note.data["id"],
-      "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
+      "published" => published,
       "context" => note.data["context"]
     }
 
index 1b71cbff384835fb033489f71d90b41b452c82c8..9d4b3d74ba77a09bb93c42c1b1c882b851bae730 100644 (file)
@@ -1863,6 +1863,39 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
              }
     end
   end
+
+  describe "GET /api/pleroma/admin/users/:nickname/statuses" do
+    setup do
+      admin = insert(:user, info: %{is_admin: true})
+      user = insert(:user)
+
+      date1 = (DateTime.to_unix(DateTime.utc_now()) + 2000) |> DateTime.from_unix!()
+      date2 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
+      date3 = (DateTime.to_unix(DateTime.utc_now()) + 3000) |> DateTime.from_unix!()
+
+      insert(:note_activity, user: user, published: date1)
+      insert(:note_activity, user: user, published: date2)
+      insert(:note_activity, user: user, published: date3)
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+
+      {:ok, conn: conn, user: user}
+    end
+
+    test "renders user's statuses", %{conn: conn, user: user} do
+      conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses")
+
+      assert json_response(conn, 200) |> length() == 3
+    end
+
+    test "renders user's statuses with a limit", %{conn: conn, user: user} do
+      conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=2")
+
+      assert json_response(conn, 200) |> length() == 2
+    end
+  end
 end
 
 # Needed for testing