2013-02-05 14:16:51 -05:00
class UserEmailObserver < ActiveRecord :: Observer
observe :notification
2013-08-24 12:21:39 +01:00
class EmailUser
attr_reader :notification
2013-07-21 18:50:01 +02:00
2013-08-24 12:21:39 +01:00
def initialize ( notification )
@notification = notification
end
2013-08-07 13:02:49 -04:00
2015-11-30 17:03:47 +11:00
def group_mentioned
enqueue :group_mentioned
end
2013-08-24 12:21:39 +01:00
def mentioned
enqueue :user_mentioned
2013-02-05 14:16:51 -05:00
end
2013-08-24 12:21:39 +01:00
def posted
enqueue :user_posted
end
2013-02-05 14:16:51 -05:00
2013-08-24 12:21:39 +01:00
def quoted
enqueue :user_quoted
end
def replied
enqueue :user_replied
end
2016-02-16 18:29:23 +01:00
def linked
enqueue :user_linked
end
2013-08-24 12:21:39 +01:00
def private_message
2016-02-01 10:14:30 +01:00
enqueue_private ( :user_private_message )
2013-08-24 12:21:39 +01:00
end
2013-02-27 15:38:44 -05:00
2013-08-24 12:21:39 +01:00
def invited_to_private_message
2016-02-01 10:14:30 +01:00
enqueue ( :user_invited_to_private_message , private_delay )
2013-08-24 12:21:39 +01:00
end
2015-03-31 00:06:47 +05:30
def invited_to_topic
2016-02-01 10:14:30 +01:00
enqueue ( :user_invited_to_topic , private_delay )
2015-03-31 00:06:47 +05:30
end
2016-01-27 12:19:49 +11:00
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
2013-08-24 12:21:39 +01:00
private
2016-01-19 01:39:57 +01:00
EMAILABLE_POST_TYPES || = Set . new [ Post . types [ :regular ] , Post . types [ :whisper ] ]
2015-11-27 19:09:35 +01:00
def enqueue ( type , delay = default_delay )
2016-02-17 15:46:19 +11:00
return unless notification . user . user_option . email_direct?
2016-02-01 19:12:10 +01:00
perform_enqueue ( type , delay )
2013-08-24 12:21:39 +01:00
end
2016-02-01 10:14:30 +01:00
def enqueue_private ( type , delay = private_delay )
2016-02-17 15:46:19 +11:00
return unless notification . user . user_option . email_private_messages?
2016-02-01 19:12:10 +01:00
perform_enqueue ( type , delay )
2016-01-27 12:19:49 +11:00
end
def perform_enqueue ( type , delay )
2016-01-19 01:39:57 +01:00
return unless notification . user . active? || notification . user . staged?
2016-02-01 19:12:10 +01:00
return unless EMAILABLE_POST_TYPES . include? ( post_type )
2015-06-09 13:05:26 +05:30
2016-01-27 12:19:49 +11:00
Jobs . enqueue_in ( delay , :user_email , self . class . notification_params ( notification , type ) )
2013-08-24 12:21:39 +01:00
end
2013-08-26 14:55:35 +10:00
2015-11-27 19:09:35 +01:00
def default_delay
2013-08-27 11:52:09 +10:00
SiteSetting . email_time_window_mins . minutes
2013-08-26 14:55:35 +10:00
end
2016-01-27 12:19:49 +11:00
2016-02-01 10:14:30 +01:00
def private_delay
2016-02-04 17:22:16 +01:00
SiteSetting . private_email_time_window_seconds
2016-02-01 10:14:30 +01:00
end
2016-02-01 19:12:10 +01: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
2013-02-05 14:16:51 -05:00
end
2013-08-24 12:21:39 +01:00
def after_commit ( notification )
2014-02-17 17:44:28 +01:00
transaction_includes_action = notification . send ( :transaction_include_any_action? , [ :create ] )
2016-01-27 12:19:49 +11:00
self . class . process_notification ( notification ) if transaction_includes_action
2013-02-05 14:16:51 -05:00
end
2016-01-27 12:19:49 +11:00
def self . process_notification ( notification )
2013-08-24 12:21:39 +01:00
email_user = EmailUser . new ( notification )
2016-01-27 12:19:49 +11:00
email_method = Notification . types [ notification . notification_type ]
2016-02-01 19:12:10 +01:00
2013-08-24 12:21:39 +01:00
email_user . send ( email_method ) if email_user . respond_to? email_method
2013-02-05 14:16:51 -05:00
end
2016-01-27 12:19:49 +11:00
2013-02-05 14:16:51 -05:00
end