aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2019-08-15 14:06:27 +0000
committerlain <lain@soykaf.club>2019-08-15 14:06:27 +0000
commit9fb71ce7f44ec4824f9b7e2acbc89d0d16ad08bf (patch)
tree274ab728f3dbb7b95d870bf31d001aa773151685
parent27b747546a7796de57e88f454b2c2810c7523f97 (diff)
parentbd5ad0af787e65bc05b7df64ef41c414900085af (diff)
downloadpleroma-9fb71ce7f44ec4824f9b7e2acbc89d0d16ad08bf.tar.gz
Merge branch 'cache-follow-state' into 'develop'
Cache follow state Closes #1180 See merge request pleroma/pleroma!1573
-rw-r--r--lib/mix/tasks/pleroma/benchmark.ex24
-rw-r--r--lib/pleroma/user.ex22
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex3
-rw-r--r--lib/pleroma/web/activity_pub/utils.ex9
-rw-r--r--lib/pleroma/web/mastodon_api/views/account_view.ex6
5 files changed, 58 insertions, 6 deletions
diff --git a/lib/mix/tasks/pleroma/benchmark.ex b/lib/mix/tasks/pleroma/benchmark.ex
index 5222cce80..a45940bf3 100644
--- a/lib/mix/tasks/pleroma/benchmark.ex
+++ b/lib/mix/tasks/pleroma/benchmark.ex
@@ -26,4 +26,28 @@ defmodule Mix.Tasks.Pleroma.Benchmark do
end
})
end
+
+ def run(["render_timeline", nickname]) do
+ start_pleroma()
+ user = Pleroma.User.get_by_nickname(nickname)
+
+ activities =
+ %{}
+ |> Map.put("type", ["Create", "Announce"])
+ |> Map.put("blocking_user", user)
+ |> Map.put("muting_user", user)
+ |> Map.put("user", user)
+ |> Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities()
+ |> Enum.reverse()
+
+ Benchee.run(%{
+ "render_timeline" => fn ->
+ Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
+ activities: activities,
+ for: user,
+ as: :activity
+ })
+ end
+ })
+ end
end
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index b67743846..a1040fe71 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -132,6 +132,28 @@ defmodule Pleroma.User do
|> Map.put(:follower_count, follower_count)
end
+ def follow_state(%User{} = user, %User{} = target) do
+ follow_activity = Utils.fetch_latest_follow(user, target)
+
+ if follow_activity,
+ do: follow_activity.data["state"],
+ # Ideally this would be nil, but then Cachex does not commit the value
+ else: false
+ 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
+
+ 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
+
def set_info_cache(user, args) do
Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user, args))
end
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index cf55c9520..01052846f 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -388,7 +388,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def follow(follower, followed, activity_id \\ nil, local \\ true) do
with data <- make_follow_data(follower, followed, activity_id),
{:ok, activity} <- insert(data, local),
- :ok <- maybe_federate(activity) do
+ :ok <- maybe_federate(activity),
+ _ <- User.set_follow_state_cache(follower.ap_id, followed.ap_id, activity.data["state"]) do
{:ok, activity}
end
end
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
index fc5305c58..1c3058658 100644
--- a/lib/pleroma/web/activity_pub/utils.ex
+++ b/lib/pleroma/web/activity_pub/utils.ex
@@ -374,6 +374,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
[state, actor, object]
)
+ User.set_follow_state_cache(actor, object, state)
activity = Activity.get_by_id(activity.id)
{:ok, activity}
rescue
@@ -382,12 +383,16 @@ defmodule Pleroma.Web.ActivityPub.Utils do
end
end
- def update_follow_state(%Activity{} = activity, state) do
+ def update_follow_state(
+ %Activity{data: %{"actor" => actor, "object" => object}} = activity,
+ state
+ ) do
with new_data <-
activity.data
|> Map.put("state", state),
changeset <- Changeset.change(activity, data: new_data),
- {:ok, activity} <- Repo.update(changeset) do
+ {:ok, activity} <- Repo.update(changeset),
+ _ <- User.set_follow_state_cache(actor, object, state) do
{:ok, activity}
end
end
diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex
index 72c092f25..0ef568f0f 100644
--- a/lib/pleroma/web/mastodon_api/views/account_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/account_view.ex
@@ -37,11 +37,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
end
def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do
- follow_activity = Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, target)
+ follow_state = User.get_cached_follow_state(user, target)
requested =
- if follow_activity && !User.following?(target, user) do
- follow_activity.data["state"] == "pending"
+ if follow_state && !User.following?(user, target) do
+ follow_state == "pending"
else
false
end