LikeValidator: Fix up context.

This commit is contained in:
lain 2020-05-04 17:18:17 +02:00
parent e03c301ebe
commit 0f9bed022f
2 changed files with 29 additions and 2 deletions

View File

@ -44,11 +44,25 @@ def changeset(struct, data) do
def fix_after_cast(cng) do def fix_after_cast(cng) do
cng cng
|> fix_recipients() |> fix_recipients()
|> fix_context()
end
def fix_context(cng) do
object = get_field(cng, :object)
with nil <- get_field(cng, :context),
%Object{data: %{"context" => context}} <- Object.get_cached_by_ap_id(object) do
cng
|> put_change(:context, context)
else
_ ->
cng
end
end end
def fix_recipients(cng) do def fix_recipients(cng) do
to = get_field(cng, :to) || [] to = get_field(cng, :to)
cc = get_field(cng, :cc) || [] cc = get_field(cng, :cc)
object = get_field(cng, :object) object = get_field(cng, :object)
with {[], []} <- {to, cc}, with {[], []} <- {to, cc},

View File

@ -49,6 +49,19 @@ test "sets the 'to' field to the object actor if no recipients are given", %{
assert object["to"] == [user.ap_id] assert object["to"] == [user.ap_id]
end end
test "sets the context field to the context of the object if no context is given", %{
valid_like: valid_like,
post_activity: post_activity
} do
without_context =
valid_like
|> Map.delete("context")
{:ok, object, _meta} = ObjectValidator.validate(without_context, [])
assert object["context"] == post_activity.data["context"]
end
test "it errors when the actor is missing or not known", %{valid_like: valid_like} do test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
without_actor = Map.delete(valid_like, "actor") without_actor = Map.delete(valid_like, "actor")