diff --git a/app/models/user_email_observer.rb b/app/models/user_email_observer.rb index ed8a14941e4..45e1adbc567 100644 --- a/app/models/user_email_observer.rb +++ b/app/models/user_email_observer.rb @@ -36,7 +36,9 @@ class UserEmailObserver < ActiveRecord::Observer def enqueue(type) return unless notification.user.email_direct? - Jobs.enqueue_in(SiteSetting.email_time_window_mins.minutes, + + + Jobs.enqueue_in(delay, :user_email, type: type, user_id: notification.user_id, @@ -45,12 +47,22 @@ class UserEmailObserver < ActiveRecord::Observer def enqueue_private(type) return unless (notification.user.email_direct? && notification.user.email_private_messages?) - Jobs.enqueue_in(SiteSetting.email_time_window_mins.minutes, + Jobs.enqueue_in(delay, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id) end + + def delay + mins = SiteSetting.email_time_window_mins.minutes + if notification.user && + (!notification.user.last_seen_at || notification.user.last_seen_at < mins.ago) + 0 + else + mins + end + end end def after_commit(notification) @@ -65,6 +77,7 @@ class UserEmailObserver < ActiveRecord::Observer private + def extract_notification_type(notification) Notification.types[notification.notification_type] end diff --git a/spec/models/user_email_observer_spec.rb b/spec/models/user_email_observer_spec.rb index f56ac08b41b..b33af44a28f 100644 --- a/spec/models/user_email_observer_spec.rb +++ b/spec/models/user_email_observer_spec.rb @@ -8,6 +8,12 @@ describe UserEmailObserver do let!(:notification) { Fabricate(:notification, user: user) } it "enqueues a job for the email" do + Jobs.expects(:enqueue_in).with(0, :user_email, type: :user_mentioned, user_id: notification.user_id, notification_id: notification.id) + UserEmailObserver.send(:new).after_commit(notification) + end + + it "enqueue a delayed job for users that are online" do + user.last_seen_at = 1.minute.ago Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_mentioned, user_id: notification.user_id, notification_id: notification.id) UserEmailObserver.send(:new).after_commit(notification) end @@ -26,7 +32,7 @@ describe UserEmailObserver do let!(:notification) { Fabricate(:notification, user: user, notification_type: 9) } it "enqueues a job for the email" do - Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_posted, user_id: notification.user_id, notification_id: notification.id) + Jobs.expects(:enqueue_in).with(0, :user_email, type: :user_posted, user_id: notification.user_id, notification_id: notification.id) UserEmailObserver.send(:new).after_commit(notification) end @@ -44,7 +50,7 @@ describe UserEmailObserver do let!(:notification) { Fabricate(:notification, user: user, notification_type: 2) } it "enqueues a job for the email" do - Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_replied, user_id: notification.user_id, notification_id: notification.id) + Jobs.expects(:enqueue_in).with(0, :user_email, type: :user_replied, user_id: notification.user_id, notification_id: notification.id) UserEmailObserver.send(:new).after_commit(notification) end @@ -62,13 +68,13 @@ describe UserEmailObserver do let!(:notification) { Fabricate(:notification, user: user, notification_type: 3) } it "enqueues a job for the email" do - Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_quoted, user_id: notification.user_id, notification_id: notification.id) + Jobs.expects(:enqueue_in).with(0, :user_email, type: :user_quoted, user_id: notification.user_id, notification_id: notification.id) UserEmailObserver.send(:new).after_commit(notification) end it "doesn't enqueue an email if the user has mention emails disabled" do user.expects(:email_direct?).returns(false) - Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_quoted)).never + Jobs.expects(:enqueue_in).with(0, :user_email, has_entry(type: :user_quoted)).never UserEmailObserver.send(:new).after_commit(notification) end @@ -80,16 +86,16 @@ describe UserEmailObserver do let!(:notification) { Fabricate(:notification, user: user, notification_type: 7) } it "enqueues a job for the email" do - Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: :user_invited_to_private_message, user_id: notification.user_id, notification_id: notification.id) + Jobs.expects(:enqueue_in).with(0, :user_email, type: :user_invited_to_private_message, user_id: notification.user_id, notification_id: notification.id) UserEmailObserver.send(:new).after_commit(notification) end it "doesn't enqueue an email if the user has mention emails disabled" do user.expects(:email_direct?).returns(false) - Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: :user_invited_to_private_message)).never + Jobs.expects(:enqueue_in).with(0, :user_email, has_entry(type: :user_invited_to_private_message)).never UserEmailObserver.send(:new).after_commit(notification) end end -end \ No newline at end of file +end