Remove User.user_info/2

This commit is contained in:
Egor Kislitsyn 2019-11-27 19:43:47 +07:00
parent 139b1feda1
commit f595cfe623
No known key found for this signature in database
GPG Key ID: 1B49CB15B71E7805
4 changed files with 23 additions and 94 deletions

View File

@ -177,20 +177,6 @@ def ap_followers(%User{} = user), do: "#{ap_id(user)}/followers"
def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa 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
following_count = Map.get(args, :following_count, user.following_count)
follower_count = Map.get(args, :follower_count, user.follower_count)
%{
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 def follow_state(%User{} = user, %User{} = target) do
case Utils.fetch_latest_follow(user, target) do case Utils.fetch_latest_follow(user, target) do
%{data: %{"state" => state}} -> state %{data: %{"state" => state}} -> state
@ -209,10 +195,6 @@ def set_follow_state_cache(user_ap_id, target_ap_id, state) do
Cachex.put(:user_cache, "follow_state:#{user_ap_id}|#{target_ap_id}", state) Cachex.put(:user_cache, "follow_state:#{user_ap_id}|#{target_ap_id}", state)
end 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() @spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()
def restrict_deactivated(query) do def restrict_deactivated(query) do
from(u in query, where: u.deactivated != ^true) from(u in query, where: u.deactivated != ^true)
@ -614,7 +596,6 @@ def set_cache({:error, err}), do: {:error, err}
def set_cache(%User{} = user) do def set_cache(%User{} = user) do
Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user) Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
Cachex.put(:user_cache, "nickname:#{user.nickname}", user) Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
{:ok, user} {:ok, user}
end end
@ -633,7 +614,6 @@ def update_and_set_cache(changeset) do
def invalidate_cache(user) do def invalidate_cache(user) do
Cachex.del(:user_cache, "ap_id:#{user.ap_id}") Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
Cachex.del(:user_cache, "nickname:#{user.nickname}") Cachex.del(:user_cache, "nickname:#{user.nickname}")
Cachex.del(:user_cache, "user_info:#{user.id}")
end end
def get_cached_by_ap_id(ap_id) do def get_cached_by_ap_id(ap_id) do
@ -701,11 +681,6 @@ def get_by_nickname_or_email(nickname_or_email) do
get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email) get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email)
end 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 fetch_by_nickname(nickname), do: ActivityPub.make_user_from_nickname(nickname)
def get_or_fetch_by_nickname(nickname) do def get_or_fetch_by_nickname(nickname) do

View File

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

View File

@ -961,9 +961,9 @@ test "hide a user from followers" do
{:ok, user} = User.follow(user, user2) {:ok, user} = User.follow(user, user2)
{:ok, _user} = User.deactivate(user) {: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) assert [] = User.get_followers(user2)
end end
@ -977,10 +977,10 @@ test "hide a user from friends" do
{:ok, _user} = User.deactivate(user) {: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 refresh_record(user2).following_count == 0
assert info.following_count == 0 assert user2.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)
end end
@ -1182,13 +1182,12 @@ test "html_filter_policy returns TwitterText scrubber when rich-text is disabled
describe "caching" do describe "caching" do
test "invalidate_cache works" do test "invalidate_cache works" do
user = insert(:user) user = insert(:user)
_user_info = User.get_cached_user_info(user)
User.set_cache(user)
User.invalidate_cache(user) User.invalidate_cache(user)
{:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}") {: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, "nickname:#{user.nickname}")
{:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
end end
test "User.delete() plugs any possible zombie objects" do test "User.delete() plugs any possible zombie objects" do
@ -1344,7 +1343,7 @@ test "follower count is updated when a follower is blocked" do
{:ok, user} = User.block(user, follower) {:ok, user} = User.block(user, follower)
assert User.user_info(user).follower_count == 2 assert user.follower_count == 2
end end
describe "list_inactive_users_query/1" do describe "list_inactive_users_query/1" do
@ -1521,51 +1520,6 @@ test "external_users/1 external active users with limit", %{user1: user1, user2:
end end
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 describe "is_internal_user?/1" do
test "non-internal user returns false" do test "non-internal user returns false" do
user = insert(:user) user = insert(:user)
@ -1622,14 +1576,14 @@ test "updates the counters normally on following/getting a follow when disabled"
ap_enabled: true ap_enabled: true
) )
assert User.user_info(other_user).following_count == 0 assert other_user.following_count == 0
assert User.user_info(other_user).follower_count == 0 assert other_user.follower_count == 0
{:ok, user} = Pleroma.User.follow(user, other_user) {:ok, user} = Pleroma.User.follow(user, other_user)
other_user = Pleroma.User.get_by_id(other_user.id) other_user = Pleroma.User.get_by_id(other_user.id)
assert User.user_info(user).following_count == 1 assert user.following_count == 1
assert User.user_info(other_user).follower_count == 1 assert other_user.follower_count == 1
end end
test "syncronizes the counters with the remote instance for the followed when enabled" do test "syncronizes the counters with the remote instance for the followed when enabled" do
@ -1645,14 +1599,14 @@ test "syncronizes the counters with the remote instance for the followed when en
ap_enabled: true ap_enabled: true
) )
assert User.user_info(other_user).following_count == 0 assert other_user.following_count == 0
assert User.user_info(other_user).follower_count == 0 assert other_user.follower_count == 0
Pleroma.Config.put([:instance, :external_user_synchronization], true) Pleroma.Config.put([:instance, :external_user_synchronization], true)
{:ok, _user} = User.follow(user, other_user) {:ok, _user} = User.follow(user, other_user)
other_user = User.get_by_id(other_user.id) 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 end
test "syncronizes the counters with the remote instance for the follower when enabled" do test "syncronizes the counters with the remote instance for the follower when enabled" do
@ -1668,13 +1622,13 @@ test "syncronizes the counters with the remote instance for the follower when en
ap_enabled: true ap_enabled: true
) )
assert User.user_info(other_user).following_count == 0 assert other_user.following_count == 0
assert User.user_info(other_user).follower_count == 0 assert other_user.follower_count == 0
Pleroma.Config.put([:instance, :external_user_synchronization], true) Pleroma.Config.put([:instance, :external_user_synchronization], true)
{:ok, other_user} = User.follow(other_user, user) {: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
end end

View File

@ -350,7 +350,8 @@ test "represent an embedded relationship" 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 end
test "returns the settings store if the requesting user is the represented user and it's requested specifically" do test "returns the settings store if the requesting user is the represented user and it's requested specifically" do