discourse/lib/jobs/user_email.rb

78 lines
2.3 KiB
Ruby
Raw Normal View History

2013-06-11 03:33:37 +08:00
require_dependency 'email/sender'
2013-02-06 03:16:51 +08:00
module Jobs
# Asynchronously send an email to a user
class UserEmail < Jobs::Base
def execute(args)
# 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
user = User.where(id: args[:user_id]).first
return if !user || user.is_banned?
2013-02-06 03:16:51 +08:00
seen_recently = (user.last_seen_at.present? && user.last_seen_at > SiteSetting.email_time_window_mins.minutes.ago)
2013-02-06 03:16:51 +08:00
email_args = {}
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
if args[:post_id]
# Don't email a user about a post when we've seen them recently.
return if seen_recently
post = Post.where(id: args[:post_id]).first
return unless post.present?
email_args[:post] = post
end
email_args[:email_token] = args[:email_token] if args[:email_token].present?
notification = nil
notification = Notification.where(id: args[:notification_id]).first if args[:notification_id].present?
if notification.present?
# Don't email a user about a post when we've seen them recently.
return if seen_recently
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
# Load the post if present
email_args[:post] ||= notification.post
2013-02-06 03:16:51 +08:00
email_args[:notification] = notification
# Don't send email if the notification this email is about has already been read
return if notification.read?
end
return if skip_email_for_post(email_args[:post], user)
2013-02-06 03:16:51 +08:00
# Make sure that mailer exists
raise Discourse::InvalidParameters.new(:type) unless UserNotifications.respond_to?(args[:type])
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
message = UserNotifications.send(args[:type], user, email_args)
# Update the to address if we have a custom one
if args[:to_address].present?
message.to = [args[:to_address]]
end
2013-06-11 03:33:37 +08:00
Email::Sender.new(message, args[:type], user).send
end
private
2013-02-06 03:16:51 +08: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)
post &&
(post.topic.blank? ||
post.user_deleted? ||
PostTiming.where(topic_id: post.topic_id, post_number: post.post_number, user_id: user.id).present?)
2013-02-06 03:16:51 +08:00
end
end
end