First Try: Include Post History (context) in Notification Emails

This commit is contained in:
Robin Ward 2013-07-22 15:06:37 -04:00
parent 112b9f9c2a
commit 3e7b418ea8
6 changed files with 57 additions and 13 deletions

View File

@ -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

View File

@ -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)

View File

@ -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}
---

View File

@ -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

View File

@ -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\]/, "<div style='margin-left: 15px'>")
result.gsub!(/\[\/email-indent\]/, "</div>")
result
end

View File

@ -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