Verified Commit bfe2dafd authored by Haelwenn's avatar Haelwenn
Browse files

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

parent c19bdc81
......@@ -120,6 +120,7 @@ def answer(user, object, name) do
%{
"type" => "Answer",
"actor" => user.ap_id,
"attributedTo" => user.ap_id,
"cc" => [object.data["actor"]],
"to" => [],
"name" => name,
......
......@@ -26,6 +26,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do
field(:name, :string)
field(:inReplyTo, :string)
field(:attributedTo, ObjectValidators.ObjectID)
# TODO: Remove actor on objects
field(:actor, ObjectValidators.ObjectID)
end
......@@ -54,8 +56,10 @@ def changeset(struct, data) do
def validate_data(data_cng) do
data_cng
|> validate_inclusion(:type, ["Answer"])
|> validate_required([:id, :inReplyTo, :name])
|> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
|> 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
......@@ -115,4 +115,22 @@ def validate_host_match(cng, fields \\ [:id, :actor]) do
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
......@@ -87,14 +87,14 @@ def validate_object_nonexistence(cng) do
end
def validate_actors_match(cng, meta) do
object_actor = meta[:object_data]["actor"]
attributed_to = meta[:object_data]["attributedTo"] || meta[:object_data]["actor"]
cng
|> validate_change(:actor, fn :actor, actor ->
if actor == object_actor do
if actor == attributed_to do
[]
else
[{:actor, "Actor doesn't match with object actor"}]
[{:actor, "Actor doesn't match with object attributedTo"}]
end
end)
end
......
......@@ -28,7 +28,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
field(:type, :string)
field(:content, :string)
field(:context, :string)
# TODO: Remove actor on objects
field(:actor, ObjectValidators.ObjectID)
field(:attributedTo, ObjectValidators.ObjectID)
field(:summary, :string)
field(:published, ObjectValidators.DateTime)
......@@ -108,8 +111,9 @@ def changeset(struct, data) do
def validate_data(data_cng) do
data_cng
|> 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_fields_match([:actor, :attributedTo])
|> CommonValidations.validate_actor_is_active()
|> CommonValidations.validate_any_presence([:oneOf, :anyOf])
|> CommonValidations.validate_host_match()
......
......@@ -157,7 +157,12 @@ def fix_addressing(object) do
end
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
def fix_in_reply_to(object, options \\ [])
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment