aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorhref <href@random.sh>2019-01-26 15:55:53 +0100
committerhref <href@random.sh>2019-01-26 15:55:53 +0100
commit8018ae7ae5ab3747e5fefdc9a094fe4b3d90dbb6 (patch)
tree1d6582ccb1a768ad1d574367b4e83d5840491183 /lib
parent6383fa3a5d1a269315001b445dafc73a059dd8cb (diff)
downloadpleroma-8018ae7ae5ab3747e5fefdc9a094fe4b3d90dbb6.tar.gz
Join on preloads to avoid N+1 queries
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/notification.ex6
-rw-r--r--lib/pleroma/plugs/oauth_plug.ex7
2 files changed, 10 insertions, 3 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index e47145601..2364d36da 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -35,7 +35,8 @@ defmodule Pleroma.Notification do
n in Notification,
where: n.user_id == ^user.id,
order_by: [desc: n.id],
- preload: [:activity],
+ join: activity in assoc(n, :activity),
+ preload: [activity: activity],
limit: 20
)
@@ -66,7 +67,8 @@ defmodule Pleroma.Notification do
from(
n in Notification,
where: n.id == ^id,
- preload: [:activity]
+ join: activity in assoc(n, :activity),
+ preload: [activity: activity]
)
notification = Repo.one(query)
diff --git a/lib/pleroma/plugs/oauth_plug.ex b/lib/pleroma/plugs/oauth_plug.ex
index 437aa95b3..945a1d49f 100644
--- a/lib/pleroma/plugs/oauth_plug.ex
+++ b/lib/pleroma/plugs/oauth_plug.ex
@@ -33,7 +33,12 @@ defmodule Pleroma.Plugs.OAuthPlug do
#
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
defp fetch_user_and_token(token) do
- query = from(q in Token, where: q.token == ^token, preload: [:user])
+ query =
+ from(t in Token,
+ where: t.token == ^token,
+ join: user in assoc(t, :user),
+ preload: [user: user]
+ )
with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
{:ok, user, token_record}