From c18a9004fd4b0b7436114fb73b8f74e366608a27 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 23 Feb 2021 21:21:46 +0400 Subject: [PATCH] Delete chats when deleting user --- CHANGELOG.md | 1 + lib/pleroma/chat.ex | 10 ++++++++-- lib/pleroma/user.ex | 1 + .../web/admin_api/controllers/user_controller_test.exs | 13 +++++++++++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74473b3d0..eaf50ca22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Removed duplicate jobs to purge expired activities - File extensions of some attachments were incorrectly changed. This feature has been disabled for now. - Mix task pleroma.instance creates missing parent directories if the configuration or SQL output paths are changed. +- Delete chats when deleting user.
API Changes diff --git a/lib/pleroma/chat.ex b/lib/pleroma/chat.ex index bacff24b5..a1ccc4a00 100644 --- a/lib/pleroma/chat.ex +++ b/lib/pleroma/chat.ex @@ -8,7 +8,6 @@ defmodule Pleroma.Chat do import Ecto.Changeset import Ecto.Query - alias Pleroma.Chat alias Pleroma.Repo alias Pleroma.User @@ -89,9 +88,16 @@ defmodule Pleroma.Chat do @spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t() def for_user_query(user_id) do - from(c in Chat, + from(c in __MODULE__, where: c.user_id == ^user_id, order_by: [desc: c.updated_at] ) end + + def delete_all_by_user(%User{id: user_id, ap_id: ap_id}) do + __MODULE__ + |> where(user_id: ^user_id) + |> or_where(recipient: ^ap_id) + |> Repo.delete_all() + end end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 51f5bc8ea..73895afe8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1763,6 +1763,7 @@ defmodule Pleroma.User do end) delete_user_activities(user) + Pleroma.Chat.delete_all_by_user(user) delete_notifications_from_user_activities(user) delete_outgoing_pending_follow_requests(user) diff --git a/test/pleroma/web/admin_api/controllers/user_controller_test.exs b/test/pleroma/web/admin_api/controllers/user_controller_test.exs index beb8a5d58..3299b1867 100644 --- a/test/pleroma/web/admin_api/controllers/user_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/user_controller_test.exs @@ -9,6 +9,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do import Mock import Pleroma.Factory + alias Pleroma.Chat alias Pleroma.HTML alias Pleroma.ModerationLog alias Pleroma.Repo @@ -108,11 +109,17 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do {:ok, _} = CommonAPI.post(user, %{status: "test"}) {:ok, _, _, _} = CommonAPI.follow(user, follower) {:ok, _, _, _} = CommonAPI.follow(follower, user) - user = Repo.get(User, user.id) + + {:ok, _create} = CommonAPI.post_chat_message(user, follower, "sup") + + user = refresh_record(user) assert user.note_count == 1 assert user.follower_count == 1 assert user.following_count == 1 assert user.is_active + assert %Chat{} = chat = Chat.get(user.id, follower.ap_id) + + assert [%Chat.MessageReference{}] = Chat.MessageReference.for_chat_query(chat) |> Repo.all() with_mock Pleroma.Web.Federator, publish: fn _ -> nil end, @@ -143,7 +150,9 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do assert user.following_count == 0 assert user.bio == "" assert user.name == nil - + assert [] == Chat.MessageReference.for_chat_query(chat) |> Repo.all() + assert Chat.get(user.id, follower.ap_id) == nil + assert Chat.get(follower.id, user.ap_id) == nil assert called(Pleroma.Web.Federator.publish(:_)) end end