From f124c2f65a544200eb08555723d80949fcd760b9 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Wed, 24 Oct 2018 17:44:59 -0400 Subject: [PATCH] Fix html variant image prefix issue + other small fixes (#1005) --- app/helpers/application_helper.rb | 2 +- app/models/comment.rb | 2 +- app/models/html_variant.rb | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bd2787b66..0f5ab4d51 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -106,7 +106,7 @@ module ApplicationHelper Rails.cache.fetch(cache_key, expires_in: 1.hour) do src = GeneratedImage.new(article).social_image - return src if src.include? "res.cloudinary" + return src if src.start_with? "https://res.cloudinary.com/" cl_image_path(src, type: "fetch", width: "1000", diff --git a/app/models/comment.rb b/app/models/comment.rb index 36f9683dd..8a61d5bc0 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -306,12 +306,12 @@ class Comment < ApplicationRecord def before_destroy_actions bust_cache - Comment.delay.comment_async_bust(commentable, user.username) remove_algolia_index reactions.destroy_all end def bust_cache + Comment.delay.comment_async_bust(commentable, user.username) expire_root_fragment cache_buster = CacheBuster.new cache_buster.bust(commentable.path.to_s) if commentable diff --git a/app/models/html_variant.rb b/app/models/html_variant.rb index da6e2da40..5e5dadf0e 100644 --- a/app/models/html_variant.rb +++ b/app/models/html_variant.rb @@ -24,7 +24,7 @@ class HtmlVariant < ApplicationRecord end def self.find_top_for_test(tags_array) - where(group: "article_show_sidebar_cta", approved: true, published: true, target_tag: tags_array).order("success_rate DESC").limit(rand(1..10)).sample + where(group: "article_show_sidebar_cta", approved: true, published: true, target_tag: tags_array).order("success_rate DESC").limit(rand(1..15)).sample end def self.find_random_for_test(tags_array) @@ -45,7 +45,7 @@ class HtmlVariant < ApplicationRecord doc.css("img").each do |img| src = img.attr("src") next unless src - # allow image to render as-is + next if whitelisted_image_host?(src) img["src"] = if giphy_img?(src) src.gsub("https://media.", "https://i.") else @@ -65,6 +65,10 @@ class HtmlVariant < ApplicationRecord uri.path.ends_with?(".gif") end + def whitelisted_image_host?(src) + src.start_with?("https://res.cloudinary.com/") + end + def img_of_size(source, width = 420) quality = if source && (source.include? ".gif") 66