Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into bugfix/1442-dont-return-nil-for-following-count

This commit is contained in:
lain 2019-11-27 14:48:28 +01:00
commit 50b152766f
4 changed files with 31 additions and 102 deletions

View File

@ -177,20 +177,6 @@ defmodule Pleroma.User do
def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa
def ap_following(%User{} = user), do: "#{ap_id(user)}/following"
def user_info(%User{} = user, args \\ %{}) do
following_count = Map.get(args, :following_count, user.following_count) || 0
follower_count = Map.get(args, :follower_count, user.follower_count) || 0
%{
note_count: user.note_count,
locked: user.locked,
confirmation_pending: user.confirmation_pending,
default_scope: user.default_scope,
follower_count: follower_count,
following_count: following_count
}
end
def follow_state(%User{} = user, %User{} = target) do
case Utils.fetch_latest_follow(user, target) do
%{data: %{"state" => state}} -> state
@ -209,10 +195,6 @@ defmodule Pleroma.User do
Cachex.put(:user_cache, "follow_state:#{user_ap_id}|#{target_ap_id}", state)
end
def set_info_cache(user, args) do
Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user, args))
end
@spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()
def restrict_deactivated(query) do
from(u in query, where: u.deactivated != ^true)
@ -614,7 +596,6 @@ defmodule Pleroma.User do
def set_cache(%User{} = user) do
Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
{:ok, user}
end
@ -633,7 +614,6 @@ defmodule Pleroma.User do
def invalidate_cache(user) do
Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
Cachex.del(:user_cache, "nickname:#{user.nickname}")
Cachex.del(:user_cache, "user_info:#{user.id}")
end
def get_cached_by_ap_id(ap_id) do
@ -701,11 +681,6 @@ defmodule Pleroma.User do
get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email)
end
def get_cached_user_info(user) do
key = "user_info:#{user.id}"
Cachex.fetch!(:user_cache, key, fn -> user_info(user) end)
end
def fetch_by_nickname(nickname), do: ActivityPub.make_user_from_nickname(nickname)
def get_or_fetch_by_nickname(nickname) do

View File

@ -71,18 +71,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
image = User.avatar_url(user) |> MediaProxy.url()
header = User.banner_url(user) |> MediaProxy.url()
user_info = User.get_cached_user_info(user)
following_count =
if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
user_info.following_count
user.following_count || 0
else
0
end
followers_count =
if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
user_info.follower_count
user.follower_count || 0
else
0
end
@ -144,7 +143,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
# Pleroma extension
pleroma: %{
confirmation_pending: user_info.confirmation_pending,
confirmation_pending: user.confirmation_pending,
tags: user.tags,
hide_followers_count: user.hide_followers_count,
hide_follows_count: user.hide_follows_count,
@ -157,7 +156,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
}
}
|> maybe_put_role(user, opts[:for])
|> maybe_put_settings(user, opts[:for], user_info)
|> maybe_put_settings(user, opts[:for], opts)
|> maybe_put_notification_settings(user, opts[:for])
|> maybe_put_settings_store(user, opts[:for], opts)
|> maybe_put_chat_token(user, opts[:for], opts)
@ -191,7 +190,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
data,
%User{id: user_id} = user,
%User{id: user_id},
_user_info
_opts
) do
data
|> Kernel.put_in([:source, :privacy], user.default_scope)

View File

