mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
First Try: Include Post History (context) in Notification Emails
This commit is contained in:
parent
112b9f9c2a
commit
3e7b418ea8
6 changed files with 57 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
||||||
require_dependency 'markdown_linker'
|
require_dependency 'markdown_linker'
|
||||||
require_dependency 'email/message_builder'
|
require_dependency 'email/message_builder'
|
||||||
|
require_dependency 'age_words'
|
||||||
|
|
||||||
class UserNotifications < ActionMailer::Base
|
class UserNotifications < ActionMailer::Base
|
||||||
default charset: 'UTF-8'
|
default charset: 'UTF-8'
|
||||||
|
@ -29,7 +30,6 @@ class UserNotifications < ActionMailer::Base
|
||||||
|
|
||||||
def private_message(user, opts={})
|
def private_message(user, opts={})
|
||||||
post = opts[:post]
|
post = opts[:post]
|
||||||
|
|
||||||
build_email user.email,
|
build_email user.email,
|
||||||
template: "user_notifications.private_message",
|
template: "user_notifications.private_message",
|
||||||
message: post.raw,
|
message: post.raw,
|
||||||
|
@ -94,6 +94,14 @@ class UserNotifications < ActionMailer::Base
|
||||||
|
|
||||||
protected
|
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)
|
def notification_email(user, opts)
|
||||||
@notification = opts[:notification]
|
@notification = opts[:notification]
|
||||||
return unless @notification.present?
|
return unless @notification.present?
|
||||||
|
@ -104,12 +112,26 @@ class UserNotifications < ActionMailer::Base
|
||||||
username = @notification.data_hash[:display_username]
|
username = @notification.data_hash[:display_username]
|
||||||
notification_type = Notification.types[opts[:notification].notification_type].to_s
|
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 = {
|
email_opts = {
|
||||||
topic_title: @notification.data_hash[:topic_title],
|
topic_title: @notification.data_hash[:topic_title],
|
||||||
message: @post.raw,
|
message: email_post_markdown(@post),
|
||||||
url: @post.url,
|
url: @post.url,
|
||||||
post_id: @post.id,
|
post_id: @post.id,
|
||||||
topic_id: @post.topic_id,
|
topic_id: @post.topic_id,
|
||||||
|
context: context,
|
||||||
username: username,
|
username: username,
|
||||||
add_unsubscribe_link: true,
|
add_unsubscribe_link: true,
|
||||||
allow_reply_by_email: opts[:allow_reply_by_email],
|
allow_reply_by_email: opts[:allow_reply_by_email],
|
||||||
|
@ -124,6 +146,4 @@ class UserNotifications < ActionMailer::Base
|
||||||
build_email(user.email, email_opts)
|
build_email(user.email, email_opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -110,6 +110,7 @@ class SiteSetting < ActiveRecord::Base
|
||||||
setting(:max_favorites_per_day, 20)
|
setting(:max_favorites_per_day, 20)
|
||||||
|
|
||||||
setting(:email_time_window_mins, 10)
|
setting(:email_time_window_mins, 10)
|
||||||
|
setting(:email_posts_context, 5)
|
||||||
|
|
||||||
# How many characters we can import into a onebox
|
# How many characters we can import into a onebox
|
||||||
setting(:onebox_max_chars, 5000)
|
setting(:onebox_max_chars, 5000)
|
||||||
|
|
|
@ -617,6 +617,7 @@ en:
|
||||||
create_thumbnails: "Create thumbnails for lightboxed images"
|
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_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"
|
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"
|
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"
|
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."
|
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."
|
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:
|
user_invited_to_private_message:
|
||||||
subject_template: "[%{site_name}] %{username} invited you to a private message '%{topic_title}'"
|
subject_template: "[%{site_name}] %{username} invited you to a private message '%{topic_title}'"
|
||||||
text_body_template: |
|
text_body_template: |
|
||||||
|
@ -952,9 +956,10 @@ en:
|
||||||
text_body_template: |
|
text_body_template: |
|
||||||
%{username} replied to your post in '%{topic_title}' on %{site_name}:
|
%{username} replied to your post in '%{topic_title}' on %{site_name}:
|
||||||
|
|
||||||
---
|
|
||||||
%{message}
|
%{message}
|
||||||
|
|
||||||
|
%{context}
|
||||||
|
|
||||||
---
|
---
|
||||||
%{respond_instructions}
|
%{respond_instructions}
|
||||||
|
|
||||||
|
@ -963,20 +968,22 @@ en:
|
||||||
text_body_template: |
|
text_body_template: |
|
||||||
%{username} quoted you in '%{topic_title}' on %{site_name}:
|
%{username} quoted you in '%{topic_title}' on %{site_name}:
|
||||||
|
|
||||||
---
|
|
||||||
%{message}
|
%{message}
|
||||||
|
|
||||||
|
%{context}
|
||||||
|
|
||||||
---
|
---
|
||||||
%{respond_instructions}
|
%{respond_instructions}
|
||||||
|
|
||||||
user_mentioned:
|
user_mentioned:
|
||||||
subject_template: "[%{site_name}] %{username} mentioned you in '%{topic_title}'"
|
subject_template: "[%{site_name}] %{username} mentioned you in '%{topic_title}'"
|
||||||
text_body_template: |
|
text_body_template: |
|
||||||
%{username} mentioned you in '%{topic_title}' on %{site_name}:
|
### %{username} mentioned you in '%{topic_title}' on %{site_name}:
|
||||||
|
|
||||||
---
|
|
||||||
%{message}
|
%{message}
|
||||||
|
|
||||||
|
%{context}
|
||||||
|
|
||||||
---
|
---
|
||||||
%{respond_instructions}
|
%{respond_instructions}
|
||||||
|
|
||||||
|
@ -985,9 +992,10 @@ en:
|
||||||
text_body_template: |
|
text_body_template: |
|
||||||
%{username} posted in '%{topic_title}' on %{site_name}:
|
%{username} posted in '%{topic_title}' on %{site_name}:
|
||||||
|
|
||||||
---
|
|
||||||
%{message}
|
%{message}
|
||||||
|
|
||||||
|
%{context}
|
||||||
|
|
||||||
---
|
---
|
||||||
%{respond_instructions}
|
%{respond_instructions}
|
||||||
|
|
||||||
|
@ -1006,7 +1014,6 @@ en:
|
||||||
text_body_template: |
|
text_body_template: |
|
||||||
%{private_message_from} just sent you a private message
|
%{private_message_from} just sent you a private message
|
||||||
|
|
||||||
---
|
|
||||||
%{message}
|
%{message}
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
|
@ -37,6 +37,9 @@ module Email
|
||||||
body renderer.html
|
body renderer.html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@message.parts[0].body = @message.parts[0].body.to_s.gsub!(/\[\/?email-indent\]/, '')
|
||||||
|
|
||||||
|
|
||||||
@message.text_part.content_type = 'text/plain; charset=UTF-8'
|
@message.text_part.content_type = 'text/plain; charset=UTF-8'
|
||||||
|
|
||||||
# Set up the email log
|
# Set up the email log
|
||||||
|
|
|
@ -22,11 +22,16 @@ module Email
|
||||||
if img['src'][0] == "/"
|
if img['src'][0] == "/"
|
||||||
img['src'] = "#{Discourse.base_url}#{img['src']}"
|
img['src'] = "#{Discourse.base_url}#{img['src']}"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@fragment.css('div.post-indent').each do |div|
|
||||||
|
div['style'] = 'margin-left: 15px; margin-top: 20px; max-width: 694px;'
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_html
|
def format_html
|
||||||
|
|
||||||
@fragment.css('h3').each do |h3|
|
@fragment.css('h3').each do |h3|
|
||||||
h3['style'] = 'margin: 15px 0 20px 0; border-bottom: 1px solid #ddd;'
|
h3['style'] = 'margin: 15px 0 20px 0; border-bottom: 1px solid #ddd;'
|
||||||
end
|
end
|
||||||
|
@ -55,10 +60,14 @@ module Email
|
||||||
@fragment.css('div.digest-post').each do |div|
|
@fragment.css('div.digest-post').each do |div|
|
||||||
div['style'] = 'margin-left: 15px; margin-top: 20px; max-width: 694px;'
|
div['style'] = 'margin-left: 15px; margin-top: 20px; max-width: 694px;'
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_html
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -91,7 +91,11 @@ describe UserNotifications do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "has a message" do
|
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
|
end
|
||||||
|
|
||||||
it "has an unsubscribe link" do
|
it "has an unsubscribe link" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue