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(%{"type" => "Like"} = object, meta) do
with {_, %{valid?: true, changes: object}} <-
{:validate_object, LikeValidator.cast_and_validate(object)} do
object = stringify_keys(object)
with {_, {:ok, object}} <-
{:validate_object,
object |> LikeValidator.cast_and_validate() |> Ecto.Changeset.apply_action(:insert)} do
object = stringify_keys(object |> Map.from_struct())
{:ok, object, meta}
else
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
use Ecto.Schema
import Ecto.Changeset
alias Pleroma.Object
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ObjectValidators.Types
alias Pleroma.Web.ActivityPub.Utils
import Ecto.Changeset
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
@primary_key false
embedded_schema do
@ -38,8 +38,8 @@ def validate_data(data_cng) do
data_cng
|> validate_inclusion(:type, ["Like"])
|> validate_required([:id, :type, :object, :actor, :context, :to, :cc])
|> validate_change(:actor, &actor_valid?/2)
|> validate_change(:object, &object_valid?/2)
|> validate_actor_presence()
|> validate_object_presence()
|> validate_existing_like()
end
@ -54,20 +54,4 @@ def validate_existing_like(%{changes: %{actor: actor, object: object}} = cng) do
end
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

View File

@ -566,8 +566,11 @@ def handle_incoming(
end
def handle_incoming(%{"type" => "Like"} = data, _options) do
with {_, %{changes: cast_data}} <- {:casting_data, LikeValidator.cast_data(data)},
cast_data <- ObjectValidator.stringify_keys(cast_data),
with {_, {:ok, cast_data_sym}} <-
{: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, cast_data}} <- {:maybe_add_context, maybe_add_context_from_object(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, 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}
end