Admin API: Reports, grouped by status
authorMaxim Filippov <colixer@gmail.com>
Mon, 7 Oct 2019 12:01:18 +0000 (15:01 +0300)
committerMaxim Filippov <colixer@gmail.com>
Mon, 7 Oct 2019 12:39:07 +0000 (15:39 +0300)
CHANGELOG.md
docs/api/admin_api.md
lib/pleroma/activity.ex
lib/pleroma/web/activity_pub/utils.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/admin_api/views/report_view.ex
lib/pleroma/web/router.ex
test/web/admin_api/admin_api_controller_test.exs

index d7afed7831e1bd841b2479e682f35ea493e30f8b..7956a6527fa958715d24cc2c8edda8e11bf74d32 100644 (file)
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Pleroma API: `GET /api/v1/pleroma/accounts/:id/scrobbles` to get a list of recently scrobbled items
 - Pleroma API: `POST /api/v1/pleroma/scrobble` to scrobble a media item
 - Mastodon API: Add `upload_limit`, `avatar_upload_limit`, `background_upload_limit`, and `banner_upload_limit` to `/api/v1/instance`
+- Admin API: Add ability to fetch reports, grouped by status `GET /api/pleroma/admin/grouped_reports`
 
 ### Changed
 - **Breaking:** Elixir >=1.8 is now required (was >= 1.7)
index 045686bf4c1c9d39f314493b14968f657ba605ef..e8232225c6f969428f35567eb267c4523f17a447 100644 (file)
@@ -468,18 +468,32 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
         }
       ]
     }
-  ],
-  "totalGroupedReports": 1,
-  "groupedReports": [
+  ]
+}
+```
+
+## `GET /api/pleroma/admin/grouped_reports`
+
+### Get a list of reports, grouped by status
+
+- Params: none
+- On success: JSON, returns a list of reports, where:
+  - `date`: date of the latest report
+  - `account`: the user who has been reported (see `/api/pleroma/admin/reports` for reference)
+  - `status`: reported status (see `/api/pleroma/admin/reports` for reference)
+  - `actors`: users who had reported this status (see `/api/pleroma/admin/reports` for reference)
+  - `reports`: reports (see `/api/pleroma/admin/reports` for reference)
+
+```json
+  "reports": [
     {
-      "date": "2019-01-01", // date of the latest report
-      "account": { ... }, // author of the reported status
-      "status": { ... }, // reported status
-      "actors": [{ ... }, { ... }], // accounts that sent reports on the status
+      "date": "2019-10-07T12:31:39.615149Z",
+      "account": { ... },
+      "status": { ... },
+      "actors": [{ ... }, { ... }],
       "reports": [{ ... }]
     }
   ]
