2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'markdown_linker'
|
|
|
|
require_dependency 'email_builder'
|
|
|
|
|
|
|
|
class UserNotifications < ActionMailer::Base
|
2013-04-26 02:56:28 -04:00
|
|
|
default charset: 'UTF-8'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
include EmailBuilder
|
|
|
|
|
|
|
|
def signup(user, opts={})
|
|
|
|
build_email(user.email, "user_notifications.signup", email_token: opts[:email_token])
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_email(user, opts={})
|
|
|
|
build_email(user.email, "user_notifications.authorize_email", email_token: opts[:email_token])
|
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password(user, opts={})
|
|
|
|
build_email(user.email, "user_notifications.forgot_password", email_token: opts[:email_token])
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_message(user, opts={})
|
|
|
|
post = opts[:post]
|
|
|
|
|
|
|
|
build_email user.email,
|
|
|
|
"user_notifications.private_message",
|
|
|
|
message: post.raw,
|
|
|
|
url: post.url,
|
|
|
|
subject_prefix: post.post_number != 1 ? "re: " : "",
|
|
|
|
topic_title: post.topic.title,
|
2013-03-01 14:56:52 -05:00
|
|
|
private_message_from: post.user.name,
|
|
|
|
from: "#{I18n.t(:via, username: post.user.name, site_name: SiteSetting.title)} <#{SiteSetting.notification_email}>",
|
2013-02-05 14:16:51 -05:00
|
|
|
add_unsubscribe_link: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def digest(user, opts={})
|
|
|
|
@user = user
|
|
|
|
@base_url = Discourse.base_url
|
|
|
|
|
2013-06-03 16:12:24 -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
|
|
|
|
|
|
|
@site_name = SiteSetting.title
|
|
|
|
|
2013-03-12 14:30:56 -04:00
|
|
|
@last_seen_at = I18n.l(@user.last_seen_at || @user.created_at, format: :short)
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-06-03 16:12:24 -04:00
|
|
|
# A list of topics to show the user
|
|
|
|
@new_topics = Topic.for_digest(user, min_date)
|
2013-02-05 14:16:51 -05:00
|
|
|
@markdown_linker = MarkdownLinker.new(Discourse.base_url)
|
|
|
|
|
|
|
|
# Don't send email unless there is content in it
|
2013-06-03 16:12:24 -04:00
|
|
|
if @new_topics.present?
|
2013-02-07 10:45:24 -05:00
|
|
|
mail to: user.email,
|
2013-03-05 16:53:07 -05:00
|
|
|
from: "#{I18n.t('user_notifications.digest.from', site_name: SiteSetting.title)} <#{SiteSetting.notification_email}>",
|
2013-02-05 14:16:51 -05:00
|
|
|
subject: I18n.t('user_notifications.digest.subject_template',
|
2013-03-23 11:02:59 -04:00
|
|
|
site_name: @site_name,
|
|
|
|
date: I18n.l(Time.now, format: :short))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def notification_template(user, opts)
|
|
|
|
@notification = opts[:notification]
|
|
|
|
return unless @notification.present?
|
|
|
|
|
|
|
|
@post = opts[:post]
|
|
|
|
return unless @post.present?
|
|
|
|
|
2013-02-27 18:30:14 -05:00
|
|
|
username = @notification.data_hash[:display_username]
|
2013-03-01 07:07:44 -05:00
|
|
|
notification_type = Notification.types[opts[:notification].notification_type].to_s
|
2013-02-27 18:30:14 -05:00
|
|
|
|
|
|
|
email_opts = {
|
|
|
|
topic_title: @notification.data_hash[:topic_title],
|
|
|
|
message: @post.raw,
|
|
|
|
url: @post.url,
|
|
|
|
username: username,
|
|
|
|
add_unsubscribe_link: true
|
|
|
|
}
|
|
|
|
|
|
|
|
# If we have a display name, change the from address
|
|
|
|
if username.present?
|
2013-02-27 19:01:45 -05:00
|
|
|
aliased = I18n.t(:via, username: username, site_name: SiteSetting.title)
|
|
|
|
email_opts[:from] = "#{aliased} <#{SiteSetting.notification_email}>"
|
2013-02-27 18:30:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
email = build_email user.email, "user_notifications.user_#{notification_type}", email_opts
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-27 18:30:14 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
alias :user_invited_to_private_message :notification_template
|
|
|
|
alias :user_replied :notification_template
|
|
|
|
alias :user_quoted :notification_template
|
|
|
|
alias :user_mentioned :notification_template
|
2013-02-27 15:38:44 -05:00
|
|
|
alias :user_posted :notification_template
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
end
|