Delete chats when deleting user

This commit is contained in:
Egor Kislitsyn 2021-02-23 21:21:46 +04:00
parent d113ed94e7
commit c18a9004fd
No known key found for this signature in database
GPG Key ID: 1B49CB15B71E7805
4 changed files with 21 additions and 4 deletions

View File

@ -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 - Removed duplicate jobs to purge expired activities
- File extensions of some attachments were incorrectly changed. This feature has been disabled for now. - 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. - Mix task pleroma.instance creates missing parent directories if the configuration or SQL output paths are changed.
- Delete chats when deleting user.
<details> <details>
<summary>API Changes</summary> <summary>API Changes</summary>

View File

@ -8,7 +8,6 @@ defmodule Pleroma.Chat do
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
alias Pleroma.Chat
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
@ -89,9 +88,16 @@ defmodule Pleroma.Chat do
@spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t() @spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t()
def for_user_query(user_id) do def for_user_query(user_id) do
from(c in Chat, from(c in __MODULE__,
where: c.user_id == ^user_id, where: c.user_id == ^user_id,
order_by: [desc: c.updated_at] order_by: [desc: c.updated_at]
) )
end 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 end

View File

@ -1763,6 +1763,7 @@ defmodule Pleroma.User do
end) end)
delete_user_activities(user) delete_user_activities(user)
Pleroma.Chat.delete_all_by_user(user)
delete_notifications_from_user_activities(user) delete_notifications_from_user_activities(user)
delete_outgoing_pending_follow_requests(user) delete_outgoing_pending_follow_requests(user)

View File

@ -9,6 +9,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
import Mock import Mock
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Chat
alias Pleroma.HTML alias Pleroma.HTML
alias Pleroma.ModerationLog alias Pleroma.ModerationLog
alias Pleroma.Repo alias Pleroma.Repo
@ -108,11 +109,17 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
{:ok, _} = CommonAPI.post(user, %{status: "test"}) {:ok, _} = CommonAPI.post(user, %{status: "test"})
{:ok, _, _, _} = CommonAPI.follow(user, follower) {:ok, _, _, _} = CommonAPI.follow(user, follower)
{:ok, _, _, _} = CommonAPI.follow(follower, user) {: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.note_count == 1
assert user.follower_count == 1 assert user.follower_count == 1
assert user.following_count == 1 assert user.following_count == 1
assert user.is_active 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, with_mock Pleroma.Web.Federator,
publish: fn _ -> nil end, publish: fn _ -> nil end,
@ -143,7 +150,9 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
assert user.following_count == 0 assert user.following_count == 0
assert user.bio == "" assert user.bio == ""
assert user.name == nil 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(:_)) assert called(Pleroma.Web.Federator.publish(:_))
end end
end end