Merge branch 'feature/admin-api-render-whole-status' into 'develop'

Miscellaneous grouped reports fixes

Closes admin-fe#48 and admin-fe#51

See merge request pleroma/pleroma!2007
This commit is contained in:
feld 2019-12-05 13:34:34 +00:00
commit d0bd4348b3
7 changed files with 199 additions and 125 deletions

View File

@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Mastodon API: `pleroma.thread_muted` to the Status entity - Mastodon API: `pleroma.thread_muted` to the Status entity
- Mastodon API: Mark the direct conversation as read for the author when they send a new direct message - Mastodon API: Mark the direct conversation as read for the author when they send a new direct message
- Mastodon API, streaming: Add `pleroma.direct_conversation_id` to the `conversation` stream event payload. - Mastodon API, streaming: Add `pleroma.direct_conversation_id` to the `conversation` stream event payload.
- Admin API: Render whole status in grouped reports
</details> </details>
### Added ### Added
@ -83,6 +84,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Mastodon API: Fix private and direct statuses not being filtered out from the public timeline for an authenticated user (`GET /api/v1/timelines/public`) - Mastodon API: Fix private and direct statuses not being filtered out from the public timeline for an authenticated user (`GET /api/v1/timelines/public`)
- Mastodon API: Inability to get some local users by nickname in `/api/v1/accounts/:id_or_nickname` - Mastodon API: Inability to get some local users by nickname in `/api/v1/accounts/:id_or_nickname`
- AdminAPI: If some status received reports both in the "new" format and "old" format it was considered reports on two different statuses (in the context of grouped reports)
- Admin API: Error when trying to update reports in the "old" format - Admin API: Error when trying to update reports in the "old" format
</details> </details>

View File

