diff --git a/app/models/topic.rb b/app/models/topic.rb index 8533894aa0f..26a310e6b49 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -719,6 +719,21 @@ SQL # ensure all the notifications are out PostAlerter.new.after_save_post(last_post) add_small_action(user, "invited_group", group.name) + + group.users.where( + "group_users.notification_level > ?", NotificationLevels.all[:muted] + ).find_each do |u| + + u.notifications.create!( + notification_type: Notification.types[:invited_to_private_message], + topic_id: self.id, + post_number: 1, + data: { + topic_title: self.title, + display_username: user.username + }.to_json + ) + end end true diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 1cee451bafe..207bee1efb2 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -439,22 +439,42 @@ describe Topic do let(:walter) { Fabricate(:walter_white) } context 'by group name' do + let(:group) { Fabricate(:group) } it 'can add admin to allowed groups' do admins = Group[:admins] - admins.alias_level = Group::ALIAS_LEVELS[:everyone] - admins.save + admins.update!(alias_level: Group::ALIAS_LEVELS[:everyone]) expect(topic.invite_group(topic.user, admins)).to eq(true) - expect(topic.allowed_groups.include?(admins)).to eq(true) - expect(topic.remove_allowed_group(topic.user, 'admins')).to eq(true) - topic.reload - expect(topic.allowed_groups.include?(admins)).to eq(false) end + it 'creates a notification for each user in the group' do + user = Fabricate(:user) + user_2 = Fabricate(:user) + Fabricate(:post, topic: topic) + + group.add(user) + group.add(user_2) + + group.group_users.find_by(user: user_2).update!( + notification_level: NotificationLevels.all[:muted] + ) + + expect { topic.invite_group(topic.user, group) } + .to change { Notification.count }.by(1) + + notification = Notification.last + + expect(notification.user).to eq(user) + expect(notification.topic).to eq(topic) + + expect(notification.notification_type) + .to eq(Notification.types[:invited_to_private_message]) + end + end context 'by username' do