discourse/app/models/user_email_observer.rb

117 lines
2.8 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
class UserEmailObserver < ActiveRecord::Observer
observe :notification
class EmailUser
attr_reader :notification
2013-07-22 00:50:01 +08:00
def initialize(notification)
@notification = notification
end
2016-04-01 05:33:25 +08:00
def group_mentioned
enqueue :group_mentioned
end
def mentioned
enqueue :user_mentioned
2013-02-06 03:16:51 +08:00
end
def posted
enqueue :user_posted
end
2013-02-06 03:16:51 +08:00
def quoted
enqueue :user_quoted
end
def replied
enqueue :user_replied
end
2016-04-01 05:33:25 +08:00
def linked
enqueue :user_linked
end
def private_message
2016-04-01 05:33:25 +08:00
enqueue_private(:user_private_message)
end
def invited_to_private_message
2016-04-01 05:33:25 +08:00
enqueue(:user_invited_to_private_message, private_delay)
end
def invited_to_topic
2016-04-01 05:33:25 +08:00
enqueue(:user_invited_to_topic, private_delay)
end
def self.notification_params(notification, type)
post_id = (notification.data_hash[:original_post_id] || notification.post_id).to_i
hash = {
type: type,
user_id: notification.user_id,
notification_id: notification.id,
notification_data_hash: notification.data_hash,
notification_type: Notification.types[notification.notification_type],
}
hash[:post_id] = post_id if post_id > 0
hash
end
private
2016-04-01 05:33:25 +08:00
EMAILABLE_POST_TYPES ||= Set.new [Post.types[:regular], Post.types[:whisper]]
2016-04-01 05:33:25 +08:00
def enqueue(type, delay=default_delay)
return unless notification.user.user_option.email_direct?
perform_enqueue(type, delay)
end
2016-04-01 05:33:25 +08:00
def enqueue_private(type, delay=private_delay)
return unless notification.user.user_option.email_private_messages?
perform_enqueue(type, delay)
end
2016-04-01 05:33:25 +08:00
def perform_enqueue(type, delay)
user = notification.user
return unless user.active? || user.staged?
return if SiteSetting.must_approve_users? && !user.approved?
2016-04-01 05:33:25 +08:00
return unless EMAILABLE_POST_TYPES.include?(post_type)
Jobs.enqueue_in(delay, :user_email, self.class.notification_params(notification, type))
end
2016-04-01 05:33:25 +08:00
def default_delay
2013-08-27 09:52:09 +08:00
SiteSetting.email_time_window_mins.minutes
end
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08:00
def private_delay
SiteSetting.private_email_time_window_seconds
end
2013-02-06 03:16:51 +08:00
2016-04-01 05:33:25 +08:00
def post_type
@post_type ||= begin
type = notification.data_hash["original_post_type"] if notification.data_hash
type ||= notification.post.try(:post_type)
type
end
end
2016-04-01 05:33:25 +08:00
end
2016-04-01 05:33:25 +08:00
def after_commit(notification)
transaction_includes_action = notification.send(:transaction_include_any_action?, [:create])
self.class.process_notification(notification) if transaction_includes_action
end
2016-04-01 05:33:25 +08:00
def self.process_notification(notification)
email_user = EmailUser.new(notification)
2016-04-01 05:33:25 +08:00
email_method = Notification.types[notification.notification_type]
email_user.send(email_method) if email_user.respond_to? email_method
2013-02-06 03:16:51 +08:00
end
2016-04-01 05:33:25 +08:00
2013-02-06 03:16:51 +08:00
end