Fix deactivated user deletion

This commit is contained in:
Maxim Filippov 2019-08-09 23:05:28 +03:00
parent 29807ef6a5
commit dfae61c25c
7 changed files with 36 additions and 26 deletions

View File

@ -176,7 +176,7 @@ def run(["rm", nickname]) do
start_pleroma() start_pleroma()
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
User.perform(:delete, user) User.perform(:delete, user, nil)
shell_info("User #{nickname} deleted.") shell_info("User #{nickname} deleted.")
else else
_ -> _ ->

View File

@ -1029,13 +1029,26 @@ def update_notification_settings(%User{} = user, settings \\ %{}) do
|> update_and_set_cache() |> update_and_set_cache()
end end
@spec perform(atom(), User.t()) :: {:ok, User.t()}
def perform(:fetch_initial_posts, %User{} = user) do
pages = Pleroma.Config.get!([:fetch_initial_posts, :pages])
Enum.each(
# Insert all the posts in reverse order, so they're in the right order on the timeline
Enum.reverse(Utils.fetch_ordered_collection(user.info.source_data["outbox"], pages)),
&Pleroma.Web.Federator.incoming_ap_doc/1
)
{:ok, user}
end
@spec delete(User.t()) :: :ok @spec delete(User.t()) :: :ok
def delete(%User{} = user), def delete(%User{} = user, actor \\ nil),
do: PleromaJobQueue.enqueue(:background, __MODULE__, [:delete, user]) do: PleromaJobQueue.enqueue(:background, __MODULE__, [:delete, user, actor])
@spec perform(atom(), User.t()) :: {:ok, User.t()} @spec perform(atom(), User.t()) :: {:ok, User.t()}
def perform(:delete, %User{} = user) do def perform(:delete, %User{} = user, actor) do
{:ok, _user} = ActivityPub.delete(user) {:ok, _user} = ActivityPub.delete(user, actor: actor)
# Remove all relationships # Remove all relationships
{:ok, followers} = User.get_followers(user) {:ok, followers} = User.get_followers(user)
@ -1057,19 +1070,6 @@ def perform(:delete, %User{} = user) do
Repo.delete(user) Repo.delete(user)
end end
@spec perform(atom(), User.t()) :: {:ok, User.t()}
def perform(:fetch_initial_posts, %User{} = user) do
pages = Pleroma.Config.get!([:fetch_initial_posts, :pages])
Enum.each(
# Insert all the posts in reverse order, so they're in the right order on the timeline
Enum.reverse(Utils.fetch_ordered_collection(user.info.source_data["outbox"], pages)),
&Pleroma.Web.Federator.incoming_ap_doc/1
)
{:ok, user}
end
def perform(:deactivate_async, user, status), do: deactivate(user, status) def perform(:deactivate_async, user, status), do: deactivate(user, status)
@spec perform(atom(), User.t(), list()) :: list() | {:error, any()} @spec perform(atom(), User.t(), list()) :: list() | {:error, any()}

View File

@ -403,11 +403,13 @@ def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
end end
end end
def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do def delete(data, opts \\ %{actor: nil, local: true})
def delete(%User{ap_id: ap_id, follower_address: follower_address} = user, opts) do
with data <- %{ with data <- %{
"to" => [follower_address], "to" => [follower_address],
"type" => "Delete", "type" => "Delete",
"actor" => ap_id, "actor" => opts[:actor] || ap_id,
"object" => %{"type" => "Person", "id" => ap_id} "object" => %{"type" => "Person", "id" => ap_id}
}, },
{:ok, activity} <- insert(data, true, true), {:ok, activity} <- insert(data, true, true),
@ -416,7 +418,7 @@ def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
end end
end end
def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, opts) do
user = User.get_cached_by_ap_id(actor) user = User.get_cached_by_ap_id(actor)
to = (object.data["to"] || []) ++ (object.data["cc"] || []) to = (object.data["to"] || []) ++ (object.data["cc"] || [])
@ -428,7 +430,7 @@ def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ tru
"to" => to, "to" => to,
"deleted_activity_id" => activity && activity.id "deleted_activity_id" => activity && activity.id
}, },
{:ok, activity} <- insert(data, local, false), {:ok, activity} <- insert(data, opts[:local], false),
stream_out_participations(object, user), stream_out_participations(object, user),
_ <- decrease_replies_count_if_reply(object), _ <- decrease_replies_count_if_reply(object),
# Changing note count prior to enqueuing federation task in order to avoid # Changing note count prior to enqueuing federation task in order to avoid

View File

@ -649,7 +649,7 @@ def handle_incoming(
{:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor), {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
{:ok, object} <- get_obj_helper(object_id), {:ok, object} <- get_obj_helper(object_id),
:ok <- Containment.contain_origin(actor.ap_id, object.data), :ok <- Containment.contain_origin(actor.ap_id, object.data),
{:ok, activity} <- ActivityPub.delete(object, false) do {:ok, activity} <- ActivityPub.delete(object, local: false) do
{:ok, activity} {:ok, activity}
else else
nil -> nil ->

View File

@ -25,9 +25,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
action_fallback(:errors) action_fallback(:errors)
def user_delete(conn, %{"nickname" => nickname}) do def user_delete(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
User.get_cached_by_nickname(nickname) User.get_cached_by_nickname(nickname)
|> User.delete() |> User.delete(admin.ap_id)
conn conn
|> json(nickname) |> json(nickname)

View File

@ -11,7 +11,7 @@ defmodule Pleroma.Web.OStatus.DeleteHandler do
def handle_delete(entry, _doc \\ nil) do def handle_delete(entry, _doc \\ nil) do
with id <- XML.string_from_xpath("//id", entry), with id <- XML.string_from_xpath("//id", entry),
%Object{} = object <- Object.normalize(id), %Object{} = object <- Object.normalize(id),
{:ok, delete} <- ActivityPub.delete(object, false) do {:ok, delete} <- ActivityPub.delete(object, local: false) do
delete delete
end end
end end

View File

@ -998,6 +998,14 @@ test ".delete_user_activities deletes all create activities", %{user: user} do
refute Activity.get_by_id(activity.id) refute Activity.get_by_id(activity.id)
end end
test "it deletes deactivated user" do
admin = insert(:user, %{info: %{is_admin: true}})
{:ok, user} = insert(:user, info: %{deactivated: true}) |> User.set_cache()
assert {:ok, _} = User.delete(user, admin.ap_id)
refute User.get_by_id(user.id)
end
test "it deletes a user, all follow relationships and all activities", %{user: user} do test "it deletes a user, all follow relationships and all activities", %{user: user} do
follower = insert(:user) follower = insert(:user)
{:ok, follower} = User.follow(follower, user) {:ok, follower} = User.follow(follower, user)