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:
Sojan Jose 2022-09-20 06:06:01 +05:30 committed by GitHub
parent a773ad7d08
commit 99de8f4500
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 35 additions and 23 deletions

View file

@ -3,6 +3,8 @@ SECRET_KEY_BASE=replace_with_lengthy_secure_hex
# Replace with the URL you are planning to use for your app # Replace with the URL you are planning to use for your app
FRONTEND_URL=http://0.0.0.0:3000 FRONTEND_URL=http://0.0.0.0:3000
# To use a dedicated URL for help center pages
# HELPCENTER_URL=http://0.0.0.0:3000
# If the variable is set, all non-authenticated pages would fallback to the default locale. # If the variable is set, all non-authenticated pages would fallback to the default locale.
# Whenever a new account is created, the default language will be DEFAULT_LOCALE instead of en # Whenever a new account is created, the default language will be DEFAULT_LOCALE instead of en

View file

@ -4,7 +4,7 @@ class DashboardController < ActionController::Base
before_action :set_global_config before_action :set_global_config
around_action :switch_locale around_action :switch_locale
before_action :ensure_installation_onboarding, only: [:index] 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' layout 'vueapp'
@ -38,13 +38,14 @@ class DashboardController < ActionController::Base
redirect_to '/installation/onboarding' if ::Redis::Alfred.get(::Redis::Alfred::CHATWOOT_INSTALLATION_ONBOARDING) redirect_to '/installation/onboarding' if ::Redis::Alfred.get(::Redis::Alfred::CHATWOOT_INSTALLATION_ONBOARDING)
end end
def redirect_to_custom_domain_page def render_hc_if_custom_domain
custom_domain = request.host domain = request.host
portal = Portal.find_by(custom_domain: custom_domain) 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 end
def app_config def app_config

View file

@ -1,6 +1,6 @@
class Public::Api::V1::Portals::ArticlesController < PublicController class Public::Api::V1::Portals::ArticlesController < PublicController
before_action :ensure_custom_domain_request, only: [:show, :index] before_action :ensure_custom_domain_request, only: [:show, :index]
before_action :set_portal before_action :portal
before_action :set_category before_action :set_category
before_action :set_article, only: [:show] before_action :set_article, only: [:show]
layout 'portal' layout 'portal'
@ -23,8 +23,8 @@ class Public::Api::V1::Portals::ArticlesController < PublicController
@category = @portal.categories.find_by!(slug: params[:category_slug]) @category = @portal.categories.find_by!(slug: params[:category_slug])
end end
def set_portal def portal
@portal = @portals.find_by!(slug: params[:slug], archived: false) @portal ||= Portal.find_by!(slug: params[:slug], archived: false)
end end
def list_params def list_params

View file

@ -1,6 +1,6 @@
class Public::Api::V1::Portals::CategoriesController < PublicController class Public::Api::V1::Portals::CategoriesController < PublicController
before_action :ensure_custom_domain_request, only: [:show, :index] before_action :ensure_custom_domain_request, only: [:show, :index]
before_action :set_portal before_action :portal
before_action :set_category, only: [:show] before_action :set_category, only: [:show]
layout 'portal' 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]) @category = @portal.categories.find_by!(locale: params[:locale], slug: params[:category_slug])
end end
def set_portal def portal
@portal = @portals.find_by!(slug: params[:slug], archived: false) @portal ||= Portal.find_by!(slug: params[:slug], archived: false)
end end
end end

View file

@ -1,13 +1,13 @@
class Public::Api::V1::PortalsController < PublicController class Public::Api::V1::PortalsController < PublicController
before_action :ensure_custom_domain_request, only: [:show] before_action :ensure_custom_domain_request, only: [:show]
before_action :set_portal before_action :portal
layout 'portal' layout 'portal'
def show; end def show; end
private private
def set_portal def portal
@portal = @portals.find_by!(slug: params[:slug], archived: false) @portal ||= Portal.find_by!(slug: params[:slug], archived: false)
end end
end end

View file

@ -7,14 +7,15 @@ class PublicController < ActionController::Base
private private
def ensure_custom_domain_request 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: { 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" Please send us an email at support@chatwoot.com with the custom domain name and account API key"
}, status: :unauthorized and return }, status: :unauthorized and return
end end

View file

@ -18,6 +18,7 @@
# #
# Indexes # Indexes
# #
# index_portals_on_custom_domain (custom_domain) UNIQUE
# index_portals_on_slug (slug) UNIQUE # index_portals_on_slug (slug) UNIQUE
# #
class Portal < ApplicationRecord class Portal < ApplicationRecord
@ -40,6 +41,7 @@ class Portal < ApplicationRecord
validates :account_id, presence: true validates :account_id, presence: true
validates :name, presence: true validates :name, presence: true
validates :slug, presence: true, uniqueness: true validates :slug, presence: true, uniqueness: true
validates :custom_domain, uniqueness: true, allow_nil: true
validate :config_json_format validate :config_json_format
accepts_nested_attributes_for :members accepts_nested_attributes_for :members

View file

@ -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="max-w-4xl w-full flex-grow mx-auto py-16">
<div class="grid grid-cols-2 gap-x-32 gap-y-12"> <div class="grid grid-cols-2 gap-x-32 gap-y-12">
<% @portal.categories.each do |category| %> <% @portal.categories.each do |category| %>
<%= render "category-block", category: category, portal: @portal %> <%= render "public/api/v1/portals/category-block", category: category, portal: @portal %>
<% end %> <% end %>
</div> </div>
</div> </div>

View file

@ -0,0 +1,5 @@
class MakeHelpCenterCustomDomainUnique < ActiveRecord::Migration[6.1]
def change
add_index :portals, :custom_domain, unique: true
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_08_09_104508) do ActiveRecord::Schema.define(version: 2022_09_19_225556) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
@ -708,6 +708,7 @@ ActiveRecord::Schema.define(version: 2022_08_09_104508) do
t.datetime "updated_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false
t.jsonb "config", default: {"allowed_locales"=>["en"]} t.jsonb "config", default: {"allowed_locales"=>["en"]}
t.boolean "archived", default: false t.boolean "archived", default: false
t.index ["custom_domain"], name: "index_portals_on_custom_domain", unique: true
t.index ["slug"], name: "index_portals_on_slug", unique: true t.index ["slug"], name: "index_portals_on_slug", unique: true
end end