@ -722,16 +722,22 @@ defmodule Pleroma.Web.ActivityPub.Utils do
act when is_binary(act) -> act act when is_binary(act) -> act
end end
activity = Activity.get_by_ap_id_with_object(id) case Activity.get_by_ap_id_with_object(id) do
actor = User.get_by_ap_id(activity.object.data["actor"]) %Activity{} = activity ->
%{ %{
"type" => "Note", "type" => "Note",
"id" => activity.data["id"], "id" => activity.data["id"],
"content" => activity.object.data["content"], "content" => activity.object.data["content"],
"published" => activity.object.data["published"], "published" => activity.object.data["published"],
"actor" => AccountView.render("show.json", %{user: actor}) "actor" =>
AccountView.render("show.json", %{
user: User.get_by_ap_id(activity.object.data["actor"])
})
} }
_ ->
%{"id" => id, "deleted" => true}
end
end end
defp build_flag_object(_), do: [] defp build_flag_object(_), do: []
@ -788,7 +794,52 @@ defmodule Pleroma.Web.ActivityPub.Utils do
ActivityPub.fetch_activities([], params, :offset) ActivityPub.fetch_activities([], params, :offset)
end end
@spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{ def parse_report_group(activity) do
reports = get_reports_by_status_id(activity["id"])
max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"]))
actors = Enum.map(reports, & &1.user_actor)
[%{data: %{"object" => [account_id | _]}} | _] = reports
account =
AccountView.render("show.json", %{
user: User.get_by_ap_id(account_id)
})
status = get_status_data(activity)
%{
date: max_date.data["published"],
account: account,
status: status,
actors: Enum.uniq(actors),
reports: reports
}
end
defp get_status_data(status) do
case status["deleted"] do
true ->
%{
"id" => status["id"],
"deleted" => true
}
_ ->
Activity.get_by_ap_id(status["id"])
end
end
def get_reports_by_status_id(ap_id) do
from(a in Activity,
where: fragment("(?)->>'type' = 'Flag'", a.data),
where: fragment("(?)->'object' @> ?", a.data, ^[%{id: ap_id}]),
or_where: fragment("(?)->'object' @> ?", a.data, ^[ap_id])
)
|> Activity.with_preloaded_user_actor()
|> Repo.all()
end
@spec get_reports_grouped_by_status([String.t()]) :: %{
required(:groups) => [ required(:groups) => [
%{ %{
required(:date) => String.t(), required(:date) => String.t(),
@ -797,20 +848,15 @@ defmodule Pleroma.Web.ActivityPub.Utils do
required(:actors) => [%User{}], required(:actors) => [%User{}],
required(:reports) => [%Activity{}] required(:reports) => [%Activity{}]
} }
], ]
required(:total) => integer
} }
def get_reports_grouped_by_status(groups) do def get_reports_grouped_by_status(activity_ids) do
parsed_groups = parsed_groups =
groups activity_ids
|> Enum.map(fn entry -> |> Enum.map(fn id ->
activity = id
case Jason.decode(entry.activity) do |> build_flag_object()
{:ok, activity} -> activity |> parse_report_group()
_ -> build_flag_object(entry.activity)
end
parse_report_group(activity)
end) end)
%{ %{
@ -818,33 +864,6 @@ defmodule Pleroma.Web.ActivityPub.Utils do
} }
end end
def parse_report_group(activity) do
reports = get_reports_by_status_id(activity["id"])
max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"]))
actors = Enum.map(reports, & &1.user_actor)
%{
date: max_date.data["published"],
account: activity["actor"],
status: %{
id: activity["id"],
content: activity["content"],
published: activity["published"]
},
actors: Enum.uniq(actors),
reports: reports
}
end
def get_reports_by_status_id(ap_id) do
from(a in Activity,
where: fragment("(?)->>'type' = 'Flag'", a.data),
where: fragment("(?)->'object' @> ?", a.data, ^[%{id: ap_id}])
)
|> Activity.with_preloaded_user_actor()
|> Repo.all()
end
@spec get_reported_activities() :: [ @spec get_reported_activities() :: [
%{ %{
required(:activity) => String.t(), required(:activity) => String.t(),
@ -852,17 +871,23 @@ defmodule Pleroma.Web.ActivityPub.Utils do
} }
] ]
def get_reported_activities do def get_reported_activities do
reported_activities_query =
from(a in Activity, from(a in Activity,
where: fragment("(?)->>'type' = 'Flag'", a.data), where: fragment("(?)->>'type' = 'Flag'", a.data),
select: %{ select: %{
date: fragment("max(?->>'published') date", a.data), activity: fragment("jsonb_array_elements((? #- '{object,0}')->'object')", a.data)
activity:
fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity", a.data)
}, },
group_by: fragment("activity"), group_by: fragment("activity")
order_by: fragment("date DESC") )
from(a in subquery(reported_activities_query),
distinct: true,
select: %{
id: fragment("COALESCE(?->>'id'::text, ? #>> '{}')", a.activity, a.activity)
}
) )
|> Repo.all() |> Repo.all()
|> Enum.map(& &1.id)
end end
def update_report_state(%Activity{} = activity, state) def update_report_state(%Activity{} = activity, state)

View File

@ -647,11 +647,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end end
def list_grouped_reports(conn, _params) do def list_grouped_reports(conn, _params) do
reports = Utils.get_reported_activities() statuses = Utils.get_reported_activities()
conn conn
|> put_view(ReportView) |> put_view(ReportView)
|> render("index_grouped.json", Utils.get_reports_grouped_by_status(reports)) |> render("index_grouped.json", Utils.get_reports_grouped_by_status(statuses))
end end
def report_show(conn, %{"id" => id}) do def report_show(conn, %{"id" => id}) do

View File

