{Answer,Question}Validator: Keep both actor and attributedTo for now but sync them

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-06-26 00:07:43 +02:00
parent c19bdc811e
commit bfe2dafd39
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
6 changed files with 39 additions and 7 deletions

View File

@ -120,6 +120,7 @@ def answer(user, object, name) do
%{ %{
"type" => "Answer", "type" => "Answer",
"actor" => user.ap_id, "actor" => user.ap_id,
"attributedTo" => user.ap_id,
"cc" => [object.data["actor"]], "cc" => [object.data["actor"]],
"to" => [], "to" => [],
"name" => name, "name" => name,

View File

@ -26,6 +26,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do
field(:name, :string) field(:name, :string)
field(:inReplyTo, :string) field(:inReplyTo, :string)
field(:attributedTo, ObjectValidators.ObjectID) field(:attributedTo, ObjectValidators.ObjectID)
# TODO: Remove actor on objects
field(:actor, ObjectValidators.ObjectID) field(:actor, ObjectValidators.ObjectID)
end end
@ -54,8 +56,10 @@ def changeset(struct, data) do
def validate_data(data_cng) do def validate_data(data_cng) do
data_cng data_cng
|> validate_inclusion(:type, ["Answer"]) |> validate_inclusion(:type, ["Answer"])
|> validate_required([:id, :inReplyTo, :name]) |> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
|> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_any_presence([:cc, :to])
|> CommonValidations.validate_actor_presence() |> CommonValidations.validate_fields_match([:actor, :attributedTo])
|> CommonValidations.validate_actor_is_active()
|> CommonValidations.validate_host_match()
end end
end end

View File

@ -115,4 +115,22 @@ def validate_host_match(cng, fields \\ [:id, :actor]) do
end) end)
end end
end end
def validate_fields_match(cng, fields) do
unique_fields =
fields
|> Enum.map(fn field -> get_field(cng, field) end)
|> Enum.uniq()
|> Enum.count()
if unique_fields == 1 do
cng
else
fields
|> Enum.reduce(cng, fn field, cng ->
cng
|> add_error(field, "Fields #{inspect(fields)} aren't matching")
end)
end
end
end end

View File

@ -87,14 +87,14 @@ def validate_object_nonexistence(cng) do
end end
def validate_actors_match(cng, meta) do def validate_actors_match(cng, meta) do
object_actor = meta[:object_data]["actor"] attributed_to = meta[:object_data]["attributedTo"] || meta[:object_data]["actor"]
cng cng
|> validate_change(:actor, fn :actor, actor -> |> validate_change(:actor, fn :actor, actor ->
if actor == object_actor do if actor == attributed_to do
[] []
else else
[{:actor, "Actor doesn't match with object actor"}] [{:actor, "Actor doesn't match with object attributedTo"}]
end end
end) end)
end end

View File

@ -28,7 +28,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
field(:type, :string) field(:type, :string)
field(:content, :string) field(:content, :string)
field(:context, :string) field(:context, :string)
# TODO: Remove actor on objects
field(:actor, ObjectValidators.ObjectID) field(:actor, ObjectValidators.ObjectID)
field(:attributedTo, ObjectValidators.ObjectID) field(:attributedTo, ObjectValidators.ObjectID)
field(:summary, :string) field(:summary, :string)
field(:published, ObjectValidators.DateTime) field(:published, ObjectValidators.DateTime)
@ -108,8 +111,9 @@ def changeset(struct, data) do
def validate_data(data_cng) do def validate_data(data_cng) do
data_cng data_cng
|> validate_inclusion(:type, ["Question"]) |> validate_inclusion(:type, ["Question"])
|> validate_required([:id, :actor, :type, :content, :context]) |> validate_required([:id, :actor, :attributedTo, :type, :content, :context])
|> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_any_presence([:cc, :to])
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|> CommonValidations.validate_actor_is_active() |> CommonValidations.validate_actor_is_active()
|> CommonValidations.validate_any_presence([:oneOf, :anyOf]) |> CommonValidations.validate_any_presence([:oneOf, :anyOf])
|> CommonValidations.validate_host_match() |> CommonValidations.validate_host_match()

View File

@ -157,7 +157,12 @@ def fix_addressing(object) do
end end
def fix_actor(%{"attributedTo" => actor} = object) do def fix_actor(%{"attributedTo" => actor} = object) do
Map.put(object, "actor", Containment.get_actor(%{"actor" => actor})) actor = Containment.get_actor(%{"actor" => actor})
# TODO: Remove actor field for Objects
object
|> Map.put("actor", actor)
|> Map.put("attributedTo", actor)
end end
def fix_in_reply_to(object, options \\ []) def fix_in_reply_to(object, options \\ [])