2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'markdown_linker'
|
2013-06-10 16:46:08 -04:00
|
|
|
require_dependency 'email/message_builder'
|
2013-07-22 15:06:37 -04:00
|
|
|
require_dependency 'age_words'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class UserNotifications < ActionMailer::Base
|
2017-01-09 13:27:27 -05:00
|
|
|
include UserNotificationsHelper
|
2017-04-10 13:15:25 -04:00
|
|
|
include ApplicationHelper
|
2017-04-24 15:26:06 -04:00
|
|
|
helper :application, :email
|
2013-04-26 02:56:28 -04:00
|
|
|
default charset: 'UTF-8'
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
include Email::BuildEmailHelper
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def signup(user, opts={})
|
2013-06-10 16:46:08 -04:00
|
|
|
build_email(user.email,
|
2013-06-12 16:35:46 -04:00
|
|
|
template: "user_notifications.signup",
|
2016-02-09 18:54:13 -05:00
|
|
|
locale: user_locale(user),
|
2013-06-12 16:35:46 -04:00
|
|
|
email_token: opts[:email_token])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-05 23:16:31 -04:00
|
|
|
def signup_after_approval(user, opts={})
|
2013-06-10 16:46:08 -04:00
|
|
|
build_email(user.email,
|
2013-06-12 16:35:46 -04:00
|
|
|
template: 'user_notifications.signup_after_approval',
|
2016-02-09 18:54:13 -05:00
|
|
|
locale: user_locale(user),
|
2013-06-12 16:35:46 -04:00
|
|
|
email_token: opts[:email_token],
|
2016-02-09 18:54:13 -05:00
|
|
|
new_user_tips: I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url, locale: locale))
|
2013-06-05 23:16:31 -04:00
|
|
|
end
|
|
|
|
|
2016-03-07 14:40:11 -05:00
|
|
|
def notify_old_email(user, opts={})
|
2016-02-03 13:27:58 -05:00
|
|
|
build_email(user.email,
|
2016-03-07 14:40:11 -05:00
|
|
|
template: "user_notifications.notify_old_email",
|
|
|
|
locale: user_locale(user),
|
|
|
|
new_email: opts[:new_email])
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_old_email(user, opts={})
|
|
|
|
build_email(user.email,
|
|
|
|
template: "user_notifications.confirm_old_email",
|
|
|
|
locale: user_locale(user),
|
|
|
|
email_token: opts[:email_token])
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_new_email(user, opts={})
|
|
|
|
build_email(user.email,
|
|
|
|
template: "user_notifications.confirm_new_email",
|
2016-02-09 18:54:13 -05:00
|
|
|
locale: user_locale(user),
|
2016-02-03 13:27:58 -05:00
|
|
|
email_token: opts[:email_token])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password(user, opts={})
|
2016-02-03 13:27:58 -05:00
|
|
|
build_email(user.email,
|
|
|
|
template: user.has_password? ? "user_notifications.forgot_password" : "user_notifications.set_password",
|
2016-02-09 18:54:13 -05:00
|
|
|
locale: user_locale(user),
|
2016-02-03 13:27:58 -05:00
|
|
|
email_token: opts[:email_token])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2015-04-27 06:59:48 -04:00
|
|
|
|
|
|
|
def admin_login(user, opts={})
|
2016-02-03 13:27:58 -05:00
|
|
|
build_email(user.email,
|
|
|
|
template: "user_notifications.admin_login",
|
2016-02-09 18:54:13 -05:00
|
|
|
locale: user_locale(user),
|
2016-02-03 13:27:58 -05:00
|
|
|
email_token: opts[:email_token])
|
2015-04-27 06:59:48 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-09-11 12:47:17 -04:00
|
|
|
def account_created(user, opts={})
|
2016-02-03 13:27:58 -05:00
|
|
|
build_email(user.email,
|
|
|
|
template: "user_notifications.account_created",
|
2016-02-09 18:54:13 -05:00
|
|
|
locale: user_locale(user),
|
2016-02-03 13:27:58 -05:00
|
|
|
email_token: opts[:email_token])
|
2014-09-11 12:47:17 -04:00
|
|
|
end
|
|
|
|
|
2015-05-26 11:59:32 -04:00
|
|
|
def short_date(dt)
|
2015-11-13 15:45:52 -05:00
|
|
|
if dt.year == Time.now.year
|
2016-01-28 13:06:36 -05:00
|
|
|
I18n.l(dt, format: :short_no_year)
|
2015-10-28 12:45:07 -04:00
|
|
|
else
|
2016-01-28 13:06:36 -05:00
|
|
|
I18n.l(dt, format: :date_only)
|
2015-10-28 12:45:07 -04:00
|
|
|
end
|
2015-05-26 11:59:32 -04:00
|
|
|
end
|
2014-04-17 16:42:40 -04:00
|
|
|
|
2017-02-08 12:13:29 -05:00
|
|
|
# This is the "mailing list summary email"
|
2016-05-21 09:17:54 -04:00
|
|
|
def mailing_list(user, opts={})
|
|
|
|
@since = opts[:since] || 1.day.ago
|
|
|
|
@since_formatted = short_date(@since)
|
|
|
|
|
2017-05-03 02:20:49 -04:00
|
|
|
topics = Topic
|
|
|
|
.joins(:posts)
|
|
|
|
.includes(:posts)
|
|
|
|
.for_digest(user, 100.years.ago)
|
|
|
|
.where("posts.created_at > ?", @since)
|
|
|
|
|
|
|
|
unless user.staff?
|
|
|
|
topics = topics.where("posts.post_type <> ?", Post.types[:whisper])
|
|
|
|
end
|
|
|
|
|
|
|
|
@new_topics = topics.where("topics.created_at > ?", @since).uniq
|
|
|
|
@existing_topics = topics.where("topics.created_at <= ?", @since).uniq
|
|
|
|
@topics = topics.uniq
|
|
|
|
|
|
|
|
return if @topics.empty?
|
2016-05-21 09:17:54 -04:00
|
|
|
|
|
|
|
build_summary_for(user)
|
2016-08-01 14:19:00 -04:00
|
|
|
opts = {
|
2016-06-21 11:12:30 -04:00
|
|
|
from_alias: I18n.t('user_notifications.mailing_list.from', site_name: SiteSetting.title),
|
2017-03-21 09:11:15 -04:00
|
|
|
subject: I18n.t('user_notifications.mailing_list.subject_template', email_prefix: @email_prefix, date: @date),
|
2016-08-01 14:19:00 -04:00
|
|
|
mailing_list_mode: true,
|
|
|
|
add_unsubscribe_link: true,
|
|
|
|
unsubscribe_url: "#{Discourse.base_url}/email/unsubscribe/#{@unsubscribe_key}",
|
|
|
|
}
|
2017-05-03 02:20:49 -04:00
|
|
|
|
2016-08-01 14:19:00 -04:00
|
|
|
apply_notification_styles(build_email(@user.email, opts))
|
2016-05-21 09:17:54 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-05-21 09:17:54 -04:00
|
|
|
def digest(user, opts={})
|
|
|
|
build_summary_for(user)
|
2016-07-15 05:39:29 -04:00
|
|
|
min_date = opts[:since] || user.last_emailed_at || user.last_seen_at || 1.month.ago
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-12-30 15:18:05 -05:00
|
|
|
# Fetch some topics and posts to show
|
2016-12-19 14:53:53 -05:00
|
|
|
digest_opts = {limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics, top_order: true}
|
|
|
|
topics_for_digest = Topic.for_digest(user, min_date, digest_opts).to_a
|
|
|
|
if topics_for_digest.empty? && !user.user_option.try(:include_tl0_in_digests)
|
2016-12-30 15:18:05 -05:00
|
|
|
# Find some topics from new users that are at least 24 hours old
|
2016-12-19 14:53:53 -05:00
|
|
|
topics_for_digest = Topic.for_digest(user, min_date, digest_opts.merge(include_tl0: true)).where('topics.created_at < ?', 24.hours.ago).to_a
|
|
|
|
end
|
2014-04-17 16:04:26 -04:00
|
|
|
|
2016-11-10 18:16:24 -05:00
|
|
|
@popular_topics = topics_for_digest[0,SiteSetting.digest_topics]
|
2016-11-22 13:23:21 -05:00
|
|
|
|
2016-11-10 18:16:24 -05:00
|
|
|
if @popular_topics.present?
|
2017-01-09 13:27:27 -05:00
|
|
|
@other_new_for_you = topics_for_digest.size > SiteSetting.digest_topics ? topics_for_digest[SiteSetting.digest_topics..-1] : []
|
|
|
|
|
|
|
|
@popular_posts = if SiteSetting.digest_posts > 0
|
|
|
|
Post.order("posts.score DESC")
|
|
|
|
.for_mailing_list(user, min_date)
|
|
|
|
.where('posts.post_type = ?', Post.types[:regular])
|
|
|
|
.where('posts.deleted_at IS NULL AND posts.hidden = false AND posts.user_deleted = false')
|
2017-04-12 14:39:59 -04:00
|
|
|
.where("posts.post_number > ? AND posts.score > ?", 1, ScoreCalculator.default_score_weights[:like_score] * 5.0)
|
2017-01-09 13:27:27 -05:00
|
|
|
.limit(SiteSetting.digest_posts)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
@excerpts = {}
|
|
|
|
|
|
|
|
@popular_topics.map do |t|
|
|
|
|
@excerpts[t.first_post.id] = email_excerpt(t.first_post.cooked) if t.first_post.present?
|
|
|
|
end
|
|
|
|
|
2016-12-30 15:18:05 -05:00
|
|
|
# Try to find 3 interesting stats for the top of the digest
|
|
|
|
|
|
|
|
new_topics_count = Topic.new_since_last_seen(user, min_date).count
|
|
|
|
if new_topics_count == 0
|
|
|
|
# We used topics from new users instead, so count should match
|
|
|
|
new_topics_count = topics_for_digest.size
|
|
|
|
end
|
|
|
|
@counts = [{label_key: 'user_notifications.digest.new_topics',
|
|
|
|
value: new_topics_count,
|
|
|
|
href: "#{Discourse.base_url}/new"}]
|
|
|
|
|
|
|
|
value = user.unread_notifications
|
|
|
|
@counts << {label_key: 'user_notifications.digest.unread_notifications', value: value, href: "#{Discourse.base_url}/my/notifications"} if value > 0
|
|
|
|
|
|
|
|
value = user.unread_private_messages
|
|
|
|
@counts << {label_key: 'user_notifications.digest.unread_messages', value: value, href: "#{Discourse.base_url}/my/messages"} if value > 0
|
|
|
|
|
|
|
|
if @counts.size < 3
|
|
|
|
value = user.unread_notifications_of_type(Notification.types[:liked])
|
|
|
|
@counts << {label_key: 'user_notifications.digest.liked_received', value: value, href: "#{Discourse.base_url}/my/notifications"} if value > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if @counts.size < 3
|
|
|
|
value = Post.for_mailing_list(user, min_date).where("posts.post_number > ?", 1).count
|
|
|
|
@counts << { label_key: 'user_notifications.digest.new_posts', value: value, href: "#{Discourse.base_url}/new" } if value > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
if @counts.size < 3
|
|
|
|
value = User.real.where(active: true, staged: false).not_suspended.where("created_at > ?", min_date).count
|
|
|
|
@counts << { label_key: 'user_notifications.digest.new_users', value: value, href: "#{Discourse.base_url}/about" } if value > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
@last_seen_at = short_date(user.last_seen_at || user.created_at)
|
|
|
|
|
|
|
|
@preheader_text = I18n.t('user_notifications.digest.preheader', last_seen_at: @last_seen_at)
|
|
|
|
|
2016-07-15 05:39:29 -04:00
|
|
|
opts = {
|
|
|
|
from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title),
|
2017-03-21 09:11:15 -04:00
|
|
|
subject: I18n.t('user_notifications.digest.subject_template', email_prefix: @email_prefix, date: short_date(Time.now)),
|
2016-07-15 05:39:29 -04:00
|
|
|
add_unsubscribe_link: true,
|
|
|
|
unsubscribe_url: "#{Discourse.base_url}/email/unsubscribe/#{@unsubscribe_key}",
|
|
|
|
}
|
|
|
|
|
|
|
|
build_email(user.email, opts)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2016-11-10 18:16:24 -05:00
|
|
|
|
2013-06-12 16:35:46 -04:00
|
|
|
def user_replied(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 01:16:55 -04:00
|
|
|
opts[:use_site_subject] = true
|
2014-10-03 07:44:08 -04:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 16:35:46 -04:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_quoted(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 01:16:55 -04:00
|
|
|
opts[:use_site_subject] = true
|
2014-10-03 07:44:08 -04:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 16:35:46 -04:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2016-02-16 12:29:23 -05:00
|
|
|
def user_linked(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 16:35:46 -04:00
|
|
|
def user_mentioned(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 01:16:55 -04:00
|
|
|
opts[:use_site_subject] = true
|
2014-10-03 07:44:08 -04:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 16:35:46 -04:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:12:55 -05: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 16:35:46 -04:00
|
|
|
def user_posted(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 01:16:55 -04:00
|
|
|
opts[:use_site_subject] = true
|
|
|
|
opts[:add_re_to_subject] = true
|
2014-10-03 07:44:08 -04:00
|
|
|
opts[:show_category_in_subject] = true
|
2013-06-12 16:35:46 -04:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2013-08-07 13:02:49 -04:00
|
|
|
def user_private_message(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
2014-09-29 01:16:55 -04:00
|
|
|
opts[:use_site_subject] = true
|
|
|
|
opts[:add_re_to_subject] = true
|
2014-10-03 07:44:08 -04:00
|
|
|
opts[:show_category_in_subject] = false
|
2013-08-07 13:02:49 -04:00
|
|
|
|
|
|
|
# We use the 'user_posted' event when you are emailed a post in a PM.
|
2014-06-16 02:36:02 -04:00
|
|
|
opts[:notification_type] = 'posted'
|
2013-08-07 13:02:49 -04:00
|
|
|
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2015-04-07 02:21:38 -04:00
|
|
|
def user_invited_to_private_message(user, opts)
|
2016-03-25 08:26:52 -04:00
|
|
|
opts[:allow_reply_by_email] = false
|
|
|
|
opts[:use_invite_template] = true
|
2015-04-07 02:21:38 -04:00
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2015-03-30 14:36:47 -04:00
|
|
|
def user_invited_to_topic(user, opts)
|
2016-03-25 08:26:52 -04:00
|
|
|
opts[:allow_reply_by_email] = false
|
|
|
|
opts[:use_invite_template] = true
|
2015-03-30 14:36:47 -04:00
|
|
|
opts[:show_category_in_subject] = true
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2016-07-07 12:23:19 -04:00
|
|
|
def user_watching_first_post(user, opts)
|
|
|
|
user_posted(user, opts)
|
|
|
|
end
|
|
|
|
|
2014-02-06 19:06:35 -05:00
|
|
|
def mailing_list_notify(user, post)
|
2016-02-03 13:27:58 -05:00
|
|
|
opts = {
|
2014-02-06 19:06:35 -05:00
|
|
|
post: post,
|
|
|
|
allow_reply_by_email: true,
|
2014-09-29 01:16:55 -04:00
|
|
|
use_site_subject: true,
|
|
|
|
add_re_to_subject: true,
|
2014-10-03 07:44:08 -04:00
|
|
|
show_category_in_subject: true,
|
2014-02-06 19:06:35 -05:00
|
|
|
notification_type: "posted",
|
2016-02-03 13:27:58 -05:00
|
|
|
notification_data_hash: {
|
|
|
|
original_username: post.user.username,
|
|
|
|
topic_title: post.topic.title,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
notification_email(user, opts)
|
2014-02-06 19:06:35 -05:00
|
|
|
end
|
|
|
|
|
2013-06-12 16:35:46 -04:00
|
|
|
protected
|
|
|
|
|
2016-02-09 18:54:13 -05:00
|
|
|
def user_locale(user)
|
2016-03-04 16:21:30 -05:00
|
|
|
(user.locale.present? && I18n.available_locales.include?(user.locale.to_sym)) ? user.locale : nil
|
2016-02-09 18:54:13 -05:00
|
|
|
end
|
|
|
|
|
2016-04-11 13:06:10 -04:00
|
|
|
def email_post_markdown(post, add_posted_by=false)
|
2013-07-22 15:06:37 -04:00
|
|
|
result = "[email-indent]\n"
|
|
|
|
result << "#{post.raw}\n\n"
|
2016-04-11 13:06:10 -04:00
|
|
|
if add_posted_by
|
|
|
|
result << "#{I18n.t('user_notifications.posted_by', username: post.username, post_date: post.created_at.strftime("%m/%d/%Y"))}\n\n"
|
|
|
|
end
|
2013-07-22 15:06:37 -04:00
|
|
|
result << "[/email-indent]\n"
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-07-24 03:13:15 -04:00
|
|
|
class UserNotificationRenderer < ActionView::Base
|
|
|
|
include UserNotificationsHelper
|
2017-04-24 15:26:06 -04:00
|
|
|
include EmailHelper
|
2013-07-24 03:13:15 -04:00
|
|
|
end
|
|
|
|
|
2016-02-25 08:05:40 -05:00
|
|
|
def self.get_context_posts(post, topic_user, user)
|
2017-04-24 15:26:06 -04:00
|
|
|
if (user.user_option.email_previous_replies == UserOption.previous_replies_type[:never]) ||
|
|
|
|
SiteSetting.private_email?
|
2016-02-18 21:56:52 -05:00
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
2016-02-12 06:10:30 -05:00
|
|
|
allowed_post_types = [Post.types[:regular]]
|
|
|
|
allowed_post_types << Post.types[:whisper] if topic_user.try(:user).try(:staff?)
|
|
|
|
|
2014-01-31 00:37:40 -05:00
|
|
|
context_posts = Post.where(topic_id: post.topic_id)
|
|
|
|
.where("post_number < ?", post.post_number)
|
|
|
|
.where(user_deleted: false)
|
|
|
|
.where(hidden: false)
|
2016-02-12 06:10:30 -05:00
|
|
|
.where(post_type: allowed_post_types)
|
2014-01-31 00:37:40 -05:00
|
|
|
.order('created_at desc')
|
|
|
|
.limit(SiteSetting.email_posts_context)
|
|
|
|
|
2016-02-25 08:05:40 -05:00
|
|
|
if topic_user && topic_user.last_emailed_post_number && user.user_option.email_previous_replies == UserOption.previous_replies_type[:unless_emailed]
|
2014-01-31 00:37:40 -05:00
|
|
|
context_posts = context_posts.where("post_number > ?", topic_user.last_emailed_post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
context_posts
|
|
|
|
end
|
|
|
|
|
2013-06-12 16:35:46 -04:00
|
|
|
def notification_email(user, opts)
|
2016-02-03 13:27:58 -05:00
|
|
|
notification_type = opts[:notification_type]
|
|
|
|
notification_data = opts[:notification_data_hash]
|
|
|
|
post = opts[:post]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-01-26 20:19:49 -05: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 22:55:33 -04:00
|
|
|
user_name = name unless name.blank?
|
2014-10-21 10:06:55 -04:00
|
|
|
end
|
|
|
|
|
2014-05-06 15:01:19 -04:00
|
|
|
allow_reply_by_email = opts[:allow_reply_by_email] unless user.suspended?
|
2016-01-26 20:19:49 -05:00
|
|
|
original_username = notification_data[:original_username] || notification_data[:display_username]
|
2014-02-06 19:06:35 -05:00
|
|
|
|
|
|
|
send_notification_email(
|
2016-02-22 19:34:16 -05:00
|
|
|
title: notification_data[:topic_title],
|
2016-01-26 20:19:49 -05:00
|
|
|
post: post,
|
2015-03-30 14:36:47 -04:00
|
|
|
username: original_username,
|
2014-10-21 10:06:55 -04:00
|
|
|
from_alias: user_name,
|
2014-02-06 19:06:35 -05:00
|
|
|
allow_reply_by_email: allow_reply_by_email,
|
2016-02-22 19:34:16 -05:00
|
|
|
use_site_subject: opts[:use_site_subject],
|
|
|
|
add_re_to_subject: opts[:add_re_to_subject],
|
|
|
|
show_category_in_subject: opts[:show_category_in_subject],
|
2014-02-06 19:06:35 -05:00
|
|
|
notification_type: notification_type,
|
2016-03-25 08:26:52 -04:00
|
|
|
use_invite_template: opts[:use_invite_template],
|
2014-02-06 19:06:35 -05:00
|
|
|
user: user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_notification_email(opts)
|
|
|
|
post = opts[:post]
|
|
|
|
title = opts[:title]
|
2017-04-24 15:26:06 -04:00
|
|
|
|
2014-02-06 19:06:35 -05:00
|
|
|
allow_reply_by_email = opts[:allow_reply_by_email]
|
2014-09-29 01:16:55 -04:00
|
|
|
use_site_subject = opts[:use_site_subject]
|
|
|
|
add_re_to_subject = opts[:add_re_to_subject] && post.post_number > 1
|
2014-10-21 10:06:55 -04:00
|
|
|
username = opts[:username]
|
2014-02-06 19:06:35 -05:00
|
|
|
from_alias = opts[:from_alias]
|
|
|
|
notification_type = opts[:notification_type]
|
|
|
|
user = opts[:user]
|
2016-02-09 18:54:13 -05:00
|
|
|
locale = user_locale(user)
|
2014-02-06 19:06:35 -05:00
|
|
|
|
2017-03-10 14:07:41 -05:00
|
|
|
template = "user_notifications.user_#{notification_type}"
|
|
|
|
if post.topic.private_message?
|
|
|
|
template << "_pm"
|
|
|
|
template << "_staged" if user.staged?
|
|
|
|
end
|
|
|
|
|
2014-10-03 07:44:08 -04:00
|
|
|
# category name
|
|
|
|
category = Topic.find_by(id: post.topic_id).category
|
2015-06-01 01:43:16 -04:00
|
|
|
if opts[:show_category_in_subject] && post.topic_id && category && !category.uncategorized?
|
2014-10-03 07:44:08 -04: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
|
|
|
|
|
2017-04-24 15:26:06 -04:00
|
|
|
if SiteSetting.private_email?
|
|
|
|
title = I18n.t("system_messages.private_topic_title", id: post.topic_id)
|
|
|
|
end
|
|
|
|
|
2013-07-22 15:06:37 -04:00
|
|
|
context = ""
|
2014-02-06 19:06:35 -05:00
|
|
|
tu = TopicUser.get(post.topic_id, user)
|
2016-02-25 08:05:40 -05:00
|
|
|
context_posts = self.class.get_context_posts(post, tu, user)
|
2013-12-29 21:02:12 -05:00
|
|
|
|
|
|
|
# make .present? cheaper
|
|
|
|
context_posts = context_posts.to_a
|
|
|
|
|
2013-07-22 15:06:37 -04:00
|
|
|
if context_posts.present?
|
2016-02-11 12:48:09 -05:00
|
|
|
context << "-- \n*#{I18n.t('user_notifications.previous_discussion')}*\n"
|
2013-07-22 15:06:37 -04:00
|
|
|
context_posts.each do |cp|
|
2016-04-11 13:06:10 -04:00
|
|
|
context << email_post_markdown(cp, true)
|
2013-07-22 15:06:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-10 14:07:41 -05:00
|
|
|
translation_override_exists = TranslationOverride.where(locale: SiteSetting.default_locale, translation_key: "#{template}.text_body_template").exists?
|
2016-03-23 00:08:34 -04:00
|
|
|
|
2016-03-25 08:26:52 -04:00
|
|
|
if opts[:use_invite_template]
|
|
|
|
if post.topic.private_message?
|
|
|
|
invite_template = "user_notifications.invited_to_private_message_body"
|
|
|
|
else
|
|
|
|
invite_template = "user_notifications.invited_to_topic_body"
|
|
|
|
end
|
2016-06-10 22:37:33 -04:00
|
|
|
topic_excerpt = post.excerpt.tr("\n", " ") if post.is_first_post? && post.excerpt
|
2017-04-27 11:49:39 -04:00
|
|
|
topic_excerpt = "" if SiteSetting.private_email?
|
2017-04-10 13:15:25 -04:00
|
|
|
message = I18n.t(invite_template, username: username, topic_title: gsub_emoji_to_unicode(title), topic_excerpt: topic_excerpt, site_title: SiteSetting.title, site_description: SiteSetting.site_description)
|
2017-03-10 14:07:41 -05:00
|
|
|
|
|
|
|
unless translation_override_exists
|
|
|
|
html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
|
|
|
|
template: 'email/invite',
|
|
|
|
format: :html,
|
|
|
|
locals: { message: PrettyText.cook(message, sanitize: false).html_safe,
|
|
|
|
classes: RTL.new(user).css_class
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2015-04-07 02:21:38 -04:00
|
|
|
else
|
2017-03-10 14:07:41 -05:00
|
|
|
reached_limit = SiteSetting.max_emails_per_day_per_user > 0
|
|
|
|
reached_limit &&= (EmailLog.where(user_id: user.id, skipped: false)
|
|
|
|
.where('created_at > ?', 1.day.ago)
|
|
|
|
.count) >= (SiteSetting.max_emails_per_day_per_user-1)
|
|
|
|
|
2016-02-25 08:05:40 -05:00
|
|
|
in_reply_to_post = post.reply_to_post if user.user_option.email_in_reply_to
|
2017-04-24 15:26:06 -04:00
|
|
|
if SiteSetting.private_email?
|
|
|
|
message = I18n.t('system_messages.contents_hidden')
|
|
|
|
else
|
|
|
|
message = email_post_markdown(post) + (reached_limit ? "\n\n#{I18n.t "user_notifications.reached_limit", count: SiteSetting.max_emails_per_day_per_user}" : "");
|
|
|
|
end
|
|
|
|
|
2013-07-24 03:13:15 -04:00
|
|
|
|
2017-03-10 14:07:41 -05:00
|
|
|
unless translation_override_exists
|
|
|
|
html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
|
|
|
|
template: 'email/notification',
|
|
|
|
format: :html,
|
|
|
|
locals: { context_posts: context_posts,
|
|
|
|
reached_limit: reached_limit,
|
|
|
|
post: post,
|
|
|
|
in_reply_to_post: in_reply_to_post,
|
|
|
|
classes: RTL.new(user).css_class
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2015-12-09 13:45:46 -05:00
|
|
|
end
|
2013-07-24 03:13:15 -04:00
|
|
|
|
2013-02-27 18:30:14 -05:00
|
|
|
email_opts = {
|
2017-04-10 13:15:25 -04:00
|
|
|
topic_title: gsub_emoji_to_unicode(title),
|
2017-03-10 14:07:41 -05:00
|
|
|
topic_title_url_encoded: title ? URI.encode(title) : title,
|
2016-03-25 08:26:52 -04:00
|
|
|
message: message,
|
2017-04-24 15:26:06 -04:00
|
|
|
url: post.url(without_slug: SiteSetting.private_email?),
|
2014-02-06 19:06:35 -05:00
|
|
|
post_id: post.id,
|
|
|
|
topic_id: post.topic_id,
|
2013-07-22 15:06:37 -04:00
|
|
|
context: context,
|
2014-10-21 10:06:55 -04:00
|
|
|
username: username,
|
2015-11-24 10:58:26 -05:00
|
|
|
add_unsubscribe_link: !user.staged,
|
2016-06-03 09:48:54 -04:00
|
|
|
mailing_list_mode: user.user_option.mailing_list_mode,
|
2016-06-16 21:27:52 -04:00
|
|
|
unsubscribe_url: post.unsubscribe_url(user),
|
2014-02-06 19:06:35 -05:00
|
|
|
allow_reply_by_email: allow_reply_by_email,
|
2016-02-26 17:56:56 -05:00
|
|
|
only_reply_by_email: allow_reply_by_email && user.staged,
|
2014-09-29 01:16:55 -04:00
|
|
|
use_site_subject: use_site_subject,
|
|
|
|
add_re_to_subject: add_re_to_subject,
|
2014-10-03 07:44:08 -04:00
|
|
|
show_category_in_subject: show_category_in_subject,
|
2014-06-06 09:09:00 -04:00
|
|
|
private_reply: post.topic.private_message?,
|
2016-04-11 13:06:10 -04:00
|
|
|
include_respond_instructions: !(user.suspended? || user.staged?),
|
2014-01-21 01:24:05 -05:00
|
|
|
template: template,
|
2015-04-07 02:21:38 -04:00
|
|
|
site_description: SiteSetting.site_description,
|
|
|
|
site_title: SiteSetting.title,
|
2017-03-10 12:06:15 -05:00
|
|
|
site_title_url_encoded: URI.encode(SiteSetting.title),
|
2016-02-26 17:56:56 -05:00
|
|
|
style: :notification,
|
2016-02-09 18:54:13 -05:00
|
|
|
locale: locale
|
2013-02-27 18:30:14 -05:00
|
|
|
}
|
|
|
|
|
2017-03-10 14:07:41 -05:00
|
|
|
unless translation_override_exists
|
|
|
|
email_opts[:html_override] = html
|
|
|
|
end
|
|
|
|
|
2013-02-27 18:30:14 -05:00
|
|
|
# If we have a display name, change the from address
|
2015-08-12 17:00:16 -04:00
|
|
|
email_opts[:from_alias] = from_alias if from_alias.present?
|
2013-02-27 18:30:14 -05:00
|
|
|
|
2014-02-06 19:06:35 -05:00
|
|
|
TopicUser.change(user.id, post.topic_id, last_emailed_post_number: post.post_number)
|
2013-12-29 21:02:12 -05:00
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
build_email(user.email, email_opts)
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2016-05-21 09:17:54 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_summary_for(user)
|
2017-04-28 10:37:52 -04:00
|
|
|
@site_name = SiteSetting.email_prefix.presence || SiteSetting.title # used by I18n
|
2016-05-21 09:17:54 -04:00
|
|
|
@user = user
|
|
|
|
@date = short_date(Time.now)
|
|
|
|
@base_url = Discourse.base_url
|
2017-03-21 09:11:15 -04:00
|
|
|
@email_prefix = SiteSetting.email_prefix.presence || SiteSetting.title
|
2016-11-10 18:16:24 -05:00
|
|
|
@header_color = ColorScheme.hex_for_name('header_primary')
|
|
|
|
@header_bgcolor = ColorScheme.hex_for_name('header_background')
|
2016-05-21 09:17:54 -04:00
|
|
|
@anchor_color = ColorScheme.hex_for_name('tertiary')
|
|
|
|
@markdown_linker = MarkdownLinker.new(@base_url)
|
2016-06-16 21:27:52 -04:00
|
|
|
@unsubscribe_key = UnsubscribeKey.create_key_for(@user, "digest")
|
2016-05-21 09:17:54 -04:00
|
|
|
end
|
2016-06-21 11:12:30 -04:00
|
|
|
|
|
|
|
def apply_notification_styles(email)
|
|
|
|
email.html_part.body = Email::Styles.new(email.html_part.body.to_s).tap do |styles|
|
|
|
|
styles.format_basic
|
|
|
|
styles.format_notification
|
|
|
|
end.to_html
|
|
|
|
email
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|