Object Validators: Extract common validations.

This commit is contained in:
lain 2019-11-05 15:02:09 +01:00
parent 25077812bf
commit 3d1b445cbf
5 changed files with 47 additions and 27 deletions

View file

@ -17,9 +17,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
def validate(object, meta) def validate(object, meta)
def validate(%{"type" => "Like"} = object, meta) do def validate(%{"type" => "Like"} = object, meta) do
with {_, %{valid?: true, changes: object}} <- with {_, {:ok, object}} <-
{:validate_object, LikeValidator.cast_and_validate(object)} do {:validate_object,
object = stringify_keys(object) object |> LikeValidator.cast_and_validate() |> Ecto.Changeset.apply_action(:insert)} do
object = stringify_keys(object |> Map.from_struct())
{:ok, object, meta} {:ok, object, meta}
else else
e -> {:error, e} e -> {:error, e}

View file

@ -0,0 +1,32 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
import Ecto.Changeset
alias Pleroma.Object
alias Pleroma.User
def validate_actor_presence(cng, field_name \\ :actor) do
cng
|> validate_change(field_name, fn field_name, actor ->
if User.get_cached_by_ap_id(actor) do
[]
else
[{field_name, "can't find user"}]
end
end)
end
def validate_object_presence(cng, field_name \\ :object) do
cng
|> validate_change(field_name, fn field_name, actor ->
if Object.get_cached_by_ap_id(actor) do
[]
else
[{field_name, "can't find user"}]
end
end)
end
end

View file

@ -4,13 +4,13 @@
defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset
alias Pleroma.Object
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ObjectValidators.Types alias Pleroma.Web.ActivityPub.ObjectValidators.Types
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
import Ecto.Changeset
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
@primary_key false @primary_key false
embedded_schema do embedded_schema do
@ -38,8 +38,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
data_cng data_cng
|> validate_inclusion(:type, ["Like"]) |> validate_inclusion(:type, ["Like"])
|> validate_required([:id, :type, :object, :actor, :context, :to, :cc]) |> validate_required([:id, :type, :object, :actor, :context, :to, :cc])
|> validate_change(:actor, &actor_valid?/2) |> validate_actor_presence()
|> validate_change(:object, &object_valid?/2) |> validate_object_presence()
|> validate_existing_like() |> validate_existing_like()
end end
@ -54,20 +54,4 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
end end
def validate_existing_like(cng), do: cng def validate_existing_like(cng), do: cng
def actor_valid?(field_name, actor) do
if User.get_cached_by_ap_id(actor) do
[]
else
[{field_name, "can't find user"}]
end
end
def object_valid?(field_name, object) do
if Object.get_cached_by_ap_id(object) do
[]
else
[{field_name, "can't find object"}]
end
end
end end

View file

@ -566,8 +566,11 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
end end
def handle_incoming(%{"type" => "Like"} = data, _options) do def handle_incoming(%{"type" => "Like"} = data, _options) do
with {_, %{changes: cast_data}} <- {:casting_data, LikeValidator.cast_data(data)}, with {_, {:ok, cast_data_sym}} <-
cast_data <- ObjectValidator.stringify_keys(cast_data), {:casting_data,
data |> LikeValidator.cast_data() |> Ecto.Changeset.apply_action(:insert)},
{_, cast_data} <-
{:stringify_keys, ObjectValidator.stringify_keys(cast_data_sym |> Map.from_struct())},
:ok <- ObjectValidator.fetch_actor_and_object(cast_data), :ok <- ObjectValidator.fetch_actor_and_object(cast_data),
{_, {:ok, cast_data}} <- {:maybe_add_context, maybe_add_context_from_object(cast_data)}, {_, {:ok, cast_data}} <- {:maybe_add_context, maybe_add_context_from_object(cast_data)},
{_, {:ok, cast_data}} <- {_, {:ok, cast_data}} <-

View file

@ -19,7 +19,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
{:ok, post} = CommonAPI.post(user, %{"status" => "hey"}) {:ok, post} = CommonAPI.post(user, %{"status" => "hey"})
{:ok, like_data, _meta} = Builder.like(user, post.object) {:ok, like_data, _meta} = Builder.like(user, post.object)
{:ok, like, _meta} = ActivityPub.persist(like_data, [local: true]) {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
%{like: like, user: user} %{like: like, user: user}
end end