- @spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{
- 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(groups) do
- parsed_groups =
- groups
- |> Enum.map(fn entry ->
- activity =
- case Jason.decode(entry.activity) do
- {:ok, activity} -> activity
- _ -> build_flag_object(entry.activity)
- end
-
- parse_report_group(activity)
- end)
-
- %{
- groups: parsed_groups
- }
- 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() :: [
- %{
- required(:activity) => String.t(),
- required(:date) => String.t()
- }
- ]
- def get_reported_activities 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
-