diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index f620c4648a4..060b4cbefb6 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -187,15 +187,30 @@ SQL end unless attrs[:notification_level] - auto_track_after = UserOption.where(user_id: user_id).pluck(:auto_track_topics_after_msecs).first - auto_track_after ||= SiteSetting.default_other_auto_track_topics_after_msecs + if Topic.private_messages.where(id: topic_id).exists? && + Notification.where( + user_id: user_id, + topic_id: topic_id, + notification_type: Notification.types[:invited_to_private_message] + ).exists? - if auto_track_after >= 0 && auto_track_after <= (attrs[:total_msecs_viewed].to_i || 0) - attrs[:notification_level] ||= notification_levels[:tracking] + attrs[:notification_level] = notification_levels[:watching] + else + auto_track_after = UserOption.where(user_id: user_id).pluck(:auto_track_topics_after_msecs).first + auto_track_after ||= SiteSetting.default_other_auto_track_topics_after_msecs + + if auto_track_after >= 0 && auto_track_after <= (attrs[:total_msecs_viewed].to_i || 0) + attrs[:notification_level] ||= notification_levels[:tracking] + end end end - TopicUser.create(attrs.merge!(user_id: user_id, topic_id: topic_id, first_visited_at: now , last_visited_at: now)) + TopicUser.create!(attrs.merge!( + user_id: user_id, + topic_id: topic_id, + first_visited_at: now , + last_visited_at: now + )) end def track_visit!(topic_id, user_id) diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb index 1c47b31a8b2..65438aafdb1 100644 --- a/spec/models/topic_user_spec.rb +++ b/spec/models/topic_user_spec.rb @@ -234,13 +234,38 @@ describe TopicUser do end context 'private messages' do + let(:target_user) { Fabricate(:user) } + + let(:post) do + create_post( + archetype: Archetype.private_message, + target_usernames: target_user.username + ); + end + + let(:topic) { post.topic } + it 'should ensure recepients and senders are watching' do + expect(TopicUser.get(topic, post.user).notification_level) + .to eq(TopicUser.notification_levels[:watching]) - target_user = Fabricate(:user) - post = create_post(archetype: Archetype.private_message, target_usernames: target_user.username); + expect(TopicUser.get(topic, target_user).notification_level) + .to eq(TopicUser.notification_levels[:watching]) + end - expect(TopicUser.get(post.topic, post.user).notification_level).to eq(TopicUser.notification_levels[:watching]) - expect(TopicUser.get(post.topic, target_user).notification_level).to eq(TopicUser.notification_levels[:watching]) + it 'should ensure invited user is watching once visited' do + another_user = Fabricate(:user) + topic.invite(target_user, another_user.username) + TopicUser.track_visit!(topic.id, another_user.id) + + expect(TopicUser.get(topic, another_user).notification_level) + .to eq(TopicUser.notification_levels[:watching]) + + another_user = Fabricate(:user) + TopicUser.track_visit!(topic.id, another_user.id) + + expect(TopicUser.get(topic, another_user).notification_level) + .to eq(TopicUser.notification_levels[:regular]) end end