FIX: Render links with subfolders properly in Discobot

Currently, some links aren’t properly built in Discobot when Discourse
is hosted in a subfolder. This is because we’re providing
`Discourse.base_url` to the Rails helpers which contains the base URL
*with* the prefix. But Rails helpers already handle this prefix so the
resulting link gets the prefix twice.

The fix is quite simple: use `Discourse.base_url_no_prefix` instead of
`Discourse.base_url`.
This commit is contained in:
Loïc Guitaut 2023-04-06 17:55:30 +02:00 committed by Loïc Guitaut
parent a5235f7d16
commit 430d6308a8
3 changed files with 21 additions and 12 deletions

View File

@ -638,7 +638,10 @@ module DiscourseNarrativeBot
end
def url_helpers(url, opts = {})
Rails.application.routes.url_helpers.public_send(url, opts.merge(host: Discourse.base_url))
Rails.application.routes.url_helpers.public_send(
url,
opts.merge(host: Discourse.base_url_no_prefix),
)
end
end
end

View File

@ -286,22 +286,27 @@ RSpec.describe DiscourseNarrativeBot::NewUserNarrative do
)
end
it "should create the right reply when the bookmark is created" do
post.update!(user: discobot_user)
narrative.expects(:enqueue_timeout_job).with(user)
narrative.input(:bookmark, user, post: post)
new_post = Post.last
profile_page_url = "#{Discourse.base_url}/u/#{user.username}"
expected_raw = <<~RAW
context "when the bookmark is created" do
let(:profile_page_url) { "#{Discourse.base_url_no_prefix}/prefix/u/#{user.username}" }
let(:new_post) { Post.last }
let(:user_state) { narrative.get_data(user)[:state].to_sym }
let(:expected_raw) { <<~RAW }
#{I18n.t("discourse_narrative_bot.new_user_narrative.bookmark.reply", bookmark_url: "#{profile_page_url}/activity/bookmarks", base_uri: "")}
#{I18n.t("discourse_narrative_bot.new_user_narrative.onebox.instructions", base_uri: "")}
RAW
expect(new_post.raw).to eq(expected_raw.chomp)
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_onebox)
before do
set_subfolder("/prefix")
post.update!(user: discobot_user)
narrative.stubs(:enqueue_timeout_job).with(user)
end
it "creates the right reply" do
narrative.input(:bookmark, user, post: post)
expect(new_post.raw).to eq(expected_raw.chomp)
expect(user_state).to eq(:tutorial_onebox)
end
end
it "should skip tutorials in SiteSetting.discourse_narrative_bot_skip_tutorials" do

View File

@ -153,6 +153,7 @@ module Helpers
global_setting :relative_url_root, f
old_root = ActionController::Base.config.relative_url_root
ActionController::Base.config.relative_url_root = f
Rails.application.routes.stubs(:relative_url_root).returns(f)
before_next_spec { ActionController::Base.config.relative_url_root = old_root }
end