diff --git a/app/models/notification.rb b/app/models/notification.rb index db91c05c6..bd172edf7 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -25,9 +25,13 @@ class Notification < ActiveRecord::Base else n = note_type.make_notification(recipient, target, actor, note_type) end - n.email_the_user(target, actor) if n - n.socket_to_user(recipient, :actor => actor) if n - n + if n + n.email_the_user(target, actor) if n + n.socket_to_user(recipient, :actor => actor) if n + n + else + nil + end end end end @@ -43,6 +47,7 @@ class Notification < ActiveRecord::Base private def self.concatenate_or_create(recipient, target, actor, notification_type) + return nil if post_visiblity_is_hidden?(recipient, target) if n = notification_type.where(:target_id => target.id, :target_type => target.class.base_class, :recipient_id => recipient.id, @@ -61,7 +66,9 @@ private end end + def self.make_notification(recipient, target, actor, notification_type) + return nil if post_visiblity_is_hidden?(recipient, target) n = notification_type.new(:target => target, :recipient_id => recipient.id) n.actors = n.actors | [actor] @@ -69,4 +76,11 @@ private n.save! n end + + def self.post_visiblity_is_hidden?(recipient, post) + contact = recipient.contact_for(post.author) + return false unless contact && recipient && post + pv = PostVisibility.where(:contact_id => contact.id, :post_id => post.id).first + pv.present? ? pv.hidden? : false + end end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 4a4fd5a5c..e8d8960ac 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -55,6 +55,13 @@ describe Notification do Notification.notify(@user, @sm, @person) end + it 'does not create a notification if the post visibility is hidden' do + Notification.stub(:post_visiblity_is_hidden).and_return(true) + expect{ + Notification.notify(@user, @sm, @person) + }.to change(Notification, :count).by(0) + end + context 'with a request' do before do @request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect)