UserEmailObserver is now removed

no big surprises here was pretty straightforward

after_commit semantics sure are weird though
This commit is contained in:
Sam 2016-12-22 15:29:34 +11:00
parent 2f6a4cc6de
commit 019f1a1d06
7 changed files with 38 additions and 34 deletions

View File

@ -15,6 +15,8 @@ class Notification < ActiveRecord::Base
after_save :refresh_notification_count after_save :refresh_notification_count
after_destroy :refresh_notification_count after_destroy :refresh_notification_count
after_commit :send_email
def self.ensure_consistency! def self.ensure_consistency!
Notification.exec_sql(" Notification.exec_sql("
DELETE FROM Notifications n WHERE notification_type = :id AND DELETE FROM Notifications n WHERE notification_type = :id AND
@ -196,6 +198,11 @@ class Notification < ActiveRecord::Base
user.publish_notifications_state user.publish_notifications_state
end end
def send_email
transaction_includes_action = self.send(:transaction_include_any_action?, [:create])
NotificationEmailer.process_notification(self) if transaction_includes_action
end
end end
# == Schema Information # == Schema Information

View File

@ -1,5 +1,4 @@
class UserEmailObserver < ActiveRecord::Observer class NotificationEmailer
observe :notification
class EmailUser class EmailUser
attr_reader :notification attr_reader :notification
@ -105,12 +104,17 @@ class UserEmailObserver < ActiveRecord::Observer
end end
def after_commit(notification) def self.disable
transaction_includes_action = notification.send(:transaction_include_any_action?, [:create]) @disabled = true
self.class.process_notification(notification) if transaction_includes_action end
def self.enable
@disabled = false
end end
def self.process_notification(notification) def self.process_notification(notification)
return if @disabled
email_user = EmailUser.new(notification) email_user = EmailUser.new(notification)
email_method = Notification.types[notification.notification_type] email_method = Notification.types[notification.notification_type]

View File

@ -82,7 +82,6 @@ module Discourse
# Activate observers that should always be running. # Activate observers that should always be running.
config.active_record.observers = [ config.active_record.observers = [
:user_email_observer,
:post_alert_observer, :post_alert_observer,
] ]

View File

@ -69,6 +69,7 @@ describe CategoryUser do
context 'integration' do context 'integration' do
before do before do
ActiveRecord::Base.observers.enable :all ActiveRecord::Base.observers.enable :all
NotificationEmailer.enable
end end
it 'should operate correctly' do it 'should operate correctly' do

View File

@ -3,6 +3,7 @@ require 'rails_helper'
describe Notification do describe Notification do
before do before do
ActiveRecord::Base.observers.enable :all ActiveRecord::Base.observers.enable :all
NotificationEmailer.enable
end end
it { is_expected.to validate_presence_of :notification_type } it { is_expected.to validate_presence_of :notification_type }
@ -139,21 +140,6 @@ describe Notification do
end end
end end
describe '@mention' do
it "calls email_user_mentioned on creating a notification" do
UserEmailObserver.any_instance.expects(:after_commit).with(instance_of(Notification))
Fabricate(:notification)
end
end
describe '@mention' do
it "calls email_user_quoted on creating a quote notification" do
UserEmailObserver.any_instance.expects(:after_commit).with(instance_of(Notification))
Fabricate(:quote_notification)
end
end
describe 'private message' do describe 'private message' do
before do before do
@ -250,6 +236,8 @@ describe Notification do
it 'deletes notifications if post is missing or deleted' do it 'deletes notifications if post is missing or deleted' do
ActiveRecord::Base.observers.disable :all ActiveRecord::Base.observers.disable :all
NotificationEmailer.disable
p = Fabricate(:post) p = Fabricate(:post)
p2 = Fabricate(:post) p2 = Fabricate(:post)

View File

@ -109,6 +109,7 @@ Spork.prefork do
ActiveRecord::Base.observers.disable :all ActiveRecord::Base.observers.disable :all
SearchIndexer.disable SearchIndexer.disable
UserActionCreator.disable UserActionCreator.disable
NotificationEmailer.disable
SiteSetting.provider.all.each do |setting| SiteSetting.provider.all.each do |setting|
SiteSetting.remove_override!(setting.name) SiteSetting.remove_override!(setting.name)

View File

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
describe UserEmailObserver do describe NotificationEmailer do
before do
NotificationEmailer.enable
end
let(:topic) { Fabricate(:topic) } let(:topic) { Fabricate(:topic) }
let(:post) { Fabricate(:post, topic: topic) } let(:post) { Fabricate(:post, topic: topic) }
@ -18,8 +22,8 @@ describe UserEmailObserver do
shared_examples "enqueue" do shared_examples "enqueue" do
it "enqueues a job for the email" do it "enqueues a job for the email" do
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type)) Jobs.expects(:enqueue_in).with(delay, :user_email, NotificationEmailer::EmailUser.notification_params(notification,type))
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
context "inactive user" do context "inactive user" do
@ -27,13 +31,13 @@ describe UserEmailObserver do
it "doesn't enqueue a job" do it "doesn't enqueue a job" do
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
it "enqueues a job if the user is staged" do it "enqueues a job if the user is staged" do
notification.user.staged = true notification.user.staged = true
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type)) Jobs.expects(:enqueue_in).with(delay, :user_email, NotificationEmailer::EmailUser.notification_params(notification,type))
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
end end
@ -46,7 +50,7 @@ describe UserEmailObserver do
it "doesn't enqueue a job" do it "doesn't enqueue a job" do
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
end end
@ -55,7 +59,7 @@ describe UserEmailObserver do
it "doesn't enqueue a job" do it "doesn't enqueue a job" do
Post.any_instance.expects(:post_type).returns(Post.types[:small_action]) Post.any_instance.expects(:post_type).returns(Post.types[:small_action])
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
end end
@ -68,7 +72,7 @@ describe UserEmailObserver do
it "doesn't enqueue a job if the user has mention emails disabled" do it "doesn't enqueue a job if the user has mention emails disabled" do
notification.user.user_option.update_columns(email_direct: false) notification.user.user_option.update_columns(email_direct: false)
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
end end
@ -78,7 +82,7 @@ describe UserEmailObserver do
it "doesn't enqueue a job if the user has private message emails disabled" do it "doesn't enqueue a job if the user has private message emails disabled" do
notification.user.user_option.update_columns(email_private_messages: false) notification.user.user_option.update_columns(email_private_messages: false)
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
end end
@ -92,8 +96,8 @@ describe UserEmailObserver do
it "enqueue a delayed job for users that are online" do it "enqueue a delayed job for users that are online" do
notification.user.last_seen_at = 1.minute.ago notification.user.last_seen_at = 1.minute.ago
Jobs.expects(:enqueue_in).with(delay, :user_email, UserEmailObserver::EmailUser.notification_params(notification,type)) Jobs.expects(:enqueue_in).with(delay, :user_email, NotificationEmailer::EmailUser.notification_params(notification,type))
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
end end
@ -140,7 +144,7 @@ describe UserEmailObserver do
it "doesn't enqueue a job for a small action" do it "doesn't enqueue a job for a small action" do
notification.data_hash["original_post_type"] = Post.types[:small_action] notification.data_hash["original_post_type"] = Post.types[:small_action]
Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never
UserEmailObserver.process_notification(notification) NotificationEmailer.process_notification(notification)
end end
end end