diff --git a/app/models/post_alert_observer.rb b/app/models/post_alert_observer.rb index 20fa9e67c9b..ce2b366526a 100644 --- a/app/models/post_alert_observer.rb +++ b/app/models/post_alert_observer.rb @@ -35,7 +35,8 @@ class PostAlertObserver < ActiveRecord::Observer Notification.types[:liked], post, display_username: post_action.user.username, - post_action_id: post_action.id + post_action_id: post_action.id, + user_id: post_action.user_id ) end diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index e62c06c43b3..eaf1df3ee38 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -86,8 +86,13 @@ class PostAlerter # Make sure the user can see the post return unless Guardian.new(user).can_see?(post) + notifier_id = opts[:user_id] || post.user_id + # apply muting here - return if post.user_id && MutedUser.where(user_id: user.id, muted_user_id: post.user_id).exists? + return if notifier_id && MutedUser.where(user_id: user.id, muted_user_id: notifier_id) + .joins(:muted_user) + .where('NOT admin AND NOT moderator') + .exists? # skip if muted on the topic return if TopicUser.get(post.topic, user).try(:notification_level) == TopicUser.notification_levels[:muted] diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 3dc1d3b1078..49df9db989e 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -198,6 +198,28 @@ describe PostAction do end describe 'when a user likes something' do + + it 'should generate notifications correctly' do + ActiveRecord::Base.observers.enable :all + PostAction.act(codinghorror, post, PostActionType.types[:like]) + Notification.count.should == 1 + + mutee = Fabricate(:user) + + post = Fabricate(:post) + MutedUser.create!(user_id: post.user.id, muted_user_id: mutee.id) + PostAction.act(mutee, post, PostActionType.types[:like]) + + Notification.count.should == 1 + + # you can not mute admin, sorry + MutedUser.create!(user_id: post.user.id, muted_user_id: admin.id) + PostAction.act(admin, post, PostActionType.types[:like]) + + Notification.count.should == 2 + + end + it 'should increase the `like_count` and `like_score` when a user likes something' do PostAction.act(codinghorror, post, PostActionType.types[:like]) post.reload @@ -265,7 +287,7 @@ describe PostAction do # A post with no flags has 0 for flag counts expect(PostAction.flag_counts_for(post.id)).to eq([0, 0]) - flag = PostAction.act(eviltrout, post, PostActionType.types[:spam]) + _flag = PostAction.act(eviltrout, post, PostActionType.types[:spam]) expect(PostAction.flag_counts_for(post.id)).to eq([0, 1]) # If staff takes action, it is ranked higher @@ -367,7 +389,7 @@ describe PostAction do it "can flag the topic instead of a post" do post1 = create_post - post2 = create_post(topic: post1.topic) + _post2 = create_post(topic: post1.topic) post_action = PostAction.act(Fabricate(:user), post1, PostActionType.types[:spam], { flag_topic: true }) expect(post_action.targets_topic).to eq(true) end