Add language from activity context in ObjectValidator

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-12-28 15:52:59 +01:00
parent c5a05a8e14
commit 281c82a49b
5 changed files with 54 additions and 42 deletions

View file

@ -24,6 +24,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
alias Pleroma.Web.ActivityPub.ObjectValidators.AudioImageVideoValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
@ -106,7 +107,9 @@ def validate(
)
when objtype in ~w[Question Answer Audio Video Image Event Article Note Page] do
with {:ok, object_data} <-
cast_and_apply_and_stringify_with_history(object, activity_data: create_activity),
object
|> CommonFixes.maybe_add_language_from_activity(create_activity)
|> cast_and_apply_and_stringify_with_history(),
meta = Keyword.put(meta, :object_data, object_data),
{:ok, create_activity} <-
create_activity
@ -156,7 +159,11 @@ def validate(
)
when objtype in ~w[Question Answer Audio Video Event Article Note Page] do
with {_, false} <- {:local, Access.get(meta, :local, false)},
{_, {:ok, object_data, _}} <- {:object_validation, validate(object, meta)},
{_, {:ok, object_data, _}} <-
{:object_validation,
object
|> CommonFixes.maybe_add_language_from_activity(update_activity)
|> validate(meta)},
meta = Keyword.put(meta, :object_data, object_data),
{:ok, update_activity} <-
update_activity
@ -221,42 +228,40 @@ def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
def validate(o, m), do: {:error, {:validator_not_set, {o, m}}}
def cast_and_apply_and_stringify_with_history(object, meta \\ []) do
def cast_and_apply_and_stringify_with_history(object) do
do_separate_with_history(object, fn object ->
with {:ok, object_data} <- cast_and_apply(object, meta),
with {:ok, object_data} <- cast_and_apply(object),
object_data <- object_data |> stringify_keys() do
{:ok, object_data}
end
end)
end
def cast_and_apply(object, meta \\ [])
def cast_and_apply(%{"type" => "ChatMessage"} = object, _) do
def cast_and_apply(%{"type" => "ChatMessage"} = object) do
ChatMessageValidator.cast_and_apply(object)
end
def cast_and_apply(%{"type" => "Question"} = object, _) do
def cast_and_apply(%{"type" => "Question"} = object) do
QuestionValidator.cast_and_apply(object)
end
def cast_and_apply(%{"type" => "Answer"} = object, _) do
def cast_and_apply(%{"type" => "Answer"} = object) do
AnswerValidator.cast_and_apply(object)
end
def cast_and_apply(%{"type" => type} = object, _) when type in ~w[Audio Image Video] do
def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Image Video] do
AudioImageVideoValidator.cast_and_apply(object)
end
def cast_and_apply(%{"type" => "Event"} = object, meta) do
EventValidator.cast_and_apply(object, meta)
def cast_and_apply(%{"type" => "Event"} = object) do
EventValidator.cast_and_apply(object)
end
def cast_and_apply(%{"type" => type} = object, meta) when type in ~w[Article Note Page] do
ArticleNotePageValidator.cast_and_apply(object, meta)
def cast_and_apply(%{"type" => type} = object) when type in ~w[Article Note Page] do
ArticleNotePageValidator.cast_and_apply(object)
end
def cast_and_apply(o, _), do: {:error, {:validator_not_set, o}}
def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
def stringify_keys(object) when is_struct(object) do
object

View file

@ -28,21 +28,21 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
field(:replies, {:array, ObjectValidators.ObjectID}, default: [])
end
def cast_and_apply(data, meta \\ []) do
def cast_and_apply(data) do
data
|> cast_data(meta)
|> cast_data()
|> apply_action(:insert)
end
def cast_and_validate(data, meta \\ []) do
def cast_and_validate(data) do
data
|> cast_data(meta)
|> cast_data()
|> validate_data()
end
def cast_data(data, meta \\ []) do
def cast_data(data) do
%__MODULE__{}
|> changeset(data, meta)
|> changeset(data)
end
defp fix_url(%{"url" => url} = data) when is_bitstring(url), do: data
@ -97,7 +97,7 @@ def fix_attachments(%{"attachment" => attachment} = data) when is_map(attachment
def fix_attachments(data), do: data
defp fix(data, meta) do
defp fix(data) do
data
|> CommonFixes.fix_actor()
|> CommonFixes.fix_object_defaults()
@ -109,12 +109,12 @@ defp fix(data, meta) do
|> CommonFixes.fix_quote_url()
|> Transmogrifier.fix_emoji()
|> Transmogrifier.fix_content_map()
|> CommonFixes.maybe_add_language(meta)
|> CommonFixes.maybe_add_language()
|> CommonFixes.maybe_add_content_map()
end
def changeset(struct, data, meta \\ []) do
data = fix(data, meta)
def changeset(struct, data) do
data = fix(data)
struct
|> cast(data, __schema__(:fields) -- [:attachment, :tag])

View file

@ -131,11 +131,20 @@ def object_link_tag?(%{
def object_link_tag?(_), do: false
def maybe_add_language(object, meta \\ []) do
def maybe_add_language_from_activity(object, activity) do
language = get_language_from_context(activity)
if language do
Map.put(object, "language", language)
else
object
end
end
def maybe_add_language(object) do
language =
[
get_language_from_context(object),
get_language_from_context(Keyword.get(meta, :activity_data)),
get_language_from_content_map(object),
get_language_from_content(object)
]

View file

@ -39,34 +39,35 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EventValidator do
field(:participation_request_count, :integer, default: 0)
end
def cast_and_apply(data, meta \\ []) do
def cast_and_apply(data) do
data
|> cast_data(meta)
|> cast_data()
|> apply_action(:insert)
end
def cast_and_validate(data, meta \\ []) do
def cast_and_validate(data) do
data
|> cast_data(meta)
|> cast_data()
|> validate_data()
end
def cast_data(data, meta \\ []) do
@spec cast_data(map()) :: map()
def cast_data(data) do
%__MODULE__{}
|> changeset(data, meta)
|> changeset(data)
end
defp fix(data, meta) do
defp fix(data) do
data
|> CommonFixes.fix_actor()
|> CommonFixes.fix_object_defaults()
|> Transmogrifier.fix_emoji()
|> CommonFixes.maybe_add_language(meta)
|> CommonFixes.maybe_add_language()
|> CommonFixes.maybe_add_content_map()
end
def changeset(struct, data, meta \\ []) do
data = fix(data, meta)
def changeset(struct, data) do
data = fix(data)
struct
|> cast(data, __schema__(:fields) -- [:attachment, :tag, :location])

View file

@ -197,12 +197,9 @@ test "it detects language from JSON-LD context" do
"actor" => user.ap_id
}
{:ok, object} =
ArticleNotePageValidator.cast_and_apply(note_activity["object"],
activity_data: note_activity
)
{:ok, _create_activity, meta} = ObjectValidator.validate(note_activity, []) |> IO.inspect()
assert object.language == "pl"
assert meta[:object_data]["language"] == "pl"
end
test "it detects language from contentMap" do