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
|
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",
|
|
|
|
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',
|
|
|
|
email_token: opts[:email_token],
|
|
|
|
new_user_tips: SiteContent.content_for(:usage_tips))
|
2013-06-05 23:16:31 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def authorize_email(user, opts={})
|
2013-06-10 16:46:08 -04:00
|
|
|
build_email(user.email, template: "user_notifications.authorize_email", email_token: opts[:email_token])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password(user, opts={})
|
2013-06-10 16:46:08 -04:00
|
|
|
build_email(user.email, template: "user_notifications.forgot_password", email_token: opts[:email_token])
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2013-08-09 14:43:02 -04:00
|
|
|
@featured_topics = Topic.for_digest(user, min_date).to_a
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Don't send email unless there is content in it
|
2013-08-09 14:43:02 -04:00
|
|
|
if @featured_topics.present?
|
|
|
|
@featured_topics, @new_topics = @featured_topics[0..4], @featured_topics[5..-1]
|
2013-11-29 13:00:10 -05:00
|
|
|
|
|
|
|
# Sort the new topics by score
|
2013-12-03 10:10:53 -05:00
|
|
|
@new_topics.sort! {|a, b| (b.score || 0) - (a.score || 0) } if @new_topics.present?
|
2013-11-29 13:00:10 -05:00
|
|
|
|
2013-08-09 14:43:02 -04:00
|
|
|
@markdown_linker = MarkdownLinker.new(Discourse.base_url)
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
build_email user.email,
|
2013-06-12 16:35:46 -04:00
|
|
|
from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title),
|
|
|
|
subject: I18n.t('user_notifications.digest.subject_template',
|
|
|
|
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-06-12 16:35:46 -04:00
|
|
|
def user_invited_to_private_message(user, opts)
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_replied(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_quoted(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_mentioned(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_posted(user, opts)
|
|
|
|
opts[:allow_reply_by_email] = true
|
|
|
|
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
|
|
|
|
|
|
|
|
# We use the 'user_posted' event when you are emailed a post in a PM.
|
|
|
|
opts[:notification_type] = 'posted'
|
|
|
|
|
|
|
|
notification_email(user, opts)
|
|
|
|
end
|
|
|
|
|
2013-06-12 16:35:46 -04:00
|
|
|
protected
|
|
|
|
|
2013-07-22 15:06:37 -04:00
|
|
|
def email_post_markdown(post)
|
|
|
|
result = "[email-indent]\n"
|
|
|
|
result << "#{post.raw}\n\n"
|
2013-07-24 03:13:15 -04: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 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
|
|
|
|
end
|
|
|
|
|
2013-06-12 16:35:46 -04:00
|
|
|
def notification_email(user, opts)
|
2013-07-26 03:27:46 -04:00
|
|
|
return unless @notification = opts[:notification]
|
|
|
|
return unless @post = opts[:post]
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-27 18:30:14 -05:00
|
|
|
username = @notification.data_hash[:display_username]
|
2013-08-07 13:02:49 -04:00
|
|
|
notification_type = opts[:notification_type] || Notification.types[@notification.notification_type].to_s
|
2013-02-27 18:30:14 -05:00
|
|
|
|
2013-07-22 15:06:37 -04:00
|
|
|
context = ""
|
2013-12-29 21:02:12 -05:00
|
|
|
tu = TopicUser.get(@post.topic_id, user)
|
|
|
|
|
2013-07-22 15:06:37 -04:00
|
|
|
context_posts = Post.where(topic_id: @post.topic_id)
|
|
|
|
.where("post_number < ?", @post.post_number)
|
2013-08-08 10:32:51 -04:00
|
|
|
.where(user_deleted: false)
|
2013-07-22 15:06:37 -04:00
|
|
|
.order('created_at desc')
|
|
|
|
.limit(SiteSetting.email_posts_context)
|
|
|
|
|
2013-12-29 21:02:12 -05:00
|
|
|
if tu && tu.last_emailed_post_number
|
|
|
|
context_posts = context_posts.where("post_number > ?", tu.last_emailed_post_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
# make .present? cheaper
|
|
|
|
context_posts = context_posts.to_a
|
|
|
|
|
2013-07-22 15:06:37 -04:00
|
|
|
if context_posts.present?
|
2013-07-24 03:13:15 -04:00
|
|
|
context << "---\n*#{I18n.t('user_notifications.previous_discussion')}*\n"
|
2013-07-22 15:06:37 -04:00
|
|
|
context_posts.each do |cp|
|
|
|
|
context << email_post_markdown(cp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-24 03:13:15 -04:00
|
|
|
html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
|
2013-08-07 13:02:49 -04:00
|
|
|
template: 'email/notification',
|
|
|
|
format: :html,
|
|
|
|
locals: { context_posts: context_posts, post: @post }
|
2013-07-24 03:13:15 -04:00
|
|
|
)
|
|
|
|
|
2013-08-07 13:02:49 -04:00
|
|
|
if @post.topic.private_message?
|
|
|
|
opts[:subject_prefix] = "[#{I18n.t('private_message_abbrev')}] "
|
|
|
|
end
|
2013-07-24 03:13:15 -04:00
|
|
|
|
2013-02-27 18:30:14 -05:00
|
|
|
email_opts = {
|
|
|
|
topic_title: @notification.data_hash[:topic_title],
|
2013-07-22 15:06:37 -04:00
|
|
|
message: email_post_markdown(@post),
|
2013-02-27 18:30:14 -05:00
|
|
|
url: @post.url,
|
2013-06-13 18:11:10 -04:00
|
|
|
post_id: @post.id,
|
|
|
|
topic_id: @post.topic_id,
|
2013-07-22 15:06:37 -04:00
|
|
|
context: context,
|
2013-02-27 18:30:14 -05:00
|
|
|
username: username,
|
2013-06-10 16:46:08 -04:00
|
|
|
add_unsubscribe_link: true,
|
2013-06-13 10:56:16 -04:00
|
|
|
allow_reply_by_email: opts[:allow_reply_by_email],
|
2013-07-24 03:13:15 -04:00
|
|
|
template: "user_notifications.user_#{notification_type}",
|
2013-07-26 03:27:46 -04:00
|
|
|
html_override: html,
|
2013-08-07 13:02:49 -04:00
|
|
|
style: :notification,
|
|
|
|
subject_prefix: opts[:subject_prefix] || ''
|
2013-02-27 18:30:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# If we have a display name, change the from address
|
|
|
|
if username.present?
|
2013-09-12 01:38:18 -04:00
|
|
|
email_opts[:from_alias] = username
|
2013-02-27 18:30:14 -05:00
|
|
|
end
|
|
|
|
|
2013-12-29 21:02:12 -05:00
|
|
|
TopicUser.change(user.id, @post.topic_id, last_emailed_post_number: @post.post_number)
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
build_email(user.email, 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
|
|
|
end
|