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}
--- /dev/null
+# 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
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
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
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
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}} <-
{: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