From 3e7b418ea8d7bb57e5d939ab43c9338170e8f734 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 22 Jul 2013 15:06:37 -0400 Subject: [PATCH] First Try: Include Post History (context) in Notification Emails --- app/mailers/user_notifications.rb | 28 +++++++++++++++++++++---- app/models/site_setting.rb | 1 + config/locales/server.en.yml | 19 +++++++++++------ lib/email/sender.rb | 3 +++ lib/email/styles.rb | 13 ++++++++++-- spec/mailers/user_notifications_spec.rb | 6 +++++- 6 files changed, 57 insertions(+), 13 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 7019b396b39..d1fce949caf 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -1,5 +1,6 @@ require_dependency 'markdown_linker' require_dependency 'email/message_builder' +require_dependency 'age_words' class UserNotifications < ActionMailer::Base default charset: 'UTF-8' @@ -29,7 +30,6 @@ class UserNotifications < ActionMailer::Base def private_message(user, opts={}) post = opts[:post] - build_email user.email, template: "user_notifications.private_message", message: post.raw, @@ -94,6 +94,14 @@ class UserNotifications < ActionMailer::Base protected + def email_post_markdown(post) + result = "[email-indent]\n" + result << "#### #{I18n.t('user_notifications.posted_by', username: post.username, post_date: post.created_at.strftime("%m/%d/%Y"))}\n\n" + result << "#{post.raw}\n\n" + result << "[/email-indent]\n" + result + end + def notification_email(user, opts) @notification = opts[:notification] return unless @notification.present? @@ -104,12 +112,26 @@ class UserNotifications < ActionMailer::Base username = @notification.data_hash[:display_username] notification_type = Notification.types[opts[:notification].notification_type].to_s + context = "" + context_posts = Post.where(topic_id: @post.topic_id) + .where("post_number < ?", @post.post_number) + .order('created_at desc') + .limit(SiteSetting.email_posts_context) + + if context_posts.present? + context << "---\n### #{I18n.t('user_notifications.post_history')}\n" + context_posts.each do |cp| + context << email_post_markdown(cp) + end + end + email_opts = { topic_title: @notification.data_hash[:topic_title], - message: @post.raw, + message: email_post_markdown(@post), url: @post.url, post_id: @post.id, topic_id: @post.topic_id, + context: context, username: username, add_unsubscribe_link: true, allow_reply_by_email: opts[:allow_reply_by_email], @@ -124,6 +146,4 @@ class UserNotifications < ActionMailer::Base build_email(user.email, email_opts) end - - end diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index a8c2e16bd57..4908a26db31 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -110,6 +110,7 @@ class SiteSetting < ActiveRecord::Base setting(:max_favorites_per_day, 20) setting(:email_time_window_mins, 10) + setting(:email_posts_context, 5) # How many characters we can import into a onebox setting(:onebox_max_chars, 5000) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 19695a6a7fc..7e30be78ef4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -617,6 +617,7 @@ en: create_thumbnails: "Create thumbnails for lightboxed images" email_time_window_mins: "How many minutes we wait before sending a user mail, to give them a chance to see it first" + email_posts_context: "How many posts to include in emails to show context" flush_timings_secs: "How frequently we flush timing data to the server, in seconds" max_word_length: "The maximum allowed word length, in characters, in a topic title" title_min_entropy: "The minimum allowed entropy (unique characters, non-english count for more) required for a topic title" @@ -940,6 +941,9 @@ en: reply_by_email: "To respond, reply to this email or visit %{base_url}%{url} in your browser." visit_link_to_respond: "To respond, visit %{base_url}%{url} in your browser." + post_history: "Post History" + posted_by: "Posted by %{username} on %{post_date}" + user_invited_to_private_message: subject_template: "[%{site_name}] %{username} invited you to a private message '%{topic_title}'" text_body_template: | @@ -952,9 +956,10 @@ en: text_body_template: | %{username} replied to your post in '%{topic_title}' on %{site_name}: - --- %{message} + %{context} + --- %{respond_instructions} @@ -963,20 +968,22 @@ en: text_body_template: | %{username} quoted you in '%{topic_title}' on %{site_name}: - --- %{message} + %{context} + --- %{respond_instructions} user_mentioned: subject_template: "[%{site_name}] %{username} mentioned you in '%{topic_title}'" text_body_template: | - %{username} mentioned you in '%{topic_title}' on %{site_name}: + ### %{username} mentioned you in '%{topic_title}' on %{site_name}: - --- %{message} + %{context} + --- %{respond_instructions} @@ -985,9 +992,10 @@ en: text_body_template: | %{username} posted in '%{topic_title}' on %{site_name}: - --- %{message} + %{context} + --- %{respond_instructions} @@ -1006,7 +1014,6 @@ en: text_body_template: | %{private_message_from} just sent you a private message - --- %{message} --- diff --git a/lib/email/sender.rb b/lib/email/sender.rb index f770ba302c5..f311d322473 100644 --- a/lib/email/sender.rb +++ b/lib/email/sender.rb @@ -37,6 +37,9 @@ module Email body renderer.html end + @message.parts[0].body = @message.parts[0].body.to_s.gsub!(/\[\/?email-indent\]/, '') + + @message.text_part.content_type = 'text/plain; charset=UTF-8' # Set up the email log diff --git a/lib/email/styles.rb b/lib/email/styles.rb index 9bba50bc6e6..abcca0da14b 100644 --- a/lib/email/styles.rb +++ b/lib/email/styles.rb @@ -22,11 +22,16 @@ module Email if img['src'][0] == "/" img['src'] = "#{Discourse.base_url}#{img['src']}" end - end + + @fragment.css('div.post-indent').each do |div| + div['style'] = 'margin-left: 15px; margin-top: 20px; max-width: 694px;' + end + end def format_html + @fragment.css('h3').each do |h3| h3['style'] = 'margin: 15px 0 20px 0; border-bottom: 1px solid #ddd;' end @@ -55,10 +60,14 @@ module Email @fragment.css('div.digest-post').each do |div| div['style'] = 'margin-left: 15px; margin-top: 20px; max-width: 694px;' end + end def to_html - @fragment.to_html + result = @fragment.to_html + result.gsub!(/\[email-indent\]/, "
") + result.gsub!(/\[\/email-indent\]/, "
") + result end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 2ee2c4f14db..69be03406f5 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -91,7 +91,11 @@ describe UserNotifications do end it "has a message" do - expects_build_with(has_entry(:message, post.raw)) + expects_build_with(has_key(:message)) + end + + it "has a context" do + expects_build_with(has_key(:context)) end it "has an unsubscribe link" do