send invite & message notification emails immediately

This commit is contained in:
Régis Hanol 2015-11-27 19:09:35 +01:00
parent 08cd17b4cf
commit 914b854d92
2 changed files with 19 additions and 13 deletions

View File

@ -25,20 +25,20 @@ class UserEmailObserver < ActiveRecord::Observer
end
def private_message
enqueue_private :user_private_message
enqueue_private(:user_private_message, 0)
end
def invited_to_private_message
enqueue :user_invited_to_private_message
enqueue(:user_invited_to_private_message, 0)
end
def invited_to_topic
enqueue :user_invited_to_topic
enqueue(:user_invited_to_topic, 0)
end
private
def enqueue(type)
def enqueue(type, delay=default_delay)
return unless notification.user.email_direct? && (notification.user.active? || notification.user.staged?)
Jobs.enqueue_in(delay,
@ -48,7 +48,7 @@ class UserEmailObserver < ActiveRecord::Observer
notification_id: notification.id)
end
def enqueue_private(type)
def enqueue_private(type, delay=default_delay)
return unless notification.user.email_private_messages? && (notification.user.active? || notification.user.staged?)
Jobs.enqueue_in(delay,
@ -58,14 +58,13 @@ class UserEmailObserver < ActiveRecord::Observer
notification_id: notification.id)
end
def delay
def default_delay
SiteSetting.email_time_window_mins.minutes
end
end
def after_commit(notification)
transaction_includes_action = notification.send(:transaction_include_any_action?, [:create])
delegate_to_email_user notification if transaction_includes_action
end

View File

@ -11,7 +11,7 @@ describe UserEmailObserver do
shared_examples "enqueue" do
it "enqueues a job for the email" do
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
Jobs.expects(:enqueue_in).with(delay, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
@ -20,13 +20,13 @@ describe UserEmailObserver do
before { notification.user.active = false }
it "doesn't enqueue a job" do
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: type)).never
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.send(:new).after_commit(notification)
end
it "enqueues a job if the user is staged" do
notification.user.staged = true
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
Jobs.expects(:enqueue_in).with(delay, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
@ -39,7 +39,7 @@ describe UserEmailObserver do
it "doesn't enqueue a job if the user has mention emails disabled" do
notification.user.expects(:email_direct?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: type)).never
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.send(:new).after_commit(notification)
end
end
@ -49,20 +49,21 @@ describe UserEmailObserver do
it "doesn't enqueue a job if the user has private message emails disabled" do
notification.user.expects(:email_private_messages?).returns(false)
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, has_entry(type: type)).never
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.send(:new).after_commit(notification)
end
end
context 'user_mentioned' do
let(:type) { :user_mentioned }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(1) }
include_examples "enqueue_public"
it "enqueue a delayed job for users that are online" do
notification.user.last_seen_at = 1.minute.ago
Jobs.expects(:enqueue_in).with(SiteSetting.email_time_window_mins.minutes, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
Jobs.expects(:enqueue_in).with(delay, :user_email, type: type, user_id: notification.user_id, notification_id: notification.id)
UserEmailObserver.send(:new).after_commit(notification)
end
@ -70,6 +71,7 @@ describe UserEmailObserver do
context 'user_replied' do
let(:type) { :user_replied }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(2) }
include_examples "enqueue_public"
@ -77,6 +79,7 @@ describe UserEmailObserver do
context 'user_quoted' do
let(:type) { :user_quoted }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(3) }
include_examples "enqueue_public"
@ -84,6 +87,7 @@ describe UserEmailObserver do
context 'user_posted' do
let(:type) { :user_posted }
let(:delay) { SiteSetting.email_time_window_mins.minutes }
let!(:notification) { create_notification(9) }
include_examples "enqueue_public"
@ -91,6 +95,7 @@ describe UserEmailObserver do
context 'user_private_message' do
let(:type) { :user_private_message }
let(:delay) { 0 }
let!(:notification) { create_notification(6) }
include_examples "enqueue_private"
@ -98,6 +103,7 @@ describe UserEmailObserver do
context 'user_invited_to_private_message' do
let(:type) { :user_invited_to_private_message }
let(:delay) { 0 }
let!(:notification) { create_notification(7) }
include_examples "enqueue_public"
@ -105,6 +111,7 @@ describe UserEmailObserver do
context 'user_invited_to_topic' do
let(:type) { :user_invited_to_topic }
let(:delay) { 0 }
let!(:notification) { create_notification(13) }
include_examples "enqueue_public"