Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Support new Discourse Home Pages Theme Component for highly integrated presentation #30

Merged
merged 9 commits into from
Jun 14, 2024
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"
merefield marked this conversation as resolved.
Show resolved Hide resolved
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