Apply notification styles to mailing list email manually (#4283)

* Apply notification styles to mailing list email manually

* Fix failing spec
This commit is contained in:
James Kiesel 2016-06-21 11:12:30 -04:00 committed by Arpit Jalan
parent 0ed4d3d313
commit 7a6bc3f1d7
6 changed files with 37 additions and 16 deletions

View file

@ -65,9 +65,9 @@ module UserNotificationsHelper
normalize_name(post.user.name) != normalize_name(post.user.username) normalize_name(post.user.name) != normalize_name(post.user.username)
end end
def format_for_email(post, use_excerpt, style = nil) def format_for_email(post, use_excerpt)
html = use_excerpt ? post.excerpt : post.cooked html = use_excerpt ? post.excerpt : post.cooked
PrettyText.format_for_email(html, post, style).html_safe PrettyText.format_for_email(html, post).html_safe
end end
end end

View file

@ -83,11 +83,9 @@ class UserNotifications < ActionMailer::Base
return unless @posts_by_topic.present? return unless @posts_by_topic.present?
build_summary_for(user) build_summary_for(user)
build_email @user.email, apply_notification_styles build_email @user.email,
from_alias: I18n.t('user_notifications.mailing_list.from', site_name: SiteSetting.title), from_alias: I18n.t('user_notifications.mailing_list.from', site_name: SiteSetting.title),
subject: I18n.t('user_notifications.mailing_list.subject_template', subject: I18n.t('user_notifications.mailing_list.subject_template', site_name: @site_name, date: @date)
site_name: @site_name,
date: @date)
end end
def digest(user, opts={}) def digest(user, opts={})
@ -418,4 +416,12 @@ class UserNotifications < ActionMailer::Base
@markdown_linker = MarkdownLinker.new(@base_url) @markdown_linker = MarkdownLinker.new(@base_url)
@unsubscribe_key = UnsubscribeKey.create_key_for(@user, "digest") @unsubscribe_key = UnsubscribeKey.create_key_for(@user, "digest")
end end
def apply_notification_styles(email)
email.html_part.body = Email::Styles.new(email.html_part.body.to_s).tap do |styles|
styles.format_basic
styles.format_notification
end.to_html
email
end
end end

View file

@ -23,7 +23,7 @@
</td> </td>
</tr> </tr>
<tr> <tr>
<td class='body'><%= format_for_email(post, use_excerpt, :notification) %></td> <td class='body'><%= format_for_email(post, use_excerpt) %></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View file

@ -60,7 +60,7 @@
<span> - </span> <span> - </span>
<span><%= I18n.l(post.created_at, format: :long) %></span> <span><%= I18n.l(post.created_at, format: :long) %></span>
</p> </p>
<%= raw format_for_email(post, false, :notification) %> <%= raw format_for_email(post, false) %>
<hr /> <hr />
</div> </div>
<% end %> <% end %>

View file

@ -394,16 +394,31 @@ JS
fragment.to_html fragment.to_html
end end
# Given a Nokogiri doc, convert all links to absolute
def self.make_all_links_absolute(doc)
site_uri = nil
doc.css("a").each do |link|
href = link["href"].to_s
begin
uri = URI(href)
site_uri ||= URI(Discourse.base_url)
link["href"] = "#{site_uri}#{link['href']}" unless uri.host.present?
rescue URI::InvalidURIError, URI::InvalidComponentError
# leave it
end
end
end
def self.strip_image_wrapping(doc) def self.strip_image_wrapping(doc)
doc.css(".lightbox-wrapper .meta").remove doc.css(".lightbox-wrapper .meta").remove
end end
def self.format_for_email(html, post = nil, style = nil) def self.format_for_email(html, post = nil)
Email::Styles.new(html, style: style).tap do |doc| doc = Nokogiri::HTML.fragment(html)
DiscourseEvent.trigger(:reduce_cooked, doc, post) DiscourseEvent.trigger(:reduce_cooked, doc, post)
doc.make_all_links_absolute strip_image_wrapping(doc)
doc.send :"format_#{style}" if style make_all_links_absolute(doc)
end.to_html doc.to_html
end end
protected protected

View file

@ -318,7 +318,7 @@ HTML
it "adds base url to relative links" do it "adds base url to relative links" do
html = "<p><a class=\"mention\" href=\"/users/wiseguy\">@wiseguy</a>, <a class=\"mention\" href=\"/users/trollol\">@trollol</a> what do you guys think? </p>" html = "<p><a class=\"mention\" href=\"/users/wiseguy\">@wiseguy</a>, <a class=\"mention\" href=\"/users/trollol\">@trollol</a> what do you guys think? </p>"
output = described_class.format_for_email(html, post) output = described_class.format_for_email(html, post)
expect(output).to eq("<p><a href=\"#{base_url}/users/wiseguy\">@wiseguy</a>, <a href=\"#{base_url}/users/trollol\">@trollol</a> what do you guys think? </p>") expect(output).to eq("<p><a class=\"mention\" href=\"#{base_url}/users/wiseguy\">@wiseguy</a>, <a class=\"mention\" href=\"#{base_url}/users/trollol\">@trollol</a> what do you guys think? </p>")
end end
it "doesn't change external absolute links" do it "doesn't change external absolute links" do