diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index a02a223f..feb9cce4 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -10,7 +10,6 @@ defmodule PhilomenaWeb.ImageController do Images.Image, Comments.Comment, Galleries.Gallery, - Galleries.Interaction, Textile.Renderer } @@ -77,14 +76,13 @@ defmodule PhilomenaWeb.ImageController do watching = Images.subscribed?(image, conn.assigns.current_user) - {user_galleries, image_galleries} = image_and_user_galleries(image, conn.assigns.current_user) + user_galleries = user_galleries(image, conn.assigns.current_user) assigns = [ image: image, comments: comments, image_changeset: image_changeset, comment_changeset: comment_changeset, - image_galleries: image_galleries, user_galleries: user_galleries, description: description, interactions: interactions, @@ -132,28 +130,23 @@ defmodule PhilomenaWeb.ImageController do end end - defp image_and_user_galleries(_image, nil), do: {[], []} + defp user_galleries(_image, nil), do: [] - defp image_and_user_galleries(image, user) do - image_galleries = - Gallery - |> where(creator_id: ^user.id) - |> join(:inner, [g], gi in Interaction, - on: g.id == gi.gallery_id and gi.image_id == ^image.id + defp user_galleries(image, user) do + Gallery + |> where(creator_id: ^user.id) + |> join( + :inner_lateral, + [g], + _ in fragment( + "SELECT EXISTS(SELECT 1 FROM gallery_interactions gi WHERE gi.image_id = ? AND gi.gallery_id = ?)", + ^image.id, + g.id ) - |> order_by(desc: :created_at) - |> Repo.all() - - image_gallery_ids = Enum.map(image_galleries, & &1.id) - - user_galleries = - Gallery - |> where(creator_id: ^user.id) - |> where([g], g.id not in ^image_gallery_ids) - |> order_by(desc: :created_at) - |> Repo.all() - - {user_galleries, image_galleries} + ) + |> select([g, e], {g, e.exists}) + |> order_by(desc: :updated_at) + |> Repo.all() end defp load_image(conn, _opts) do diff --git a/lib/philomena_web/templates/image/_add_to_gallery_dropdown.html.slime b/lib/philomena_web/templates/image/_add_to_gallery_dropdown.html.slime index 9fb9a7c9..a7e1bdb6 100644 --- a/lib/philomena_web/templates/image/_add_to_gallery_dropdown.html.slime +++ b/lib/philomena_web/templates/image/_add_to_gallery_dropdown.html.slime @@ -12,21 +12,23 @@ i.fa.fa-table> span.hide-mobile Featured in - = if Enum.any?(@image_galleries) or Enum.any?(@user_galleries) do + = if Enum.any?(@user_galleries) do ul.block__list.js-gallery-list - = for gallery <- @user_galleries do - li id="gallery_#{gallery.id}" - a.block__list__link.js-gallery-add data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-add" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-remove" data-method="post" data-remote="true" href=Routes.gallery_image_path(@conn, :create, gallery, image_id: @image.id) - = gallery.title - a.block__list__link.active.js-gallery-remove.hidden data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-remove" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-add" data-method="delete" data-remote="true" href=Routes.gallery_image_path(@conn, :delete, gallery, image_id: @image.id) - = gallery.title - - = for gallery <- @image_galleries do - li id="gallery_#{gallery.id}" - a.block__list__link.js-gallery-add.hidden data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-add" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-remove" data-method="post" data-remote="true" href=Routes.gallery_image_path(@conn, :create, gallery, image_id: @image.id) - = gallery.title - a.block__list__link.active.js-gallery-remove data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-remove" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-add" data-method="delete" data-remote="true" href=Routes.gallery_image_path(@conn, :delete, gallery, image_id: @image.id) - = gallery.title + = for {gallery, present} <- @user_galleries do + = if present do + / Options to remove + li id="gallery_#{gallery.id}" + a.block__list__link.js-gallery-add.hidden data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-add" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-remove" data-method="post" data-remote="true" href=Routes.gallery_image_path(@conn, :create, gallery, image_id: @image.id) + = gallery.title + a.block__list__link.active.js-gallery-remove data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-remove" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-add" data-method="delete" data-remote="true" href=Routes.gallery_image_path(@conn, :delete, gallery, image_id: @image.id) + = gallery.title + - else + / Options to add + li id="gallery_#{gallery.id}" + a.block__list__link.js-gallery-add data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-add" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-remove" data-method="post" data-remote="true" href=Routes.gallery_image_path(@conn, :create, gallery, image_id: @image.id) + = gallery.title + a.block__list__link.active.js-gallery-remove.hidden data-fetchcomplete-hide="#gallery_#{gallery.id} .js-gallery-remove" data-fetchcomplete-show="#gallery_#{gallery.id} .js-gallery-add" data-method="delete" data-remote="true" href=Routes.gallery_image_path(@conn, :delete, gallery, image_id: @image.id) + = gallery.title .block__list = if @conn.assigns.current_user do diff --git a/lib/philomena_web/templates/image/_image_meta.html.slime b/lib/philomena_web/templates/image/_image_meta.html.slime index 2d883d47..04555ddf 100644 --- a/lib/philomena_web/templates/image/_image_meta.html.slime +++ b/lib/philomena_web/templates/image/_image_meta.html.slime @@ -33,7 +33,7 @@ i.fa.fa-eye-slash .stretched-mobile-links = render PhilomenaWeb.Image.SubscriptionView, "_subscription.html", watching: @watching, image: @image, conn: @conn - = render PhilomenaWeb.ImageView, "_add_to_gallery_dropdown.html", image: @image, image_galleries: @image_galleries, user_galleries: @user_galleries, conn: @conn + = render PhilomenaWeb.ImageView, "_add_to_gallery_dropdown.html", image: @image, user_galleries: @user_galleries, conn: @conn a href=Routes.image_related_path(@conn, :index, @image) title="Related Images" i.fa.fa-sitemap> span.hide-limited-desktop.hide-mobile Related diff --git a/lib/philomena_web/templates/image/show.html.slime b/lib/philomena_web/templates/image/show.html.slime index e1195272..9a5fd499 100644 --- a/lib/philomena_web/templates/image/show.html.slime +++ b/lib/philomena_web/templates/image/show.html.slime @@ -1,4 +1,4 @@ -= render PhilomenaWeb.ImageView, "_image_meta.html", image: @image, watching: @watching, image_galleries: @image_galleries, user_galleries: @user_galleries, changeset: @image_changeset, conn: @conn += render PhilomenaWeb.ImageView, "_image_meta.html", image: @image, watching: @watching, user_galleries: @user_galleries, changeset: @image_changeset, conn: @conn = render PhilomenaWeb.ImageView, "_image_page.html", image: @image, conn: @conn .layout--narrow