ActivityPub.fetch_activities([], params, :offset)
end
- @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"]))
|> Repo.all()
end
+ @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(activity_ids) do
+ parsed_groups =
+ activity_ids
+ |> Enum.map(fn id ->
+ id
+ |> build_flag_object()
+ |> parse_report_group()
+ end)
+
+ %{
+ groups: parsed_groups
+ }
+ end
+
@spec get_reported_activities() :: [
%{
required(:activity) => String.t(),
}
]
def get_reported_activities do
- from(a in Activity,
- where: fragment("(?)->>'type' = 'Flag'", a.data),
+ reported_activities_query =
+ from(a in Activity,
+ where: fragment("(?)->>'type' = 'Flag'", a.data),
+ select: %{
+ activity: fragment("jsonb_array_elements((? #- '{object,0}')->'object')", a.data)
+ },
+ group_by: fragment("activity")
+ )
+
+ from(a in subquery(reported_activities_query),
+ distinct: true,
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")
+ id: fragment("COALESCE(?->>'id'::text, ? #>> '{}')", a.activity, a.activity)
+ }
)
|> Repo.all()
+ |> Enum.map(& &1.id)
end
def update_report_state(%Activity{} = activity, state)
assert updated_object.data["announcement_count"] == 1
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