field(:context, :string)
end
- def cast_data(data) do
+ def cast_data(data, meta \\ []) do
+ data = fix(data, meta)
+
%__MODULE__{}
|> changeset(data)
end
def cast_and_validate(data, meta \\ []) do
data
- |> cast_data
+ |> cast_data(meta)
|> validate_data(meta)
end
|> cast(data, __schema__(:fields))
end
+ defp fix_context(data, meta) do
+ if object = meta[:object_data] do
+ Map.put_new(data, "context", object["context"])
+ else
+ data
+ end
+ end
+
+ defp fix(data, meta) do
+ data
+ |> fix_context(meta)
+ end
+
def validate_data(cng, meta \\ []) do
cng
|> validate_required([:actor, :type, :object])
# based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults
defp fix_defaults(data) do
- %{data: %{"id" => context}, id: context_id} = Utils.create_context(data["context"])
+ %{data: %{"id" => context}, id: context_id} =
+ Utils.create_context(data["context"] || data["conversation"])
data
- |> Map.put_new_lazy("id", &Utils.generate_object_id/0)
|> Map.put_new_lazy("published", &Utils.make_date/0)
|> Map.put_new("context", context)
|> Map.put_new("context_id", context_id)
end
+ defp fix_attribution(data) do
+ data
+ |> Map.put_new("actor", data["attributedTo"])
+ end
+
defp fix(data) do
data
+ |> fix_attribution()
|> fix_closed()
|> fix_defaults()
end
end
def handle_incoming(
- %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
+ %{"type" => "Create", "object" => %{"type" => objtype}} = data,
_options
)
- when objtype in ["Question", "Answer"] do
- data =
- data
- |> Map.put("object", fix_object(object))
- |> fix_addressing()
-
- data = Map.put_new(data, "context", data["object"]["context"])
-
+ when objtype in ["Question", "Answer", "ChatMessage"] do
with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
{:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
{:ok, activity}
def render("show.json", %{object: object} = params) do
case object.data do
- %{"anyOf" => [ _ | _] = options} ->
+ %{"anyOf" => [_ | _] = options} ->
render(__MODULE__, "show.json", Map.merge(params, %{multiple: true, options: options}))
- %{"oneOf" => [ _ | _] = options} ->
+ %{"oneOf" => [_ | _] = options} ->
render(__MODULE__, "show.json", Map.merge(params, %{multiple: false, options: options}))
_ ->