diff --git a/lib/philomena/textile/renderer.ex b/lib/philomena/textile/renderer.ex index 850cea46..c3d9fcf8 100644 --- a/lib/philomena/textile/renderer.ex +++ b/lib/philomena/textile/renderer.ex @@ -101,17 +101,21 @@ defmodule Philomena.Textile.Renderer do end defp find_images(text_segments) do - image_ids = - text_segments - |> Enum.flat_map(fn t -> - Regex.scan(~r|>>(\d+)|, t, capture: :all_but_first) - |> Enum.map(fn [first] -> String.to_integer(first) end) - end) + text_segments + |> Enum.flat_map(fn t -> + Regex.scan(~r|>>(\d+)|, t, capture: :all_but_first) + |> Enum.map(fn [first] -> String.to_integer(first) end) + end) + |> find_image_ids() + end + defp find_image_ids([]), do: %{} + defp find_image_ids(image_ids) do Image |> where([i], i.id in ^image_ids) |> where([i], i.hidden_from_users == false) - |> preload(:tags) + |> join(:left, [i], _ in assoc(i, :tags)) + |> preload([_i, t], tags: t) |> Repo.all() |> Map.new(fn image -> {image.id, image} end) end diff --git a/lib/philomena_web/comment_loader.ex b/lib/philomena_web/comment_loader.ex index 542d9461..df034ef5 100644 --- a/lib/philomena_web/comment_loader.ex +++ b/lib/philomena_web/comment_loader.ex @@ -3,6 +3,15 @@ defmodule PhilomenaWeb.CommentLoader do alias Philomena.Repo import Ecto.Query + # More aggressive preloading skipped due to overhead + # from scrivener: + # + # |> join(:left, [c], _ in assoc(c, :image)) + # |> join(:left, [c, _i], _ in assoc(c, :user)) + # |> join(:left, [_c, _i, u], _ in assoc(u, :awards)) + # |> join(:left, [_c, _i, _u, a], _ in assoc(a, :badge)) + # |> preload([_c, i, u, a, b], [:deleted_by, image: i, user: {u, awards: {a, badge: b}}]) + def load_comments(conn, image) do pref = load_direction(conn.assigns.current_user) diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 74eb8282..097eab42 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -147,8 +147,12 @@ defmodule PhilomenaWeb.ImageController do |> where(id: ^id) |> join(:inner_lateral, [i], _ in fragment("SELECT COUNT(*) FROM tag_changes t WHERE t.image_id = ?", i.id)) |> join(:inner_lateral, [i, _], _ in fragment("SELECT COUNT(*) FROM source_changes s WHERE s.image_id = ?", i.id)) - |> preload([:tags, :deleter, user: [awards: :badge]]) - |> select([i, t, s], {i, t.count, s.count}) + |> join(:left, [i, _tc, _sc], _ in assoc(i, :user)) + |> join(:left, [_i, _tc, _sc, u], _ in assoc(u, :awards)) + |> join(:left, [_i, _tc, _sc, _u, a], _ in assoc(a, :badge)) + |> join(:left, [i, _tc, _sc, _u, _a, _b], _ in assoc(i, :tags)) + |> preload([_i, _tc, _sc, u, a, b, t], [:deleter, tags: t, user: {u, awards: {a, badge: b}}]) + |> select([i, tc, sc, _u], {i, tc.count, sc.count}) |> Repo.one() |> case do nil -> diff --git a/lib/philomena_web/image_loader.ex b/lib/philomena_web/image_loader.ex index 6b4125a8..d8cf1211 100644 --- a/lib/philomena_web/image_loader.ex +++ b/lib/philomena_web/image_loader.ex @@ -20,7 +20,7 @@ defmodule PhilomenaWeb.ImageLoader do sort_queries = Keyword.get(options, :queries, []) sort_sorts = Keyword.get(options, :sorts, [%{created_at: :desc}]) pagination = Keyword.get(options, :pagination, conn.assigns.image_pagination) - queryable = Keyword.get(options, :queryable, Image |> preload(:tags)) + queryable = Keyword.get(options, :queryable, default_queryable()) user = conn.assigns.current_user filter = conn.assigns.compiled_filter @@ -126,4 +126,10 @@ defmodule PhilomenaWeb.ImageLoader do [{tag, description, dnp_entries}] end defp render_bodies(tags, _conn), do: tags + + defp default_queryable do + Image + |> join(:left, [i], _ in assoc(i, :tags)) + |> preload([_i, t], tags: t) + end end diff --git a/lib/philomena_web/plugs/advert_plug.ex b/lib/philomena_web/plugs/advert_plug.ex index 09246743..0b864c2b 100644 --- a/lib/philomena_web/plugs/advert_plug.ex +++ b/lib/philomena_web/plugs/advert_plug.ex @@ -24,4 +24,4 @@ defmodule PhilomenaWeb.AdvertPlug do do: true defp show_ads?(_user), do: true -end \ No newline at end of file +end