-
- defp maybe_add_context_from_object(%{"context" => context} = data) when is_binary(context),
- do: {:ok, data}
-
- defp maybe_add_context_from_object(%{"object" => object} = data) when is_binary(object) do
- with %{data: %{"context" => context}} when is_binary(context) <- Object.normalize(object) do
- {:ok, Map.put(data, "context", context)}
- else
- _ ->
- {:error, :no_context}
- end
- end
-
- defp maybe_add_context_from_object(_) do
- {:error, :no_context}
- end
-
- defp maybe_add_recipients_from_object(%{"to" => [_ | _], "cc" => [_ | _]} = data), do: {:ok, data}
-
- defp maybe_add_recipients_from_object(%{"object" => object} = data) do
- case Object.normalize(object) do
- %{data: {"actor" => actor}} ->
- data =
- data
- |> Map.put("to", [actor])
- |> Map.put("cc", data["cc"] || [])
-
- {:ok, data}
- nil -> {:error, :no_object}
- _ -> {:error, :no_actor}
- end
-
- defp maybe_add_recipients_from_object(_) do
- {:error, :no_object}
- end