Fix user.following_count

This commit is contained in:
Egor Kislitsyn 2019-11-21 16:31:13 +07:00
parent 937d6c6b32
commit d38ab4bb89
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
2 changed files with 34 additions and 27 deletions

View file

@ -67,8 +67,7 @@ defmodule Pleroma.User do
field(:source_data, :map, default: %{}) field(:source_data, :map, default: %{})
field(:note_count, :integer, default: 0) field(:note_count, :integer, default: 0)
field(:follower_count, :integer, default: 0) field(:follower_count, :integer, default: 0)
# Should be filled in only for remote users field(:following_count, :integer, default: 0)
field(:following_count, :integer, default: nil)
field(:locked, :boolean, default: false) field(:locked, :boolean, default: false)
field(:confirmation_pending, :boolean, default: false) field(:confirmation_pending, :boolean, default: false)
field(:password_reset_pending, :boolean, default: false) field(:password_reset_pending, :boolean, default: false)
@ -177,19 +176,17 @@ def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa
def ap_following(%User{} = user), do: "#{ap_id(user)}/following" def ap_following(%User{} = user), do: "#{ap_id(user)}/following"
def user_info(%User{} = user, args \\ %{}) do def user_info(%User{} = user, args \\ %{}) do
following_count = following_count = Map.get(args, :following_count, user.following_count)
Map.get(args, :following_count, user.following_count || following_count(user))
follower_count = Map.get(args, :follower_count, user.follower_count) follower_count = Map.get(args, :follower_count, user.follower_count)
%{ %{
note_count: user.note_count, note_count: user.note_count,
locked: user.locked, locked: user.locked,
confirmation_pending: user.confirmation_pending, confirmation_pending: user.confirmation_pending,
default_scope: user.default_scope default_scope: user.default_scope,
follower_count: follower_count,
following_count: following_count
} }
|> Map.put(:following_count, following_count)
|> Map.put(:follower_count, follower_count)
end end
def follow_state(%User{} = user, %User{} = target) do def follow_state(%User{} = user, %User{} = target) do
@ -522,14 +519,9 @@ def maybe_direct_follow(%User{} = follower, %User{} = followed) do
@doc "A mass follow for local users. Respects blocks in both directions but does not create activities." @doc "A mass follow for local users. Respects blocks in both directions but does not create activities."
@spec follow_all(User.t(), list(User.t())) :: {atom(), User.t()} @spec follow_all(User.t(), list(User.t())) :: {atom(), User.t()}
def follow_all(follower, followeds) do def follow_all(follower, followeds) do
followeds = followeds
Enum.reject(followeds, fn followed -> |> Enum.reject(fn followed -> blocks?(follower, followed) || blocks?(followed, follower) end)
blocks?(follower, followed) || blocks?(followed, follower) |> Enum.each(&follow(follower, &1, "accept"))
end)
Enum.each(followeds, &follow(follower, &1, "accept"))
Enum.each(followeds, &update_follower_count/1)
set_cache(follower) set_cache(follower)
end end
@ -549,11 +541,11 @@ def follow(%User{} = follower, %User{} = followed, state \\ "accept") do
true -> true ->
FollowingRelationship.follow(follower, followed, state) FollowingRelationship.follow(follower, followed, state)
follower = maybe_update_following_count(follower)
{:ok, _} = update_follower_count(followed) {:ok, _} = update_follower_count(followed)
set_cache(follower) follower
|> update_following_count()
|> set_cache()
end end
end end
@ -561,11 +553,12 @@ def unfollow(%User{} = follower, %User{} = followed) do
if following?(follower, followed) and follower.ap_id != followed.ap_id do if following?(follower, followed) and follower.ap_id != followed.ap_id do
FollowingRelationship.unfollow(follower, followed) FollowingRelationship.unfollow(follower, followed)
follower = maybe_update_following_count(follower)
{:ok, followed} = update_follower_count(followed) {:ok, followed} = update_follower_count(followed)
set_cache(follower) {:ok, follower} =
follower
|> update_following_count()
|> set_cache()
{:ok, follower, Utils.fetch_latest_follow(follower, followed)} {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
else else
@ -895,8 +888,8 @@ def update_follower_count(%User{} = user) do
end end
end end
@spec maybe_update_following_count(User.t()) :: User.t() @spec update_following_count(User.t()) :: User.t()
def maybe_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) maybe_fetch_follow_information(user)
else else
@ -904,7 +897,13 @@ def maybe_update_following_count(%User{local: false} = user) do
end end
end end
def maybe_update_following_count(user), do: user def update_following_count(%User{local: true} = user) do
following_count = FollowingRelationship.following_count(user)
user
|> follow_information_changeset(%{following_count: following_count})
|> Repo.update!()
end
def set_unread_conversation_count(%User{local: true} = user) do def set_unread_conversation_count(%User{local: true} = user) do
unread_query = Participation.unread_conversation_count_for_user(user) unread_query = Participation.unread_conversation_count_for_user(user)
@ -1097,7 +1096,12 @@ def deactivate(users, status) when is_list(users) do
def deactivate(%User{} = user, status) do def deactivate(%User{} = user, status) do
with {:ok, user} <- set_activation_status(user, status) do with {:ok, user} <- set_activation_status(user, status) do
Enum.each(get_followers(user), &invalidate_cache/1) user
|> get_followers()
|> Enum.filter(& &1.local)
|> Enum.each(fn follower ->
follower |> update_following_count() |> set_cache()
end)
# Only update local user counts, remote will be update during the next pull. # Only update local user counts, remote will be update during the next pull.
user user

View file

@ -148,9 +148,10 @@ test "follow takes a user and another user" do
{:ok, user} = User.follow(user, followed) {:ok, user} = User.follow(user, followed)
user = User.get_cached_by_id(user.id) user = User.get_cached_by_id(user.id)
followed = User.get_cached_by_ap_id(followed.ap_id) followed = User.get_cached_by_ap_id(followed.ap_id)
assert followed.follower_count == 1 assert followed.follower_count == 1
assert user.following_count == 1
assert User.ap_followers(followed) in User.following(user) assert User.ap_followers(followed) in User.following(user)
end end
@ -952,12 +953,14 @@ test "hide a user from friends" do
user2 = insert(:user) user2 = insert(:user)
{:ok, user2} = User.follow(user2, user) {:ok, user2} = User.follow(user2, user)
assert user2.following_count == 1
assert User.following_count(user2) == 1 assert User.following_count(user2) == 1
{:ok, _user} = User.deactivate(user) {:ok, _user} = User.deactivate(user)
info = User.get_cached_user_info(user2) info = User.get_cached_user_info(user2)
assert refresh_record(user2).following_count == 0
assert info.following_count == 0 assert info.following_count == 0
assert User.following_count(user2) == 0 assert User.following_count(user2) == 0
assert [] = User.get_friends(user2) assert [] = User.get_friends(user2)