-}
 ```
 
 ## `GET /api/pleroma/admin/reports/:id`
index c1065611bf31d9eccc5e8eba005ac8cc210c55e4..daf0ed89f7150504d5903f64b9c0716eae152c03 100644 (file)
@@ -41,6 +41,9 @@ defmodule Pleroma.Activity do
     field(:actor, :string)
     field(:recipients, {:array, :string}, default: [])
     field(:thread_muted?, :boolean, virtual: true)
+
+    # This is a fake relation, do not use outside of with_preloaded_user_actor/with_joined_user_actor
+    has_one(:user_actor, User, on_delete: :nothing, foreign_key: :id)
     # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
     has_one(:bookmark, Bookmark)
     has_many(:notifications, Notification, on_delete: :delete_all)
@@ -86,6 +89,24 @@ defmodule Pleroma.Activity do
     |> preload([activity, object: object], object: object)
   end
 
+  def with_joined_user_actor(query, join_type \\ :inner) do
+    join(query, join_type, [activity], u in User,
+      on:
+        fragment(
+          "? = ?->>'actor'",
+          u.ap_id,
+          activity.data
+        ),
+      as: :user_actor
+    )
+  end
+
+  def with_preloaded_user_actor(query, join_type \\ :inner) do
+    query
+    |> with_joined_user_actor(join_type)
+    |> preload([activity, user_actor: user_actor], user_actor: user_actor)
+  end
+
   def with_preloaded_bookmark(query, %User{} = user) do
     from([a] in query,
       left_join: b in Bookmark,
index 8249573142a2d0c779d8fd23e11a129328d0a85d..74eb994abc6a4176b303843750d75bdf9617ce99 100644 (file)
@@ -6,11 +6,13 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   alias Ecto.Changeset
   alias Ecto.UUID
   alias Pleroma.Activity
+  alias Pleroma.Activity.Queries
   alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.Repo
   alias Pleroma.User
   alias Pleroma.Web
+  alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.ActivityPub.Visibility
   alias Pleroma.Web.Endpoint
   alias Pleroma.Web.Router.Helpers
@@ -664,6 +666,93 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   #### Report-related helpers
 
+  def get_reports(params, page, page_size) do
+    params =
+      params
+      |> Map.put("type", "Flag")
+      |> Map.put("skip_preload", true)
+      |> Map.put("total", true)
+      |> Map.put("limit", page_size)
+      |> Map.put("offset", (page - 1) * page_size)
+
+    ActivityPub.fetch_activities([], params, :offset)
+  end
+
+  @spec get_reports_grouped_by_status() :: %{
+          required(:groups) => [
+            %{
+              required(:date) => String.t(),
+              required(:account) => %User{},
+              required(:status) => %Activity{},
+              required(:actors) => [%User{}],
+              required(:reports) => [%Activity{}]
+            }
+          ],
+          required(:total) => integer
+        }
+  def get_reports_grouped_by_status do
+    paginated_activities = get_reported_status_ids()
+
+    groups =
+      paginated_activities
+      |> Enum.map(fn entry ->
+        status =
+          Activity
+          |> Queries.by_ap_id(entry[:activity_id])
+          |> Activity.with_preloaded_object(:left)
+          |> Activity.with_preloaded_user_actor()
+          |> Repo.one()
+
+        reports = get_reports_by_status_id(status.data["id"])
+
+        max_date =
+          Enum.max_by(reports, &Pleroma.Web.CommonAPI.Utils.to_masto_date(&1.data["published"])).data[
+            "published"
+          ]
+
+        actors = Enum.map(reports, & &1.user_actor)
+
+        %{
+          date: max_date,
+          account: status.user_actor,
+          status: status,
+          actors: actors,
+          reports: reports
+        }
+      end)
+
+    %{
+      groups: groups
+    }
+  end
+
+  def get_reports_by_status_id(status_id) do
+    from(a in Activity,
+      where: fragment("(?)->>'type' = 'Flag'", a.data),
+      where: fragment("(?)->'object' \\? (?)", a.data, ^status_id)
+    )
+    |> Activity.with_preloaded_user_actor()
+    |> Repo.all()
+  end
+
+  @spec get_reported_status_ids() :: %{
+          required(:items) => [%Activity{}],
+          required(:total) => integer
+        }
+  def get_reported_status_ids do
+    from(a in Activity,
+      where: fragment("(?)->>'type' = 'Flag'", a.data),
+      select: %{
+        date: fragment("max(?->>'published') date", a.data),
+        activity_id:
+          fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity_id", a.data)
+      },
+      group_by: fragment("activity_id"),
+      order_by: fragment("date DESC")
+    )
+    |> Repo.all()
+  end
+
   def update_report_state(%Activity{} = activity, state) when state in @supported_report_states do
     new_data = Map.put(activity.data, "state", state)
 
index 0e8c9dac896075fe0d177310ef3705d80483ae75..463dd327aeaa4547c09327ef9641e995f58a65f9 100644 (file)
@@ -10,6 +10,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   alias Pleroma.UserInviteToken
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.ActivityPub.Relay
+  alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.Web.AdminAPI.AccountView
   alias Pleroma.Web.AdminAPI.Config
   alias Pleroma.Web.AdminAPI.ConfigView
@@ -455,19 +456,15 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   def list_reports(conn, params) do
     {page, page_size} = page_params(params)
 
-    params =
-      params
-      |> Map.put("type", "Flag")
-      |> Map.put("skip_preload", true)
-      |> Map.put("total", true)
-      |> Map.put("limit", page_size)
-      |> Map.put("offset", (page - 1) * page_size)
-
-    reports = ActivityPub.fetch_activities([], params, :offset)
+    conn
+    |> put_view(ReportView)
+    |> render("index.json", %{reports: Utils.get_reports(params, page, page_size)})
+  end
 
+  def list_grouped_reports(conn, _params) do
     conn
     |> put_view(ReportView)
-    |> render("index.json", %{reports: reports})
+    |> render("index_grouped.json", Utils.get_reports_grouped_by_status())
   end
 
   def report_show(conn, %{"id" => id}) do
index 101a74c630833ad17c123c355e32c9563097b1b8..ac25925dafd4442a12b394a9c67d5885827d81d3 100644 (file)
@@ -42,6 +42,26 @@ defmodule Pleroma.Web.AdminAPI.ReportView do
     }
   end
 
+  def render("index_grouped.json", %{groups: groups}) do
+    reports =
+      Enum.map(groups, fn group ->
+        %{
+          date: group[:date],
+          account: merge_account_views(group[:account]),
+          status: StatusView.render("show.json", %{activity: group[:status]}),
+          actors: Enum.map(group[:actors], &merge_account_views/1),
+          reports:
+            group[:reports]
+            |> Enum.map(&Report.extract_report_info(&1))
+            |> Enum.map(&render(__MODULE__, "show.json", &1))
+        }
+      end)
+
+    %{
+      reports: reports
+    }
+  end
+
   defp merge_account_views(%User{} = user) do
     Pleroma.Web.MastodonAPI.AccountView.render("show.json", %{user: user})
     |> Map.merge(Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}))
index 563b01dc5c69fab33456786e520f21c1ad9a9d5a..b895a7b7e6cb5989749f81d51c2453aeeca492e0 100644 (file)
@@ -193,6 +193,7 @@ defmodule Pleroma.Web.Router do
     get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses)
 
     get("/reports", AdminAPIController, :list_reports)
+    get("/grouped_reports", AdminAPIController, :list_grouped_reports)
     get("/reports/:id", AdminAPIController, :report_show)
     patch("/reports", AdminAPIController, :reports_update)
     post("/reports/:id/respond", AdminAPIController, :report_respond)
index cec3570ebfa1bd4253af035127dd759434de4cf4..daa0631db518ce3e80e2976675c23459b89fe6dc 100644 (file)
@@ -1461,7 +1461,74 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     end
   end
 
-  #
+  describe "GET /api/pleroma/admin/grouped_reports" do
+    setup %{conn: conn} do
+      admin = insert(:user, info: %{is_admin: true})
+      [reporter, target_user] = insert_pair(:user)
+
+      date1 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
+      date2 = (DateTime.to_unix(DateTime.utc_now()) + 2000) |> DateTime.from_unix!()
+      date3 = (DateTime.to_unix(DateTime.utc_now()) + 3000) |> DateTime.from_unix!()
+
+      first_status =
+        insert(:note_activity, user: target_user, data_attrs: %{"published" => date1})
+
+      second_status =
+        insert(:note_activity, user: target_user, data_attrs: %{"published" => date2})
+
+      third_status =
+        insert(:note_activity, user: target_user, data_attrs: %{"published" => date3})
+
+      %{
+        conn: assign(conn, :user, admin),
+        reporter: reporter,
+        target_user: target_user,
+        first_status: first_status,
+        second_status: second_status,
+        third_status: third_status
+      }
+    end
+
+    test "returns reports grouped by status", %{
+      conn: conn,
+      reporter: reporter,
+      target_user: target_user,
+      first_status: first_status,
+      second_status: second_status,
+      third_status: third_status
+    } do
+      {:ok, %{id: _}} =
+        CommonAPI.report(reporter, %{
+          "account_id" => target_user.id,
+          "status_ids" => [first_status.id, second_status.id, third_status.id]
+        })
+
+      {:ok, %{id: _}} =
+        CommonAPI.report(reporter, %{
+          "account_id" => target_user.id,
+          "status_ids" => [first_status.id, second_status.id]
+        })
+
+      {:ok, %{id: _}} =
+        CommonAPI.report(reporter, %{
+          "account_id" => target_user.id,
+          "status_ids" => [first_status.id]
+        })
+
+      response =
+        conn
+        |> get("/api/pleroma/admin/grouped_reports")
+        |> json_response(:ok)
+
+      assert length(response["reports"]) == 3
+      [third_group, second_group, first_group] = response["reports"]
+
+      assert length(third_group["reports"]) == 3
+      assert length(second_group["reports"]) == 2
+      assert length(first_group["reports"]) == 1
+    end
+  end
+
   describe "POST /api/pleroma/admin/reports/:id/respond" do
     setup %{conn: conn} do
       admin = insert(:user, info: %{is_admin: true})