From 8519364e77cf3be17d0508b4a947f067f2cea037 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 4 Nov 2021 18:34:43 +0100 Subject: [PATCH] Fix action logs containing group suspension events Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/admin.ex | 37 +++++++++++++++++++++------------- lib/graphql/schema/admin.ex | 4 ++-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/graphql/resolvers/admin.ex b/lib/graphql/resolvers/admin.ex index efaccaad1..36857e7dd 100644 --- a/lib/graphql/resolvers/admin.ex +++ b/lib/graphql/resolvers/admin.ex @@ -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,22 +133,22 @@ 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 - struct(struct, data) - end + 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 defp convert_changes_to_struct(struct, changes) do - with 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 + changeset = struct.__changeset__ + + data = + for( + {key, val} <- changes, + into: %{}, + do: {String.to_existing_atom(key), process_eventual_type(changeset, key, val)} + ) + + struct(struct, data) end # datetimes are not unserialized as DateTime/NaiveDateTime so we do it manually with changeset data @@ -150,6 +156,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do 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 diff --git a/lib/graphql/schema/admin.ex b/lib/graphql/schema/admin.ex index 057548d84..dfe02c093 100644 --- a/lib/graphql/schema/admin.ex +++ b/lib/graphql/schema/admin.ex @@ -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 _, _ ->