mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-03 20:48:07 +00:00 
			
		
		
		
	feat: Fetching the portal data related to a specific custom domain (#5249)
This commit is contained in:
		@@ -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],
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -281,17 +281,18 @@ Rails.application.routes.draw do
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        resources :portals, only: [:show], param: :slug do
 | 
			
		||||
          scope module: :portals do
 | 
			
		||||
            resources :categories, only: [:index, :show], param: :slug
 | 
			
		||||
            resources :articles, only: [:index, :show]
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        resources :csat_survey, only: [:show, :update]
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  get 'hc/:slug/:locale', to: 'public/api/v1/portals#show', format: 'json'
 | 
			
		||||
  get 'hc/:slug/:locale/categories', to: 'public/api/v1/portals/categories#index', format: 'json'
 | 
			
		||||
  get 'hc/:slug/:locale/:category_slug', to: 'public/api/v1/portals/categories#show', format: 'json'
 | 
			
		||||
  get 'hc/:slug/:locale/:category_slug/articles', to: 'public/api/v1/portals/articles#index', format: 'json'
 | 
			
		||||
  get 'hc/:slug/:locale/:category_slug/:id', to: 'public/api/v1/portals/articles#show', format: 'json'
 | 
			
		||||
 | 
			
		||||
  # ----------------------------------------------------------------------
 | 
			
		||||
  # Used in mailer templates
 | 
			
		||||
  resource :app, only: [:index] do
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ require 'rails_helper'
 | 
			
		||||
RSpec.describe 'Public Articles API', type: :request do
 | 
			
		||||
  let!(:account) { create(:account) }
 | 
			
		||||
  let(:agent) { create(:user, account: account, role: :agent) }
 | 
			
		||||
  let!(:portal) { create(:portal, slug: 'test-portal', config: { allowed_locales: %w[en es] }) }
 | 
			
		||||
  let!(:portal) { create(:portal, slug: 'test-portal', config: { allowed_locales: %w[en es] }, custom_domain: 'www.example.com') }
 | 
			
		||||
  let!(:category) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'en', slug: 'category_slug') }
 | 
			
		||||
  let!(:category_2) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'es', slug: 'category_2_slug') }
 | 
			
		||||
  let!(:article) { create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) }
 | 
			
		||||
@@ -15,9 +15,9 @@ RSpec.describe 'Public Articles API', type: :request do
 | 
			
		||||
    create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'GET /public/api/v1/portals/:portal_slug/articles' do
 | 
			
		||||
  describe 'GET /public/api/v1/portals/:slug/articles' do
 | 
			
		||||
    it 'Fetch all articles in the portal' do
 | 
			
		||||
      get "/public/api/v1/portals/#{portal.slug}/articles"
 | 
			
		||||
      get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/articles"
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(:success)
 | 
			
		||||
      json_response = JSON.parse(response.body)
 | 
			
		||||
@@ -35,7 +35,7 @@ RSpec.describe 'Public Articles API', type: :request do
 | 
			
		||||
                        content: 'this is some test and funny content')
 | 
			
		||||
      expect(article2.id).not_to be_nil
 | 
			
		||||
 | 
			
		||||
      get "/public/api/v1/portals/#{portal.slug}/articles",
 | 
			
		||||
      get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/articles",
 | 
			
		||||
          headers: agent.create_new_auth_token,
 | 
			
		||||
          params: { query: 'funny' }
 | 
			
		||||
      expect(response).to have_http_status(:success)
 | 
			
		||||
