Admin API: list all statuses from a given instance
authorMaxim Filippov <colixer@gmail.com>
Thu, 14 Nov 2019 14:44:07 +0000 (23:44 +0900)
committerMaxim Filippov <colixer@gmail.com>
Thu, 14 Nov 2019 14:44:07 +0000 (23:44 +0900)
CHANGELOG.md
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/router.ex
test/web/admin_api/admin_api_controller_test.exs

index 4ec084dbd16975d5020d88957777e059d6077e89..66644efc9c41989facfacdbd3ba490938c2c6983 100644 (file)
@@ -60,6 +60,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Mastodon API: Add `/api/v1/markers` for managing timeline read markers
 - Mastodon API: Add the `recipients` parameter to `GET /api/v1/conversations`
 - Configuration: `feed` option for user atom feed.
+- Admin API: Add `/api/pleroma/admin/instances/:instance/statuses` - lists all statuses from a given instance
 </details>
 
 ### Fixed
index 65dd251f3ea3ddb62ab6b5fa2460e41db288e162..8b5bd83e27a35625cf2f320310fe2d42c8ecefee 100644 (file)
@@ -708,6 +708,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> Enum.reverse()
   end
 
+  def fetch_instance_activities(params) do
+    params =
+      params
+      |> Map.put("type", ["Create", "Announce"])
+      |> Map.put("instance", params["instance"])
+      |> Map.put("whole_db", true)
+
+    fetch_activities([Pleroma.Constants.as_public()], params, :offset)
+    |> Enum.reverse()
+  end
+
   defp user_activities_recipients(%{"godmode" => true}) do
     []
   end
@@ -935,6 +946,20 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   defp restrict_muted_reblogs(query, _), do: query
 
+  defp restrict_instance(query, %{"instance" => instance}) do
+    users =
+      from(
+        u in User,
+        select: u.ap_id,
+        where: fragment("? LIKE ?", u.nickname, ^"%@#{instance}")
+      )
+      |> Repo.all()
+
+    from(activity in query, where: activity.actor in ^users)
+  end
+
+  defp restrict_instance(query, _), do: query
+
   defp exclude_poll_votes(query, %{"include_poll_votes" => true}), do: query
 
   defp exclude_poll_votes(query, _) do
@@ -1015,6 +1040,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> restrict_reblogs(opts)
     |> restrict_pinned(opts)
     |> restrict_muted_reblogs(opts)
+    |> restrict_instance(opts)
     |> Activity.restrict_deactivated_users()
     |> exclude_poll_votes(opts)
     |> exclude_visibility(opts)
index 30fc017558e2e4d39182e96a11246f365884ac6b..2f43f018b21c47a8848fc2e3e7ae6beb72290881 100644 (file)
@@ -226,6 +226,21 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     end
   end
 
+  def list_instance_statuses(conn, %{"instance" => instance} = params) do
+    {page, page_size} = page_params(params)
+
+    activities =
+      ActivityPub.fetch_instance_activities(%{
+        "instance" => instance,
+        "limit" => page_size,
+        "offset" => (page - 1) * page_size
+      })
+
+    conn
+    |> put_view(StatusView)
+    |> render("index.json", %{activities: activities, as: :activity})
+  end
+
   def list_user_statuses(conn, %{"nickname" => nickname} = params) do
     godmode = params["godmode"] == "true" || params["godmode"] == true
 
index 8fb4aec13efab07ad5f7ef3da4da368469d75481..1e2982b678dc136c64b668bca974b28d472e530f 100644 (file)
@@ -177,6 +177,8 @@ defmodule Pleroma.Web.Router do
     get("/users/:nickname", AdminAPIController, :user_show)
     get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses)
 
+    get("/instances/:instance/statuses", AdminAPIController, :list_instance_statuses)
+
     get("/reports", AdminAPIController, :list_reports)
     get("/reports/:id", AdminAPIController, :report_show)
     put("/reports/:id", AdminAPIController, :report_update_state)
index 2a9e4f5a01783a774f20573015702cb2078d18b3..5958e646258bbd37e0b29076acadbc17b4368cc5 100644 (file)
@@ -2640,6 +2640,43 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                "@#{admin.nickname} unfollowed relay: http://mastodon.example.org/users/admin"
     end
   end
+
+  describe "instances" do
+    test "GET /instances/:instance/statuses" do
+      admin = insert(:user, is_admin: true)
+      user = insert(:user, local: false, nickname: "archaeme@archae.me")
+      user2 = insert(:user, local: false, nickname: "test@test.com")
+      insert_pair(:note_activity, user: user)
+      insert(:note_activity, user: user2)
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> get("/api/pleroma/admin/instances/archae.me/statuses")
+
+      response = json_response(conn, 200)
+
+      assert length(response) == 2
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> get("/api/pleroma/admin/instances/test.com/statuses")
+
+      response = json_response(conn, 200)
+
+      assert length(response) == 1
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> get("/api/pleroma/admin/instances/nonexistent.com/statuses")
+
+      response = json_response(conn, 200)
+
+      assert length(response) == 0
+    end
+  end
 end
 
 # Needed for testing