Fix action logs containing group suspension events

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-11-04 18:34:43 +01:00
parent 39ea05a04a
commit 8519364e77
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 25 additions and 16 deletions

View File

@ -42,7 +42,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do
target_type
|> String.to_existing_atom()
|> transform_action_log(action, action_log)
|> Map.merge(%{actor: actor, id: id, inserted_at: inserted_at})
|> add_extra_data(actor, id, inserted_at)
end)
|> Enum.filter(& &1)
@ -54,6 +54,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do
{:error, dgettext("errors", "You need to be logged-in and a moderator to list action logs")}
end
defp add_extra_data(nil, _actor, _id, _inserted_at), do: nil
defp add_extra_data(map, actor, id, inserted_at) do
Map.merge(map, %{actor: actor, id: id, inserted_at: inserted_at})
end
@spec transform_action_log(module(), atom(), ActionLog.t()) :: map()
defp transform_action_log(
Report,
@ -127,29 +133,32 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do
# Changes are stored as %{"key" => "value"} so we need to convert them back as struct
@spec convert_changes_to_struct(module(), map()) :: struct()
defp convert_changes_to_struct(struct, %{"report_id" => _report_id} = changes) do
with data <- for({key, val} <- changes, into: %{}, do: {String.to_existing_atom(key), val}),
data <- Map.put(data, :report, Mobilizon.Reports.get_report(data.report_id)) do
data = for({key, val} <- changes, into: %{}, do: {String.to_existing_atom(key), val})
data = Map.put(data, :report, Mobilizon.Reports.get_report(data.report_id))
struct(struct, data)
end
end
defp convert_changes_to_struct(struct, changes) do
with changeset <- struct.__changeset__,
data <-
changeset = struct.__changeset__
data =
for(
{key, val} <- changes,
into: %{},
do: {String.to_existing_atom(key), process_eventual_type(changeset, key, val)}
) do
)
struct(struct, data)
end
end
# datetimes are not unserialized as DateTime/NaiveDateTime so we do it manually with changeset data
@spec process_eventual_type(Ecto.Changeset.t(), String.t(), String.t() | nil) ::
DateTime.t() | NaiveDateTime.t() | any()
defp process_eventual_type(changeset, key, val) do
cond do
changeset[String.to_existing_atom(key)] == Mobilizon.Actors.ActorType and not is_nil(val) ->
String.to_existing_atom(val)
changeset[String.to_existing_atom(key)] == :utc_datetime and not is_nil(val) ->
{:ok, datetime, _} = DateTime.from_iso8601(val)
datetime

View File

@ -64,13 +64,13 @@ defmodule Mobilizon.GraphQL.Schema.AdminType do
%Comment{}, _ ->
:comment
%Actor{type: "Person"}, _ ->
%Actor{type: :Person}, _ ->
:person
%User{}, _ ->
:user
%Actor{type: "Group"}, _ ->
%Actor{type: :Group}, _ ->
:group
_, _ ->