chore: Improve Helpcenter custom domains (#5456)
- Support rendering articles over frontend URL - Support rendering articles over help center URL - Support rendering help center home page in the custom domain root
This commit is contained in:
@@ -4,7 +4,7 @@ class DashboardController < ActionController::Base
|
||||
before_action :set_global_config
|
||||
around_action :switch_locale
|
||||
before_action :ensure_installation_onboarding, only: [:index]
|
||||
before_action :redirect_to_custom_domain_page
|
||||
before_action :render_hc_if_custom_domain, only: [:index]
|
||||
|
||||
layout 'vueapp'
|
||||
|
||||
@@ -38,13 +38,14 @@ class DashboardController < ActionController::Base
|
||||
redirect_to '/installation/onboarding' if ::Redis::Alfred.get(::Redis::Alfred::CHATWOOT_INSTALLATION_ONBOARDING)
|
||||
end
|
||||
|
||||
def redirect_to_custom_domain_page
|
||||
custom_domain = request.host
|
||||
portal = Portal.find_by(custom_domain: custom_domain)
|
||||
def render_hc_if_custom_domain
|
||||
domain = request.host
|
||||
return if domain == URI.parse(ENV.fetch('FRONTEND_URL', '')).host
|
||||
|
||||
return unless portal
|
||||
@portal = Portal.find_by(custom_domain: domain)
|
||||
return unless @portal
|
||||
|
||||
redirect_to "/hc/#{portal.slug}"
|
||||
render 'public/api/v1/portals/show', layout: 'portal', portal: @portal and return
|
||||
end
|
||||
|
||||
def app_config
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class Public::Api::V1::Portals::ArticlesController < PublicController
|
||||
before_action :ensure_custom_domain_request, only: [:show, :index]
|
||||
before_action :set_portal
|
||||
before_action :portal
|
||||
before_action :set_category
|
||||
before_action :set_article, only: [:show]
|
||||
layout 'portal'
|
||||
@@ -23,8 +23,8 @@ class Public::Api::V1::Portals::ArticlesController < PublicController
|
||||
@category = @portal.categories.find_by!(slug: params[:category_slug])
|
||||
end
|
||||
|
||||
def set_portal
|
||||
@portal = @portals.find_by!(slug: params[:slug], archived: false)
|
||||
def portal
|
||||
@portal ||= Portal.find_by!(slug: params[:slug], archived: false)
|
||||
end
|
||||
|
||||
def list_params
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class Public::Api::V1::Portals::CategoriesController < PublicController
|
||||
before_action :ensure_custom_domain_request, only: [:show, :index]
|
||||
before_action :set_portal
|
||||
before_action :portal
|
||||
before_action :set_category, only: [:show]
|
||||
layout 'portal'
|
||||
|
||||
@@ -16,7 +16,7 @@ class Public::Api::V1::Portals::CategoriesController < PublicController
|
||||
@category = @portal.categories.find_by!(locale: params[:locale], slug: params[:category_slug])
|
||||
end
|
||||
|
||||
def set_portal
|
||||
@portal = @portals.find_by!(slug: params[:slug], archived: false)
|
||||
def portal
|
||||
@portal ||= Portal.find_by!(slug: params[:slug], archived: false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
class Public::Api::V1::PortalsController < PublicController
|
||||
before_action :ensure_custom_domain_request, only: [:show]
|
||||
before_action :set_portal
|
||||
before_action :portal
|
||||
layout 'portal'
|
||||
|
||||
def show; end
|
||||
|
||||
private
|
||||
|
||||
def set_portal
|
||||
@portal = @portals.find_by!(slug: params[:slug], archived: false)
|
||||
def portal
|
||||
@portal ||= Portal.find_by!(slug: params[:slug], archived: false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,14 +7,15 @@ class PublicController < ActionController::Base
|
||||
private
|
||||
|
||||
def ensure_custom_domain_request
|
||||
custom_domain = request.host
|
||||
domain = request.host
|
||||
|
||||
@portals = ::Portal.where(custom_domain: custom_domain)
|
||||
return if [URI.parse(ENV.fetch('FRONTEND_URL', '')).host, URI.parse(ENV.fetch('HELPCENTER_URL', '')).host].include?(domain)
|
||||
|
||||
return if @portals.present?
|
||||
@portal = ::Portal.find_by(custom_domain: domain)
|
||||
return if @portal.present?
|
||||
|
||||
render json: {
|
||||
error: "Domain: #{custom_domain} is not registered with us. \
|
||||
error: "Domain: #{domain} is not registered with us. \
|
||||
Please send us an email at support@chatwoot.com with the custom domain name and account API key"
|
||||
}, status: :unauthorized and return
|
||||
end
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_portals_on_slug (slug) UNIQUE
|
||||
# index_portals_on_custom_domain (custom_domain) UNIQUE
|
||||
# index_portals_on_slug (slug) UNIQUE
|
||||
#
|
||||
class Portal < ApplicationRecord
|
||||
include Rails.application.routes.url_helpers
|
||||
@@ -40,6 +41,7 @@ class Portal < ApplicationRecord
|
||||
validates :account_id, presence: true
|
||||
validates :name, presence: true
|
||||
validates :slug, presence: true, uniqueness: true
|
||||
validates :custom_domain, uniqueness: true, allow_nil: true
|
||||
validate :config_json_format
|
||||
|
||||
accepts_nested_attributes_for :members
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
|
||||
<%= render "hero", portal: @portal %>
|
||||
<%= render "public/api/v1/portals/hero", portal: @portal %>
|
||||
|
||||
<div class="max-w-4xl w-full flex-grow mx-auto py-16">
|
||||
<div class="grid grid-cols-2 gap-x-32 gap-y-12">
|
||||
<% @portal.categories.each do |category| %>
|
||||
<%= render "category-block", category: category, portal: @portal %>
|
||||
<%= render "public/api/v1/portals/category-block", category: category, portal: @portal %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user