FIX: only send email notifications for regular and whisper type of posts

This commit is contained in:
Régis Hanol 2016-01-19 01:39:57 +01:00
parent 02d9ec5bde
commit 61cc776fdd
2 changed files with 26 additions and 7 deletions

View file

@ -42,18 +42,24 @@ class UserEmailObserver < ActiveRecord::Observer
private private
EMAILABLE_POST_TYPES ||= Set.new [Post.types[:regular], Post.types[:whisper]]
def enqueue(type, delay=default_delay) 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, Jobs.enqueue_in(delay,
:user_email, :user_email,
type: type, type: type,
user_id: notification.user_id, user_id: notification.user_id,
notification_id: notification.id) notification_id: notification.id)
end end
def enqueue_private(type, delay=default_delay) 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, Jobs.enqueue_in(delay,
:user_email, :user_email,

View file

@ -2,10 +2,13 @@ require 'rails_helper'
describe UserEmailObserver do describe UserEmailObserver do
let(:topic) { Fabricate(:topic) }
let(:post) { Fabricate(:post, topic: topic) }
# something is off with fabricator # something is off with fabricator
def create_notification(type, user=nil) def create_notification(type, user=nil)
user ||= Fabricate(:user) 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 end
shared_examples "enqueue" do shared_examples "enqueue" do
@ -32,6 +35,16 @@ describe UserEmailObserver do
end 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 end
shared_examples "enqueue_public" do shared_examples "enqueue_public" do