From e0e0388abfcd05ad4201bbf6cdeb7950c52b9eae Mon Sep 17 00:00:00 2001 From: donrestarone Date: Sun, 1 Dec 2024 17:09:38 -0500 Subject: [PATCH 1/2] overload CMS system controllers to show where to integrate S2 links (smart & snapping links) for smart / snapping redirection --- app/controllers/comfy/cms/base_controller.rb | 33 +++++++ .../comfy/cms/content_controller.rb | 97 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 app/controllers/comfy/cms/base_controller.rb create mode 100644 app/controllers/comfy/cms/content_controller.rb diff --git a/app/controllers/comfy/cms/base_controller.rb b/app/controllers/comfy/cms/base_controller.rb new file mode 100644 index 000000000..33302c617 --- /dev/null +++ b/app/controllers/comfy/cms/base_controller.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +class Comfy::Cms::BaseController < ComfortableMexicanSofa.config.public_base_controller.to_s.constantize + + before_action :load_cms_site + + helper Comfy::CmsHelper + +protected + + def load_cms_site + @cms_site ||= + if params[:site_id] + ::Comfy::Cms::Site.find_by_id(params[:site_id]) + else + ::Comfy::Cms::Site.find_site(request.host_with_port.downcase, request.fullpath) + end + + if @cms_site + if @cms_site.path.present? && !params[:site_id] + if params[:cms_path]&.match(%r{\A#{@cms_site.path}}) + params[:cms_path].gsub!(%r{\A#{@cms_site.path}}, "") + params[:cms_path]&.gsub!(%r{\A/}, "") + else + raise ActionController::RoutingError, "Site Not Found" + end + end + else + raise ActionController::RoutingError, "Site Not Found" + end + end + +end \ No newline at end of file diff --git a/app/controllers/comfy/cms/content_controller.rb b/app/controllers/comfy/cms/content_controller.rb new file mode 100644 index 000000000..2035e9f8b --- /dev/null +++ b/app/controllers/comfy/cms/content_controller.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +class Comfy::Cms::ContentController < Comfy::Cms::BaseController + + # Authentication module must have `authenticate` method + include ComfortableMexicanSofa.config.public_auth.to_s.constantize + + # Authorization module must have `authorize` method + include ComfortableMexicanSofa.config.public_authorization.to_s.constantize + + before_action :load_seeds + before_action :load_cms_page, + :authenticate, + :authorize, + only: :show + + def show + if @cms_page.target_page.present? + redirect_to @cms_page.target_page.url(relative: true) + else + respond_to do |format| + format.html { render_page } + format.json do + @cms_page.content = render_to_string( + inline: @cms_page.content_cache, + layout: false + ) + json_page = @cms_page.as_json(ComfortableMexicanSofa.config.page_to_json_options) + render json: json_page + end + end + end + end + +protected + + def render_page(status = :ok) + render inline: @cms_page.content_cache, + layout: app_layout, + status: status, + content_type: mime_type + end + + # it's possible to control mimetype of a page by creating a `mime_type` field + def mime_type + mime_block = @cms_page.fragments.detect { |f| f.identifier == "mime_type" } + mime_block&.content&.strip || "text/html" + end + + def app_layout + return false if request.xhr? || !@cms_layout + @cms_layout.app_layout.present? ? @cms_layout.app_layout : false + end + + def load_seeds + return unless ComfortableMexicanSofa.config.enable_seeds + ComfortableMexicanSofa::Seeds::Importer.new(@cms_site.identifier).import! + end + + # Attempting to populate @cms_page and @cms_layout instance variables so they + # can be used in view helpers/partials + def load_cms_page + unless find_cms_page_by_full_path("/#{params[:cms_path]}") + if find_cms_page_by_full_path("/404") + render_page(:not_found) + else + message = "Page Not Found at: \"#{params[:cms_path]}\"" + raise ActionController::RoutingError, message + end + end + end + + # Getting page and setting content_cache and fragments data if we need to + # serve translation data + def find_cms_page_by_full_path(full_path) + @cms_page = @cms_site.pages.published.find_by!(full_path: full_path) + + @cms_page.translate! + @cms_layout = @cms_page.layout + + @cms_page + + rescue ActiveRecord::RecordNotFound + # page miss in CMS system, process via smart links + top_level_subdomain_targets = Subdomain.all.pluck(:name) + request_path = request.path.gsub('/', '') + user = Current.user + api_renderer_request = Current.is_api_html_renderer_request + visit = Current.visit + if request_path == 'google.com' + # proof of concept + return redirect_to "https://#{request_path}" + end + nil + end + +end \ No newline at end of file From c1dfc0f5de08c23ab23fd9fe56d647aed2bf381e Mon Sep 17 00:00:00 2001 From: donrestarone Date: Sun, 1 Dec 2024 18:52:40 -0500 Subject: [PATCH 2/2] spike: capture and redirect accordingly --- app/controllers/comfy/cms/base_controller.rb | 33 ------------------- .../comfy/cms/content_controller.rb | 5 +++ app/controllers/content_controller.rb | 15 ++++++++- 3 files changed, 19 insertions(+), 34 deletions(-) delete mode 100644 app/controllers/comfy/cms/base_controller.rb diff --git a/app/controllers/comfy/cms/base_controller.rb b/app/controllers/comfy/cms/base_controller.rb deleted file mode 100644 index 33302c617..000000000 --- a/app/controllers/comfy/cms/base_controller.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -class Comfy::Cms::BaseController < ComfortableMexicanSofa.config.public_base_controller.to_s.constantize - - before_action :load_cms_site - - helper Comfy::CmsHelper - -protected - - def load_cms_site - @cms_site ||= - if params[:site_id] - ::Comfy::Cms::Site.find_by_id(params[:site_id]) - else - ::Comfy::Cms::Site.find_site(request.host_with_port.downcase, request.fullpath) - end - - if @cms_site - if @cms_site.path.present? && !params[:site_id] - if params[:cms_path]&.match(%r{\A#{@cms_site.path}}) - params[:cms_path].gsub!(%r{\A#{@cms_site.path}}, "") - params[:cms_path]&.gsub!(%r{\A/}, "") - else - raise ActionController::RoutingError, "Site Not Found" - end - end - else - raise ActionController::RoutingError, "Site Not Found" - end - end - -end \ No newline at end of file diff --git a/app/controllers/comfy/cms/content_controller.rb b/app/controllers/comfy/cms/content_controller.rb index 2035e9f8b..28fb14e6d 100644 --- a/app/controllers/comfy/cms/content_controller.rb +++ b/app/controllers/comfy/cms/content_controller.rb @@ -15,6 +15,11 @@ class Comfy::Cms::ContentController < Comfy::Cms::BaseController only: :show def show + if params[:s2_redirect_to] && params[:s2_query] && params[:s2_url_params] && params[:s2_subdomain] + # s2 link detected, process accordingly + return redirect_to "https://#{params[:s2_redirect_to]}" + end + if @cms_page.target_page.present? redirect_to @cms_page.target_page.url(relative: true) else diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index 2796ad19a..552522014 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -1,3 +1,16 @@ class ContentController < ApplicationController - before_action :track_ahoy_visit, raise: false + before_action :track_ahoy_visit, :process_subdomain_smart_link_redirect, raise: false + + private + + def process_subdomain_smart_link_redirect + subdomain = request.subdomain + url_parameters = request.path + if !subdomain.blank? + unless Subdomain.all.pluck(:name).any?{|name| subdomain == name} + # process S2 link - append parameters for 2nd redirect + return redirect_to "#{root_url(subdomain: Subdomain.current.name)}?s2_redirect_to=#{subdomain}&s2_query=#{subdomain}&s2_url_params=#{url_parameters}&s2_subdomain=#{subdomain}" + end + end + end end