Skip to content

Commit

Permalink
Merge pull request #30 from merefield/main
Browse files Browse the repository at this point in the history
FEATURE: Support new Discourse Home Pages Theme Component for highly integrated presentation
  • Loading branch information
angusmcleod committed Jun 14, 2024
2 parents 052f215 + d2db740 commit e012082
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
20 changes: 19 additions & 1 deletion app/controllers/landing_pages/landing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,30 @@ def show
@footer = @global.footer if @global.footer.present?
end

render inline: @page.body, layout: "landing"
if request.format == "json"
render json: { page: @page.body }
else
if !SiteSetting.landing_redirect_to_homepages || visit_from_crawler?
render inline: @page.body, layout: "landing"
else
redirect_to path("/#{SiteSetting.landing_redirect_to_homepages_root_path}/#{request.path.split("/").last}")
end
end
else
redirect_to path("/")
end
end

def path(p)
"#{Discourse.base_url}#{p}"
end

def visit_from_crawler?
request.user_agent && (request.media_type.blank? || request.media_type.include?("html")) &&
!%w[json rss].include?(params[:format]) &&
CrawlerDetection.crawler?(request.user_agent, request.headers["HTTP_VIA"])
end

def contact
Jobs.enqueue(
:send_contact_email,
Expand Down
4 changes: 3 additions & 1 deletion config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ en:
site_settings:
landing_contact_email: "Email to send landing page contacts to."
landing_authorized_extensions: "Authorized file extensions for landing pages."
landing_authorized_extensions: "Authorized file extensions for landing pages."
landing_redirect_to_homepages: "Redirect to landing pages to be consumed and presented by Home Pages Theme Component."
landing_redirect_to_homepages_root_path: "Root path for Landing Pages when presented via Home Pages Theme Component."
7 changes: 6 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ plugins:
landing_authorized_extensions:
default: "jpg|jpeg|png|woff|woff2|svg|eot|ttf|otf|gif|js|json"
type: list
list_type: compact
list_type: compact
landing_redirect_to_homepages:
default: false
type: boolean
landing_redirect_to_homepages_root_path:
default: '/home-pages/sp'
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# name: discourse-landing-pages
# about: Adds landing pages to Discourse
# version: 0.3.0
# version: 0.3.1
# authors: Angus McLeod, Pablo Cabido
# url: https://github.com/paviliondev/discourse-landing-pages

Expand Down
20 changes: 20 additions & 0 deletions spec/requests/landing_pages/landing_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,24 @@
get "/private"
expect(response.status).to eq(403)
end

it "sends back a json representation of the page" do
get "/public.json"
expect(response.status).to eq(200)
expect(response.parsed_body["page"]["body"]).to eq("body")
end

it "shows the normal page if the user agent is a bot even if redirect to Home Page TC is set" do
SiteSetting.landing_redirect_to_homepages = true
get "/public", headers: { "HTTP_USER_AGENT" => "Googlebot" }
expect(response.status).to eq(200)
expect(response.parsed_body["body"]).to eq("body")
end

it "shows the normal page if redirect to Home Page TC is set to false" do
SiteSetting.landing_redirect_to_homepages = false
get "/public"
expect(response.status).to eq(200)
expect(response.parsed_body["body"]).to eq("body")
end
end

0 comments on commit e012082

Please sign in to comment.