# an error or a tombstone. This would allow us to verify that a deletion actually took
# place.
def handle_incoming(
- %{"type" => "Delete", "object" => object_id, "actor" => _actor, "id" => _id} = data,
+ %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = data,
_options
) do
object_id = Utils.get_ap_id(object_id)
{:ok, activity} <- ActivityPub.delete(object, false) do
{:ok, activity}
else
- _e -> :error
+ nil ->
+ case User.get_cached_by_ap_id(object_id) do
+ %User{ap_id: ^actor} = user ->
+ {:ok, followers} = User.get_followers(user)
+
+ Enum.each(followers, fn follower ->
+ User.unfollow(follower, user)
+ end)
+
+ {:ok, friends} = User.get_friends(user)
+
+ Enum.each(friends, fn followed ->
+ User.unfollow(user, followed)
+ end)
+
+ User.invalidate_cache(user)
+ Repo.delete(user)
+
+ nil ->
+ :error
+ end
+
+ _e ->
+ :error
end
end
use Pleroma.DataCase
import Pleroma.Factory
+ import Mock
setup_all do
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
end
end
- test ".delete_user_activities deletes all create activities" do
- user = insert(:user)
+ describe "delete" do
+ setup do
+ {:ok, user} = insert(:user) |> User.set_cache()
- {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
+ [user: user]
+ end
- {:ok, _} = User.delete_user_activities(user)
+ test ".delete_user_activities deletes all create activities", %{user: user} do
+ {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
- # TODO: Remove favorites, repeats, delete activities.
- refute Activity.get_by_id(activity.id)
- end
+ {:ok, _} = User.delete_user_activities(user)
- test ".delete deactivates a user, all follow relationships and all activities" do
- user = insert(:user)
- follower = insert(:user)
+ # TODO: Remove favorites, repeats, delete activities.
+ refute Activity.get_by_id(activity.id)
+ end
- {:ok, follower} = User.follow(follower, user)
+ test "it deletes a user, all follow relationships and all activities", %{user: user} do
+ follower = insert(:user)
+ {:ok, follower} = User.follow(follower, user)
- {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
- {:ok, activity_two} = CommonAPI.post(follower, %{"status" => "3hu"})
+ object = insert(:note, user: user)
+ activity = insert(:note_activity, user: user, note: object)
- {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
- {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
- {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
+ object_two = insert(:note, user: follower)
+ activity_two = insert(:note_activity, user: follower, note: object_two)
- {:ok, _} = User.delete(user)
+ {:ok, like, _} = CommonAPI.favorite(activity_two.id, user)
+ {:ok, like_two, _} = CommonAPI.favorite(activity.id, follower)
+ {:ok, repeat, _} = CommonAPI.repeat(activity_two.id, user)
- follower = User.get_cached_by_id(follower.id)
+ {:ok, _} = User.delete(user)
+
+ follower = User.get_cached_by_id(follower.id)
+
+ refute User.following?(follower, user)
+ refute User.get_by_id(user.id)
+ assert {:ok, nil} == Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
+
+ user_activities =
+ user.ap_id
+ |> Activity.query_by_actor()
+ |> Repo.all()
+ |> Enum.map(fn act -> act.data["type"] end)
+
+ assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
- refute User.following?(follower, user)
- refute User.get_by_id(user.id)
+ refute Activity.get_by_id(activity.id)
+ refute Activity.get_by_id(like.id)
+ refute Activity.get_by_id(like_two.id)
+ refute Activity.get_by_id(repeat.id)
+ end
+
+ test_with_mock "it sends out User Delete activity",
+ %{user: user},
+ Pleroma.Web.ActivityPub.Publisher,
+ [:passthrough],
+ [] do
+ config_path = [:instance, :federating]
+ initial_setting = Pleroma.Config.get(config_path)
+ Pleroma.Config.put(config_path, true)
- user_activities =
- user.ap_id
- |> Activity.query_by_actor()
- |> Repo.all()
- |> Enum.map(fn act -> act.data["type"] end)
+ {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
+ {:ok, _} = User.follow(follower, user)
- assert Enum.all?(user_activities, fn act -> act in ~w(Delete Undo) end)
+ {:ok, _user} = User.delete(user)
- refute Activity.get_by_id(activity.id)
- refute Activity.get_by_id(like.id)
- refute Activity.get_by_id(like_two.id)
- refute Activity.get_by_id(repeat.id)
+ assert called(
+ Pleroma.Web.ActivityPub.Publisher.publish_one(%{
+ inbox: "http://mastodon.example.org/inbox"
+ })
+ )
+
+ Pleroma.Config.put(config_path, initial_setting)
+ end
end
test "get_public_key_for_ap_id fetches a user that's not in the db" do