1
0
Fork 0
forked from mirrors/akkoma

Support old flag format

This commit is contained in:
Maxim Filippov 2019-11-07 22:45:36 +10:00
parent f171095960
commit 7258db023e
3 changed files with 100 additions and 41 deletions

View file

@ -616,8 +616,14 @@ def make_flag_data(%{actor: actor, context: context, content: content} = params,
def make_flag_data(_, _), do: %{}
defp build_flag_object(%{account: account, statuses: statuses} = _) do
[account.ap_id] ++
Enum.map(statuses || [], fn act ->
[account.ap_id] ++ build_flag_object(%{statuses: statuses})
end
defp build_flag_object(%{statuses: statuses}) do
Enum.map(statuses || [], &build_flag_object/1)
end
defp build_flag_object(act) when is_map(act) or is_binary(act) do
id =
case act do
%Activity{} = act -> act.data["id"]
@ -635,7 +641,6 @@ defp build_flag_object(%{account: account, statuses: statuses} = _) do
"published" => activity.object.data["published"],
"actor" => AccountView.render("show.json", %{user: actor})
}
end)
end
defp build_flag_object(_), do: []
@ -692,7 +697,7 @@ def get_reports(params, page, page_size) do
ActivityPub.fetch_activities([], params, :offset)
end
@spec get_reports_grouped_by_status() :: %{
@spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{
required(:groups) => [
%{
required(:date) => String.t(),
@ -704,11 +709,25 @@ def get_reports(params, page, page_size) do
],
required(:total) => integer
}
def get_reports_grouped_by_status do
groups =
get_reported_status_ids()
def get_reports_grouped_by_status(groups) do
parsed_groups =
groups
|> Enum.map(fn entry ->
activity = Jason.decode!(entry.activity)
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)
@ -724,11 +743,6 @@ def get_reports_grouped_by_status do
actors: Enum.uniq(actors),
reports: reports
}
end)
%{
groups: groups
}
end
def get_reports_by_status_id(ap_id) do
@ -740,13 +754,13 @@ def get_reports_by_status_id(ap_id) do
|> Repo.all()
end
@spec get_reported_status_ids() :: [
@spec get_reported_activities() :: [
%{
required(:activity) => String.t(),
required(:date) => String.t()
}
]
def get_reported_status_ids do
def get_reported_activities do
from(a in Activity,
where: fragment("(?)->>'type' = 'Flag'", a.data),
select: %{

View file

@ -625,9 +625,11 @@ def list_reports(conn, params) do
end
def list_grouped_reports(conn, _params) do
reports = Utils.get_reported_activities()
conn
|> put_view(ReportView)
|> render("index_grouped.json", Utils.get_reports_grouped_by_status())
|> render("index_grouped.json", Utils.get_reports_grouped_by_status(reports))
end
def report_show(conn, %{"id" => id}) do

View file

@ -636,4 +636,47 @@ test "removes actor from announcements" do
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