Move tag fixup to object_validator

This commit is contained in:
Haelwenn (lanodan) Monnier 2021-04-01 13:49:04 +02:00
parent 7ebfe89900
commit ef36f7fa5c
No known key found for this signature in database
GPG key ID: D5B7A8E43C997DEE
2 changed files with 26 additions and 20 deletions

View file

@ -113,9 +113,34 @@ def validate(
end end
end end
def validate(%{"type" => type} = object, meta)
when type in ~w[Event Question Audio Video Article] do
validator =
case type do
"Event" -> EventValidator
"Question" -> QuestionValidator
"Audio" -> AudioVideoValidator
"Video" -> AudioVideoValidator
"Article" -> ArticleNoteValidator
end
with {:ok, object} <-
object
|> validator.cast_and_validate()
|> Ecto.Changeset.apply_action(:insert) do
object = stringify_keys(object)
# Insert copy of hashtags as strings for the non-hashtag table indexing
tag = (object["tag"] || []) ++ Object.hashtags(%Object{data: object})
object = Map.put(object, "tag", tag)
{:ok, object, meta}
end
end
def validate(%{"type" => type} = object, meta) def validate(%{"type" => type} = object, meta)
when type in ~w[Accept Reject Follow Update Like EmojiReact Announce when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
Event ChatMessage Question Audio Video Article Answer] do ChatMessage Answer] do
validator = validator =
case type do case type do
"Accept" -> AcceptRejectValidator "Accept" -> AcceptRejectValidator
@ -125,12 +150,7 @@ def validate(%{"type" => type} = object, meta)
"Like" -> LikeValidator "Like" -> LikeValidator
"EmojiReact" -> EmojiReactValidator "EmojiReact" -> EmojiReactValidator
"Announce" -> AnnounceValidator "Announce" -> AnnounceValidator
"Event" -> EventValidator
"ChatMessage" -> ChatMessageValidator "ChatMessage" -> ChatMessageValidator
"Question" -> QuestionValidator
"Audio" -> AudioVideoValidator
"Video" -> AudioVideoValidator
"Article" -> ArticleNoteValidator
"Answer" -> AnswerValidator "Answer" -> AnswerValidator
end end

View file

@ -44,7 +44,6 @@ def do_common_pipeline(%{__struct__: _}, _meta), do: {:error, :is_struct}
def do_common_pipeline(message, meta) do def do_common_pipeline(message, meta) do
with {_, {:ok, message, meta}} <- {:validate, @object_validator.validate(message, meta)}, with {_, {:ok, message, meta}} <- {:validate, @object_validator.validate(message, meta)},
{_, {:ok, message, meta}} <- {:fixup, validation_fixups(message, meta)},
{_, {:ok, message, meta}} <- {:mrf, @mrf.pipeline_filter(message, meta)}, {_, {:ok, message, meta}} <- {:mrf, @mrf.pipeline_filter(message, meta)},
{_, {:ok, message, meta}} <- {:persist, @activity_pub.persist(message, meta)}, {_, {:ok, message, meta}} <- {:persist, @activity_pub.persist(message, meta)},
{_, {:ok, message, meta}} <- {:side_effects, @side_effects.handle(message, meta)}, {_, {:ok, message, meta}} <- {:side_effects, @side_effects.handle(message, meta)},
@ -56,19 +55,6 @@ def do_common_pipeline(message, meta) do
end end
end end
defp validation_fixups(message, meta) do
# Insert copy of hashtags as strings for the non-hashtag table indexing
message =
if message["tag"] do
tag = Object.hashtags(%Object{data: message}) ++ (message["tag"] || [])
Map.put(message, "tag", tag)
else
message
end
{:ok, message, meta}
end
defp maybe_federate(%Object{}, _), do: {:ok, :not_federated} defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
defp maybe_federate(%Activity{} = activity, meta) do defp maybe_federate(%Activity{} = activity, meta) do