From 61cc776fdd08bc692ea2391a10d84c06c096906a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 19 Jan 2016 01:39:57 +0100 Subject: [PATCH] FIX: only send email notifications for regular and whisper type of posts --- app/models/user_email_observer.rb | 18 ++++++++++++------ spec/models/user_email_observer_spec.rb | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/models/user_email_observer.rb b/app/models/user_email_observer.rb index e19c51e2f..67bcc5d6f 100644 --- a/app/models/user_email_observer.rb +++ b/app/models/user_email_observer.rb @@ -42,18 +42,24 @@ class UserEmailObserver < ActiveRecord::Observer private + EMAILABLE_POST_TYPES ||= Set.new [Post.types[:regular], Post.types[:whisper]] + def enqueue(type, delay=default_delay) - return unless notification.user.email_direct? && (notification.user.active? || notification.user.staged?) + return unless notification.user.email_direct? + return unless notification.user.active? || notification.user.staged? + return unless EMAILABLE_POST_TYPES.include? notification.post.try(:post_type) Jobs.enqueue_in(delay, - :user_email, - type: type, - user_id: notification.user_id, - notification_id: notification.id) + :user_email, + type: type, + user_id: notification.user_id, + notification_id: notification.id) end def enqueue_private(type, delay=default_delay) - return unless notification.user.email_private_messages? && (notification.user.active? || notification.user.staged?) + return unless notification.user.email_private_messages? + return unless notification.user.active? || notification.user.staged? + return unless EMAILABLE_POST_TYPES.include? notification.post.try(:post_type) Jobs.enqueue_in(delay, :user_email, diff --git a/spec/models/user_email_observer_spec.rb b/spec/models/user_email_observer_spec.rb index 1fb5be42b..9dfe1cdae 100644 --- a/spec/models/user_email_observer_spec.rb +++ b/spec/models/user_email_observer_spec.rb @@ -2,10 +2,13 @@ require 'rails_helper' describe UserEmailObserver do + let(:topic) { Fabricate(:topic) } + let(:post) { Fabricate(:post, topic: topic) } + # something is off with fabricator def create_notification(type, user=nil) user ||= Fabricate(:user) - Notification.create(data: '', user: user, notification_type: type) + Notification.create(data: '', user: user, notification_type: type, topic: topic, post_number: post.post_number) end shared_examples "enqueue" do @@ -32,6 +35,16 @@ describe UserEmailObserver do end + context "small action" do + + it "doesn't enqueue a job" do + Post.any_instance.expects(:post_type).returns(Post.types[:small_action]) + Jobs.expects(:enqueue_in).with(delay, :user_email, has_entry(type: type)).never + UserEmailObserver.send(:new).after_commit(notification) + end + + end + end shared_examples "enqueue_public" do