From ce20a74b204d70c13397a9bdfd9d7d97eed1eeda Mon Sep 17 00:00:00 2001 From: rinpatch Date: Thu, 25 Feb 2021 12:41:22 +0300 Subject: [PATCH] Fix deletion by making it possible to insert activities as a deactivated user --- lib/pleroma/object/fetcher.ex | 2 +- lib/pleroma/user.ex | 29 +++++++++------------- lib/pleroma/web/activity_pub/activity_pub.ex | 12 +++++---- lib/pleroma/web/activity_pub/object_validator.ex | 28 ++++++++++----------- .../object_validators/accept_reject_validator.ex | 8 +++--- .../object_validators/announce_validator.ex | 8 +++--- .../object_validators/answer_validator.ex | 8 +++--- .../object_validators/article_note_validator.ex | 8 +++--- .../object_validators/audio_video_validator.ex | 8 +++--- .../object_validators/block_validator.ex | 8 +++--- .../object_validators/common_validations.ex | 6 ++++- .../object_validators/create_generic_validator.ex | 2 +- .../object_validators/delete_validator.ex | 8 +++--- .../object_validators/emoji_react_validator.ex | 8 +++--- .../object_validators/event_validator.ex | 8 +++--- .../object_validators/follow_validator.ex | 8 +++--- .../object_validators/like_validator.ex | 8 +++--- .../object_validators/question_validator.ex | 8 +++--- .../object_validators/undo_validator.ex | 8 +++--- .../object_validators/update_validator.ex | 8 +++--- lib/pleroma/web/activity_pub/pipeline.ex | 4 ++- .../article_note_validator_test.exs | 2 +- .../object_validators/like_validation_test.exs | 14 +++++------ 23 files changed, 107 insertions(+), 104 deletions(-) diff --git a/lib/pleroma/object/fetcher.ex b/lib/pleroma/object/fetcher.ex index bcccf1c4c..9bc798cec 100644 --- a/lib/pleroma/object/fetcher.ex +++ b/lib/pleroma/object/fetcher.ex @@ -37,7 +37,7 @@ defmodule Pleroma.Object.Fetcher do Logger.debug("Reinjecting object #{new_data["id"]}") with data <- maybe_reinject_internal_fields(object, new_data), - {:ok, data, _} <- ObjectValidator.validate(data, %{}), + {:ok, data, _} <- ObjectValidator.validate(data, []), changeset <- Object.change(object, %{data: data}), changeset <- touch_changeset(changeset), {:ok, object} <- Repo.insert_or_update(changeset), diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 9837166ea..66815c729 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1738,21 +1738,16 @@ defmodule Pleroma.User do defp delete_or_deactivate(%User{local: false} = user), do: delete_and_invalidate_cache(user) + defp delete_or_deactivate(%User{is_confirmed: false} = user), + do: delete_and_invalidate_cache(user) + + defp delete_or_deactivate(%User{is_approved: false} = user), + do: delete_and_invalidate_cache(user) + defp delete_or_deactivate(%User{local: true} = user) do - status = account_status(user) - - case status do - :confirmation_pending -> - delete_and_invalidate_cache(user) - - :approval_pending -> - delete_and_invalidate_cache(user) - - _ -> - user - |> purge_user_changeset() - |> update_and_set_cache() - end + user + |> purge_user_changeset() + |> update_and_set_cache() end def perform(:force_password_reset, user), do: force_password_reset(user) @@ -1778,7 +1773,7 @@ defmodule Pleroma.User do user |> get_friends() |> Enum.each(fn followed -> - ActivityPub.unfollow(user, followed) + ActivityPub.unfollow(user, followed, nil, true, true) unfollow(user, followed) end) @@ -1904,7 +1899,7 @@ defmodule Pleroma.User do defp delete_activity(%{data: %{"type" => "Create", "object" => object}} = activity, user) do with {_, %Object{}} <- {:find_object, Object.get_by_ap_id(object)}, {:ok, delete_data, _} <- Builder.delete(user, object) do - Pipeline.common_pipeline(delete_data, local: user.local) + Pipeline.common_pipeline(delete_data, local: user.local, allow_deactivated_actor: true) else {:find_object, nil} -> # We have the create activity, but not the object, it was probably pruned. @@ -1922,7 +1917,7 @@ defmodule Pleroma.User do defp delete_activity(%{data: %{"type" => type}} = activity, user) when type in ["Like", "Announce"] do with {:ok, undo, _} <- Builder.undo(user, activity) do - Pipeline.common_pipeline(undo, local: user.local) + Pipeline.common_pipeline(undo, local: user.local, allow_deactivated_actor: true) else e -> e end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 5b45e2ca1..db9baed8d 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -318,20 +318,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - @spec unfollow(User.t(), User.t(), String.t() | nil, boolean()) :: + @spec unfollow(User.t(), User.t(), String.t() | nil, boolean(), boolean()) :: {:ok, Activity.t()} | nil | {:error, any()} - def unfollow(follower, followed, activity_id \\ nil, local \\ true) do + def unfollow(follower, followed, activity_id \\ nil, local \\ true, bypass_actor_check \\ false) do with {:ok, result} <- - Repo.transaction(fn -> do_unfollow(follower, followed, activity_id, local) end) do + Repo.transaction(fn -> + do_unfollow(follower, followed, activity_id, local, bypass_actor_check) + end) do result end end - defp do_unfollow(follower, followed, activity_id, local) do + defp do_unfollow(follower, followed, activity_id, local, bypass_actor_check) do with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed), {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"), unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id), - {:ok, activity} <- insert(unfollow_data, local), + {:ok, activity} <- insert(unfollow_data, local, false, bypass_actor_check), _ <- notify_and_stream(activity), :ok <- maybe_federate(activity) do {:ok, activity} diff --git a/lib/pleroma/web/activity_pub/object_validator.ex b/lib/pleroma/web/activity_pub/object_validator.ex index 297c19cc0..7177f9d20 100644 --- a/lib/pleroma/web/activity_pub/object_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validator.ex @@ -41,7 +41,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do when type in ~w[Accept Reject] do with {:ok, object} <- object - |> AcceptRejectValidator.cast_and_validate() + |> AcceptRejectValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -51,7 +51,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Event"} = object, meta) do with {:ok, object} <- object - |> EventValidator.cast_and_validate() + |> EventValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -61,7 +61,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Follow"} = object, meta) do with {:ok, object} <- object - |> FollowValidator.cast_and_validate() + |> FollowValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -71,7 +71,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Block"} = block_activity, meta) do with {:ok, block_activity} <- block_activity - |> BlockValidator.cast_and_validate() + |> BlockValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do block_activity = stringify_keys(block_activity) outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks]) @@ -90,7 +90,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Update"} = update_activity, meta) do with {:ok, update_activity} <- update_activity - |> UpdateValidator.cast_and_validate() + |> UpdateValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do update_activity = stringify_keys(update_activity) {:ok, update_activity, meta} @@ -100,7 +100,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Undo"} = object, meta) do with {:ok, object} <- object - |> UndoValidator.cast_and_validate() + |> UndoValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) undone_object = Activity.get_by_ap_id(object["object"]) @@ -114,7 +114,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do end def validate(%{"type" => "Delete"} = object, meta) do - with cng <- DeleteValidator.cast_and_validate(object), + with cng <- DeleteValidator.cast_and_validate(object, meta), do_not_federate <- DeleteValidator.do_not_federate?(cng), {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do object = stringify_keys(object) @@ -126,7 +126,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Like"} = object, meta) do with {:ok, object} <- object - |> LikeValidator.cast_and_validate() + |> LikeValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -146,7 +146,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Question"} = object, meta) do with {:ok, object} <- object - |> QuestionValidator.cast_and_validate() + |> QuestionValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -156,7 +156,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => type} = object, meta) when type in ~w[Audio Video] do with {:ok, object} <- object - |> AudioVideoValidator.cast_and_validate() + |> AudioVideoValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -166,7 +166,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Article"} = object, meta) do with {:ok, object} <- object - |> ArticleNoteValidator.cast_and_validate() + |> ArticleNoteValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -176,7 +176,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Answer"} = object, meta) do with {:ok, object} <- object - |> AnswerValidator.cast_and_validate() + |> AnswerValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -186,7 +186,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "EmojiReact"} = object, meta) do with {:ok, object} <- object - |> EmojiReactValidator.cast_and_validate() + |> EmojiReactValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} @@ -227,7 +227,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do def validate(%{"type" => "Announce"} = object, meta) do with {:ok, object} <- object - |> AnnounceValidator.cast_and_validate() + |> AnnounceValidator.cast_and_validate(meta) |> Ecto.Changeset.apply_action(:insert) do object = stringify_keys(object) {:ok, object, meta} diff --git a/lib/pleroma/web/activity_pub/object_validators/accept_reject_validator.ex b/lib/pleroma/web/activity_pub/object_validators/accept_reject_validator.ex index d31e780c3..d109a686e 100644 --- a/lib/pleroma/web/activity_pub/object_validators/accept_reject_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/accept_reject_validator.ex @@ -27,19 +27,19 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator do |> cast(data, __schema__(:fields)) end - def validate_data(cng) do + def validate_data(cng, meta \\ []) do cng |> validate_required([:id, :type, :actor, :to, :cc, :object]) |> validate_inclusion(:type, ["Accept", "Reject"]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_object_presence(allowed_types: ["Follow"]) |> validate_accept_reject_rights() end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data - |> validate_data + |> validate_data(meta) end def validate_accept_reject_rights(cng) do diff --git a/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex b/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex index b08a33e68..9d67cf941 100644 --- a/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex @@ -29,10 +29,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator do field(:published, ObjectValidators.DateTime) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -50,11 +50,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator do cng end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["Announce"]) |> validate_required([:id, :type, :object, :actor, :to, :cc]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_object_presence() |> validate_existing_announce() |> validate_announcable() diff --git a/lib/pleroma/web/activity_pub/object_validators/answer_validator.ex b/lib/pleroma/web/activity_pub/object_validators/answer_validator.ex index 15e4413cd..0a85ff9d3 100644 --- a/lib/pleroma/web/activity_pub/object_validators/answer_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/answer_validator.ex @@ -34,10 +34,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do |> apply_action(:insert) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -50,13 +50,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do |> cast(data, __schema__(:fields)) end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["Answer"]) |> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor]) |> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_fields_match([:actor, :attributedTo]) - |> CommonValidations.validate_actor_presence() + |> CommonValidations.validate_actor_presence(meta) |> CommonValidations.validate_host_match() end end diff --git a/lib/pleroma/web/activity_pub/object_validators/article_note_validator.ex b/lib/pleroma/web/activity_pub/object_validators/article_note_validator.ex index b0388ef3b..e644d8a2d 100644 --- a/lib/pleroma/web/activity_pub/object_validators/article_note_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/article_note_validator.ex @@ -58,10 +58,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidator do |> apply_action(:insert) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -94,13 +94,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidator do |> cast_embed(:attachment) end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["Article", "Note"]) |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id]) |> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_fields_match([:actor, :attributedTo]) - |> CommonValidations.validate_actor_presence() + |> CommonValidations.validate_actor_presence(meta) |> CommonValidations.validate_host_match() end end diff --git a/lib/pleroma/web/activity_pub/object_validators/audio_video_validator.ex b/lib/pleroma/web/activity_pub/object_validators/audio_video_validator.ex index b3e738d8d..444aa361e 100644 --- a/lib/pleroma/web/activity_pub/object_validators/audio_video_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/audio_video_validator.ex @@ -59,10 +59,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator do |> apply_action(:insert) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -122,13 +122,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator do |> cast_embed(:attachment) end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["Audio", "Video"]) |> validate_required([:id, :actor, :attributedTo, :type, :context, :attachment]) |> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_fields_match([:actor, :attributedTo]) - |> CommonValidations.validate_actor_presence() + |> CommonValidations.validate_actor_presence(meta) |> CommonValidations.validate_host_match() end end diff --git a/lib/pleroma/web/activity_pub/object_validators/block_validator.ex b/lib/pleroma/web/activity_pub/object_validators/block_validator.ex index c5f77bb76..282c41bf3 100644 --- a/lib/pleroma/web/activity_pub/object_validators/block_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/block_validator.ex @@ -26,17 +26,17 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator do |> cast(data, __schema__(:fields)) end - def validate_data(cng) do + def validate_data(cng, meta \\ []) do cng |> validate_required([:id, :type, :actor, :to, :cc, :object]) |> validate_inclusion(:type, ["Block"]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_actor_presence(field_name: :object) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data - |> validate_data + |> validate_data(meta) end end diff --git a/lib/pleroma/web/activity_pub/object_validators/common_validations.ex b/lib/pleroma/web/activity_pub/object_validators/common_validations.ex index 093549a45..933c8f384 100644 --- a/lib/pleroma/web/activity_pub/object_validators/common_validations.ex +++ b/lib/pleroma/web/activity_pub/object_validators/common_validations.ex @@ -36,7 +36,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do |> validate_change(field_name, fn field_name, actor -> case User.get_cached_by_ap_id(actor) do %User{is_active: false} -> - [{field_name, "user is deactivated"}] + unless options[:allow_deactivated_actor] do + [{field_name, "user is deactivated"}] + else + [] + end %User{} -> [] diff --git a/lib/pleroma/web/activity_pub/object_validators/create_generic_validator.ex b/lib/pleroma/web/activity_pub/object_validators/create_generic_validator.ex index bf56a918c..4796af548 100644 --- a/lib/pleroma/web/activity_pub/object_validators/create_generic_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/create_generic_validator.ex @@ -83,7 +83,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do cng |> validate_required([:actor, :type, :object]) |> validate_inclusion(:type, ["Create"]) - |> CommonValidations.validate_actor_presence() + |> CommonValidations.validate_actor_presence(meta) |> CommonValidations.validate_any_presence([:to, :cc]) |> validate_actors_match(meta) |> validate_context_match(meta) diff --git a/lib/pleroma/web/activity_pub/object_validators/delete_validator.ex b/lib/pleroma/web/activity_pub/object_validators/delete_validator.ex index fc1a79a72..0e6a6fac3 100644 --- a/lib/pleroma/web/activity_pub/object_validators/delete_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/delete_validator.ex @@ -53,11 +53,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator do Tombstone Video } - def validate_data(cng) do + def validate_data(cng, meta \\ []) do cng |> validate_required([:id, :type, :actor, :to, :cc, :object]) |> validate_inclusion(:type, ["Delete"]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_modification_rights() |> validate_object_or_user_presence(allowed_types: @deletable_types) |> add_deleted_activity_id() @@ -67,9 +67,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator do !same_domain?(cng) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data - |> validate_data + |> validate_data(meta) end end diff --git a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex index 1906e597e..b2006759b 100644 --- a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex @@ -24,10 +24,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do field(:cc, ObjectValidators.Recipients, default: []) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -70,11 +70,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do end end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["EmojiReact"]) |> validate_required([:id, :type, :object, :actor, :context, :to, :cc, :content]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_object_presence() |> validate_emoji() end diff --git a/lib/pleroma/web/activity_pub/object_validators/event_validator.ex b/lib/pleroma/web/activity_pub/object_validators/event_validator.ex index 2e26726f8..065c91e1a 100644 --- a/lib/pleroma/web/activity_pub/object_validators/event_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/event_validator.ex @@ -59,10 +59,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EventValidator do |> apply_action(:insert) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -85,13 +85,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EventValidator do |> cast_embed(:attachment) end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["Event"]) |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id]) |> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_fields_match([:actor, :attributedTo]) - |> CommonValidations.validate_actor_presence() + |> CommonValidations.validate_actor_presence(meta) |> CommonValidations.validate_host_match() end end diff --git a/lib/pleroma/web/activity_pub/object_validators/follow_validator.ex b/lib/pleroma/web/activity_pub/object_validators/follow_validator.ex index 6e428bacc..56509ba98 100644 --- a/lib/pleroma/web/activity_pub/object_validators/follow_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/follow_validator.ex @@ -27,18 +27,18 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator do |> cast(data, __schema__(:fields)) end - def validate_data(cng) do + def validate_data(cng, meta \\ []) do cng |> validate_required([:id, :type, :actor, :to, :cc, :object]) |> validate_inclusion(:type, ["Follow"]) |> validate_inclusion(:state, ~w{pending reject accept}) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_actor_presence(field_name: :object) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data - |> validate_data + |> validate_data(meta) end end diff --git a/lib/pleroma/web/activity_pub/object_validators/like_validator.ex b/lib/pleroma/web/activity_pub/object_validators/like_validator.ex index 30c40b238..dff1e8444 100644 --- a/lib/pleroma/web/activity_pub/object_validators/like_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/like_validator.ex @@ -24,10 +24,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do field(:cc, ObjectValidators.Recipients, default: []) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -76,11 +76,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do end end - def validate_data(data_cng) do + def validate_data(data_cng, meta) do data_cng |> validate_inclusion(:type, ["Like"]) |> validate_required([:id, :type, :object, :actor, :context, :to, :cc]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_object_presence() |> validate_existing_like() end diff --git a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex index 6b746c997..6c4b109bc 100644 --- a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex @@ -62,10 +62,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do |> apply_action(:insert) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -99,13 +99,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do |> cast_embed(:oneOf) end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["Question"]) |> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id]) |> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_fields_match([:actor, :attributedTo]) - |> CommonValidations.validate_actor_presence() + |> CommonValidations.validate_actor_presence(meta) |> CommonValidations.validate_any_presence([:oneOf, :anyOf]) |> CommonValidations.validate_host_match() end diff --git a/lib/pleroma/web/activity_pub/object_validators/undo_validator.ex b/lib/pleroma/web/activity_pub/object_validators/undo_validator.ex index 783a79ddb..c11f98e5e 100644 --- a/lib/pleroma/web/activity_pub/object_validators/undo_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/undo_validator.ex @@ -22,10 +22,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator do field(:cc, ObjectValidators.Recipients, default: []) end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data() - |> validate_data() + |> validate_data(meta) end def cast_data(data) do @@ -38,11 +38,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator do |> cast(data, __schema__(:fields)) end - def validate_data(data_cng) do + def validate_data(data_cng, meta \\ []) do data_cng |> validate_inclusion(:type, ["Undo"]) |> validate_required([:id, :type, :object, :actor, :to, :cc]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_object_presence() |> validate_undo_rights() end diff --git a/lib/pleroma/web/activity_pub/object_validators/update_validator.ex b/lib/pleroma/web/activity_pub/object_validators/update_validator.ex index a66d41400..bb459ac54 100644 --- a/lib/pleroma/web/activity_pub/object_validators/update_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/update_validator.ex @@ -28,18 +28,18 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator do |> cast(data, __schema__(:fields)) end - def validate_data(cng) do + def validate_data(cng, meta \\ []) do cng |> validate_required([:id, :type, :actor, :to, :cc, :object]) |> validate_inclusion(:type, ["Update"]) - |> validate_actor_presence() + |> validate_actor_presence(meta) |> validate_updating_rights() end - def cast_and_validate(data) do + def cast_and_validate(data, meta) do data |> cast_data - |> validate_data + |> validate_data(meta) end # For now we only support updating users, and here the rule is easy: diff --git a/lib/pleroma/web/activity_pub/pipeline.ex b/lib/pleroma/web/activity_pub/pipeline.ex index 405649fb1..0e23ec79e 100644 --- a/lib/pleroma/web/activity_pub/pipeline.ex +++ b/lib/pleroma/web/activity_pub/pipeline.ex @@ -22,7 +22,9 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do @activity_pub Config.get([:pipeline, :activity_pub], ActivityPub) @config Config.get([:pipeline, :config], Config) - @spec common_pipeline(map(), keyword()) :: + @type common_pipeline_meta_option :: + {:local, boolean()} | {:allow_deactivated_actor, boolean()} | {atom(), term()} + @spec common_pipeline(map(), [common_pipeline_meta_option()]) :: {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()} def common_pipeline(object, meta) do case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do diff --git a/test/pleroma/web/activity_pub/object_validators/article_note_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/article_note_validator_test.exs index e408c85c3..6e9ddf1e0 100644 --- a/test/pleroma/web/activity_pub/object_validators/article_note_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/article_note_validator_test.exs @@ -29,7 +29,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidatorTest do end test "a basic note validates", %{note: note} do - %{valid?: true} = ArticleNoteValidator.cast_and_validate(note) + %{valid?: true} = ArticleNoteValidator.cast_and_validate(note, []) end end end diff --git a/test/pleroma/web/activity_pub/object_validators/like_validation_test.exs b/test/pleroma/web/activity_pub/object_validators/like_validation_test.exs index 55f67232e..992648a7a 100644 --- a/test/pleroma/web/activity_pub/object_validators/like_validation_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/like_validation_test.exs @@ -37,7 +37,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do end test "is valid for a valid object", %{valid_like: valid_like} do - assert LikeValidator.cast_and_validate(valid_like).valid? + assert LikeValidator.cast_and_validate(valid_like, []).valid? end test "sets the 'to' field to the object actor if no recipients are given", %{ @@ -69,21 +69,21 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do test "it errors when the actor is missing or not known", %{valid_like: valid_like} do without_actor = Map.delete(valid_like, "actor") - refute LikeValidator.cast_and_validate(without_actor).valid? + refute LikeValidator.cast_and_validate(without_actor, []).valid? with_invalid_actor = Map.put(valid_like, "actor", "invalidactor") - refute LikeValidator.cast_and_validate(with_invalid_actor).valid? + refute LikeValidator.cast_and_validate(with_invalid_actor, []).valid? end test "it errors when the object is missing or not known", %{valid_like: valid_like} do without_object = Map.delete(valid_like, "object") - refute LikeValidator.cast_and_validate(without_object).valid? + refute LikeValidator.cast_and_validate(without_object, []).valid? with_invalid_object = Map.put(valid_like, "object", "invalidobject") - refute LikeValidator.cast_and_validate(with_invalid_object).valid? + refute LikeValidator.cast_and_validate(with_invalid_object, []).valid? end test "it errors when the actor has already like the object", %{ @@ -93,7 +93,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do } do _like = CommonAPI.favorite(user, post_activity.id) - refute LikeValidator.cast_and_validate(valid_like).valid? + refute LikeValidator.cast_and_validate(valid_like, []).valid? end test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do @@ -102,7 +102,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do |> Map.put("actor", %{"id" => valid_like["actor"]}) |> Map.put("object", %{"id" => valid_like["object"]}) - validated = LikeValidator.cast_and_validate(wrapped_like) + validated = LikeValidator.cast_and_validate(wrapped_like, []) assert validated.valid?