User: update_follower_count refactor.

This commit is contained in:
lain 2020-06-15 14:29:34 +02:00
parent 448e93ce2c
commit e1ee8bc1da
1 changed files with 9 additions and 21 deletions

View File

@ -747,7 +747,6 @@ def follow(%User{} = follower, %User{} = followed, state \\ :follow_accept) do
follower follower
|> update_following_count() |> update_following_count()
|> set_cache()
end end
end end
@ -776,7 +775,6 @@ defp do_unfollow(%User{} = follower, %User{} = followed) do
{:ok, follower} = {:ok, follower} =
follower follower
|> update_following_count() |> update_following_count()
|> set_cache()
{:ok, follower, followed} {:ok, follower, followed}
@ -1128,35 +1126,25 @@ defp follow_information_changeset(user, params) do
]) ])
end end
@spec update_follower_count(User.t()) :: {:ok, User.t()}
def update_follower_count(%User{} = user) do def update_follower_count(%User{} = user) do
if user.local or !Pleroma.Config.get([:instance, :external_user_synchronization]) do if user.local or !Pleroma.Config.get([:instance, :external_user_synchronization]) do
follower_count_query = follower_count = FollowingRelationship.follower_count(user)
User.Query.build(%{followers: user, deactivated: false})
|> select([u], %{count: count(u.id)})
User user
|> where(id: ^user.id) |> follow_information_changeset(%{follower_count: follower_count})
|> join(:inner, [u], s in subquery(follower_count_query)) |> update_and_set_cache
|> update([u, s],
set: [follower_count: s.count]
)
|> select([u], u)
|> Repo.update_all([])
|> case do
{1, [user]} -> set_cache(user)
_ -> {:error, user}
end
else else
{:ok, maybe_fetch_follow_information(user)} {:ok, maybe_fetch_follow_information(user)}
end end
end end
@spec update_following_count(User.t()) :: User.t() @spec update_following_count(User.t()) :: {:ok, User.t()}
def update_following_count(%User{local: false} = user) do def update_following_count(%User{local: false} = user) do
if Pleroma.Config.get([:instance, :external_user_synchronization]) do if Pleroma.Config.get([:instance, :external_user_synchronization]) do
maybe_fetch_follow_information(user) {:ok, maybe_fetch_follow_information(user)}
else else
user {:ok, user}
end end
end end
@ -1165,7 +1153,7 @@ def update_following_count(%User{local: true} = user) do
user user
|> follow_information_changeset(%{following_count: following_count}) |> follow_information_changeset(%{following_count: following_count})
|> Repo.update!() |> update_and_set_cache()
end end
def set_unread_conversation_count(%User{local: true} = user) do def set_unread_conversation_count(%User{local: true} = user) do