Mastodon API Account view: Remove an outdated hack

The hack with caching the follow relationship was introduced
when we still were storing it inside the follow activity, resulting in
slow queries. Now we store follow state in `FollowRelationship` table,
so this is no longer necessary.
This commit is contained in:
rinpatch 2020-03-22 17:10:37 +03:00
parent d74405fc1a
commit 981e015f1b
4 changed files with 5 additions and 34 deletions

View file

@ -292,24 +292,6 @@ defmodule Pleroma.User do
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 follow_state(%User{} = user, %User{} = target) do
case Utils.fetch_latest_follow(user, target) do
%{data: %{"state" => state}} -> state
# Ideally this would be nil, but then Cachex does not commit the value
_ -> false
end
end
def get_cached_follow_state(user, target) do
key = "follow_state:#{user.ap_id}|#{target.ap_id}"
Cachex.fetch!(:user_cache, key, fn _ -> {:commit, follow_state(user, target)} end)
end
@spec set_follow_state_cache(String.t(), String.t(), String.t()) :: {:ok | :error, boolean()}
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)
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)

View file

@ -503,8 +503,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp do_follow(follower, followed, activity_id, local) do defp do_follow(follower, followed, activity_id, local) do
with data <- make_follow_data(follower, followed, activity_id), with data <- make_follow_data(follower, followed, activity_id),
{:ok, activity} <- insert(data, local), {:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity), :ok <- maybe_federate(activity) do
_ <- User.set_follow_state_cache(follower.ap_id, followed.ap_id, activity.data["state"]) do
{:ok, activity} {:ok, activity}
else else
{:error, error} -> Repo.rollback(error) {:error, error} -> Repo.rollback(error)

View file

@ -440,22 +440,19 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)]) |> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)])
|> Repo.update_all([]) |> Repo.update_all([])
User.set_follow_state_cache(actor, object, state)
activity = Activity.get_by_id(activity.id) activity = Activity.get_by_id(activity.id)
{:ok, activity} {:ok, activity}
end end
def update_follow_state( def update_follow_state(
%Activity{data: %{"actor" => actor, "object" => object}} = activity, %Activity{} = activity,
state state
) do ) do
new_data = Map.put(activity.data, "state", state) new_data = Map.put(activity.data, "state", state)
changeset = Changeset.change(activity, data: new_data) changeset = Changeset.change(activity, data: new_data)
with {:ok, activity} <- Repo.update(changeset) do with {:ok, activity} <- Repo.update(changeset) do
User.set_follow_state_cache(actor, object, state)
{:ok, activity} {:ok, activity}
end end
end end

View file

@ -36,25 +36,18 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
end end
def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do
follow_state = User.get_cached_follow_state(user, target) follow_state = User.get_follow_state(user, target)
requested =
if follow_state && !User.following?(user, target) do
follow_state == "pending"
else
false
end
%{ %{
id: to_string(target.id), id: to_string(target.id),
following: User.following?(user, target), following: follow_state == "accept",
followed_by: User.following?(target, user), followed_by: User.following?(target, user),
blocking: User.blocks_user?(user, target), blocking: User.blocks_user?(user, target),
blocked_by: User.blocks_user?(target, user), blocked_by: User.blocks_user?(target, user),
muting: User.mutes?(user, target), muting: User.mutes?(user, target),
muting_notifications: User.muted_notifications?(user, target), muting_notifications: User.muted_notifications?(user, target),
subscribing: User.subscribed_to?(user, target), subscribing: User.subscribed_to?(user, target),
requested: requested, requested: follow_state == "pending",
domain_blocking: User.blocks_domain?(user, target), domain_blocking: User.blocks_domain?(user, target),
showing_reblogs: User.showing_reblogs?(user, target), showing_reblogs: User.showing_reblogs?(user, target),
endorsed: false endorsed: false