mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-06-01 08:28:06 +00:00
Fix deletion by making it possible to insert activities as a deactivated user
This commit is contained in:
parent
4286a383df
commit
ce20a74b20
|
@ -37,7 +37,7 @@ defmodule Pleroma.Object.Fetcher do
|
||||||
Logger.debug("Reinjecting object #{new_data["id"]}")
|
Logger.debug("Reinjecting object #{new_data["id"]}")
|
||||||
|
|
||||||
with data <- maybe_reinject_internal_fields(object, new_data),
|
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 <- Object.change(object, %{data: data}),
|
||||||
changeset <- touch_changeset(changeset),
|
changeset <- touch_changeset(changeset),
|
||||||
{:ok, object} <- Repo.insert_or_update(changeset),
|
{:ok, object} <- Repo.insert_or_update(changeset),
|
||||||
|
|
|
@ -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{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
|
defp delete_or_deactivate(%User{local: true} = user) do
|
||||||
status = account_status(user)
|
user
|
||||||
|
|> purge_user_changeset()
|
||||||
case status do
|
|> update_and_set_cache()
|
||||||
:confirmation_pending ->
|
|
||||||
delete_and_invalidate_cache(user)
|
|
||||||
|
|
||||||
:approval_pending ->
|
|
||||||
delete_and_invalidate_cache(user)
|
|
||||||
|
|
||||||
_ ->
|
|
||||||
user
|
|
||||||
|> purge_user_changeset()
|
|
||||||
|> update_and_set_cache()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform(:force_password_reset, user), do: force_password_reset(user)
|
def perform(:force_password_reset, user), do: force_password_reset(user)
|
||||||
|
@ -1778,7 +1773,7 @@ defmodule Pleroma.User do
|
||||||
user
|
user
|
||||||
|> get_friends()
|
|> get_friends()
|
||||||
|> Enum.each(fn followed ->
|
|> Enum.each(fn followed ->
|
||||||
ActivityPub.unfollow(user, followed)
|
ActivityPub.unfollow(user, followed, nil, true, true)
|
||||||
unfollow(user, followed)
|
unfollow(user, followed)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -1904,7 +1899,7 @@ defmodule Pleroma.User do
|
||||||
defp delete_activity(%{data: %{"type" => "Create", "object" => object}} = activity, user) do
|
defp delete_activity(%{data: %{"type" => "Create", "object" => object}} = activity, user) do
|
||||||
with {_, %Object{}} <- {:find_object, Object.get_by_ap_id(object)},
|
with {_, %Object{}} <- {:find_object, Object.get_by_ap_id(object)},
|
||||||
{:ok, delete_data, _} <- Builder.delete(user, object) do
|
{: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
|
else
|
||||||
{:find_object, nil} ->
|
{:find_object, nil} ->
|
||||||
# We have the create activity, but not the object, it was probably pruned.
|
# 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)
|
defp delete_activity(%{data: %{"type" => type}} = activity, user)
|
||||||
when type in ["Like", "Announce"] do
|
when type in ["Like", "Announce"] do
|
||||||
with {:ok, undo, _} <- Builder.undo(user, activity) 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
|
else
|
||||||
e -> e
|
e -> e
|
||||||
end
|
end
|
||||||
|
|
|
@ -318,20 +318,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
end
|
end
|
||||||
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()}
|
{: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} <-
|
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
|
result
|
||||||
end
|
end
|
||||||
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),
|
with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
|
||||||
{:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
|
{:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
|
||||||
unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
|
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),
|
_ <- notify_and_stream(activity),
|
||||||
:ok <- maybe_federate(activity) do
|
:ok <- maybe_federate(activity) do
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
|
|
|
@ -41,7 +41,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
when type in ~w[Accept Reject] do
|
when type in ~w[Accept Reject] do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> AcceptRejectValidator.cast_and_validate()
|
|> AcceptRejectValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -51,7 +51,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Event"} = object, meta) do
|
def validate(%{"type" => "Event"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> EventValidator.cast_and_validate()
|
|> EventValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -61,7 +61,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Follow"} = object, meta) do
|
def validate(%{"type" => "Follow"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> FollowValidator.cast_and_validate()
|
|> FollowValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -71,7 +71,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Block"} = block_activity, meta) do
|
def validate(%{"type" => "Block"} = block_activity, meta) do
|
||||||
with {:ok, block_activity} <-
|
with {:ok, block_activity} <-
|
||||||
block_activity
|
block_activity
|
||||||
|> BlockValidator.cast_and_validate()
|
|> BlockValidator.cast_and_validate(meta)
|
||||||
|> Ecto.Changeset.apply_action(:insert) do
|
|> Ecto.Changeset.apply_action(:insert) do
|
||||||
block_activity = stringify_keys(block_activity)
|
block_activity = stringify_keys(block_activity)
|
||||||
outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
|
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
|
def validate(%{"type" => "Update"} = update_activity, meta) do
|
||||||
with {:ok, update_activity} <-
|
with {:ok, update_activity} <-
|
||||||
update_activity
|
update_activity
|
||||||
|> UpdateValidator.cast_and_validate()
|
|> UpdateValidator.cast_and_validate(meta)
|
||||||
|> Ecto.Changeset.apply_action(:insert) do
|
|> Ecto.Changeset.apply_action(:insert) do
|
||||||
update_activity = stringify_keys(update_activity)
|
update_activity = stringify_keys(update_activity)
|
||||||
{:ok, update_activity, meta}
|
{:ok, update_activity, meta}
|
||||||
|
@ -100,7 +100,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Undo"} = object, meta) do
|
def validate(%{"type" => "Undo"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> UndoValidator.cast_and_validate()
|
|> UndoValidator.cast_and_validate(meta)
|
||||||
|> Ecto.Changeset.apply_action(:insert) do
|
|> Ecto.Changeset.apply_action(:insert) do
|
||||||
object = stringify_keys(object)
|
object = stringify_keys(object)
|
||||||
undone_object = Activity.get_by_ap_id(object["object"])
|
undone_object = Activity.get_by_ap_id(object["object"])
|
||||||
|
@ -114,7 +114,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate(%{"type" => "Delete"} = object, meta) do
|
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),
|
do_not_federate <- DeleteValidator.do_not_federate?(cng),
|
||||||
{:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
|
{:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
|
||||||
object = stringify_keys(object)
|
object = stringify_keys(object)
|
||||||
|
@ -126,7 +126,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Like"} = object, meta) do
|
def validate(%{"type" => "Like"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> LikeValidator.cast_and_validate()
|
|> LikeValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -146,7 +146,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Question"} = object, meta) do
|
def validate(%{"type" => "Question"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> QuestionValidator.cast_and_validate()
|
|> QuestionValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -156,7 +156,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => type} = object, meta) when type in ~w[Audio Video] do
|
def validate(%{"type" => type} = object, meta) when type in ~w[Audio Video] do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> AudioVideoValidator.cast_and_validate()
|
|> AudioVideoValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -166,7 +166,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Article"} = object, meta) do
|
def validate(%{"type" => "Article"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> ArticleNoteValidator.cast_and_validate()
|
|> ArticleNoteValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -176,7 +176,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Answer"} = object, meta) do
|
def validate(%{"type" => "Answer"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> AnswerValidator.cast_and_validate()
|
|> AnswerValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -186,7 +186,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "EmojiReact"} = object, meta) do
|
def validate(%{"type" => "EmojiReact"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> EmojiReactValidator.cast_and_validate()
|
|> EmojiReactValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
@ -227,7 +227,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
||||||
def validate(%{"type" => "Announce"} = object, meta) do
|
def validate(%{"type" => "Announce"} = object, meta) do
|
||||||
with {:ok, object} <-
|
with {:ok, object} <-
|
||||||
object
|
object
|
||||||
|> AnnounceValidator.cast_and_validate()
|
|> AnnounceValidator.cast_and_validate(meta)
|
||||||
|> 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}
|
||||||
|
|
|
@ -27,19 +27,19 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator do
|
||||||
|> cast(data, __schema__(:fields))
|
|> cast(data, __schema__(:fields))
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(cng) do
|
def validate_data(cng, meta \\ []) do
|
||||||
cng
|
cng
|
||||||
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
||||||
|> validate_inclusion(:type, ["Accept", "Reject"])
|
|> validate_inclusion(:type, ["Accept", "Reject"])
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_object_presence(allowed_types: ["Follow"])
|
|> validate_object_presence(allowed_types: ["Follow"])
|
||||||
|> validate_accept_reject_rights()
|
|> validate_accept_reject_rights()
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data
|
|> cast_data
|
||||||
|> validate_data
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_accept_reject_rights(cng) do
|
def validate_accept_reject_rights(cng) do
|
||||||
|
|
|
@ -29,10 +29,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator do
|
||||||
field(:published, ObjectValidators.DateTime)
|
field(:published, ObjectValidators.DateTime)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -50,11 +50,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator do
|
||||||
cng
|
cng
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["Announce"])
|
|> validate_inclusion(:type, ["Announce"])
|
||||||
|> validate_required([:id, :type, :object, :actor, :to, :cc])
|
|> validate_required([:id, :type, :object, :actor, :to, :cc])
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_object_presence()
|
|> validate_object_presence()
|
||||||
|> validate_existing_announce()
|
|> validate_existing_announce()
|
||||||
|> validate_announcable()
|
|> validate_announcable()
|
||||||
|
|
|
@ -34,10 +34,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do
|
||||||
|> apply_action(:insert)
|
|> apply_action(:insert)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -50,13 +50,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator do
|
||||||
|> cast(data, __schema__(:fields))
|
|> cast(data, __schema__(:fields))
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["Answer"])
|
|> validate_inclusion(:type, ["Answer"])
|
||||||
|> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
|
|> validate_required([:id, :inReplyTo, :name, :attributedTo, :actor])
|
||||||
|> CommonValidations.validate_any_presence([:cc, :to])
|
|> CommonValidations.validate_any_presence([:cc, :to])
|
||||||
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
||||||
|> CommonValidations.validate_actor_presence()
|
|> CommonValidations.validate_actor_presence(meta)
|
||||||
|> CommonValidations.validate_host_match()
|
|> CommonValidations.validate_host_match()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -58,10 +58,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidator do
|
||||||
|> apply_action(:insert)
|
|> apply_action(:insert)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -94,13 +94,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidator do
|
||||||
|> cast_embed(:attachment)
|
|> cast_embed(:attachment)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["Article", "Note"])
|
|> validate_inclusion(:type, ["Article", "Note"])
|
||||||
|> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
|
|> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
|
||||||
|> CommonValidations.validate_any_presence([:cc, :to])
|
|> CommonValidations.validate_any_presence([:cc, :to])
|
||||||
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
||||||
|> CommonValidations.validate_actor_presence()
|
|> CommonValidations.validate_actor_presence(meta)
|
||||||
|> CommonValidations.validate_host_match()
|
|> CommonValidations.validate_host_match()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,10 +59,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator do
|
||||||
|> apply_action(:insert)
|
|> apply_action(:insert)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -122,13 +122,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator do
|
||||||
|> cast_embed(:attachment)
|
|> cast_embed(:attachment)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["Audio", "Video"])
|
|> validate_inclusion(:type, ["Audio", "Video"])
|
||||||
|> validate_required([:id, :actor, :attributedTo, :type, :context, :attachment])
|
|> validate_required([:id, :actor, :attributedTo, :type, :context, :attachment])
|
||||||
|> CommonValidations.validate_any_presence([:cc, :to])
|
|> CommonValidations.validate_any_presence([:cc, :to])
|
||||||
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
||||||
|> CommonValidations.validate_actor_presence()
|
|> CommonValidations.validate_actor_presence(meta)
|
||||||
|> CommonValidations.validate_host_match()
|
|> CommonValidations.validate_host_match()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,17 +26,17 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator do
|
||||||
|> cast(data, __schema__(:fields))
|
|> cast(data, __schema__(:fields))
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(cng) do
|
def validate_data(cng, meta \\ []) do
|
||||||
cng
|
cng
|
||||||
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
||||||
|> validate_inclusion(:type, ["Block"])
|
|> validate_inclusion(:type, ["Block"])
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_actor_presence(field_name: :object)
|
|> validate_actor_presence(field_name: :object)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data
|
|> cast_data
|
||||||
|> validate_data
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,7 +36,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
|
||||||
|> validate_change(field_name, fn field_name, actor ->
|
|> validate_change(field_name, fn field_name, actor ->
|
||||||
case User.get_cached_by_ap_id(actor) do
|
case User.get_cached_by_ap_id(actor) do
|
||||||
%User{is_active: false} ->
|
%User{is_active: false} ->
|
||||||
[{field_name, "user is deactivated"}]
|
unless options[:allow_deactivated_actor] do
|
||||||
|
[{field_name, "user is deactivated"}]
|
||||||
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
%User{} ->
|
%User{} ->
|
||||||
[]
|
[]
|
||||||
|
|
|
@ -83,7 +83,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do
|
||||||
cng
|
cng
|
||||||
|> validate_required([:actor, :type, :object])
|
|> validate_required([:actor, :type, :object])
|
||||||
|> validate_inclusion(:type, ["Create"])
|
|> validate_inclusion(:type, ["Create"])
|
||||||
|> CommonValidations.validate_actor_presence()
|
|> CommonValidations.validate_actor_presence(meta)
|
||||||
|> CommonValidations.validate_any_presence([:to, :cc])
|
|> CommonValidations.validate_any_presence([:to, :cc])
|
||||||
|> validate_actors_match(meta)
|
|> validate_actors_match(meta)
|
||||||
|> validate_context_match(meta)
|
|> validate_context_match(meta)
|
||||||
|
|
|
@ -53,11 +53,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator do
|
||||||
Tombstone
|
Tombstone
|
||||||
Video
|
Video
|
||||||
}
|
}
|
||||||
def validate_data(cng) do
|
def validate_data(cng, meta \\ []) do
|
||||||
cng
|
cng
|
||||||
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
||||||
|> validate_inclusion(:type, ["Delete"])
|
|> validate_inclusion(:type, ["Delete"])
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_modification_rights()
|
|> validate_modification_rights()
|
||||||
|> validate_object_or_user_presence(allowed_types: @deletable_types)
|
|> validate_object_or_user_presence(allowed_types: @deletable_types)
|
||||||
|> add_deleted_activity_id()
|
|> add_deleted_activity_id()
|
||||||
|
@ -67,9 +67,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator do
|
||||||
!same_domain?(cng)
|
!same_domain?(cng)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data
|
|> cast_data
|
||||||
|> validate_data
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -24,10 +24,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
||||||
field(:cc, ObjectValidators.Recipients, default: [])
|
field(:cc, ObjectValidators.Recipients, default: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -70,11 +70,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["EmojiReact"])
|
|> validate_inclusion(:type, ["EmojiReact"])
|
||||||
|> validate_required([:id, :type, :object, :actor, :context, :to, :cc, :content])
|
|> validate_required([:id, :type, :object, :actor, :context, :to, :cc, :content])
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_object_presence()
|
|> validate_object_presence()
|
||||||
|> validate_emoji()
|
|> validate_emoji()
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,10 +59,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EventValidator do
|
||||||
|> apply_action(:insert)
|
|> apply_action(:insert)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -85,13 +85,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EventValidator do
|
||||||
|> cast_embed(:attachment)
|
|> cast_embed(:attachment)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["Event"])
|
|> validate_inclusion(:type, ["Event"])
|
||||||
|> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
|
|> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
|
||||||
|> CommonValidations.validate_any_presence([:cc, :to])
|
|> CommonValidations.validate_any_presence([:cc, :to])
|
||||||
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
||||||
|> CommonValidations.validate_actor_presence()
|
|> CommonValidations.validate_actor_presence(meta)
|
||||||
|> CommonValidations.validate_host_match()
|
|> CommonValidations.validate_host_match()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,18 +27,18 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator do
|
||||||
|> cast(data, __schema__(:fields))
|
|> cast(data, __schema__(:fields))
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(cng) do
|
def validate_data(cng, meta \\ []) do
|
||||||
cng
|
cng
|
||||||
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
||||||
|> validate_inclusion(:type, ["Follow"])
|
|> validate_inclusion(:type, ["Follow"])
|
||||||
|> validate_inclusion(:state, ~w{pending reject accept})
|
|> validate_inclusion(:state, ~w{pending reject accept})
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_actor_presence(field_name: :object)
|
|> validate_actor_presence(field_name: :object)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data
|
|> cast_data
|
||||||
|> validate_data
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -24,10 +24,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
|
||||||
field(:cc, ObjectValidators.Recipients, default: [])
|
field(:cc, ObjectValidators.Recipients, default: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -76,11 +76,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta) 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_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_object_presence()
|
|> validate_object_presence()
|
||||||
|> validate_existing_like()
|
|> validate_existing_like()
|
||||||
end
|
end
|
||||||
|
|
|
@ -62,10 +62,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
|
||||||
|> apply_action(:insert)
|
|> apply_action(:insert)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -99,13 +99,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
|
||||||
|> cast_embed(:oneOf)
|
|> cast_embed(:oneOf)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["Question"])
|
|> validate_inclusion(:type, ["Question"])
|
||||||
|> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
|
|> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
|
||||||
|> CommonValidations.validate_any_presence([:cc, :to])
|
|> CommonValidations.validate_any_presence([:cc, :to])
|
||||||
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
||||||
|> CommonValidations.validate_actor_presence()
|
|> CommonValidations.validate_actor_presence(meta)
|
||||||
|> CommonValidations.validate_any_presence([:oneOf, :anyOf])
|
|> CommonValidations.validate_any_presence([:oneOf, :anyOf])
|
||||||
|> CommonValidations.validate_host_match()
|
|> CommonValidations.validate_host_match()
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,10 +22,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator do
|
||||||
field(:cc, ObjectValidators.Recipients, default: [])
|
field(:cc, ObjectValidators.Recipients, default: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data()
|
|> cast_data()
|
||||||
|> validate_data()
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_data(data) do
|
def cast_data(data) do
|
||||||
|
@ -38,11 +38,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator do
|
||||||
|> cast(data, __schema__(:fields))
|
|> cast(data, __schema__(:fields))
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(data_cng) do
|
def validate_data(data_cng, meta \\ []) do
|
||||||
data_cng
|
data_cng
|
||||||
|> validate_inclusion(:type, ["Undo"])
|
|> validate_inclusion(:type, ["Undo"])
|
||||||
|> validate_required([:id, :type, :object, :actor, :to, :cc])
|
|> validate_required([:id, :type, :object, :actor, :to, :cc])
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_object_presence()
|
|> validate_object_presence()
|
||||||
|> validate_undo_rights()
|
|> validate_undo_rights()
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,18 +28,18 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator do
|
||||||
|> cast(data, __schema__(:fields))
|
|> cast(data, __schema__(:fields))
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_data(cng) do
|
def validate_data(cng, meta \\ []) do
|
||||||
cng
|
cng
|
||||||
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
||||||
|> validate_inclusion(:type, ["Update"])
|
|> validate_inclusion(:type, ["Update"])
|
||||||
|> validate_actor_presence()
|
|> validate_actor_presence(meta)
|
||||||
|> validate_updating_rights()
|
|> validate_updating_rights()
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_and_validate(data) do
|
def cast_and_validate(data, meta) do
|
||||||
data
|
data
|
||||||
|> cast_data
|
|> cast_data
|
||||||
|> validate_data
|
|> validate_data(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
# For now we only support updating users, and here the rule is easy:
|
# For now we only support updating users, and here the rule is easy:
|
||||||
|
|
|
@ -22,7 +22,9 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do
|
||||||
@activity_pub Config.get([:pipeline, :activity_pub], ActivityPub)
|
@activity_pub Config.get([:pipeline, :activity_pub], ActivityPub)
|
||||||
@config Config.get([:pipeline, :config], Config)
|
@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()}
|
{:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
|
||||||
def common_pipeline(object, meta) do
|
def common_pipeline(object, meta) do
|
||||||
case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do
|
case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do
|
||||||
|
|
|
@ -29,7 +29,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidatorTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "a basic note validates", %{note: note} do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,7 +37,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "is valid for a valid object", %{valid_like: valid_like} do
|
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
|
end
|
||||||
|
|
||||||
test "sets the 'to' field to the object actor if no recipients are given", %{
|
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
|
test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
|
||||||
without_actor = Map.delete(valid_like, "actor")
|
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")
|
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
|
end
|
||||||
|
|
||||||
test "it errors when the object is missing or not known", %{valid_like: valid_like} do
|
test "it errors when the object is missing or not known", %{valid_like: valid_like} do
|
||||||
without_object = Map.delete(valid_like, "object")
|
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")
|
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
|
end
|
||||||
|
|
||||||
test "it errors when the actor has already like the object", %{
|
test "it errors when the actor has already like the object", %{
|
||||||
|
@ -93,7 +93,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidationTest do
|
||||||
} do
|
} do
|
||||||
_like = CommonAPI.favorite(user, post_activity.id)
|
_like = CommonAPI.favorite(user, post_activity.id)
|
||||||
|
|
||||||
refute LikeValidator.cast_and_validate(valid_like).valid?
|
refute LikeValidator.cast_and_validate(valid_like, []).valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
|
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("actor", %{"id" => valid_like["actor"]})
|
||||||
|> Map.put("object", %{"id" => valid_like["object"]})
|
|> Map.put("object", %{"id" => valid_like["object"]})
|
||||||
|
|
||||||
validated = LikeValidator.cast_and_validate(wrapped_like)
|
validated = LikeValidator.cast_and_validate(wrapped_like, [])
|
||||||
|
|
||||||
assert validated.valid?
|
assert validated.valid?
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue