expanding AddRemoveValidator

This commit is contained in:
Alexander Strizhakov 2021-03-19 17:25:12 +03:00
parent 8857242c95
commit 2a520ba008
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
4 changed files with 27 additions and 26 deletions

View file

@ -238,7 +238,7 @@ def validate(%{"type" => "Announce"} = object, meta) do
def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
with {:ok, object} <- with {:ok, object} <-
object object
|> AddRemoveValidator.cast_and_validate(meta) |> AddRemoveValidator.cast_and_validate()
|> Ecto.Changeset.apply_action(:insert) do |> Ecto.Changeset.apply_action(:insert) do
object = stringify_keys(object) object = stringify_keys(object)
{:ok, object, meta} {:ok, object, meta}

View file

@ -8,6 +8,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AddRemoveValidator do
import Ecto.Changeset import Ecto.Changeset
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
require Pleroma.Constants
alias Pleroma.EctoType.ActivityPub.ObjectValidators alias Pleroma.EctoType.ActivityPub.ObjectValidators
@primary_key false @primary_key false
@ -22,28 +24,40 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AddRemoveValidator do
field(:cc, ObjectValidators.Recipients, default: []) field(:cc, ObjectValidators.Recipients, default: [])
end end
def cast_and_validate(data, meta) do def cast_and_validate(data) do
data data
|> maybe_fix_data_for_mastodon()
|> cast_data() |> cast_data()
|> validate_data(meta) |> validate_data()
end
defp maybe_fix_data_for_mastodon(data) do
{:ok, actor} = Pleroma.User.get_or_fetch_by_ap_id(data["actor"])
# Mastodon sends pin/unpin objects without id, to, cc fields
data
|> Map.put_new("id", Pleroma.Web.ActivityPub.Utils.generate_activity_id())
|> Map.put_new("to", [Pleroma.Constants.as_public()])
|> Map.put_new("cc", [actor.follower_address])
end end
defp cast_data(data) do defp cast_data(data) do
cast(%__MODULE__{}, data, __schema__(:fields)) cast(%__MODULE__{}, data, __schema__(:fields))
end end
defp validate_data(changeset, meta) do defp validate_data(changeset) do
changeset changeset
|> validate_required([:id, :target, :object, :actor, :type, :to, :cc]) |> validate_required([:id, :target, :object, :actor, :type, :to, :cc])
|> validate_inclusion(:type, ~w(Add Remove)) |> validate_inclusion(:type, ~w(Add Remove))
|> validate_actor_presence() |> validate_actor_presence()
|> validate_collection_belongs_to_actor(meta) |> validate_collection_belongs_to_actor()
|> validate_object_presence() |> validate_object_presence()
end end
defp validate_collection_belongs_to_actor(changeset, meta) do defp validate_collection_belongs_to_actor(changeset) do
{:ok, actor} = Pleroma.User.get_or_fetch_by_ap_id(changeset.changes[:actor])
validate_change(changeset, :target, fn :target, target -> validate_change(changeset, :target, fn :target, target ->
if target == meta[:featured_address] do if target == actor.featured_address do
[] []
else else
[target: "collection doesn't belong to actor"] [target: "collection doesn't belong to actor"]

View file

@ -557,24 +557,12 @@ def handle_incoming(
end end
def handle_incoming(%{"type" => type} = data, _options) when type in ~w(Add Remove) do def handle_incoming(%{"type" => type} = data, _options) when type in ~w(Add Remove) do
with {:ok, %User{} = user} <- ObjectValidator.fetch_actor(data), with :ok <- ObjectValidator.fetch_actor_and_object(data),
{:ok, actor} <- Pleroma.User.get_or_fetch_by_ap_id(data["actor"]),
# maybe locally user doesn't have featured_address # maybe locally user doesn't have featured_address
{:ok, user} <- maybe_refetch_user(user), {:ok, _} <- maybe_refetch_user(actor),
%Object{} <- Object.normalize(data["object"], fetch: true) do {:ok, activity, _meta} <- Pipeline.common_pipeline(data, local: false) do
# Mastodon sends pin/unpin objects without id, to, cc fields {:ok, activity}
data =
data
|> Map.put_new("id", Utils.generate_activity_id())
|> Map.put_new("to", [Pleroma.Constants.as_public()])
|> Map.put_new("cc", [user.follower_address])
case Pipeline.common_pipeline(data,
local: false,
featured_address: user.featured_address
) do
{:ok, activity, _meta} -> {:ok, activity}
error -> error
end
end end
end end

View file

@ -421,8 +421,7 @@ def pin(id, %User{} = user) do
{:ok, _pin, _} <- {:ok, _pin, _} <-
Pipeline.common_pipeline(pin_data, Pipeline.common_pipeline(pin_data,
local: true, local: true,
activity_id: id, activity_id: id
featured_address: user.featured_address
) do ) do
{:ok, activity} {:ok, activity}
else else