From 42e0aaed613a88ae5682cd0ae0f57a0083d1a52a Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 5 Mar 2018 13:35:15 +0800 Subject: [PATCH] FIX: Set first visit PM notification level to group default notification level. https://meta.discourse.org/t/notifications-not-received-for-private-messages-im-invited-to/71577/21?u=tgxworld --- app/models/topic_user.rb | 13 ++++++++++++- spec/models/topic_user_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 060b4cbefb6..a6cff083ded 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -194,7 +194,18 @@ SQL notification_type: Notification.types[:invited_to_private_message] ).exists? - attrs[:notification_level] = notification_levels[:watching] + group_notification_level = Group + .joins("LEFT OUTER JOIN group_users gu ON gu.group_id = groups.id AND gu.user_id = #{user_id}") + .joins("LEFT OUTER JOIN topic_allowed_groups tag ON tag.topic_id = #{topic_id}") + .where("gu.id IS NOT NULL AND tag.id IS NOT NULL") + .pluck(:default_notification_level) + .first + + if group_notification_level.present? + attrs[:notification_level] = group_notification_level + else + attrs[:notification_level] = notification_levels[:watching] + end 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 diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb index 65438aafdb1..0e9351fd849 100644 --- a/spec/models/topic_user_spec.rb +++ b/spec/models/topic_user_spec.rb @@ -267,6 +267,31 @@ describe TopicUser do expect(TopicUser.get(topic, another_user).notification_level) .to eq(TopicUser.notification_levels[:regular]) end + + describe 'inviting a group' do + let(:group) do + Fabricate(:group, + default_notification_level: NotificationLevels.topic_levels[:tracking] + ) + end + + it "should use group's default notification level" do + another_user = Fabricate(:user) + group.add(another_user) + topic.invite_group(target_user, group) + TopicUser.track_visit!(topic.id, another_user.id) + + expect(TopicUser.get(topic, another_user).notification_level) + .to eq(TopicUser.notification_levels[:tracking]) + + 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]) + end + end end context 'auto tracking' do