Admin API: `GET /api/pleroma/admin/statuses` - list all statuses (accepts `godmode...
authoreugenijm <eugenijm@protonmail.com>
Mon, 10 Feb 2020 11:32:38 +0000 (14:32 +0300)
committereugenijm <eugenijm@protonmail.com>
Wed, 26 Feb 2020 17:21:38 +0000 (20:21 +0300)
CHANGELOG.md
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/admin_api/views/status_view.ex
lib/pleroma/web/router.ex
test/web/admin_api/admin_api_controller_test.exs

index 08bb7e1c78aec560582b0a734d6ccff38c11908e..d87cec4148dda37f4df30490b43e657cc5726ac8 100644 (file)
@@ -121,6 +121,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Pleroma API: Add reactions for a single emoji.
 - ActivityPub: `[:activitypub, :note_replies_output_limit]` setting sets the number of note self-replies to output on outgoing federation.
 - Admin API: `GET /api/pleroma/admin/stats` to get status count by visibility scope
+- Admin API: `GET /api/pleroma/admin/statuses` - list all statuses (accepts `godmode` and `local_only`)
 </details>
 
 ### Fixed
index 5c436941ae7aa8f6eea72cdb42256298bca69bfb..12695b3f9218725701fe689eb00f9b2662646183 100644 (file)
@@ -770,13 +770,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> Enum.reverse()
   end
 
-  def fetch_instance_activities(params) do
+  def fetch_statuses(reading_user, params) do
     params =
       params
       |> Map.put("type", ["Create", "Announce"])
-      |> Map.put("instance", params["instance"])
 
-    fetch_activities([Pleroma.Constants.as_public()], params, :offset)
+    recipients =
+      user_activities_recipients(%{
+        "godmode" => params["godmode"],
+        "reading_user" => reading_user
+      })
+
+    fetch_activities(recipients, params, :offset)
     |> Enum.reverse()
   end
 
index 816b8938c8f7e602706e50a37506c8d6f4f150ff..9cb63bc2ea5f1c5613452ce3cde03da899fe9f28 100644 (file)
@@ -247,7 +247,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     {page, page_size} = page_params(params)
 
     activities =
-      ActivityPub.fetch_instance_activities(%{
+      ActivityPub.fetch_statuses(nil, %{
         "instance" => instance,
         "limit" => page_size,
         "offset" => (page - 1) * page_size
@@ -741,6 +741,24 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     end
   end
 
+  def list_statuses(%{assigns: %{user: admin}} = conn, params) do
+    godmode = params["godmode"] == "true" || params["godmode"] == true
+    local_only = params["local_only"] == "true" || params["local_only"] == true
+    {page, page_size} = page_params(params)
+
+    activities =
+      ActivityPub.fetch_statuses(admin, %{
+        "godmode" => godmode,
+        "local_only" => local_only,
+        "limit" => page_size,
+        "offset" => (page - 1) * page_size
+      })
+
+    conn
+    |> put_view(Pleroma.Web.AdminAPI.StatusView)
+    |> render("index.json", %{activities: activities, as: :activity})
+  end
+
   def status_update(%{assigns: %{user: admin}} = conn, %{"id" => id} = params) do
     with {:ok, activity} <- CommonAPI.update_activity_scope(id, params) do
       {:ok, sensitive} = Ecto.Type.cast(:boolean, params["sensitive"])
index 6f2b2b09ccd69c6fb168f396247d52c4be74fabe..8ae8a7afe41674521f063b2ba33a5da2149d1d12 100644 (file)
@@ -10,7 +10,7 @@ defmodule Pleroma.Web.AdminAPI.StatusView do
   alias Pleroma.User
 
   def render("index.json", opts) do
-    render_many(opts.activities, __MODULE__, "show.json", opts)
+    safe_render_many(opts.activities, __MODULE__, "show.json", opts)
   end
 
   def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
index c2ffb025a232a4f2b3e9e2b45775dfae08d7d948..103c638b4dcbb175fb118dd5fef333f48034c663 100644 (file)
@@ -192,6 +192,7 @@ defmodule Pleroma.Web.Router do
 
     put("/statuses/:id", AdminAPIController, :status_update)
     delete("/statuses/:id", AdminAPIController, :status_delete)
+    get("/statuses", AdminAPIController, :list_statuses)
 
     get("/config", AdminAPIController, :config_show)
     post("/config", AdminAPIController, :config_update)
index 0b79e4c5c116c9ba1d0a6a2df1d8a03bbeab0dde..faa5843903c5d8f90730a803dae220010e1de37c 100644 (file)
@@ -3065,6 +3065,52 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     end
   end
 
+  describe "GET /api/pleroma/admin/statuses" do
+    test "returns all public, unlisted, and direct statuses", %{conn: conn, admin: admin} do
+      blocked = insert(:user)
+      user = insert(:user)
+      User.block(admin, blocked)
+
+      {:ok, _} =
+        CommonAPI.post(user, %{"status" => "@#{admin.nickname}", "visibility" => "direct"})
+
+      {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
+      {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
+      {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
+      {:ok, _} = CommonAPI.post(blocked, %{"status" => ".", "visibility" => "public"})
+
+      response =
+        conn
+        |> get("/api/pleroma/admin/statuses")
+        |> json_response(200)
+
+      refute "private" in Enum.map(response, & &1["visibility"])
+      assert length(response) == 4
+    end
+
+    test "returns only local statuses with local_only on", %{conn: conn} do
+      user = insert(:user)
+      remote_user = insert(:user, local: false, nickname: "archaeme@archae.me")
+      insert(:note_activity, user: user, local: true)
+      insert(:note_activity, user: remote_user, local: false)
+
+      response =
+        conn
+        |> get("/api/pleroma/admin/statuses?local_only=true")
+        |> json_response(200)
+
+      assert length(response) == 1
+    end
+
+    test "returns private statuses with godmode on", %{conn: conn} do
+      user = insert(:user)
+      {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
+      {:ok, _} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
+      conn = get(conn, "/api/pleroma/admin/statuses?godmode=true")
+      assert json_response(conn, 200) |> length() == 2
+    end
+  end
+
   describe "GET /api/pleroma/admin/users/:nickname/statuses" do
     setup do
       user = insert(:user)