diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 2d54204d9..13e23d22d 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -265,7 +265,11 @@ class PostCreator return unless @topic.private_message? unless @topic.topic_allowed_users.where(user_id: @user.id).exists? - @topic.topic_allowed_users.create!(user_id: @user.id) + unless @topic.topic_allowed_groups.where('group_id IN ( + SELECT group_id FROM group_users where user_id = ? + )',@user.id).exists? + @topic.topic_allowed_users.create!(user_id: @user.id) + end end end diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 958cdbd8e..b3195b3c5 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -490,6 +490,16 @@ describe PostCreator do expect(post.topic.topic_allowed_users.where(user_id: admin.id).count).to eq(1) expect(UserArchivedMessage.where(user_id: target_user2.id, topic_id: post.topic_id).count).to eq(0) + + # if another admin replies and is already member of the group, don't add them to topic_allowed_users + group = Fabricate(:group) + post.topic.topic_allowed_groups.create!(group: group) + admin2 = Fabricate(:admin) + group.add(admin2) + + PostCreator.create(admin2, raw: 'I am also an admin, and a mod', topic_id: post.topic_id) + + expect(post.topic.topic_allowed_users.where(user_id: admin2.id).count).to eq(0) end end