diff --git a/lib/philomena/spoiler_executor.ex b/lib/philomena/spoiler_executor.ex index bcd21265..1f3deb6e 100644 --- a/lib/philomena/spoiler_executor.ex +++ b/lib/philomena/spoiler_executor.ex @@ -116,7 +116,7 @@ defmodule Philomena.SpoilerExecutor do {id, :complex} matched_tags -> - matched_tags + {id, matched_tags} end end diff --git a/lib/philomena_web/controllers/activity_controller.ex b/lib/philomena_web/controllers/activity_controller.ex index befbfe50..173629d2 100644 --- a/lib/philomena_web/controllers/activity_controller.ex +++ b/lib/philomena_web/controllers/activity_controller.ex @@ -14,6 +14,7 @@ defmodule PhilomenaWeb.ActivityController do } alias Philomena.Interactions + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -102,6 +103,12 @@ defmodule PhilomenaWeb.ActivityController do user ) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + [images, top_scoring, watched, featured_image] + ) + render( conn, "index.html", @@ -114,6 +121,7 @@ defmodule PhilomenaWeb.ActivityController do streams: streams, topics: topics, interactions: interactions, + spoilers: spoilers, layout_class: "layout--wide" ) end diff --git a/lib/philomena_web/controllers/duplicate_report_controller.ex b/lib/philomena_web/controllers/duplicate_report_controller.ex index 59e3072d..9ef6dbac 100644 --- a/lib/philomena_web/controllers/duplicate_report_controller.ex +++ b/lib/philomena_web/controllers/duplicate_report_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.DuplicateReportController do alias Philomena.DuplicateReports alias Philomena.DuplicateReports.DuplicateReport + alias Philomena.SpoilerExecutor alias Philomena.Images.Image alias Philomena.Repo import Ecto.Query @@ -30,9 +31,16 @@ defmodule PhilomenaWeb.DuplicateReportController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(duplicate_reports, &[&1.image, &1.duplicate_of_image]) + ) + render(conn, "index.html", title: "Duplicate Reports", duplicate_reports: duplicate_reports, + spoilers: spoilers, layout_class: "layout--wide" ) end diff --git a/lib/philomena_web/controllers/fingerprint_profile/source_change_controller.ex b/lib/philomena_web/controllers/fingerprint_profile/source_change_controller.ex index cfeaece2..58f98bac 100644 --- a/lib/philomena_web/controllers/fingerprint_profile/source_change_controller.ex +++ b/lib/philomena_web/controllers/fingerprint_profile/source_change_controller.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.FingerprintProfile.SourceChangeController do use PhilomenaWeb, :controller alias Philomena.SourceChanges.SourceChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -15,10 +16,17 @@ defmodule PhilomenaWeb.FingerprintProfile.SourceChangeController do |> preload([:user, image: [:user, :tags]]) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(source_changes, & &1.image) + ) + render(conn, "index.html", title: "Source Changes for Fingerprint `#{fingerprint}'", fingerprint: fingerprint, - source_changes: source_changes + source_changes: source_changes, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/fingerprint_profile/tag_change_controller.ex b/lib/philomena_web/controllers/fingerprint_profile/tag_change_controller.ex index af698483..b3335ec5 100644 --- a/lib/philomena_web/controllers/fingerprint_profile/tag_change_controller.ex +++ b/lib/philomena_web/controllers/fingerprint_profile/tag_change_controller.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.FingerprintProfile.TagChangeController do use PhilomenaWeb, :controller alias Philomena.TagChanges.TagChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -16,10 +17,17 @@ defmodule PhilomenaWeb.FingerprintProfile.TagChangeController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(tag_changes, & &1.image) + ) + render(conn, "index.html", title: "Tag Changes for Fingerprint `#{fingerprint}'", fingerprint: fingerprint, - tag_changes: tag_changes + tag_changes: tag_changes, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/gallery_controller.ex b/lib/philomena_web/controllers/gallery_controller.ex index cad4a057..75f7b2fb 100644 --- a/lib/philomena_web/controllers/gallery_controller.ex +++ b/lib/philomena_web/controllers/gallery_controller.ex @@ -5,6 +5,7 @@ defmodule PhilomenaWeb.GalleryController do alias PhilomenaWeb.NotificationCountPlug alias Philomena.Elasticsearch alias Philomena.Interactions + alias Philomena.SpoilerExecutor alias Philomena.Galleries.Gallery alias Philomena.Galleries import Ecto.Query @@ -33,9 +34,16 @@ defmodule PhilomenaWeb.GalleryController do Gallery |> preload([:creator, thumbnail: :tags]) ) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(galleries, & &1.thumbnail) + ) + render(conn, "index.html", title: "Galleries", galleries: galleries, + spoilers: spoilers, layout_class: "layout--wide" ) end @@ -58,8 +66,6 @@ defmodule PhilomenaWeb.GalleryController do {gallery_prev, gallery_next} = prev_next_page_images(conn, query) - interactions = Interactions.user_interactions([images, gallery_prev, gallery_next], user) - watching = Galleries.subscribed?(gallery, user) prev_image = if gallery_prev, do: [gallery_prev], else: [] @@ -70,6 +76,9 @@ defmodule PhilomenaWeb.GalleryController do Galleries.clear_notification(gallery, user) + interactions = Interactions.user_interactions(gallery_images, user) + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, gallery_images) + conn |> NotificationCountPlug.call([]) |> Map.put(:params, params) @@ -83,7 +92,8 @@ defmodule PhilomenaWeb.GalleryController do gallery_next: gallery_next, gallery_images: gallery_images, images: images, - interactions: interactions + interactions: interactions, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/image/related_controller.ex b/lib/philomena_web/controllers/image/related_controller.ex index 9cb3b3eb..75fc4a5c 100644 --- a/lib/philomena_web/controllers/image/related_controller.ex +++ b/lib/philomena_web/controllers/image/related_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Image.RelatedController do alias PhilomenaWeb.ImageLoader alias Philomena.Interactions + alias Philomena.SpoilerExecutor alias Philomena.Images.Image plug PhilomenaWeb.CanaryMapPlug, index: :show @@ -59,12 +60,14 @@ defmodule PhilomenaWeb.Image.RelatedController do ) interactions = Interactions.user_interactions(images, user) + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, images) render(conn, "index.html", title: "##{image.id} - Related Images", layout_class: "wide", images: images, - interactions: interactions + interactions: interactions, + spoilers: spoilers ) end end diff --git a/lib/philomena_web/controllers/image/reporting_controller.ex b/lib/philomena_web/controllers/image/reporting_controller.ex index a0c6ba54..cd0ffb21 100644 --- a/lib/philomena_web/controllers/image/reporting_controller.ex +++ b/lib/philomena_web/controllers/image/reporting_controller.ex @@ -4,6 +4,7 @@ defmodule PhilomenaWeb.Image.ReportingController do alias Philomena.Images.Image alias Philomena.DuplicateReports.DuplicateReport alias Philomena.DuplicateReports + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -26,11 +27,18 @@ defmodule PhilomenaWeb.Image.ReportingController do %DuplicateReport{} |> DuplicateReports.change_duplicate_report() + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(dupe_reports, &[&1.image, &1.duplicate_of_image]) + ) + render(conn, "show.html", layout: false, image: image, dupe_reports: dupe_reports, - changeset: changeset + changeset: changeset, + spoilers: spoilers ) end end diff --git a/lib/philomena_web/controllers/image/source_change_controller.ex b/lib/philomena_web/controllers/image/source_change_controller.ex index bfa16d7d..88770c39 100644 --- a/lib/philomena_web/controllers/image/source_change_controller.ex +++ b/lib/philomena_web/controllers/image/source_change_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Image.SourceChangeController do alias Philomena.Images.Image alias Philomena.SourceChanges.SourceChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -19,10 +20,17 @@ defmodule PhilomenaWeb.Image.SourceChangeController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(source_changes, & &1.image) + ) + render(conn, "index.html", title: "Source Changes on Image #{image.id}", image: image, - source_changes: source_changes + source_changes: source_changes, + spoilers: spoilers ) end end diff --git a/lib/philomena_web/controllers/image/tag_change_controller.ex b/lib/philomena_web/controllers/image/tag_change_controller.ex index f0f10142..7774c61c 100644 --- a/lib/philomena_web/controllers/image/tag_change_controller.ex +++ b/lib/philomena_web/controllers/image/tag_change_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Image.TagChangeController do alias Philomena.Images.Image alias Philomena.TagChanges.TagChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -20,10 +21,17 @@ defmodule PhilomenaWeb.Image.TagChangeController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(tag_changes, & &1.image) + ) + render(conn, "index.html", title: "Tag Changes on Image #{image.id}", image: image, - tag_changes: tag_changes + tag_changes: tag_changes, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 0f3f6c68..fd82a7a6 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -13,6 +13,7 @@ defmodule PhilomenaWeb.ImageController do Galleries.Gallery } + alias Philomena.SpoilerExecutor alias Philomena.Interactions alias Philomena.Comments alias Philomena.Repo @@ -37,12 +38,14 @@ defmodule PhilomenaWeb.ImageController do {:ok, {images, _tags}} = ImageLoader.search_string(conn, "created_at.lte:3 minutes ago") interactions = Interactions.user_interactions(images, conn.assigns.current_user) + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, images) render(conn, "index.html", title: "Images", layout_class: "layout--wide", images: images, - interactions: interactions + interactions: interactions, + spoilers: spoilers ) end @@ -66,6 +69,7 @@ defmodule PhilomenaWeb.ImageController do |> TextileRenderer.render_one(conn) interactions = Interactions.user_interactions([image], conn.assigns.current_user) + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, [image]) comment_changeset = %Comment{} @@ -89,6 +93,7 @@ defmodule PhilomenaWeb.ImageController do interactions: interactions, watching: watching, layout_class: "layout--wide", + spoilers: spoilers, title: "##{image.id} - #{image.tag_list_cache}" ] diff --git a/lib/philomena_web/controllers/ip_profile/source_change_controller.ex b/lib/philomena_web/controllers/ip_profile/source_change_controller.ex index c470d1b1..bb357192 100644 --- a/lib/philomena_web/controllers/ip_profile/source_change_controller.ex +++ b/lib/philomena_web/controllers/ip_profile/source_change_controller.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.IpProfile.SourceChangeController do use PhilomenaWeb, :controller alias Philomena.SourceChanges.SourceChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -17,10 +18,17 @@ defmodule PhilomenaWeb.IpProfile.SourceChangeController do |> preload([:user, image: [:user, :tags]]) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(source_changes, & &1.image) + ) + render(conn, "index.html", title: "Source Changes for IP `#{ip}'", ip: ip, - source_changes: source_changes + source_changes: source_changes, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/ip_profile/tag_change_controller.ex b/lib/philomena_web/controllers/ip_profile/tag_change_controller.ex index 3899cf24..0532c644 100644 --- a/lib/philomena_web/controllers/ip_profile/tag_change_controller.ex +++ b/lib/philomena_web/controllers/ip_profile/tag_change_controller.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.IpProfile.TagChangeController do use PhilomenaWeb, :controller alias Philomena.TagChanges.TagChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -18,10 +19,17 @@ defmodule PhilomenaWeb.IpProfile.TagChangeController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(tag_changes, & &1.image) + ) + render(conn, "index.html", title: "Tag Changes for IP `#{ip}'", ip: ip, - tag_changes: tag_changes + tag_changes: tag_changes, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/profile/source_change_controller.ex b/lib/philomena_web/controllers/profile/source_change_controller.ex index 521b8616..0c6097c5 100644 --- a/lib/philomena_web/controllers/profile/source_change_controller.ex +++ b/lib/philomena_web/controllers/profile/source_change_controller.ex @@ -4,6 +4,7 @@ defmodule PhilomenaWeb.Profile.SourceChangeController do alias Philomena.Users.User alias Philomena.Images.Image alias Philomena.SourceChanges.SourceChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -29,10 +30,17 @@ defmodule PhilomenaWeb.Profile.SourceChangeController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(source_changes, & &1.image) + ) + render(conn, "index.html", title: "Source Changes for User `#{user.name}'", user: user, - source_changes: source_changes + source_changes: source_changes, + spoilers: spoilers ) end end diff --git a/lib/philomena_web/controllers/profile/tag_change_controller.ex b/lib/philomena_web/controllers/profile/tag_change_controller.ex index 1254dadd..70964e60 100644 --- a/lib/philomena_web/controllers/profile/tag_change_controller.ex +++ b/lib/philomena_web/controllers/profile/tag_change_controller.ex @@ -4,6 +4,7 @@ defmodule PhilomenaWeb.Profile.TagChangeController do alias Philomena.Users.User alias Philomena.Images.Image alias Philomena.TagChanges.TagChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -25,10 +26,17 @@ defmodule PhilomenaWeb.Profile.TagChangeController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(tag_changes, & &1.image) + ) + render(conn, "index.html", title: "Tag Changes for User `#{user.name}'", user: user, - tag_changes: tag_changes + tag_changes: tag_changes, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/profile_controller.ex b/lib/philomena_web/controllers/profile_controller.ex index 77a799a3..f6403d8c 100644 --- a/lib/philomena_web/controllers/profile_controller.ex +++ b/lib/philomena_web/controllers/profile_controller.ex @@ -10,6 +10,7 @@ defmodule PhilomenaWeb.ProfileController do alias Philomena.Galleries.Gallery alias Philomena.Posts.Post alias Philomena.Comments.Comment + alias Philomena.SpoilerExecutor alias Philomena.Interactions alias Philomena.Tags.Tag alias Philomena.UserIps.UserIp @@ -137,8 +138,11 @@ defmodule PhilomenaWeb.ProfileController do statistics = calculate_statistics(user) - interactions = - Interactions.user_interactions([recent_uploads, recent_faves, recent_artwork], current_user) + images = [recent_uploads, recent_faves, recent_artwork] + + interactions = Interactions.user_interactions(images, current_user) + + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, images) forced = user.forced_filter @@ -153,6 +157,7 @@ defmodule PhilomenaWeb.ProfileController do "show.html", user: user, interactions: interactions, + spoilers: spoilers, commission_information: commission_information, recent_artwork: recent_artwork, recent_uploads: recent_uploads, diff --git a/lib/philomena_web/controllers/search/reverse_controller.ex b/lib/philomena_web/controllers/search/reverse_controller.ex index b6c6ba6b..06892d91 100644 --- a/lib/philomena_web/controllers/search/reverse_controller.ex +++ b/lib/philomena_web/controllers/search/reverse_controller.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.Search.ReverseController do use PhilomenaWeb, :controller alias PhilomenaWeb.ImageReverse + alias Philomena.SpoilerExecutor plug PhilomenaWeb.ScraperCachePlug plug PhilomenaWeb.ScraperPlug, params_key: "image", params_name: "image" @@ -12,8 +13,9 @@ defmodule PhilomenaWeb.Search.ReverseController do def create(conn, %{"image" => image_params}) when is_map(image_params) do images = ImageReverse.images(image_params) + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, images) - render(conn, "index.html", title: "Reverse Search", images: images) + render(conn, "index.html", title: "Reverse Search", images: images, spoilers: spoilers) end def create(conn, _params) do diff --git a/lib/philomena_web/controllers/search_controller.ex b/lib/philomena_web/controllers/search_controller.ex index 02a8ba61..5818c83e 100644 --- a/lib/philomena_web/controllers/search_controller.ex +++ b/lib/philomena_web/controllers/search_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.SearchController do alias PhilomenaWeb.ImageLoader alias Philomena.Interactions + alias Philomena.SpoilerExecutor def index(conn, params) do user = conn.assigns.current_user @@ -10,6 +11,7 @@ defmodule PhilomenaWeb.SearchController do case ImageLoader.search_string(conn, params["q"], include_hits: custom_ordering?(conn)) do {:ok, {images, tags}} -> interactions = Interactions.user_interactions(images, user) + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, images) conn |> render("index.html", @@ -18,6 +20,7 @@ defmodule PhilomenaWeb.SearchController do tags: tags, search_query: params["q"], interactions: interactions, + spoilers: spoilers, layout_class: "layout--wide" ) diff --git a/lib/philomena_web/controllers/tag/tag_change_controller.ex b/lib/philomena_web/controllers/tag/tag_change_controller.ex index 1313ec83..05ceb9d5 100644 --- a/lib/philomena_web/controllers/tag/tag_change_controller.ex +++ b/lib/philomena_web/controllers/tag/tag_change_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Tag.TagChangeController do alias Philomena.Tags.Tag alias Philomena.TagChanges.TagChange + alias Philomena.SpoilerExecutor alias Philomena.Repo import Ecto.Query @@ -20,10 +21,17 @@ defmodule PhilomenaWeb.Tag.TagChangeController do |> order_by(desc: :created_at) |> Repo.paginate(conn.assigns.scrivener) + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + Enum.map(tag_changes, & &1.image) + ) + render(conn, "index.html", title: "Tag Changes for Tag `#{tag.name}'", tag: tag, - tag_changes: tag_changes + tag_changes: tag_changes, + spoilers: spoilers ) end diff --git a/lib/philomena_web/controllers/tag_controller.ex b/lib/philomena_web/controllers/tag_controller.ex index de7dc727..f18b4c67 100644 --- a/lib/philomena_web/controllers/tag_controller.ex +++ b/lib/philomena_web/controllers/tag_controller.ex @@ -5,6 +5,7 @@ defmodule PhilomenaWeb.TagController do alias Philomena.Elasticsearch alias Philomena.{Tags, Tags.Tag} alias PhilomenaWeb.TextileRenderer + alias Philomena.SpoilerExecutor alias Philomena.Interactions plug PhilomenaWeb.RecodeParameterPlug, [name: "id"] when action in [:show] @@ -57,6 +58,7 @@ defmodule PhilomenaWeb.TagController do {images, _tags} = ImageLoader.query(conn, %{term: %{"namespaced_tags.name" => tag.name}}) interactions = Interactions.user_interactions(images, user) + spoilers = SpoilerExecutor.execute_spoiler(conn.assigns.compiled_spoiler, images) body = TextileRenderer.render_one(%{body: tag.description || ""}, conn) @@ -78,6 +80,7 @@ defmodule PhilomenaWeb.TagController do tags: [{tag, body, dnp_entries}], search_query: search_query, interactions: interactions, + spoilers: spoilers, images: images, layout_class: "layout--wide", title: "#{tag.name} - Tags" diff --git a/lib/philomena_web/plugs/image_filter_plug.ex b/lib/philomena_web/plugs/image_filter_plug.ex index 3281d0e8..be916ce3 100644 --- a/lib/philomena_web/plugs/image_filter_plug.ex +++ b/lib/philomena_web/plugs/image_filter_plug.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.ImageFilterPlug do import Plug.Conn import Philomena.Search.String + alias Philomena.SpoilerExecutor alias Philomena.Images.Query # No options @@ -14,7 +15,7 @@ defmodule PhilomenaWeb.ImageFilterPlug do forced = defaults(conn.assigns[:forced_filter]) tag_exclusion = %{terms: %{tag_ids: filter.hidden_tag_ids ++ forced.hidden_tag_ids}} - query_spoiler = invalid_filter_guard(user, filter.spoilered_complex_str) + query_spoiler = SpoilerExecutor.compile_spoiler(user, filter) query_exclusion = %{ bool: %{ @@ -32,8 +33,7 @@ defmodule PhilomenaWeb.ImageFilterPlug do } conn - |> assign(:compiled_complex_filter, query_exclusion) - |> assign(:compiled_complex_spoiler, query_spoiler) + |> assign(:compiled_spoiler, query_spoiler) |> assign(:compiled_filter, query) end diff --git a/lib/philomena_web/textile_renderer.ex b/lib/philomena_web/textile_renderer.ex index 193b3ba7..19a27fa7 100644 --- a/lib/philomena_web/textile_renderer.ex +++ b/lib/philomena_web/textile_renderer.ex @@ -1,6 +1,7 @@ defmodule PhilomenaWeb.TextileRenderer do alias Philomena.Textile.Parser alias Philomena.Images.Image + alias Philomena.SpoilerExecutor alias Philomena.Repo import Phoenix.HTML import Phoenix.HTML.Link @@ -17,7 +18,7 @@ defmodule PhilomenaWeb.TextileRenderer do opts = %{image_transform: &Camo.Image.image_url/1} parsed = Enum.map(posts, &Parser.parse(opts, &1.body)) - images = + {images, spoilers} = parsed |> Enum.flat_map(fn tree -> tree @@ -29,7 +30,7 @@ defmodule PhilomenaWeb.TextileRenderer do [] end) end) - |> find_images + |> find_images(conn) parsed |> Enum.map(fn tree -> @@ -38,7 +39,7 @@ defmodule PhilomenaWeb.TextileRenderer do {:text, text} -> text |> replacement_entities() - |> replacement_images(conn, images) + |> replacement_images(conn, spoilers, images) {_k, markup} -> markup @@ -59,7 +60,7 @@ defmodule PhilomenaWeb.TextileRenderer do |> String.replace("'", "’") end - defp replacement_images(t, conn, images) do + defp replacement_images(t, conn, spoilers, images) do t |> String.replace(~r|>>(\d+)([pts])?|, fn match -> # Stupid, but the method doesn't give us capture group information @@ -78,6 +79,7 @@ defmodule PhilomenaWeb.TextileRenderer do Phoenix.View.render(@image_view, "_image_target.html", image: image, size: :medium, + spoilers: spoilers, conn: conn ) |> safe_to_string() @@ -86,6 +88,7 @@ defmodule PhilomenaWeb.TextileRenderer do Phoenix.View.render(@image_view, "_image_target.html", image: image, size: :small, + spoilers: spoilers, conn: conn ) |> safe_to_string() @@ -94,6 +97,7 @@ defmodule PhilomenaWeb.TextileRenderer do Phoenix.View.render(@image_view, "_image_target.html", image: image, size: :thumb_small, + spoilers: spoilers, conn: conn ) |> safe_to_string() @@ -105,24 +109,32 @@ defmodule PhilomenaWeb.TextileRenderer do end) end - defp find_images(text_segments) do + defp find_images(text_segments, conn) do 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) |> Enum.filter(&(&1 < 2_147_483_647)) end) - |> load_images() + |> load_images(conn) end - defp load_images([]), do: %{} + defp load_images([], _conn), do: {%{}, %{}} - defp load_images(ids) do - Image - |> where([i], i.id in ^ids) - |> where([i], i.hidden_from_users == false) - |> preload(:tags) - |> Repo.all() - |> Map.new(&{&1.id, &1}) + defp load_images(ids, conn) do + images = + Image + |> where([i], i.id in ^ids) + |> where([i], i.hidden_from_users == false) + |> preload(:tags) + |> Repo.all() + + spoilers = + SpoilerExecutor.execute_spoiler( + conn.assigns.compiled_spoiler, + images + ) + + {Map.new(images, &{&1.id, &1}), spoilers} end end