- @spec get_by_id(String.t()) :: Activity.t() | nil
- def get_by_id(id) do
- case FlakeId.flake_id?(id) do
- true ->
- Activity
- |> where([a], a.id == ^id)
- |> restrict_deactivated_users()
- |> Repo.one()
-
- _ ->
- nil
- end
- end
-
- def get_by_id_with_user_actor(id) do
- case FlakeId.flake_id?(id) do
- true ->
- Activity
- |> where([a], a.id == ^id)
- |> with_preloaded_user_actor()
- |> Repo.one()
-
- _ ->
- nil
+ @doc """
+ Gets activity by ID, doesn't load activities from deactivated actors by default.
+ """
+ @spec get_by_id(String.t(), keyword()) :: t() | nil
+ def get_by_id(id, opts \\ [filter: [:restrict_deactivated]]), do: get_by_id_with_opts(id, opts)
+
+ @spec get_by_id_with_user_actor(String.t()) :: t() | nil
+ def get_by_id_with_user_actor(id), do: get_by_id_with_opts(id, preload: [:user_actor])
+
+ @spec get_by_id_with_object(String.t()) :: t() | nil
+ def get_by_id_with_object(id), do: get_by_id_with_opts(id, preload: [:object])
+
+ defp get_by_id_with_opts(id, opts) do
+ if FlakeId.flake_id?(id) do
+ query = Queries.by_id(id)
+
+ with_filters_query =
+ if is_list(opts[:filter]) do
+ Enum.reduce(opts[:filter], query, fn
+ {:type, type}, acc -> Queries.by_type(acc, type)
+ :restrict_deactivated, acc -> restrict_deactivated_users(acc)
+ _, acc -> acc
+ end)
+ else
+ query
+ end
+
+ with_preloads_query =
+ if is_list(opts[:preload]) do
+ Enum.reduce(opts[:preload], with_filters_query, fn
+ :user_actor, acc -> with_preloaded_user_actor(acc)
+ :object, acc -> with_preloaded_object(acc)
+ _, acc -> acc
+ end)
+ else
+ with_filters_query
+ end
+
+ Repo.one(with_preloads_query)