2013-06-10 15:33:37 -04:00
|
|
|
require_dependency 'email/sender'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
module Jobs
|
|
|
|
|
|
|
|
# Asynchronously send an email to a user
|
|
|
|
class UserEmail < Jobs::Base
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
@args = args
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Required parameters
|
|
|
|
raise Discourse::InvalidParameters.new(:user_id) unless args[:user_id].present?
|
|
|
|
raise Discourse::InvalidParameters.new(:type) unless args[:type].present?
|
|
|
|
|
|
|
|
# Find the user
|
2014-05-06 09:41:59 -04:00
|
|
|
@user = User.find_by(id: args[:user_id])
|
2014-02-14 13:06:21 -05:00
|
|
|
return skip(I18n.t("email_log.no_user", user_id: args[:user_id])) unless @user
|
|
|
|
return skip(I18n.t("email_log.suspended_not_pm")) if @user.suspended? && args[:type] != :user_private_message
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
seen_recently = (@user.last_seen_at.present? && @user.last_seen_at > SiteSetting.email_time_window_mins.minutes.ago)
|
|
|
|
seen_recently = false if @user.email_always
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
email_args = {}
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
if args[:post_id]
|
|
|
|
# Don't email a user about a post when we've seen them recently.
|
2014-02-14 13:06:21 -05:00
|
|
|
return skip(I18n.t('email_log.seen_recently')) if seen_recently
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
post = Post.find_by(id: args[:post_id])
|
2014-02-14 13:06:21 -05:00
|
|
|
return skip(I18n.t('email_log.post_not_found', post_id: args[:post_id])) unless post.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
email_args[:post] = post
|
|
|
|
end
|
|
|
|
|
|
|
|
email_args[:email_token] = args[:email_token] if args[:email_token].present?
|
|
|
|
|
|
|
|
notification = nil
|
2014-05-06 09:41:59 -04:00
|
|
|
notification = Notification.find_by(id: args[:notification_id]) if args[:notification_id].present?
|
2013-02-05 14:16:51 -05:00
|
|
|
if notification.present?
|
|
|
|
# Don't email a user about a post when we've seen them recently.
|
2014-02-14 13:06:21 -05:00
|
|
|
return skip(I18n.t('email_log.seen_recently')) if seen_recently && !@user.suspended?
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Load the post if present
|
2014-05-06 09:41:59 -04:00
|
|
|
email_args[:post] ||= Post.find_by(id: notification.data_hash[:original_post_id].to_i)
|
2013-06-10 12:02:04 -04:00
|
|
|
email_args[:post] ||= notification.post
|
2013-02-05 14:16:51 -05:00
|
|
|
email_args[:notification] = notification
|
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
return skip(I18n.t('email_log.notification_already_read')) if notification.read?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
skip_reason = skip_email_for_post(email_args[:post], @user)
|
|
|
|
return skip(skip_reason) if skip_reason
|
2013-06-10 12:02:04 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Make sure that mailer exists
|
|
|
|
raise Discourse::InvalidParameters.new(:type) unless UserNotifications.respond_to?(args[:type])
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
message = UserNotifications.send(args[:type], @user, email_args)
|
2013-02-05 14:16:51 -05:00
|
|
|
# Update the to address if we have a custom one
|
|
|
|
if args[:to_address].present?
|
|
|
|
message.to = [args[:to_address]]
|
|
|
|
end
|
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
Email::Sender.new(message, args[:type], @user).send
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
# If this email has a related post, don't send an email if it's been deleted or seen recently.
|
|
|
|
def skip_email_for_post(post, user)
|
2014-02-14 13:06:21 -05:00
|
|
|
if post
|
|
|
|
return I18n.t('email_log.topic_nil') if post.topic.blank?
|
|
|
|
return I18n.t('email_log.post_deleted') if post.user_deleted?
|
|
|
|
return I18n.t('email_log.user_suspended') if (user.suspended? && !post.user.try(:staff?))
|
|
|
|
return I18n.t('email_log.already_read') if PostTiming.where(topic_id: post.topic_id, post_number: post.post_number, user_id: user.id).present?
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip(reason)
|
|
|
|
EmailLog.create( email_type: @args[:type],
|
|
|
|
to_address: @args[:to_address] || @user.try(:email) || "no_email_found",
|
|
|
|
user_id: @user.try(:id),
|
|
|
|
skipped: true,
|
|
|
|
skipped_reason: reason)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|