From 16fdcdfc00793c8b4e2f1423da5e1e266debe898 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 16 Nov 2016 14:17:47 +0800 Subject: [PATCH] FIX: Add conditions on when to show first pm notification. --- app/models/user.rb | 1 + spec/models/user_spec.rb | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index cec9a235c83..fa5aa1173dc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -344,6 +344,7 @@ class User < ActiveRecord::Base end def read_first_notification? + return true if (trust_level > TrustLevel[0] || created_at < 1.week.ago) notifications.order(created_at: :asc).first&.read || false end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5979cb8e4e7..bb61d6434c2 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1318,7 +1318,7 @@ describe User do end describe '#read_first_notification?' do - let(:user) { Fabricate(:user) } + let(:user) { Fabricate(:user, trust_level: TrustLevel[0]) } let(:notification) { Fabricate(:private_message_notification, user: user) } let(:other_notification) { Fabricate(:private_message_notification, user: user) } @@ -1336,7 +1336,7 @@ describe User do notification.update_attributes!(read: true) other_notification.update_attributes!(read: false) - expect(user.read_first_notification?).to eq(true) + expect(user.reload.read_first_notification?).to eq(true) end end @@ -1345,6 +1345,22 @@ describe User do expect(user.read_first_notification?).to eq(false) end end + + describe 'when user is not trust level 0' do + it 'should return the right value' do + user.update_attributes!(trust_level: TrustLevel[1]) + + expect(user.read_first_notification?).to eq(true) + end + end + + describe 'when user is an old user' do + it 'should return the right value' do + user.update_attributes!(created_at: 1.year.ago) + + expect(user.read_first_notification?).to eq(true) + end + end end describe "#featured_user_badges" do