2013-02-05 13:16:51 -06:00
|
|
|
require_dependency 'markdown_linker'
|
2013-06-10 15:46:08 -05:00
|
|
|
require_dependency 'email/message_builder'
|
2013-07-22 14:06:37 -05:00
|
|
|
require_dependency 'age_words'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
class UserNotifications < ActionMailer::Base
|
2015-01-28 13:56:18 -06:00
|
|
|
helper :application
|
2013-04-26 01:56:28 -05:00
|
|
|
default charset: 'UTF-8'
|
|
|
|
|
2013-06-10 15:46:08 -05:00
|
|
|
include Email::BuildEmailHelper
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
def signup(user, opts={})
|
2013-06-10 15:46:08 -05:00
|
|
|
build_email(user.email,
|
2013-06-12 15:35:46 -05:00
|
|
|
template: "user_notifications.signup",
|
|
|
|
email_token: opts[:email_token])
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-06-05 22:16:31 -05:00
|
|
|
def signup_after_approval(user, opts={})
|
2013-06-10 15:46:08 -05:00
|
|
|
build_email(user.email,
|
2013-06-12 15:35:46 -05:00
|
|
|
template: 'user_notifications.signup_after_approval',
|
|
|
|
email_token: opts[:email_token],
|
2015-11-23 15:45:05 -06:00
|
|
|
new_user_tips: I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url))
|
2013-06-05 22:16:31 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def authorize_email(user, opts={})
|
2013-06-10 15:46:08 -05:00
|
|
|
build_email(user.email, template: "user_notifications.authorize_email", email_token: opts[:email_token])
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password(user, opts={})
|
2014-01-21 11:42:20 -06:00
|
|
|
build_email( user.email,
|
|
|
|
template: user.has_password? ? "user_notifications.forgot_password" : "user_notifications.set_password",
|
|
|
|
email_token: opts[:email_token])
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2015-04-27 05:59:48 -05:00
|
|
|
|
|
|
|
def admin_login(user, opts={})
|
|
|
|
build_email( user.email,
|
|
|
|
template: "user_notifications.admin_login",
|
|
|
|
email_token: opts[:email_token])
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-09-11 11:47:17 -05:00
|
|
|
def account_created(user, opts={})
|
|
|
|
build_email( user.email, template: "user_notifications.account_created", email_token: opts[:email_token])
|
|
|
|
end
|
|
|
|
|
2015-05-26 10:59:32 -05:00
|
|
|
def short_date(dt)
|
2015-11-13 14:45:52 -06:00
|
|
|
if dt.year == Time.now.year
|
|
|
|
I18n.l(dt, format: :short_no_year)
|
2015-10-28 11:45:07 -05:00
|
|
|
else
|
2015-11-13 14:45:52 -06:00
|
|
|
I18n.l(dt, format: :short)
|
2015-10-28 11:45:07 -05:00
|
|
|
end
|
2015-05-26 10:59:32 -05:00
|
|
|
end
|
2014-04-17 15:42:40 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def digest(user, opts={})
|
|
|
|
@user = user
|
|
|
|
@base_url = Discourse.base_url
|
|
|
|
|
2013-06-03 15:12:24 -05:00
|
|
|
min_date = opts[:since] || @user.last_emailed_at || @user.last_seen_at || 1.month.ago
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-06-03 11:49:51 -05:00
|
|
|
@site_name = SiteSetting.email_prefix.presence || SiteSetting.title
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-07-17 11:31:37 -05:00
|
|
|
@header_color = ColorScheme.hex_for_name('header_background')
|
2015-05-26 10:59:32 -05:00
|
|
|
@last_seen_at = short_date(@user.last_seen_at || @user.created_at)
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-06-03 15:12:24 -05:00
|
|
|
# A list of topics to show the user
|
2014-04-21 13:44:00 -05:00
|
|
|
@featured_topics = Topic.for_digest(user, min_date, limit: SiteSetting.digest_topics, top_order: true).to_a
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
# Don't send email unless there is content in it
|
2013-08-09 13:43:02 -05:00
|
|
|
if @featured_topics.present?
|
2014-04-17 15:42:40 -05:00
|
|
|
featured_topic_ids = @featured_topics.map(&:id)
|
2014-04-17 15:04:26 -05:00
|
|
|
|
2014-04-17 15:42:40 -05:00
|
|
|
@new_topics_since_seen = Topic.new_since_last_seen(user, min_date, featured_topic_ids).count
|
2014-11-07 16:16:52 -06:00
|
|
|
if @new_topics_since_seen > SiteSetting.digest_topics
|
2014-04-17 15:42:40 -05:00
|
|
|
category_counts = Topic.new_since_last_seen(user, min_date, featured_topic_ids).group(:category_id).count
|
2013-11-29 12:00:10 -06:00
|
|
|
|
2014-04-17 15:42:40 -05:00
|
|
|
@new_by_category = []
|
|
|
|
if category_counts.present?
|
|
|
|
Category.where(id: category_counts.keys).each do |c|
|
|
|
|
@new_by_category << [c, category_counts[c.id]]
|
|
|
|
end
|
|
|
|
@new_by_category.sort_by! {|c| -c[1]}
|
|
|
|
end
|
|
|
|
end
|
2014-04-17 15:04:26 -05:00
|
|
|
|
2014-04-17 15:42:40 -05:00
|
|
|
@featured_topics, @new_topics = @featured_topics[0..4], @featured_topics[5..-1]
|
|
|
|
@markdown_linker = MarkdownLinker.new(Discourse.base_url)
|
2015-02-13 13:15:49 -06:00
|
|
|
@unsubscribe_key = DigestUnsubscribeKey.create_key_for(@user)
|
2014-04-17 15:04:26 -05:00
|
|
|
|
2013-06-10 15:46:08 -05:00
|
|
|
build_email user.email,
|
2013-06-12 15:35:46 -05:00
|
|
|
from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title),
|
|
|
|
subject: I18n.t('user_notifications.digest.subject_template',
|
2015-05-26 10:59:32 -05:00
|
|
|
site_name: @site_name,
|
|
|
|
date: short_date(Time.now))
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
def user_replied(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 00:16:55 -05:00
|
|
|
opts[:use_site_subject] = true
|
2014-10-03 06:44:08 -05:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 15:35:46 -05:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_quoted(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 00:16:55 -05:00
|
|
|
opts[:use_site_subject] = true
|
2014-10-03 06:44:08 -05:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 15:35:46 -05:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_mentioned(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 00:16:55 -05:00
|
|
|
opts[:use_site_subject] = true
|
2014-10-03 06:44:08 -05:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 15:35:46 -05:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2015-11-30 19:12:55 -06:00
|
|
|
def group_mentioned(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
|
|
|
opts[:use_site_subject] = true
|
|
|
|
opts[:show_category_in_subject] = true
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
def user_posted(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 00:16:55 -05:00
|
|
|
opts[:use_site_subject] = true
|
|
|
|
opts[:add_re_to_subject] = true
|
2014-10-03 06:44:08 -05:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 15:35:46 -05:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2013-08-07 12:02:49 -05:00
|
|
|
def user_private_message(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 00:16:55 -05:00
|
|
|
opts[:use_site_subject] = true
|
|
|
|
opts[:add_re_to_subject] = true
|
2014-10-03 06:44:08 -05:00
|
|
|
opts[:show_category_in_subject] = false
|
2013-08-07 12:02:49 -05:00
|
|
|
|
|
|
|
# We use the 'user_posted' event when you are emailed a post in a PM.
|
2014-06-16 01:36:02 -05:00
|
|
|
opts[:notification_type] = 'posted'
|
2013-08-07 12:02:49 -05:00
|
|
|
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2015-04-07 01:21:38 -05:00
|
|
|
def user_invited_to_private_message(user, opts)
|
|
|
|
opts[:use_template_html] = true
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2015-03-30 13:36:47 -05:00
|
|
|
def user_invited_to_topic(user, opts)
|
2015-04-07 01:21:38 -05:00
|
|
|
opts[:use_template_html] = true
|
2015-03-30 13:36:47 -05:00
|
|
|
opts[:show_category_in_subject] = true
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2014-02-06 18:06:35 -06:00
|
|
|
def mailing_list_notify(user, post)
|
|
|
|
send_notification_email(
|
|
|
|
title: post.topic.title,
|
|
|
|
post: post,
|
2014-10-21 09:06:55 -05:00
|
|
|
username: post.user.username,
|
2015-12-14 13:53:58 -06:00
|
|
|
from_alias: (SiteSetting.enable_names &&
|
|
|
|
SiteSetting.display_name_on_email_from &&
|
|
|
|
post.user.name.present?) ? post.user.name : post.user.username,
|
2014-02-06 18:06:35 -06:00
|
|
|
allow_reply_by_email: true,
|
2014-09-29 00:16:55 -05:00
|
|
|
use_site_subject: true,
|
|
|
|
add_re_to_subject: true,
|
2014-10-03 06:44:08 -05:00
|
|
|
show_category_in_subject: true,
|
2014-02-06 18:06:35 -06:00
|
|
|
notification_type: "posted",
|
|
|
|
user: user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
protected
|
|
|
|
|
2013-07-22 14:06:37 -05:00
|
|
|
def email_post_markdown(post)
|
|
|
|
result = "[email-indent]\n"
|
|
|
|
result << "#{post.raw}\n\n"
|
2013-07-24 02:13:15 -05:00
|
|
|
result << "#{I18n.t('user_notifications.posted_by', username: post.username, post_date: post.created_at.strftime("%m/%d/%Y"))}\n\n"
|
2013-07-22 14:06:37 -05:00
|
|
|
result << "[/email-indent]\n"
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-07-24 02:13:15 -05:00
|
|
|
class UserNotificationRenderer < ActionView::Base
|
|
|
|
include UserNotificationsHelper
|
|
|
|
end
|
|
|
|
|
2014-01-30 23:37:40 -06:00
|
|
|
def self.get_context_posts(post, topic_user)
|
|
|
|
|
|
|
|
context_posts = Post.where(topic_id: post.topic_id)
|
|
|
|
.where("post_number < ?", post.post_number)
|
|
|
|
.where(user_deleted: false)
|
|
|
|
.where(hidden: false)
|
2015-09-30 11:39:56 -05:00
|
|
|
.where(post_type: Topic.visible_post_types)
|
2014-01-30 23:37:40 -06:00
|
|
|
.order('created_at desc')
|
|
|
|
.limit(SiteSetting.email_posts_context)
|
|
|
|
|
|
|
|
if topic_user && topic_user.last_emailed_post_number
|
|
|
|
context_posts = context_posts.where("post_number > ?", topic_user.last_emailed_post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
context_posts
|
|
|
|
end
|
|
|
|
|
2013-06-12 15:35:46 -05:00
|
|
|
def notification_email(user, opts)
|
2016-01-26 19:19:49 -06:00
|
|
|
return unless notification_type = opts[:notification_type]
|
|
|
|
return unless notification_data = opts[:notification_data_hash]
|
|
|
|
return unless post = opts[:post]
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-01-26 19:19:49 -06:00
|
|
|
unless String === notification_type
|
|
|
|
if Numeric === notification_type
|
|
|
|
notification_type = Notification.types[notification_type]
|
|
|
|
end
|
|
|
|
notification_type = notification_type.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
user_name = notification_data[:original_username]
|
|
|
|
|
|
|
|
if post && SiteSetting.enable_names && SiteSetting.display_name_on_email_from
|
|
|
|
name = User.where(id: post.user_id).pluck(:name).first
|
2015-05-05 21:55:33 -05:00
|
|
|
user_name = name unless name.blank?
|
2014-10-21 09:06:55 -05:00
|
|
|
end
|
|
|
|
|
2013-02-27 17:30:14 -06:00
|
|
|
|
2016-01-26 19:19:49 -06:00
|
|
|
return if user.mailing_list_mode && !post.topic.private_message? &&
|
2015-11-30 00:03:47 -06:00
|
|
|
["replied", "mentioned", "quoted", "posted", "group_mentioned"].include?(notification_type)
|
2014-02-06 18:06:35 -06:00
|
|
|
|
2016-01-26 19:19:49 -06:00
|
|
|
title = notification_data[:topic_title]
|
2014-05-06 14:01:19 -05:00
|
|
|
allow_reply_by_email = opts[:allow_reply_by_email] unless user.suspended?
|
2014-09-29 00:16:55 -05:00
|
|
|
use_site_subject = opts[:use_site_subject]
|
|
|
|
add_re_to_subject = opts[:add_re_to_subject]
|
2014-10-03 06:44:08 -05:00
|
|
|
show_category_in_subject = opts[:show_category_in_subject]
|
2015-04-07 01:21:38 -05:00
|
|
|
use_template_html = opts[:use_template_html]
|
2016-01-26 19:19:49 -06:00
|
|
|
original_username = notification_data[:original_username] || notification_data[:display_username]
|
2014-02-06 18:06:35 -06:00
|
|
|
|
|
|
|
send_notification_email(
|
|
|
|
title: title,
|
2016-01-26 19:19:49 -06:00
|
|
|
post: post,
|
2015-03-30 13:36:47 -05:00
|
|
|
username: original_username,
|
2014-10-21 09:06:55 -05:00
|
|
|
from_alias: user_name,
|
2014-02-06 18:06:35 -06:00
|
|
|
allow_reply_by_email: allow_reply_by_email,
|
2014-09-29 00:16:55 -05:00
|
|
|
use_site_subject: use_site_subject,
|
|
|
|
add_re_to_subject: add_re_to_subject,
|
2014-10-03 06:44:08 -05:00
|
|
|
show_category_in_subject: show_category_in_subject,
|
2014-02-06 18:06:35 -06:00
|
|
|
notification_type: notification_type,
|
2015-04-07 01:21:38 -05:00
|
|
|
use_template_html: use_template_html,
|
2014-02-06 18:06:35 -06:00
|
|
|
user: user
|
|
|
|
)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_notification_email(opts)
|
|
|
|
post = opts[:post]
|
|
|
|
title = opts[:title]
|
|
|
|
allow_reply_by_email = opts[:allow_reply_by_email]
|
2014-09-29 00:16:55 -05:00
|
|
|
use_site_subject = opts[:use_site_subject]
|
|
|
|
add_re_to_subject = opts[:add_re_to_subject] && post.post_number > 1
|
2014-10-21 09:06:55 -05:00
|
|
|
username = opts[:username]
|
2014-02-06 18:06:35 -06:00
|
|
|
from_alias = opts[:from_alias]
|
|
|
|
notification_type = opts[:notification_type]
|
|
|
|
user = opts[:user]
|
|
|
|
|
2014-10-03 06:44:08 -05:00
|
|
|
# category name
|
|
|
|
category = Topic.find_by(id: post.topic_id).category
|
2015-06-01 00:43:16 -05:00
|
|
|
if opts[:show_category_in_subject] && post.topic_id && category && !category.uncategorized?
|
2014-10-03 06:44:08 -05:00
|
|
|
show_category_in_subject = category.name
|
|
|
|
|
|
|
|
# subcategory case
|
|
|
|
if !category.parent_category_id.nil?
|
|
|
|
show_category_in_subject = "#{Category.find_by(id: category.parent_category_id).name}/#{show_category_in_subject}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
show_category_in_subject = nil
|
|
|
|
end
|
|
|
|
|
2013-07-22 14:06:37 -05:00
|
|
|
context = ""
|
2014-02-06 18:06:35 -06:00
|
|
|
tu = TopicUser.get(post.topic_id, user)
|
|
|
|
context_posts = self.class.get_context_posts(post, tu)
|
2013-12-29 20:02:12 -06:00
|
|
|
|
|
|
|
# make .present? cheaper
|
|
|
|
context_posts = context_posts.to_a
|
|
|
|
|
2013-07-22 14:06:37 -05:00
|
|
|
if context_posts.present?
|
2013-07-24 02:13:15 -05:00
|
|
|
context << "---\n*#{I18n.t('user_notifications.previous_discussion')}*\n"
|
2013-07-22 14:06:37 -05:00
|
|
|
context_posts.each do |cp|
|
|
|
|
context << email_post_markdown(cp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-07 01:21:38 -05:00
|
|
|
topic_excerpt = ""
|
|
|
|
if opts[:use_template_html]
|
|
|
|
topic_excerpt = post.excerpt.gsub("\n", " ") if post.is_first_post? && post.excerpt
|
|
|
|
else
|
|
|
|
html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
|
|
|
|
template: 'email/notification',
|
|
|
|
format: :html,
|
|
|
|
locals: { context_posts: context_posts,
|
|
|
|
post: post,
|
|
|
|
classes: RTL.new(user).css_class
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2013-07-24 02:13:15 -05:00
|
|
|
|
2014-01-21 00:24:05 -06:00
|
|
|
template = "user_notifications.user_#{notification_type}"
|
2015-12-09 12:45:46 -06:00
|
|
|
if post.topic.private_message?
|
|
|
|
template << "_pm"
|
|
|
|
template << "_staged" if user.staged?
|
|
|
|
end
|
2013-07-24 02:13:15 -05:00
|
|
|
|
2013-02-27 17:30:14 -06:00
|
|
|
email_opts = {
|
2014-02-06 18:06:35 -06:00
|
|
|
topic_title: title,
|
2015-04-07 01:21:38 -05:00
|
|
|
topic_excerpt: topic_excerpt,
|
2014-02-06 18:06:35 -06:00
|
|
|
message: email_post_markdown(post),
|
|
|
|
url: post.url,
|
|
|
|
post_id: post.id,
|
|
|
|
topic_id: post.topic_id,
|
2013-07-22 14:06:37 -05:00
|
|
|
context: context,
|
2014-10-21 09:06:55 -05:00
|
|
|
username: username,
|
2015-11-24 09:58:26 -06:00
|
|
|
add_unsubscribe_link: !user.staged,
|
2016-01-20 03:25:25 -06:00
|
|
|
add_unsubscribe_via_email_link: user.mailing_list_mode,
|
2015-08-12 16:00:16 -05:00
|
|
|
unsubscribe_url: post.topic.unsubscribe_url,
|
2014-02-06 18:06:35 -06:00
|
|
|
allow_reply_by_email: allow_reply_by_email,
|
2014-09-29 00:16:55 -05:00
|
|
|
use_site_subject: use_site_subject,
|
|
|
|
add_re_to_subject: add_re_to_subject,
|
2014-10-03 06:44:08 -05:00
|
|
|
show_category_in_subject: show_category_in_subject,
|
2014-06-06 08:09:00 -05:00
|
|
|
private_reply: post.topic.private_message?,
|
2014-05-06 14:01:19 -05:00
|
|
|
include_respond_instructions: !user.suspended?,
|
2014-01-21 00:24:05 -06:00
|
|
|
template: template,
|
2013-07-26 02:27:46 -05:00
|
|
|
html_override: html,
|
2015-04-07 01:21:38 -05:00
|
|
|
site_description: SiteSetting.site_description,
|
|
|
|
site_title: SiteSetting.title,
|
2014-01-21 00:24:05 -06:00
|
|
|
style: :notification
|
2013-02-27 17:30:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# If we have a display name, change the from address
|
2015-08-12 16:00:16 -05:00
|
|
|
email_opts[:from_alias] = from_alias if from_alias.present?
|
2013-02-27 17:30:14 -06:00
|
|
|
|
2014-02-06 18:06:35 -06:00
|
|
|
TopicUser.change(user.id, post.topic_id, last_emailed_post_number: post.post_number)
|
2013-12-29 20:02:12 -06:00
|
|
|
|
2013-06-10 15:46:08 -05:00
|
|
|
build_email(user.email, email_opts)
|
2013-02-07 09:45:24 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|