- @spec get_reports_grouped_by_status() :: %{
- required(:groups) => [
- %{
- required(:date) => String.t(),
- required(:account) => %{},
- required(:status) => %{},
- required(:actors) => [%User{}],
- required(:reports) => [%Activity{}]
- }
- ],
- required(:total) => integer
- }
- def get_reports_grouped_by_status do
- groups =
- get_reported_status_ids()
- |> Enum.map(fn entry ->
- activity = Jason.decode!(entry.activity)
- 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)
-
- %{
- groups: groups
- }
- 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_status_ids() :: [
- %{
- required(:activity) => String.t(),
- required(:date) => String.t()
- }
- ]
- 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:
- fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity", a.data)
- },
- group_by: fragment("activity"),
- order_by: fragment("date DESC")
- )
- |> Repo.all()
- end
-