docbrown/app/lib/url.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

77 lines
1.9 KiB
Ruby

# Utilities methods to safely build app wide URLs
module URL
SERVICE_WORKER = "/serviceworker.js".freeze
def self.protocol
ApplicationConfig["APP_PROTOCOL"]
end
def self.domain
Rails.application&.initialized? ? SiteConfig.app_domain : ApplicationConfig["APP_DOMAIN"]
end
def self.url(uri = nil)
base_url = "#{protocol}#{domain}"
return base_url unless uri
URI.parse(base_url).merge(uri).to_s
end
# Creates an article URL
#
# @param article [Article] the article to create the URL for
def self.article(article)
url(article.path)
end
# Creates a comment URL
#
# @param comment [Comment] the comment to create the URL for
def self.comment(comment)
url(comment.path)
end
# Creates a reaction URL
#
# A reaction URL is the URL of its reactable.
#
# @param reactable [Reaction] the reaction to create the URL for
def self.reaction(reaction)
url(reaction.reactable.path)
end
# Creates a tag URL
#
# @param tag [Tag] the tag to create the URL for
def self.tag(tag, page = 1)
url(["/t/#{tag.name}", ("/page/#{page}" if page > 1)].join)
end
# Creates a user URL
#
# @param user [User] the user to create the URL for
def self.user(user)
url(user.username)
rescue URI::InvalidURIError # invalid username containing spaces will result in an error
nil
end
def self.organization(organization)
url(organization.slug)
end
# Ensures we don't consider serviceworker.js as referer
#
# @param referer [String] the unsanitized referer
# @example A safe referer
# sanitized_referer("/some/path") #=> "/some/path"
# @example serviceworker.js as referer
# sanitized_referer("serviceworker.js") #=> nil
# @example An empty string
# sanitized_referer("") #=> nil
def self.sanitized_referer(referer)
return if referer.blank? || URI(referer).path == SERVICE_WORKER
referer
end
end