@ -4,6 +4,7 @@
defmodule Pleroma.Web.AdminAPI.ReportView do defmodule Pleroma.Web.AdminAPI.ReportView do
use Pleroma.Web, :view use Pleroma.Web, :view
alias Pleroma.Activity
alias Pleroma.HTML alias Pleroma.HTML
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.AdminAPI.Report alias Pleroma.Web.AdminAPI.Report
@ -45,10 +46,16 @@ defmodule Pleroma.Web.AdminAPI.ReportView do
def render("index_grouped.json", %{groups: groups}) do def render("index_grouped.json", %{groups: groups}) do
reports = reports =
Enum.map(groups, fn group -> Enum.map(groups, fn group ->
status =
case group.status do
%Activity{} = activity -> StatusView.render("show.json", %{activity: activity})
_ -> group.status
end
%{ %{
date: group[:date], date: group[:date],
account: group[:account], account: group[:account],
status: group[:status], status: Map.put_new(status, "deleted", false),
actors: Enum.map(group[:actors], &merge_account_views/1), actors: Enum.map(group[:actors], &merge_account_views/1),
reports: reports:
group[:reports] group[:reports]

View File

@ -75,6 +75,23 @@ defmodule Pleroma.Tests.Helpers do
|> Poison.decode!() |> Poison.decode!()
end end
def stringify_keys(nil), do: nil
def stringify_keys(key) when key in [true, false], do: key
def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
def stringify_keys(map) when is_map(map) do
map
|> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
|> Enum.into(%{})
end
def stringify_keys([head | rest] = list) when is_list(list) do
[stringify_keys(head) | stringify_keys(rest)]
end
def stringify_keys(key), do: key
defmacro guards_config(config_path) do defmacro guards_config(config_path) do
quote do quote do
initial_setting = Pleroma.Config.get(config_path) initial_setting = Pleroma.Config.get(config_path)

View File

@ -636,47 +636,4 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
assert updated_object.data["announcement_count"] == 1 assert updated_object.data["announcement_count"] == 1
end end
end end
describe "get_reports_grouped_by_status/1" do
setup do
[reporter, target_user] = insert_pair(:user)
first_status = insert(:note_activity, user: target_user)
second_status = insert(:note_activity, user: target_user)
CommonAPI.report(reporter, %{
"account_id" => target_user.id,
"comment" => "I feel offended",
"status_ids" => [first_status.id]
})
CommonAPI.report(reporter, %{
"account_id" => target_user.id,
"comment" => "I feel offended2",
"status_ids" => [second_status.id]
})
data = [%{activity: first_status.data["id"]}, %{activity: second_status.data["id"]}]
{:ok,
%{
first_status: first_status,
second_status: second_status,
data: data
}}
end
test "works for deprecated reports format", %{
first_status: first_status,
second_status: second_status,
data: data
} do
groups = Utils.get_reports_grouped_by_status(data).groups
first_group = Enum.find(groups, &(&1.status.id == first_status.data["id"]))
second_group = Enum.find(groups, &(&1.status.id == second_status.data["id"]))
assert first_group.status.id == first_status.data["id"]
assert second_group.status.id == second_status.data["id"]
end
end
end end

View File

@ -15,6 +15,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
alias Pleroma.UserInviteToken alias Pleroma.UserInviteToken
alias Pleroma.Web.ActivityPub.Relay alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Web.MediaProxy alias Pleroma.Web.MediaProxy
import Pleroma.Factory import Pleroma.Factory
@ -1612,6 +1613,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
first_status: Activity.get_by_ap_id_with_object(first_status.data["id"]), first_status: Activity.get_by_ap_id_with_object(first_status.data["id"]),
second_status: Activity.get_by_ap_id_with_object(second_status.data["id"]), second_status: Activity.get_by_ap_id_with_object(second_status.data["id"]),
third_status: Activity.get_by_ap_id_with_object(third_status.data["id"]), third_status: Activity.get_by_ap_id_with_object(third_status.data["id"]),
first_report: first_report,
first_status_reports: [first_report, second_report, third_report], first_status_reports: [first_report, second_report, third_report],
second_status_reports: [first_report, second_report], second_status_reports: [first_report, second_report],
third_status_reports: [first_report], third_status_reports: [first_report],
@ -1638,14 +1640,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert length(response["reports"]) == 3 assert length(response["reports"]) == 3
first_group = first_group = Enum.find(response["reports"], &(&1["status"]["id"] == first_status.id))
Enum.find(response["reports"], &(&1["status"]["id"] == first_status.data["id"]))
second_group = second_group = Enum.find(response["reports"], &(&1["status"]["id"] == second_status.id))
Enum.find(response["reports"], &(&1["status"]["id"] == second_status.data["id"]))
third_group = third_group = Enum.find(response["reports"], &(&1["status"]["id"] == third_status.id))
Enum.find(response["reports"], &(&1["status"]["id"] == third_status.data["id"]))
assert length(first_group["reports"]) == 3 assert length(first_group["reports"]) == 3
assert length(second_group["reports"]) == 2 assert length(second_group["reports"]) == 2
@ -1656,13 +1655,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
NaiveDateTime.from_iso8601!(act.data["published"]) NaiveDateTime.from_iso8601!(act.data["published"])
end).data["published"] end).data["published"]
assert first_group["status"] == %{ assert first_group["status"] ==
"id" => first_status.data["id"], Map.put(
"content" => first_status.object.data["content"], stringify_keys(StatusView.render("show.json", %{activity: first_status})),
"published" => first_status.object.data["published"] "deleted",
} false
)
assert first_group["account"]["id"] == target_user.id assert(first_group["account"]["id"] == target_user.id)
assert length(first_group["actors"]) == 1 assert length(first_group["actors"]) == 1
assert hd(first_group["actors"])["id"] == reporter.id assert hd(first_group["actors"])["id"] == reporter.id
@ -1675,11 +1675,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
NaiveDateTime.from_iso8601!(act.data["published"]) NaiveDateTime.from_iso8601!(act.data["published"])
end).data["published"] end).data["published"]
assert second_group["status"] == %{ assert second_group["status"] ==
"id" => second_status.data["id"], Map.put(
"content" => second_status.object.data["content"], stringify_keys(StatusView.render("show.json", %{activity: second_status})),
"published" => second_status.object.data["published"] "deleted",
} false
)
assert second_group["account"]["id"] == target_user.id assert second_group["account"]["id"] == target_user.id
@ -1694,11 +1695,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
NaiveDateTime.from_iso8601!(act.data["published"]) NaiveDateTime.from_iso8601!(act.data["published"])
end).data["published"] end).data["published"]
assert third_group["status"] == %{ assert third_group["status"] ==
"id" => third_status.data["id"], Map.put(
"content" => third_status.object.data["content"], stringify_keys(StatusView.render("show.json", %{activity: third_status})),
"published" => third_status.object.data["published"] "deleted",
} false
)
assert third_group["account"]["id"] == target_user.id assert third_group["account"]["id"] == target_user.id
@ -1708,6 +1710,70 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert Enum.map(third_group["reports"], & &1["id"]) -- assert Enum.map(third_group["reports"], & &1["id"]) --
Enum.map(third_status_reports, & &1.id) == [] Enum.map(third_status_reports, & &1.id) == []
end end
test "reopened report renders status data", %{
conn: conn,
first_report: first_report,
first_status: first_status
} do
{:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
response =
conn
|> get("/api/pleroma/admin/grouped_reports")
|> json_response(:ok)
first_group = Enum.find(response["reports"], &(&1["status"]["id"] == first_status.id))
assert first_group["status"] ==
Map.put(
stringify_keys(StatusView.render("show.json", %{activity: first_status})),
"deleted",
false
)
end
test "reopened report does not render status data if status has been deleted", %{
conn: conn,
first_report: first_report,
first_status: first_status,
target_user: target_user
} do
{:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
{:ok, _} = CommonAPI.delete(first_status.id, target_user)
refute Activity.get_by_ap_id(first_status.id)
response =
conn
|> get("/api/pleroma/admin/grouped_reports")
|> json_response(:ok)
assert Enum.find(response["reports"], &(&1["status"]["deleted"] == true))["status"][
"deleted"
] == true
assert length(Enum.filter(response["reports"], &(&1["status"]["deleted"] == false))) == 2
end
test "account not empty if status was deleted", %{
conn: conn,
first_report: first_report,
first_status: first_status,
target_user: target_user
} do
{:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved")
{:ok, _} = CommonAPI.delete(first_status.id, target_user)
refute Activity.get_by_ap_id(first_status.id)
response =
conn
|> get("/api/pleroma/admin/grouped_reports")
|> json_response(:ok)
assert Enum.find(response["reports"], &(&1["status"]["deleted"] == true))["account"]
end
end end
describe "POST /api/pleroma/admin/reports/:id/respond" do describe "POST /api/pleroma/admin/reports/:id/respond" do