diff --git a/app/controllers/public/api/v1/portals_controller.rb b/app/controllers/public/api/v1/portals_controller.rb index fe51ed161..e8fb867cb 100644 --- a/app/controllers/public/api/v1/portals_controller.rb +++ b/app/controllers/public/api/v1/portals_controller.rb @@ -6,6 +6,12 @@ class Public::Api::V1::PortalsController < Public::Api::V1::Portals::BaseControl def show; end + def sitemap + @help_center_url = @portal.custom_domain || ChatwootApp.help_center_root + # if help_center_url does not contain a protocol, prepend it with https + @help_center_url = "https://#{@help_center_url}" unless @help_center_url.include?('://') + end + private def portal diff --git a/app/views/public/api/v1/portals/sitemap.xml.erb b/app/views/public/api/v1/portals/sitemap.xml.erb new file mode 100644 index 000000000..d3e2b8301 --- /dev/null +++ b/app/views/public/api/v1/portals/sitemap.xml.erb @@ -0,0 +1,9 @@ + + + <% @portal.articles.where(status: :published).each do |article| %> + + <%= @help_center_url %><%= generate_article_link(@portal.slug, article.slug, false, false) %> + <%= article.updated_at.strftime("%Y-%m-%d") %> + + <% end %> + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d002bd142..658f2c1ac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -393,6 +393,7 @@ Rails.application.routes.draw do end get 'hc/:slug', to: 'public/api/v1/portals#show' + get 'hc/:slug/sitemap.xml', to: 'public/api/v1/portals#sitemap' get 'hc/:slug/:locale', to: 'public/api/v1/portals#show' get 'hc/:slug/:locale/articles', to: 'public/api/v1/portals/articles#index' get 'hc/:slug/:locale/categories', to: 'public/api/v1/portals/categories#index' diff --git a/lib/chatwoot_app.rb b/lib/chatwoot_app.rb index 842ecc479..37c90c19d 100644 --- a/lib/chatwoot_app.rb +++ b/lib/chatwoot_app.rb @@ -21,6 +21,10 @@ module ChatwootApp @custom ||= root.join('custom').exist? end + def self.help_center_root + ENV.fetch('HELPCENTER_URL', nil) || ENV.fetch('FRONTEND_URL', nil) + end + def self.extensions if custom? %w[enterprise custom] diff --git a/spec/controllers/public/api/v1/portals_controller_spec.rb b/spec/controllers/public/api/v1/portals_controller_spec.rb index 2541906f1..004d013ea 100644 --- a/spec/controllers/public/api/v1/portals_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals_controller_spec.rb @@ -1,12 +1,15 @@ require 'rails_helper' -RSpec.describe 'Public Portals API', type: :request do +RSpec.describe Public::Api::V1::PortalsController, type: :request do let!(:account) { create(:account) } + let!(:agent) { create(:user, account: account, role: :agent) } 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) create(:portal, slug: 'test-portal-2', account_id: account.id) + create_list(:article, 3, account: account, author: agent, portal: portal, status: :published) + create_list(:article, 2, account: account, author: agent, portal: portal, status: :draft) end describe 'GET /public/api/v1/portals/{portal_slug}' do @@ -28,4 +31,28 @@ RSpec.describe 'Public Portals API', type: :request do Please send us an email at support@chatwoot.com with the custom domain name and account API key" end end + + describe 'GET /public/api/v1/portals/{portal_slug}/sitemap' do + context 'when custom_domain is present' do + it 'gets a valid sitemap' do + get "/hc/#{portal.slug}/sitemap.xml" + expect(response).to have_http_status(:success) + expect(response.body).to match(/