@@ -45,9 +45,9 @@ RSpec.describe 'Public Articles API', type: :request do
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'GET /public/api/v1/portals/:portal_slug/articles/:id' do
 | 
			
		||||
  describe 'GET /public/api/v1/portals/:slug/articles/:id' do
 | 
			
		||||
    it 'Fetch article with the id' do
 | 
			
		||||
      get "/public/api/v1/portals/#{portal.slug}/articles/#{article.id}"
 | 
			
		||||
      get "/hc/#{portal.slug}/#{category.locale}/#{category.slug}/#{article.id}"
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(:success)
 | 
			
		||||
      json_response = JSON.parse(response.body)
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe 'Public Categories API', type: :request do
 | 
			
		||||
  let!(:account) { create(:account) }
 | 
			
		||||
  let!(:portal) { create(:portal, slug: 'test-portal') }
 | 
			
		||||
  let!(:portal) { create(:portal, slug: 'test-portal', custom_domain: 'www.example.com') }
 | 
			
		||||
 | 
			
		||||
  before do
 | 
			
		||||
    create(:category, slug: 'test-category-1', portal_id: portal.id, account_id: account.id)
 | 
			
		||||
@@ -12,26 +12,19 @@ RSpec.describe 'Public Categories API', type: :request do
 | 
			
		||||
 | 
			
		||||
  describe 'GET /public/api/v1/portals/:portal_slug/categories' do
 | 
			
		||||
    it 'Fetch all categories in the portal' do
 | 
			
		||||
      get "/public/api/v1/portals/#{portal.slug}/categories"
 | 
			
		||||
      get "/hc/#{portal.slug}/categories"
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(:success)
 | 
			
		||||
      json_response = JSON.parse(response.body)
 | 
			
		||||
 | 
			
		||||
      expect(json_response['payload'].length).to eql portal.categories.count
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'GET /public/api/v1/portals/:portal_slug/categories/:slug' do
 | 
			
		||||
    it 'Fetch category with the slug' do
 | 
			
		||||
      category_slug = 'test-category-3'
 | 
			
		||||
      category_locale = 'en'
 | 
			
		||||
 | 
			
		||||
      get "/public/api/v1/portals/#{portal.slug}/categories/#{category_slug}"
 | 
			
		||||
      get "/hc/#{portal.slug}/#{category_locale}/categories"
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(:success)
 | 
			
		||||
      json_response = JSON.parse(response.body)
 | 
			
		||||
 | 
			
		||||
      expect(json_response['slug']).to eql category_slug
 | 
			
		||||
      expect(json_response['meta']['articles_count']).to be 0
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe 'Public Portals API', type: :request do
 | 
			
		||||
  let!(:account) { create(:account) }
 | 
			
		||||
  let!(:portal) { create(:portal, slug: 'test-portal', account_id: account.id) }
 | 
			
		||||
  let!(:portal) { create(:portal, slug: 'test-portal', account_id: account.id, custom_domain: 'www.example.com') }
 | 
			
		||||
 | 
			
		||||
  before do
 | 
			
		||||
    create(:portal, slug: 'test-portal-1', account_id: account.id)
 | 
			
		||||
@@ -11,12 +11,24 @@ RSpec.describe 'Public Portals API', type: :request do
 | 
			
		||||
 | 
			
		||||
  describe 'GET /public/api/v1/portals/{portal_slug}' do
 | 
			
		||||
    it 'Show portal and categories belonging to the portal' do
 | 
			
		||||
      get "/public/api/v1/portals/#{portal.slug}"
 | 
			
		||||
      get "/hc/#{portal.slug}/en"
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(:success)
 | 
			
		||||
      json_response = JSON.parse(response.body)
 | 
			
		||||
      expect(json_response['slug']).to eql 'test-portal'
 | 
			
		||||
      expect(json_response['meta']['articles_count']).to be 0
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'Throws unauthorised error for unknown domain' do
 | 
			
		||||
      portal.update(custom_domain: 'www.something.com')
 | 
			
		||||
 | 
			
		||||
      get "/hc/#{portal.slug}/en"
 | 
			
		||||
 | 
			
		||||
      expect(response).to have_http_status(:unauthorized)
 | 
			
		||||
      json_response = JSON.parse(response.body)
 | 
			
		||||
 | 
			
		||||
      expect(json_response['error']).to eql "Domain: www.example.com is not registered with us. \
 | 
			
		||||
      Please send us an email at support@chatwoot.com with the custom domain name and account API key"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user