2021-05-06 16:39:59 +00:00
|
|
|
defmodule Mobilizon.Service.Activity.Utils do
|
|
|
|
@moduledoc """
|
|
|
|
Utils for activities
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Activities.Activity
|
|
|
|
alias Mobilizon.Service.Activity, as: ActivityService
|
|
|
|
|
|
|
|
def transform_activity(%Activity{} = activity) do
|
|
|
|
activity
|
|
|
|
|> Map.update(:subject_params, %{}, &transform_params/1)
|
|
|
|
|> add_activity_object()
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec add_activity_object(Activity.t()) :: Activity.t()
|
2021-05-06 16:39:59 +00:00
|
|
|
def add_activity_object(%Activity{} = activity) do
|
2021-09-24 14:46:42 +00:00
|
|
|
%Activity{activity | object: ActivityService.object(activity)}
|
2021-05-06 16:39:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec transform_params(map()) :: list()
|
|
|
|
defp transform_params(params) do
|
|
|
|
Enum.map(params, fn {key, value} -> %{key: key, value: transform_value(value)} end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp transform_value(value) when is_list(value) do
|
|
|
|
Enum.join(value, ",")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp transform_value(value), do: value
|
2021-06-23 17:49:10 +00:00
|
|
|
|
|
|
|
@spec maybe_inserted_at :: map()
|
|
|
|
def maybe_inserted_at do
|
|
|
|
if Application.fetch_env!(:mobilizon, :env) == :test do
|
|
|
|
%{}
|
|
|
|
else
|
|
|
|
%{"inserted_at" => DateTime.utc_now()}
|
|
|
|
end
|
|
|
|
end
|
2021-05-06 16:39:59 +00:00
|
|
|
end
|