docbrown/app/view_objects/articles/social_image.rb
Ben Halpern 3a469a99e2
[deploy] Enable admin-configured app_domain (#10206)
* Move app_domain from env var to siteconfig *within app area*

* Fix SiteConfig

* Don't change URL.domain yet

* Not use in fastly cache purge yet

* Session store magoo

* Revert domain issue

* Add SetCookie middleware

* Temporarily remove cookie middleware and release tasks

* Re-enable SetCookieDomain

* Add some more comments, get it ready for testing

* Remove host from url options and temporarily show errors

* Allow forwarded host to be cookie

* Fix typo

* Change invalidAuthenticityToken

* Properly set app domain

* Proper Fastly cache

* Remove config.app_domain

* Remove config.app_domain

* Explicitely set remember_user_token

* More play with cookies

* Fiddle with remember_user_token

* Add remember_user_token changes

* Monkeypatch devise

* Sessions controller

* Include rememberable

* Include rememberable

* Proper cookie monkeypatch

* Proper cookie monkeypatch

* Proper cookie monkeypatch

* Remove extra cookie code

* User sign in tweak

* Close the loop

* Experiment with carrierwave public_url

* Finalize carrierwave monkeypatch

* Remove async controller specs

* Change some tests

* Update domain test

* Remove temporary production.rb change

* modify cookie logic

* Remove unneeded changes

* Remove unneeded changes

* Explicitely pass host

* Fix linting

* Fix social image test

* Add some cookie tests

* Modify article url

* Fix canonical urls

* Fix typo

* Remove sessions controller

* Remove devise monkeypatch

* Add monkeypatch

* Remove elsif

* Update spec/helpers/application_helper_spec.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* Adjust remember_cookie_sync

* Remember user token experimentation

* Fiddle with devise

* Properly configure devise

* Fix typo

* Fix formatting issue

* Add comment about middleware

* Fix typo

Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>

* Remove commented-out code

Co-authored-by: Michael Kohl <citizen428@dev.to>
Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>
2020-09-11 09:38:34 -04:00

49 lines
1.5 KiB
Ruby

module Articles
class SocialImage
include Rails.application.routes.url_helpers
SOCIAL_PREVIEW_MIGRATION_DATETIME = Time.zone.parse("2019-04-22T00:00:00Z")
def initialize(article, **options)
@article = article
@height = options[:height] || 500
@width = options[:width] || 1000
end
def url
image = user_defined_image
if image.present?
image = image.split("w_1000/").last if image.include?("w_1000/https://")
return Images::Optimizer.call(image, width: width, height: height, crop: "imagga_scale")
end
return legacy_article_social_image unless use_new_social_url?
article_social_preview_url(article, format: :png, host: SiteConfig.app_domain)
end
private
attr_reader :article, :height, :width
def legacy_article_social_image
cache_key = "article-social-img-#{article}-#{article.updated_at.rfc3339}-#{article.comments_count}"
Rails.cache.fetch(cache_key, expires_in: 1.hour) do
src = GeneratedImage.new(article).social_image
return src if src.start_with? "https://res.cloudinary.com/"
Images::Optimizer.call(src, width: "1000", height: "500", crop: "imagga_scale")
end
end
def use_new_social_url?
article.updated_at > SOCIAL_PREVIEW_MIGRATION_DATETIME
end
def user_defined_image
return article.social_image if article.social_image.present?
return article.main_image if article.main_image.present?
return article.video_thumbnail_url if article.video_thumbnail_url.present?
end
end
end