Skip to content

A platform for community discussion. Free, open, simple.

FIX: subfolder digest emails have incorrect URLs

Neil Lalonde4e6e4a83df56
diff --git a/app/views/user_notifications/digest.html.erb b/app/views/user_notifications/digest.html.erb
index b09bc3b736c..de8e805c8d4 100644
--- a/app/views/user_notifications/digest.html.erb
+++ b/app/views/user_notifications/digest.html.erb
@@ -420,7 +420,7 @@ body, table, td, th, h1, h2, h3 {font-family: Helvetica, Arial, sans-serif !impo
<%=raw(t 'user_notifications.digest.unsubscribe',
site_link: html_site_link(@anchor_color),
email_preferences_link: link_to(t('user_notifications.digest.your_email_settings'), Discourse.base_url + '/my/preferences/emails'),
- unsubscribe_link: link_to(t('user_notifications.digest.click_here'), email_unsubscribe_url(host: Discourse.base_url, key: @unsubscribe_key), {:style=>"color: ##{@anchor_color}"})) %>
+ unsubscribe_link: link_to(t('user_notifications.digest.click_here'), "#{Discourse.base_url}/email/unsubscribe/#{@unsubscribe_key}", {:style=>"color: ##{@anchor_color}"})) %>
</div>
<%= digest_custom_html("below_footer") %>
diff --git a/app/views/user_notifications/digest.text.erb b/app/views/user_notifications/digest.text.erb
index e3bbc1f8188..40ce99b8ef3 100644
--- a/app/views/user_notifications/digest.text.erb
+++ b/app/views/user_notifications/digest.text.erb
@@ -13,7 +13,7 @@
### <%=t 'user_notifications.digest.popular_topics' %>
<%- @popular_topics.each_with_index do |t,i| %>
-<%= raw(@markdown_linker.create(t.title, t.relative_url)) %>
+<%= raw(@markdown_linker.create(t.title, t.url)) %>
<%- if t.best_post.present? %>
<%= raw(t.best_post.excerpt(1000, strip_links: true, text_entities: true, markdown_images: true)) %>
@@ -29,7 +29,7 @@
### <%=t 'user_notifications.digest.popular_posts' %>
<%- @popular_posts.each_with_index do |post,i| %>
-<%= post.user.username -%> - <%= raw(@markdown_linker.create(post.topic.title, post.topic.relative_url)) %>
+<%= post.user.username -%> - <%= raw(@markdown_linker.create(post.topic.title, post.topic.url)) %>
<%= raw(post.excerpt(1000, strip_links: true, text_entities: true, markdown_images: true)) %>
--------------------------------------------------------------------------------
@@ -41,7 +41,7 @@
**<%=t 'user_notifications.digest.more_new' %>**
<%- @other_new_for_you.each do |t| %>
-* <%= raw(@markdown_linker.create(t.title, t.relative_url)) %> - <%= t.posts_count %> - <%- if t.category %>[<%= t.category.name %>]<%- end %>
+* <%= raw(@markdown_linker.create(t.title, t.url)) %> - <%= t.posts_count %> - <%- if t.category %>[<%= t.category.name %>]<%- end %>
<%- end -%>
<%- end %>
@@ -53,7 +53,7 @@
<%=raw(t :'user_notifications.digest.unsubscribe',
site_link: site_link,
email_preferences_link: raw(@markdown_linker.create(t('user_notifications.digest.your_email_settings'), '/my/preferences/emails')),
- unsubscribe_link: raw(@markdown_linker.create(t('user_notifications.digest.click_here'), email_unsubscribe_url(key: @unsubscribe_key, only_path: true)))) %>
+ unsubscribe_link: raw(@markdown_linker.create(t('user_notifications.digest.click_here'), "/email/unsubscribe/#{@unsubscribe_key}"))) %>
<%= raw(@markdown_linker.references) %>
diff --git a/lib/markdown_linker.rb b/lib/markdown_linker.rb
index fab7c5a6b60..9e69f6c1060 100644
--- a/lib/markdown_linker.rb
+++ b/lib/markdown_linker.rb
@@ -9,7 +9,7 @@ class MarkdownLinker
end
def create(title, url)
- @markdown_links[@index] = "#{@base_url}#{url}"
+ @markdown_links[@index] = url.start_with?(@base_url) ? url : "#{@base_url}#{url}"
result = "[#{title}][#{@index}]"
@index += 1
result
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index b144995f1b6..015789e0b78 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -127,9 +127,7 @@ describe UserNotifications do
context "with new topics" do
- before do
- Fabricate(:topic, user: Fabricate(:coding_horror), created_at: 1.hour.ago)
- end
+ let!(:popular_topic) { Fabricate(:topic, user: Fabricate(:coding_horror), created_at: 1.hour.ago) }
it "works" do
expect(subject.to).to eq([user.email])
@@ -206,6 +204,22 @@ describe UserNotifications do
expect(html).to include '1E1E1E'
expect(html).to include '858585'
end
+
+ it "supports subfolder" do
+ GlobalSetting.stubs(:relative_url_root).returns('/forum')
+ Discourse.stubs(:base_uri).returns("/forum")
+ html = subject.html_part.body.to_s
+ text = subject.text_part.body.to_s
+ expect(html).to be_present
+ expect(text).to be_present
+ expect(html).to_not include("/forum/forum")
+ expect(text).to_not include("/forum/forum")
+ expect(subject.header["List-Unsubscribe"].to_s).to match(/http:\/\/test.localhost\/forum\/email\/unsubscribe\/\h{64}/)
+
+ topic_url = "http://test.localhost/forum/t/#{popular_topic.slug}/#{popular_topic.id}"
+ expect(html).to include(topic_url)
+ expect(text).to include(topic_url)
+ end
end
end