feat: Fetching the portal data related to a specific custom domain (#5249)

This commit is contained in:
Tejaswini Chile
2022-09-07 12:22:24 +05:30
committed by GitHub
parent 6574407636
commit db73d033b7
11 changed files with 74 additions and 35 deletions

View File

@@ -4,6 +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
layout 'vueapp'
@@ -37,6 +38,15 @@ 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)
return unless portal
redirect_to "/hc/#{portal.slug}"
end
def app_config
{
APP_VERSION: Chatwoot.config[:version],

View File

@@ -1,5 +1,7 @@
class Public::Api::V1::Portals::ArticlesController < ApplicationController
class Public::Api::V1::Portals::ArticlesController < PublicController
before_action :ensure_custom_domain_request, only: [:show, :index]
before_action :set_portal
before_action :set_category
before_action :set_article, only: [:show]
def index
@@ -12,11 +14,15 @@ class Public::Api::V1::Portals::ArticlesController < ApplicationController
private
def set_article
@article = @portal.articles.find(params[:id])
@article = @category.articles.find(params[:id])
end
def set_category
@category = @portal.categories.find_by!(slug: params[:category_slug])
end
def set_portal
@portal = ::Portal.find_by!(slug: params[:portal_slug], archived: false)
@portal = @portals.find_by!(slug: params[:slug], archived: false)
end
def list_params

View File

@@ -1,4 +1,5 @@
class Public::Api::V1::Portals::CategoriesController < PublicController
before_action :ensure_custom_domain_request, only: [:show, :index]
before_action :set_portal
before_action :set_category, only: [:show]
@@ -11,10 +12,10 @@ class Public::Api::V1::Portals::CategoriesController < PublicController
private
def set_category
@category = @portal.categories.find_by!(slug: params[:slug])
@category = @portal.categories.find_by!(locale: params[:locale])
end
def set_portal
@portal = ::Portal.find_by!(slug: params[:portal_slug], archived: false)
@portal = @portals.find_by!(slug: params[:slug], archived: false)
end
end

View File

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

View File

@@ -3,4 +3,19 @@
class PublicController < ActionController::Base
include RequestExceptionHandler
skip_before_action :verify_authenticity_token
private
def ensure_custom_domain_request
custom_domain = request.host
@portals = ::Portal.where(custom_domain: custom_domain)
return if @portals.present?
render json: {
error: "Domain: #{custom_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
end

View File

@@ -7,20 +7,20 @@ json.position category.position
json.related_categories do
if category.related_categories.any?
json.array! category.related_categories.each do |related_category|
json.partial! partial: 'associated_category', category: related_category
json.partial! partial: 'public/api/v1/models/associated_category.json.jbuilder', category: related_category
end
end
end
if category.parent_category.present?
json.parent_category do
json.partial! partial: 'associated_category', category: category.parent_category
json.partial! partial: 'public/api/v1/models/associated_category.json.jbuilder', category: category.parent_category
end
end
if category.root_category.present?
json.root_category do
json.partial! partial: 'associated_category', category: category.root_category
json.partial! partial: 'public/api/v1/models/associated_category.json.jbuilder', category: category.root_category
end
end

View File

@@ -8,7 +8,7 @@ json.slug portal.slug
json.categories do
if portal.categories.any?
json.array! portal.categories.each do |category|
json.partial! 'api/v1/models/category.json.jbuilder', category: category
json.partial! 'public/api/v1/models/category.json.jbuilder', category: category
end
end
end