Mastodon API Account view: Remove an outdated hack
authorrinpatch <rinpatch@sdf.org>
Sun, 22 Mar 2020 14:10:37 +0000 (17:10 +0300)
committerrinpatch <rinpatch@sdf.org>
Sun, 22 Mar 2020 14:10:37 +0000 (17:10 +0300)
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.

lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/utils.ex
lib/pleroma/web/mastodon_api/views/account_view.ex

index 8693c0b8090c28c633a49d4110efdfba71c8d157..12c2ad81502638f9afc8af5ff283922033a2abb0 100644 (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{} = 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()
   def restrict_deactivated(query) do
     from(u in query, where: u.deactivated != ^true)
index d9f74b6a4928004f074025369bd26183aa517253..30e2828404a9b522997715f644e83e76e1261491 100644 (file)
@@ -503,8 +503,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   defp do_follow(follower, followed, activity_id, local) do
     with data <- make_follow_data(follower, followed, activity_id),
          {:ok, activity} <- insert(data, local),
-         :ok <- maybe_federate(activity),
-         _ <- User.set_follow_state_cache(follower.ap_id, followed.ap_id, activity.data["state"]) do
+         :ok <- maybe_federate(activity) do
       {:ok, activity}
     else
       {:error, error} -> Repo.rollback(error)
index 15dd2ed45f5f6ffaada313e32ad51559a5580302..c65bbed672dcca53da881d44ebb933129d6ff6d3 100644 (file)
@@ -440,22 +440,19 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     |> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)])
     |> Repo.update_all([])
 
-    User.set_follow_state_cache(actor, object, state)
-
     activity = Activity.get_by_id(activity.id)
 
     {:ok, activity}
   end
 
   def update_follow_state(
-        %Activity{data: %{"actor" => actor, "object" => object}} = activity,
+        %Activity{} = activity,
         state
       ) do
     new_data = Map.put(activity.data, "state", state)
     changeset = Changeset.change(activity, data: new_data)
 
     with {:ok, activity} <- Repo.update(changeset) do
-      User.set_follow_state_cache(actor, object, state)
       {:ok, activity}
     end
   end
index 341dc2c91e2f059a7ced3c4c50d4aca387796138..4ebce73b45ee21047f3822e4bdea6b0cc8b1eac7 100644 (file)
@@ -36,25 +36,18 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
   end
 
   def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do
-    follow_state = User.get_cached_follow_state(user, target)
-
-    requested =
-      if follow_state && !User.following?(user, target) do
-        follow_state == "pending"
-      else
-        false
-      end
+    follow_state = User.get_follow_state(user, target)
 
     %{
       id: to_string(target.id),
-      following: User.following?(user, target),
+      following: follow_state == "accept",
       followed_by: User.following?(target, user),
       blocking: User.blocks_user?(user, target),
       blocked_by: User.blocks_user?(target, user),
       muting: User.mutes?(user, target),
       muting_notifications: User.muted_notifications?(user, target),
       subscribing: User.subscribed_to?(user, target),
-      requested: requested,
+      requested: follow_state == "pending",
       domain_blocking: User.blocks_domain?(user, target),
       showing_reblogs: User.showing_reblogs?(user, target),
       endorsed: false