@ -961,9 +961,9 @@ defmodule Pleroma.UserTest do
{:ok, user} = User.follow(user, user2)
{:ok, _user} = User.deactivate(user)
info = User.get_cached_user_info(user2)
user2 = User.get_cached_by_id(user2.id)
assert info.follower_count == 0
assert user2.follower_count == 0
assert [] = User.get_followers(user2)
end
@ -977,10 +977,10 @@ defmodule Pleroma.UserTest do
{:ok, _user} = User.deactivate(user)
info = User.get_cached_user_info(user2)
user2 = User.get_cached_by_id(user2.id)
assert refresh_record(user2).following_count == 0
assert info.following_count == 0
assert user2.following_count == 0
assert User.following_count(user2) == 0
assert [] = User.get_friends(user2)
end
@ -1182,13 +1182,12 @@ defmodule Pleroma.UserTest do
describe "caching" do
test "invalidate_cache works" do
user = insert(:user)
_user_info = User.get_cached_user_info(user)
User.set_cache(user)
User.invalidate_cache(user)
{:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
{:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
{:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
end
test "User.delete() plugs any possible zombie objects" do
@ -1344,15 +1343,7 @@ defmodule Pleroma.UserTest do
{:ok, user} = User.block(user, follower)
assert User.user_info(user).follower_count == 2
end
test "with nil follower count fields, 0 will be returned" do
user = insert(:user, follower_count: nil, following_count: nil)
user_info = User.user_info(user)
assert user_info.follower_count == 0
assert user_info.following_count == 0
assert user.follower_count == 2
end
describe "list_inactive_users_query/1" do
@ -1529,51 +1520,6 @@ defmodule Pleroma.UserTest do
end
end
describe "set_info_cache/2" do
setup do
user = insert(:user)
{:ok, user: user}
end
test "update from args", %{user: user} do
User.set_info_cache(user, %{following_count: 15, follower_count: 18})
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
assert followers == 18
assert following == 15
end
test "without args", %{user: user} do
User.set_info_cache(user, %{})
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
assert followers == 0
assert following == 0
end
end
describe "user_info/2" do
setup do
user = insert(:user)
{:ok, user: user}
end
test "update from args", %{user: user} do
%{follower_count: followers, following_count: following} =
User.user_info(user, %{following_count: 15, follower_count: 18})
assert followers == 18
assert following == 15
end
test "without args", %{user: user} do
%{follower_count: followers, following_count: following} = User.user_info(user)
assert followers == 0
assert following == 0
end
end
describe "is_internal_user?/1" do
test "non-internal user returns false" do
user = insert(:user)
@ -1630,14 +1576,14 @@ defmodule Pleroma.UserTest do
ap_enabled: true
)
assert User.user_info(other_user).following_count == 0
assert User.user_info(other_user).follower_count == 0
assert other_user.following_count == 0
assert other_user.follower_count == 0
{:ok, user} = Pleroma.User.follow(user, other_user)
other_user = Pleroma.User.get_by_id(other_user.id)
assert User.user_info(user).following_count == 1
assert User.user_info(other_user).follower_count == 1
assert user.following_count == 1
assert other_user.follower_count == 1
end
test "syncronizes the counters with the remote instance for the followed when enabled" do
@ -1653,14 +1599,14 @@ defmodule Pleroma.UserTest do
ap_enabled: true
)
assert User.user_info(other_user).following_count == 0
assert User.user_info(other_user).follower_count == 0
assert other_user.following_count == 0
assert other_user.follower_count == 0
Pleroma.Config.put([:instance, :external_user_synchronization], true)
{:ok, _user} = User.follow(user, other_user)
other_user = User.get_by_id(other_user.id)
assert User.user_info(other_user).follower_count == 437
assert other_user.follower_count == 437
end
test "syncronizes the counters with the remote instance for the follower when enabled" do
@ -1676,13 +1622,13 @@ defmodule Pleroma.UserTest do
ap_enabled: true
)
assert User.user_info(other_user).following_count == 0
assert User.user_info(other_user).follower_count == 0
assert other_user.following_count == 0
assert other_user.follower_count == 0
Pleroma.Config.put([:instance, :external_user_synchronization], true)
{:ok, other_user} = User.follow(other_user, user)
assert User.user_info(other_user).following_count == 152
assert other_user.following_count == 152
end
end

View File

@ -350,7 +350,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
}
}
assert expected == AccountView.render("show.json", %{user: user, for: other_user})
assert expected ==
AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
end
test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
@ -374,6 +375,14 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
refute result.display_name == "<marquee> username </marquee>"
end
test "never display nil user follow counts" do
user = insert(:user, following_count: 0, follower_count: 0)
result = AccountView.render("show.json", %{user: user})
assert result.following_count == 0
assert result.followers_count == 0
end
describe "hiding follows/following" do
test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
user =