From e1ca8df15e23aed5f138d31bcacc0a3cc9044d52 Mon Sep 17 00:00:00 2001 From: twain Date: Sat, 22 Jun 2013 15:07:42 +0200 Subject: [PATCH] added strip_markdown to notification mails --- Changelog.md | 1 + app/helpers/notifier_helper.rb | 6 ++- .../notification_mailers/also_commented.rb | 3 +- .../notification_mailers/comment_on_post.rb | 3 +- spec/helpers/notifier_helper_spec.rb | 47 ++++++++++++++++--- spec/mailers/notifier_spec.rb | 11 +++-- 6 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Changelog.md b/Changelog.md index fcee59373..24b0f1dcd 100644 --- a/Changelog.md +++ b/Changelog.md @@ -43,6 +43,7 @@ * Leaving the `to` field blank when sending a private message causes a server error [#4227](https://github.com/diaspora/diaspora/issues/4227) * Fix hashtags that start a line when posting to Facebook or Twitter [#3768](https://github.com/diaspora/diaspora/issues/3768) [#4154](https://github.com/diaspora/diaspora/issues/4154) * Show avatar of recent user in conversation list [#4237](https://github.com/diaspora/diaspora/issues/4237) +* Render markdown content for prettier email subjects and titles [#4182](https://github.com/diaspora/diaspora/issues/4182) ## Features diff --git a/app/helpers/notifier_helper.rb b/app/helpers/notifier_helper.rb index 050d98a4e..d2d56d7bd 100644 --- a/app/helpers/notifier_helper.rb +++ b/app/helpers/notifier_helper.rb @@ -6,7 +6,8 @@ module NotifierHelper def post_message(post, opts={}) opts[:length] ||= 200 if post.respond_to? :formatted_message - message = truncate(post.formatted_message(:plain_text => true), :length => opts[:length]) + message = strip_markdown(post.formatted_message(:plain_text => true)) + message = truncate(message, :length => opts[:length]) message = process_newlines(message) if opts[:process_newlines] message else @@ -19,7 +20,8 @@ module NotifierHelper # @return [String] The truncated and formatted comment. def comment_message(comment, opts={}) opts[:length] ||= 600 - text = truncate(comment.text, :length => opts[:length]) + text = strip_markdown(comment.text) + text = truncate(text, :length => opts[:length]) text = process_newlines(text) if opts[:process_newlines] text end diff --git a/app/mailers/notification_mailers/also_commented.rb b/app/mailers/notification_mailers/also_commented.rb index 786a16e8a..cb42fc4df 100644 --- a/app/mailers/notification_mailers/also_commented.rb +++ b/app/mailers/notification_mailers/also_commented.rb @@ -1,6 +1,7 @@ module NotificationMailers class AlsoCommented < NotificationMailers::Base include ActionView::Helpers::TextHelper + include MarkdownifyHelper attr_accessor :comment delegate :post, to: :comment, prefix: true @@ -10,7 +11,7 @@ module NotificationMailers if mail? @headers[:from] = "\"#{@comment.author_name} (Diaspora*)\" <#{AppConfig.mail.sender_address}>" - @headers[:subject] = truncate(@comment.comment_email_subject, :length => TRUNCATION_LEN) + @headers[:subject] = truncate(strip_markdown(@comment.comment_email_subject.squish), :length => TRUNCATION_LEN) @headers[:subject] = "Re: #{@headers[:subject]}" end end diff --git a/app/mailers/notification_mailers/comment_on_post.rb b/app/mailers/notification_mailers/comment_on_post.rb index d7735a887..4eb5faec2 100644 --- a/app/mailers/notification_mailers/comment_on_post.rb +++ b/app/mailers/notification_mailers/comment_on_post.rb @@ -1,6 +1,7 @@ module NotificationMailers class CommentOnPost < NotificationMailers::Base include ActionView::Helpers::TextHelper + include MarkdownifyHelper attr_accessor :comment @@ -8,7 +9,7 @@ module NotificationMailers @comment = Comment.find(comment_id) @headers[:from] = "\"#{@comment.author_name} (Diaspora*)\" <#{AppConfig.mail.sender_address}>" - @headers[:subject] = truncate(@comment.comment_email_subject, :length => TRUNCATION_LEN) + @headers[:subject] = truncate(strip_markdown(@comment.comment_email_subject.squish), :length => TRUNCATION_LEN) @headers[:subject] = "Re: #{@headers[:subject]}" end end diff --git a/spec/helpers/notifier_helper_spec.rb b/spec/helpers/notifier_helper_spec.rb index 421679585..23b178101 100644 --- a/spec/helpers/notifier_helper_spec.rb +++ b/spec/helpers/notifier_helper_spec.rb @@ -6,15 +6,50 @@ require 'spec_helper' describe NotifierHelper do include MarkdownifyHelper - - describe '#comment_message' do + + describe '#post_message' do before do - @comment = FactoryGirl.create(:comment) + # post for truncate test + @post = FactoryGirl.create(:status_message) + @post.text = "hi dude! "*10 + @truncated_post = "hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi du..." + # post for markdown test + @markdown_post = FactoryGirl.create(:status_message) + @markdown_post.text = "[link](http://diasporafoundation.org) **bold text** *other text*" + @striped_markdown_post = "link bold text other text" end - it 'truncates the comment' do - opts = {:length => 2} - comment_message(@comment, opts).should == truncate(@comment.text, opts) + it 'truncates in the post' do + opts = {:length => @post.text.length - 10} + post_message(@post, opts).should == @truncated_post + end + + it 'strip markdown in the post' do + opts = {:length => @markdown_post.text.length} + post_message(@markdown_post, opts).should == @striped_markdown_post + end + end + + describe '#comment_message' do + before do + # comment for truncate test + @comment = FactoryGirl.create(:comment) + @comment.text = "hi dude! "*10 + @truncated_comment = "hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi dude! hi d..." + # comment for markdown test + @markdown_comment = FactoryGirl.create(:comment) + @markdown_comment.text = "[link](http://diasporafoundation.org) **bold text** *other text*" + @striped_markdown_comment = "link bold text other text" + end + + it 'truncates in the comment' do + opts = {:length => @comment.text.length - 10} + comment_message(@comment, opts).should == @truncated_comment + end + + it 'strip markdown in the comment' do + opts = {:length => @markdown_comment.text.length} + comment_message(@markdown_comment, opts).should == @striped_markdown_comment end end end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index fab09a643..ebb076962 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe Notifier do include ActionView::Helpers::TextHelper + include MarkdownifyHelper let(:person) { FactoryGirl.create(:person) } @@ -214,7 +215,7 @@ describe Notifier do end context "comments" do - let(:commented_post) {bob.post(:status_message, :text => "It's really sunny outside today, and this is a super long status message! #notreally", :to => :all)} + let(:commented_post) {bob.post(:status_message, :text => "### Headline \r\n It's **really** sunny outside today, and this is a super long status message! #notreally", :to => :all)} let(:comment) { eve.comment!(commented_post, "Totally is")} describe ".comment_on_post" do @@ -228,8 +229,8 @@ describe Notifier do comment_mail["From"].to_s.should == "\"#{eve.name} (Diaspora*)\" <#{AppConfig.mail.sender_address}>" end - it 'SUBJECT: has a snippet of the post contents' do - comment_mail.subject.should == "Re: #{truncate(commented_post.raw_message, :length => 70)}" + it 'SUBJECT: has a snippet of the post contents, without markdown and without newlines' do + comment_mail.subject.should == "Re: Headline It's really sunny outside today, and this is a super long ..." end context 'BODY' do @@ -269,8 +270,8 @@ describe Notifier do comment_mail["From"].to_s.should == "\"#{eve.name} (Diaspora*)\" <#{AppConfig.mail.sender_address}>" end - it 'SUBJECT: has a snippet of the post contents' do - comment_mail.subject.should == "Re: #{truncate(commented_post.raw_message, :length => 70)}" + it 'SUBJECT: has a snippet of the post contents, without markdown and without newlines' do + comment_mail.subject.should == "Re: Headline It's really sunny outside today, and this is a super long ..." end context 'BODY' do