From 66c758cb13db983f586c184c9a345f578770575b Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 15 Jul 2021 20:14:41 -0400 Subject: [PATCH] Finally put tag_list_cache to rest (fixes philomena-dev/philomena#128) --- lib/philomena/images.ex | 9 +++++++++ lib/philomena/scrapers/tumblr.ex | 4 +--- lib/philomena_web/controllers/image_controller.ex | 2 +- .../templates/api/rss/watched/index.html.eex | 4 ++-- lib/philomena_web/templates/layout/_opengraph.html.slime | 4 ++-- lib/philomena_web/views/api/json/oembed_view.ex | 2 +- lib/philomena_web/views/app_view.ex | 4 ++++ lib/philomena_web/views/avatar_generator_view.ex | 6 +++--- mix.exs | 3 ++- 9 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index 569993c8..c5fd4d79 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -54,6 +54,15 @@ defmodule Philomena.Images do Repo.one!(Image |> where(id: ^id) |> preload(:tags)) end + @doc """ + Gets the tag list for a single image. + """ + def tag_list(%Image{tags: tags}) do + tags + |> Tag.display_order() + |> Enum.map_join(", ", & &1.name) + end + @doc """ Creates a image. diff --git a/lib/philomena/scrapers/tumblr.ex b/lib/philomena/scrapers/tumblr.ex index 8ce08999..ae87f7ae 100644 --- a/lib/philomena/scrapers/tumblr.ex +++ b/lib/philomena/scrapers/tumblr.ex @@ -22,9 +22,7 @@ defmodule Philomena.Scrapers.Tumblr do [post_id] = Regex.run(@url_regex, url, capture: :all_but_first) api_url = - "https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{ - tumblr_api_key() - }" + "https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{tumblr_api_key()}" Philomena.Http.get(api_url) |> json!() diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 31e1d074..87d96122 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -95,7 +95,7 @@ defmodule PhilomenaWeb.ImageController do interactions: interactions, watching: watching, layout_class: "layout--wide", - title: "##{image.id} - #{image.tag_list_cache}" + title: "##{image.id} - #{Images.tag_list(image)}" ] if image.hidden_from_users do diff --git a/lib/philomena_web/templates/api/rss/watched/index.html.eex b/lib/philomena_web/templates/api/rss/watched/index.html.eex index e96caa93..de471d21 100644 --- a/lib/philomena_web/templates/api/rss/watched/index.html.eex +++ b/lib/philomena_web/templates/api/rss/watched/index.html.eex @@ -8,10 +8,10 @@ <%= for image <- @images do %> - #<%= image.id %> - <%= image.tag_list_cache %> + #<%= image.id %> - <%= tag_list(image) %> + <% mouseovertext = "Size: #{image.image_width}x#{image.image_height} | Tagged: #{tag_list(image)}" %> <%= mouseovertext %> diff --git a/lib/philomena_web/templates/layout/_opengraph.html.slime b/lib/philomena_web/templates/layout/_opengraph.html.slime index b7fe878c..ef2638e8 100644 --- a/lib/philomena_web/templates/layout/_opengraph.html.slime +++ b/lib/philomena_web/templates/layout/_opengraph.html.slime @@ -5,9 +5,9 @@ meta name="format-detection" content="telephone=no" = if opengraph?(@conn) do - image = @conn.assigns.image - filtered = not image.thumbnails_generated - - description = "##{image.id} - #{image.tag_list_cache} - Derpibooru" + - description = "##{image.id} - #{Philomena.Images.tag_list(image)} - Derpibooru" - meta name="keywords" content=image.tag_list_cache + meta name="keywords" content=tag_list(image) meta name="description" content=description meta property="og:title" content=description meta property="og:url" content=Routes.image_url(@conn, :show, image) diff --git a/lib/philomena_web/views/api/json/oembed_view.ex b/lib/philomena_web/views/api/json/oembed_view.ex index 9f621db2..1ca62784 100644 --- a/lib/philomena_web/views/api/json/oembed_view.ex +++ b/lib/philomena_web/views/api/json/oembed_view.ex @@ -11,7 +11,7 @@ defmodule PhilomenaWeb.Api.Json.OembedView do %{ version: "1.0", type: "photo", - title: "##{image.id} - #{image.tag_list_cache} - Derpibooru", + title: "##{image.id} - #{tag_list(image)} - Derpibooru", author_url: image.source_url || "", author_name: artist_tags(image.tags), provider_name: "Derpibooru", diff --git a/lib/philomena_web/views/app_view.ex b/lib/philomena_web/views/app_view.ex index 66794166..cdf1e5ec 100644 --- a/lib/philomena_web/views/app_view.ex +++ b/lib/philomena_web/views/app_view.ex @@ -43,6 +43,10 @@ defmodule PhilomenaWeb.AppView do ) end + def tag_list(image) do + Philomena.Images.tag_list(image) + end + def distance_of_time_in_words(time_2, time_1) do seconds = abs(DateTime.diff(time_2, time_1, :second)) minutes = div(seconds, 60) diff --git a/lib/philomena_web/views/avatar_generator_view.ex b/lib/philomena_web/views/avatar_generator_view.ex index d5f149ee..e49f5096 100644 --- a/lib/philomena_web/views/avatar_generator_view.ex +++ b/lib/philomena_web/views/avatar_generator_view.ex @@ -58,10 +58,10 @@ defmodule PhilomenaWeb.AvatarGeneratorView do # 32-bit xorshift deterministic PRNG defp xorshift32(state) do state = state &&& 0xFFFF_FFFF - state = bxor(state, (state <<< 13)) - state = bxor(state, (state >>> 17)) + state = bxor(state, state <<< 13) + state = bxor(state, state >>> 17) - bxor(state, (state <<< 5)) + bxor(state, state <<< 5) end # Generate pseudorandom, clamped RGB values with a specified diff --git a/mix.exs b/mix.exs index 5f526d80..c42f8775 100644 --- a/mix.exs +++ b/mix.exs @@ -55,7 +55,8 @@ defmodule Philomena.MixProject do {:nimble_parsec, "~> 1.1"}, {:canary, "~> 1.1"}, {:scrivener_ecto, "~> 2.7"}, - {:pbkdf2, "~> 2.0", github: "code-time/erlang-pbkdf2", ref: "f8f0012a97f58ade9c70ac93260e4259e4ca4b8d"}, + {:pbkdf2, "~> 2.0", + github: "code-time/erlang-pbkdf2", ref: "f8f0012a97f58ade9c70ac93260e4259e4ca4b8d"}, {:qrcode, "~> 0.1"}, {:redix, "~> 0.10.0"}, {:bamboo, "~> 1.6"},