A platform for community discussion. Free, open, simple.
PERF: Add Users to Group Pt. 1 (#38737)
Bannon Tanner595c5b3be341
diff --git a/app/jobs/regular/bulk_grant_trust_level.rb b/app/jobs/regular/bulk_grant_trust_level.rbindex 5a5ccecf7ee..ec75bfe1845 100644--- a/app/jobs/regular/bulk_grant_trust_level.rb+++ b/app/jobs/regular/bulk_grant_trust_level.rb@@ -3,13 +3,22 @@module Jobsclass BulkGrantTrustLevel < ::Jobs::Basedef execute(args)- trust_level = args[:trust_level]user_ids = args[:user_ids]+ trust_level = args[:trust_level]+ recalculate = args[:recalculate]- raise Discourse::InvalidParameters.new(:trust_level) if trust_level.blank?raise Discourse::InvalidParameters.new(:user_ids) if user_ids.blank?+ raise Discourse::InvalidParameters.new(:trust_level) if trust_level.blank? && !recalculate- User.where(id: user_ids).find_each { |user| TrustLevelGranter.grant(trust_level, user) }+ User+ .where(id: user_ids)+ .find_each do |user|+ if recalculate+ Promotion.recalculate(user, use_previous_trust_level: true)+ else+ TrustLevelGranter.grant(trust_level, user)+ end+ endendendenddiff --git a/app/models/category_user.rb b/app/models/category_user.rbindex 22232a7fb3c..e4588f13218 100644--- a/app/models/category_user.rb+++ b/app/models/category_user.rb@@ -109,9 +109,8 @@ class CategoryUser < ActiveRecord::Basebuilder.where("tu.topic_id = :topic_id", topic_id: topic_id)end- if user_id = opts[:user_id]- builder.where("tu.user_id = :user_id", user_id: user_id)- end+ user_ids = opts[:user_ids] || opts[:user_id]+ builder.where("tu.user_id IN (:user_ids)", user_ids:) if user_ids.present?builder.exec(tracking: notification_levels[:tracking],@@ -166,9 +165,10 @@ class CategoryUser < ActiveRecord::Basebuilder.where2("tu1.topic_id = :topic_id", topic_id: topic_id)end- if user_id = opts[:user_id]- builder.where("tu.user_id = :user_id", user_id: user_id)- builder.where2("tu1.user_id = :user_id", user_id: user_id)+ user_ids = opts[:user_ids] || opts[:user_id]+ if user_ids.present?+ builder.where("tu.user_id IN (:user_ids)", user_ids:)+ builder.where2("tu1.user_id IN (:user_ids)", user_ids:)endbuilder.exec(diff --git a/app/models/group.rb b/app/models/group.rbindex e720b05636c..783d56dc2be 100644--- a/app/models/group.rb+++ b/app/models/group.rb@@ -819,41 +819,15 @@ class Group < ActiveRecord::Basedef add(user, notify: false, automatic: false)return false if user.nil?- return self if group_users.exists?(user_id: user.id)-- self.users.push(user)- send_membership_notification(user) if notify- publish_category_updates(user)- trigger_user_added_event(user, automatic)+ added_ids = GroupManager.new(self).add([user.id], automatic:)+ send_membership_notification(user) if notify && !added_ids.empty?selfenddef remove(user)return false if user.nil?- group_user = self.group_users.find_by(user: user)- return false if group_user.blank?-- group_user.destroy- publish_category_updates(user)- trigger_user_removed_event(user)- enqueue_user_removed_from_group_webhook_events(group_user)-- true- end-- def enqueue_user_removed_from_group_webhook_events(group_user)- return if !WebHook.active_web_hooks(:group_user)-- payload = WebHook.generate_payload(:group_user, group_user, WebHookGroupUserSerializer)-- WebHook.enqueue_hooks(- :group_user,- :user_removed_from_group,- id: group_user.id,- payload: payload,- group_ids: [self.id],- )+ GroupManager.new(self).remove([user.id]).present?enddef trigger_user_added_event(user, automatic)@@ -881,16 +855,19 @@ class Group < ActiveRecord::Base).firstend- def bulk_add(user_ids)- return if user_ids.blank?+ def bulk_add(user_ids, automatic: false)+ return [] if user_ids.blank?++ added_user_ids = nilGroup.transaction dosql = <<~SQLINSERT INTO group_users- (group_id, user_id, created_at, updated_at)+ (group_id, user_id, notification_level, created_at, updated_at)SELECT- #{self.id},+ :group_id,u.id,+ :notification_level,CURRENT_TIMESTAMP,CURRENT_TIMESTAMPFROM users AS u@@ -900,41 +877,119 @@ class Group < ActiveRecord::BaseWHERE gu.user_id = u.id ANDgu.group_id = :group_id)+ RETURNING user_idSQL- DB.exec(sql, group_id: self.id, user_ids: user_ids)-- user_attributes = {}-- user_attributes[:primary_group_id] = self.id if self.primary_group?+ added_user_ids =+ DB.query_single(+ sql,+ group_id: self.id,+ user_ids: user_ids,+ notification_level: self.default_notification_level,+ )- user_attributes[:title] = self.title if self.title.present?+ return [] if added_user_ids.blank?++ if self.primary_group?+ User+ .where(id: added_user_ids)+ .where("flair_group_id IS NOT DISTINCT FROM primary_group_id")+ .update_all(flair_group_id: self.id)++ DB.exec(<<~SQL, user_ids: added_user_ids, new_title: self.title)+ UPDATE users u+ SET title = :new_title+ WHERE u.id IN (:user_ids)+ AND u.primary_group_id IS NOT NULL+ AND EXISTS (+ SELECT 1 FROM groups g+ WHERE g.id = u.primary_group_id+ AND g.title = u.title+ )+ SQL++ User.where(id: added_user_ids).update_all(primary_group_id: self.id)+ end- User.where(id: user_ids).update_all(user_attributes) if user_attributes.present?+ if self.title.present?+ User.where(id: added_user_ids, title: [nil, ""]).update_all(title: self.title)+ end- # update group user count- recalculate_user_count+ Group.update_counters(self.id, user_count: added_user_ids.size)end- if self.grant_trust_level.present?- Jobs.enqueue(:bulk_grant_trust_level, user_ids: user_ids, trust_level: self.grant_trust_level)+ if self.grant_trust_level.present? && !self.grant_trust_level.zero?+ Jobs.enqueue(+ :bulk_grant_trust_level,+ user_ids: added_user_ids,+ trust_level: self.grant_trust_level,+ )end- self+ GroupUser.bulk_set_category_notifications(self, added_user_ids)+ GroupUser.bulk_set_tag_notifications(self, added_user_ids)++ User.where(id: added_user_ids).find_each { |user| trigger_user_added_event(user, automatic) }++ bulk_publish_category_updates(added_user_ids)++ added_user_idsenddef bulk_remove(user_ids)+ return [] if user_ids.blank?++ group_users_to_remove = group_users.where(user_id: user_ids)+ return [] if group_users_to_remove.empty?++ removed_user_ids = group_users_to_remove.pluck(:user_id)++ webhook_payloads = build_user_removed_webhook_payloads(group_users_to_remove)+Group.transaction do- group_users_to_be_destroyed = group_users.includes(:user).where(user_id: user_ids).destroy_all- group_users_to_be_destroyed.each do |group_user|- trigger_user_removed_event(group_user.user)- enqueue_user_removed_from_group_webhook_events(group_user)+ group_users.where(user_id: removed_user_ids).delete_all++ User.where(primary_group_id: self.id, id: removed_user_ids).update_all(primary_group_id: nil)+ User.where(flair_group_id: self.id, id: removed_user_ids).update_all(flair_group_id: nil)++ if self.title.present?+ DB.exec(<<~SQL, user_ids: removed_user_ids, title: self.title)+ UPDATE users u+ SET title = NULL+ WHERE u.id IN (:user_ids)+ AND u.title = :title+ AND NOT EXISTS (+ SELECT 1 FROM group_users gu+ JOIN groups g ON g.id = gu.group_id+ WHERE gu.user_id = u.id+ AND g.title IS NOT NULL AND g.title <> ''+ )+ AND NOT EXISTS (+ SELECT 1 FROM user_badges ub+ JOIN badges b ON b.id = ub.badge_id+ WHERE ub.user_id = u.id+ AND b.allow_title = true+ )+ SQL++ User+ .where(id: removed_user_ids, title: self.title)+ .find_each { |user| user.update_column(:title, user.next_best_title) }end++ Group.update_counters(self.id, user_count: -removed_user_ids.size)+ end++ if self.grant_trust_level.present? && !self.grant_trust_level.zero?+ Jobs.enqueue(:bulk_grant_trust_level, user_ids: removed_user_ids, recalculate: true)end- recalculate_user_count+ bulk_publish_category_updates(removed_user_ids)- true+ User.where(id: removed_user_ids).find_each { |user| trigger_user_removed_event(user) }+ enqueue_user_removed_webhook_events(webhook_payloads)++ removed_user_idsenddef recalculate_user_count@@ -1236,6 +1291,43 @@ class Group < ActiveRecord::Baseprivate+ def bulk_publish_category_updates(user_ids)+ return if user_ids.blank?+ return unless categories.exists?++ if user_ids.size == 1+ user = User.find(user_ids.first)+ publish_category_updates(user)+ else+ Discourse.request_refresh!(user_ids:)+ end+ end++ def build_user_removed_webhook_payloads(group_users_relation)+ return unless WebHook.active_web_hooks(:group_user)++ payloads = []+ group_users_relation.find_each do |gu|+ payloads << {+ id: gu.id,+ payload: WebHook.generate_payload(:group_user, gu, WebHookGroupUserSerializer),+ }+ end+ payloads+ end++ def enqueue_user_removed_webhook_events(webhook_payloads)+ webhook_payloads&.each do |webhook_payload|+ WebHook.enqueue_hooks(+ :group_user,+ :user_removed_from_group,+ id: webhook_payload[:id],+ payload: webhook_payload[:payload],+ group_ids: [self.id],+ )+ end+ end+def send_membership_notification(user)Notification.create!(notification_type: Notification.types[:membership_request_accepted],diff --git a/app/models/group_user.rb b/app/models/group_user.rbindex 4772dff4484..dddd915f2d5 100644--- a/app/models/group_user.rb+++ b/app/models/group_user.rb@@ -138,37 +138,7 @@ class GroupUser < ActiveRecord::Baseenddef self.set_category_notifications(group, user)- group_levels =- group- .group_category_notification_defaults- .each_with_object({}) do |r, h|- h[r.notification_level] ||= []- h[r.notification_level] << r.category_id- end-- return if group_levels.empty?-- user_levels =- CategoryUser- .where(user_id: user.id)- .each_with_object({}) do |r, h|- h[r.notification_level] ||= []- h[r.notification_level] << r.category_id- end-- higher_level_category_ids = user_levels.values.flatten-- %i[muted regular tracking watching_first_post watching].each do |level|- level_num = NotificationLevels.all[level]- higher_level_category_ids -= (user_levels[level_num] || [])- if group_category_ids = group_levels[level_num]- CategoryUser.batch_set(- user,- level,- group_category_ids + (user_levels[level_num] || []) - higher_level_category_ids,- )- end- end+ bulk_set_category_notifications(group, [user.id])enddef set_tag_notifications@@ -176,37 +146,52 @@ class GroupUser < ActiveRecord::Baseenddef self.set_tag_notifications(group, user)- group_levels =- group- .group_tag_notification_defaults- .each_with_object({}) do |r, h|- h[r.notification_level] ||= []- h[r.notification_level] << r.tag_id- end-- return if group_levels.empty?-- user_levels =- TagUser- .where(user_id: user.id)- .each_with_object({}) do |r, h|- h[r.notification_level] ||= []- h[r.notification_level] << r.tag_id- end-- higher_level_tag_ids = user_levels.values.flatten-- %i[muted regular tracking watching_first_post watching].each do |level|- level_num = NotificationLevels.all[level]- higher_level_tag_ids -= (user_levels[level_num] || [])- if group_tag_ids = group_levels[level_num]- TagUser.batch_set(- user,- level,- group_tag_ids + (user_levels[level_num] || []) - higher_level_tag_ids,- )- end+ bulk_set_tag_notifications(group, [user.id])+ end++ def self.bulk_set_category_notifications(group, user_ids)+ defaults = group.group_category_notification_defaults.to_a+ return if defaults.empty?++ defaults.each do |default|+ DB.exec(+ <<~SQL,+ INSERT INTO category_users (user_id, category_id, notification_level)+ SELECT unnest(ARRAY[:user_ids]::int[]), :category_id, :notification_level+ ON CONFLICT (user_id, category_id) DO UPDATE+ SET notification_level = #{semantically_higher_notification_level_sql("EXCLUDED.notification_level", "category_users.notification_level")}+ SQL+ user_ids: user_ids,+ category_id: default.category_id,+ notification_level: default.notification_level,+ )end++ CategoryUser.auto_watch(user_ids: user_ids)+ CategoryUser.auto_track(user_ids: user_ids)+ end++ def self.bulk_set_tag_notifications(group, user_ids)+ defaults = group.group_tag_notification_defaults.to_a+ return if defaults.empty?++ defaults.each do |default|+ DB.exec(+ <<~SQL,+ INSERT INTO tag_users (user_id, tag_id, notification_level, created_at, updated_at)+ SELECT unnest(ARRAY[:user_ids]::int[]), :tag_id, :notification_level, NOW(), NOW()+ ON CONFLICT (user_id, tag_id) DO UPDATE+ SET notification_level = #{semantically_higher_notification_level_sql("EXCLUDED.notification_level", "tag_users.notification_level")},+ updated_at = NOW()+ SQL+ user_ids: user_ids,+ tag_id: default.tag_id,+ notification_level: default.notification_level,+ )+ end++ TagUser.auto_watch(user_ids: user_ids)+ TagUser.auto_track(user_ids: user_ids)enddef increase_group_user_count@@ -216,6 +201,18 @@ class GroupUser < ActiveRecord::Basedef decrease_group_user_countGroup.decrement_counter(:user_count, self.group_id)end++ def self.semantically_higher_notification_level_sql(new_col, existing_col)+ <<~SQL.squish+ CASE+ WHEN (CASE #{new_col} WHEN 3 THEN 5 ELSE #{new_col} END) >=+ (CASE #{existing_col} WHEN 3 THEN 5 ELSE #{existing_col} END)+ THEN #{new_col}+ ELSE #{existing_col}+ END+ SQL+ end+ private_class_method :semantically_higher_notification_level_sqlend# == Schema Informationdiff --git a/app/models/tag_user.rb b/app/models/tag_user.rbindex a8a9fc6e588..7b76fb7a73b 100644--- a/app/models/tag_user.rb+++ b/app/models/tag_user.rb@@ -171,9 +171,8 @@ class TagUser < ActiveRecord::Basebuilder.where("tu.topic_id = :topic_id", topic_id: topic_id)end- if user_id = opts[:user_id]- builder.where("tu.user_id = :user_id", user_id: user_id)- end+ user_ids = opts[:user_ids] || opts[:user_id]+ builder.where("tu.user_id IN (:user_ids)", user_ids:) if user_ids.present?builder.exec(watching: notification_levels[:watching],@@ -206,9 +205,8 @@ class TagUser < ActiveRecord::Basebuilder.where("tu.topic_id = :topic_id", topic_id: topic_id)end- if user_id = opts[:user_id]- builder.where("tu.user_id = :user_id", user_id: user_id)- end+ user_ids = opts[:user_ids] || opts[:user_id]+ builder.where("tu.user_id IN (:user_ids)", user_ids:) if user_ids.present?builder.exec(tracking: notification_levels[:tracking],diff --git a/lib/group_manager.rb b/lib/group_manager.rbnew file mode 100644index 00000000000..e1f2b4e3b48--- /dev/null+++ b/lib/group_manager.rb@@ -0,0 +1,19 @@+# frozen_string_literal: true++class GroupManager+ include HasErrors++ def initialize(group)+ @group = group+ end++ def add(user_ids, automatic: false)+ return [] if user_ids.blank?+ @group.bulk_add(user_ids, automatic:)+ end++ def remove(user_ids)+ return [] if user_ids.blank?+ @group.bulk_remove(user_ids)+ end+enddiff --git a/plugins/discourse-data-explorer/spec/guardian_spec.rb b/plugins/discourse-data-explorer/spec/guardian_spec.rbindex d06270dc46a..33bf60e032e 100644--- a/plugins/discourse-data-explorer/spec/guardian_spec.rb+++ b/plugins/discourse-data-explorer/spec/guardian_spec.rb@@ -15,8 +15,8 @@ describe Guardian doqueryend- let(:user) { build(:user) }- let(:admin) { build(:admin) }+ fab!(:user)+ fab!(:admin)fab!(:group)describe "#user_is_a_member_of_group?" dodiff --git a/plugins/discourse-policy/plugin.rb b/plugins/discourse-policy/plugin.rbindex 2160454be63..3b22e6b7666 100644--- a/plugins/discourse-policy/plugin.rb+++ b/plugins/discourse-policy/plugin.rb@@ -140,7 +140,9 @@ after_initialize doif post_policy.add_users_to_group.present?previously_accepted_users = post_policy.accepted_policy_users- Group.find_by(id: post_policy.add_users_to_group)&.remove(previously_accepted_users)+ Group.find_by(id: post_policy.add_users_to_group)&.bulk_remove(+ previously_accepted_users.pluck(:user_id),+ )endendenddiff --git a/spec/jobs/bulk_grant_trust_level_spec.rb b/spec/jobs/bulk_grant_trust_level_spec.rbindex b2963069da6..4ff7df975d1 100644--- a/spec/jobs/bulk_grant_trust_level_spec.rb+++ b/spec/jobs/bulk_grant_trust_level_spec.rb@@ -24,4 +24,15 @@ RSpec.describe Jobs::BulkGrantTrustLevel doexpect(user1.trust_level).to eq(3)expect(user2.trust_level).to eq(3)end++ it "recalculates trust level when recalculate is true" do+ group = Fabricate(:group, grant_trust_level: 3)+ user = Fabricate(:user, trust_level: 3)+ group.bulk_add([user.id])++ group.bulk_remove([user.id])+ Jobs::BulkGrantTrustLevel.new.execute(user_ids: [user.id], recalculate: true)++ expect(user.reload.trust_level).to eq(0)+ endenddiff --git a/spec/lib/group_manager_spec.rb b/spec/lib/group_manager_spec.rbnew file mode 100644index 00000000000..62ded50137c--- /dev/null+++ b/spec/lib/group_manager_spec.rb@@ -0,0 +1,48 @@+# frozen_string_literal: true++RSpec.describe GroupManager do+ fab!(:group)+ fab!(:user)+ fab!(:user2, :user)++ subject(:manager) { GroupManager.new(group) }++ describe "#add" do+ it "delegates to group.bulk_add and returns added user IDs" do+ result = manager.add([user.id, user2.id])++ expect(result).to contain_exactly(user.id, user2.id)+ expect(group.group_users.map(&:user_id)).to contain_exactly(user.id, user2.id)+ end++ it "returns empty array for blank input" do+ expect(manager.add([])).to eq([])+ expect(manager.add(nil)).to eq([])+ end++ it "passes automatic flag through to bulk_add" do+ events =+ DiscourseEvent.track_events(:user_added_to_group) do+ manager.add([user.id], automatic: true)+ end++ expect(events.first[:params][2][:automatic]).to eq(true)+ end+ end++ describe "#remove" do+ before { group.bulk_add([user.id, user2.id]) }++ it "delegates to group.bulk_remove and returns removed user IDs" do+ result = manager.remove([user.id, user2.id])++ expect(result).to contain_exactly(user.id, user2.id)+ expect(group.group_users.count).to eq(0)+ end++ it "returns empty array for blank input" do+ expect(manager.remove([])).to eq([])+ expect(manager.remove(nil)).to eq([])+ end+ end+enddiff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rbindex a40f65ffb0d..02a607bbd7a 100644--- a/spec/lib/guardian_spec.rb+++ b/spec/lib/guardian_spec.rb@@ -2753,10 +2753,9 @@ RSpec.describe Guardian dodescribe "#can_see_group?" doit "Correctly handles owner visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:owners])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:owners])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2770,10 +2769,9 @@ RSpec.describe Guardian doendit "Correctly handles staff visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:staff])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:staff])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2787,10 +2785,9 @@ RSpec.describe Guardian doendit "Correctly handles member visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:members])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:members])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2804,9 +2801,8 @@ RSpec.describe Guardian doendit "Correctly handles logged-on-user visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:logged_on_users])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:logged_on_users])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2828,10 +2824,9 @@ RSpec.describe Guardian dodescribe "#can_see_group_members?" doit "Correctly handles group members visibility for owner" do- group = Group.new(name: "group", members_visibility_level: Group.visibility_levels[:owners])+ group = Fabricate(:group, members_visibility_level: Group.visibility_levels[:owners])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2845,10 +2840,9 @@ RSpec.describe Guardian doendit "Correctly handles group members visibility for staff" do- group = Group.new(name: "group", members_visibility_level: Group.visibility_levels[:staff])+ group = Fabricate(:group, members_visibility_level: Group.visibility_levels[:staff])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2862,10 +2856,9 @@ RSpec.describe Guardian doendit "Correctly handles group members visibility for member" do- group = Group.new(name: "group", members_visibility_level: Group.visibility_levels[:members])+ group = Fabricate(:group, members_visibility_level: Group.visibility_levels[:members])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2879,13 +2872,8 @@ RSpec.describe Guardian doendit "Correctly handles group members visibility for logged-on-user" do- group =- Group.new(- name: "group",- members_visibility_level: Group.visibility_levels[:logged_on_users],- )+ group = Fabricate(:group, members_visibility_level: Group.visibility_levels[:logged_on_users])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2907,10 +2895,9 @@ RSpec.describe Guardian dodescribe "#can_see_groups?" doit "correctly handles owner visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:owners])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:owners])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2924,12 +2911,10 @@ RSpec.describe Guardian doendit "correctly handles the case where the user does not own every group" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:owners])- group2 = Group.new(name: "group2", visibility_level: Group.visibility_levels[:owners])- group2.save!+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:owners])+ group2 = Fabricate(:group, visibility_level: Group.visibility_levels[:owners])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2943,10 +2928,9 @@ RSpec.describe Guardian doendit "correctly handles staff visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:staff])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:staff])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2960,10 +2944,9 @@ RSpec.describe Guardian doendit "correctly handles member visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:members])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:members])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2977,10 +2960,9 @@ RSpec.describe Guardian doendit "correctly handles logged-on-user visible groups" do- group = Group.new(name: "group", visibility_level: Group.visibility_levels[:logged_on_users])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:logged_on_users])group.add(member)- group.save!group.add_owner(owner)group.reload@@ -2994,12 +2976,10 @@ RSpec.describe Guardian doendit "correctly handles the case where the user is not a member of every group" do- group1 = Group.new(name: "group", visibility_level: Group.visibility_levels[:members])- group2 = Group.new(name: "group2", visibility_level: Group.visibility_levels[:members])- group2.save!+ group1 = Fabricate(:group, visibility_level: Group.visibility_levels[:members])+ group2 = Fabricate(:group, visibility_level: Group.visibility_levels[:members])group1.add(member)- group1.save!group1.add_owner(owner)group1.reloaddiff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rbindex f87aadb6cbd..a4b87cc28d9 100644--- a/spec/lib/post_creator_spec.rb+++ b/spec/lib/post_creator_spec.rb@@ -1386,10 +1386,9 @@ RSpec.describe PostCreator dofab!(:target_user1) { coding_horror }fab!(:target_user2, :moderator)let!(:group) do- g = Fabricate.build(:group, messageable_level: Group::ALIAS_LEVELS[:everyone])+ g = Fabricate(:group, messageable_level: Group::ALIAS_LEVELS[:everyone])g.add(target_user1)g.add(target_user2)- g.savegendfab!(:unrelated, :user)diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex fd51990dd3d..337f2ca5fae 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -697,9 +697,8 @@ RSpec.describe Search do# can search group PMs as well as non adminuser = Fabricate(:user)- group = Fabricate.build(:group)+ group = Fabricate(:group)group.add(user)- group.save!TopicAllowedGroup.create!(group_id: group.id, topic_id: topic.id)diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rbindex 3728a37671a..7245b76133c 100644--- a/spec/models/group_spec.rb+++ b/spec/models/group_spec.rb@@ -818,6 +818,8 @@ RSpec.describe Group doenddescribe "trust level management" do+ before { Jobs.run_immediately! }+it "correctly grants a trust level to members" dogroup = Fabricate(:group, grant_trust_level: 2)u0 = Fabricate(:user, trust_level: 0)@@ -932,11 +934,10 @@ RSpec.describe Group doendit "correctly restricts group visibility" do- group = Fabricate.build(:group, visibility_level: Group.visibility_levels[:owners])+ group = Fabricate(:group, visibility_level: Group.visibility_levels[:owners])logged_on_user = Fabricate(:user)member = Fabricate(:user)group.add(member)- group.save!owner = Fabricate(:user)group.add_owner(owner)@@ -1000,11 +1001,10 @@ RSpec.describe Group doendit "correctly restricts group members visibility" do- group = Fabricate.build(:group, members_visibility_level: Group.visibility_levels[:owners])+ group = Fabricate(:group, members_visibility_level: Group.visibility_levels[:owners])logged_on_user = Fabricate(:user)member = Fabricate(:user)group.add(member)- group.save!owner = Fabricate(:user)group.add_owner(owner)@@ -1268,54 +1268,277 @@ RSpec.describe Group doenddescribe "#bulk_add" do- it "should be able to add multiple users" do- group.bulk_add([user.id, admin.id])+ it "adds multiple users and returns their IDs" do+ result = group.bulk_add([user.id, admin.id])+ expect(result).to contain_exactly(user.id, admin.id)expect(group.group_users.map(&:user_id)).to contain_exactly(user.id, admin.id)end+ it "returns empty array for blank input" do+ expect(group.bulk_add([])).to eq([])+ expect(group.bulk_add(nil)).to eq([])+ end++ it "skips users already in the group" do+ group.bulk_add([user.id])+ result = group.bulk_add([user.id, admin.id])++ expect(result).to eq([admin.id])+ end++ it "sets notification_level to group default" do+ group.update!(default_notification_level: NotificationLevels.all[:watching])+ group.bulk_add([user.id])++ expect(GroupUser.find_by(group: group, user: user).notification_level).to eq(+ NotificationLevels.all[:watching],+ )+ end+it "updates group user count" do- expect {- group.bulk_add([user.id, admin.id])- group.reload- }.to change { group.user_count }.from(0).to(2)+ expect { group.bulk_add([user.id, admin.id]) }.to change { group.reload.user_count }.from(+ 0,+ ).to(2)+ end++ it "grants title to users without one" do+ group.update!(title: "Awesome")+ group.bulk_add([user.id])++ expect(user.reload.title).to eq("Awesome")+ end++ it "does not overwrite an existing title" do+ user.update!(title: "Already Great")+ group.update!(title: "Awesome")+ group.bulk_add([user.id])++ expect(user.reload.title).to eq("Already Great")+ end++ it "sets primary_group_id and flair_group_id when group is primary" do+ group.update!(primary_group: true)+ group.bulk_add([user.id])++ user.reload+ expect(user.primary_group_id).to eq(group.id)+ expect(user.flair_group_id).to eq(group.id)+ end++ it "replaces title from old primary group with new primary group title" do+ old_group = Fabricate(:group, primary_group: true, title: "Old Title")+ old_group.bulk_add([user.id])+ expect(user.reload.title).to eq("Old Title")++ group.update!(primary_group: true, title: "New Title")+ group.bulk_add([user.id])++ expect(user.reload.title).to eq("New Title")+ expect(user.primary_group_id).to eq(group.id)+ end++ it "enqueues bulk_grant_trust_level job when group grants trust level" do+ group.update!(grant_trust_level: 2)++ expect_enqueued_with(+ job: :bulk_grant_trust_level,+ args: {+ trust_level: 2,+ user_ids: [user.id, admin.id],+ },+ ) { group.bulk_add([user.id, admin.id]) }+ end++ it "does not enqueue trust level job when grant_trust_level is nil" do+ expect_not_enqueued_with(job: :bulk_grant_trust_level) { group.bulk_add([user.id]) }+ end++ it "does not enqueue trust level job when grant_trust_level is zero" do+ group.update!(grant_trust_level: 0)+ expect_not_enqueued_with(job: :bulk_grant_trust_level) { group.bulk_add([user.id]) }+ end++ it "triggers user_added_to_group event for each user" do+ user_ids = [user.id, admin.id]+ events = DiscourseEvent.track_events(:user_added_to_group) { group.bulk_add(user_ids) }++ expect(events.size).to eq(2)+ expect(events.map { |e| e[:params][0] }).to contain_exactly(user, admin)+ end++ it "passes automatic flag through to the event" do+ events =+ DiscourseEvent.track_events(:user_added_to_group) do+ group.bulk_add([user.id], automatic: true)+ end++ expect(events.first[:params][2][:automatic]).to eq(true)+ end++ it "sets category notification defaults" do+ category = Fabricate(:category)+ group.update!(watching_category_ids: [category.id])++ group.bulk_add([user.id])++ expect(CategoryUser.find_by(user: user, category: category).notification_level).to eq(+ CategoryUser.notification_levels[:watching],+ )+ end++ it "sets tag notification defaults" do+ tag = Fabricate(:tag)+ group.update!(watching_tags: [tag.name])++ group.bulk_add([user.id])++ expect(TagUser.find_by(user: user, tag: tag).notification_level).to eq(+ TagUser.notification_levels[:watching],+ )+ end++ it "publishes category updates for a single added user" do+ category = Fabricate(:category)+ group.update!(categories: [category])++ message = MessageBus.track_publish("/categories") { group.bulk_add([user.id]) }.first++ expect(message).to be_present+ expect(message.user_ids).to eq([user.id])+ end++ it "publishes a refresh when adding multiple users" do+ category = Fabricate(:category)+ group.update!(categories: [category])++ message =+ MessageBus.track_publish("/refresh_client") { group.bulk_add([user.id, admin.id]) }.first++ expect(message.user_ids).to contain_exactly(user.id, admin.id)endenddescribe "#bulk_remove" do- it "removes multiple users from the group and doesn't error with user_ids not present" do- group.bulk_add([user.id, admin.id])+ before { group.bulk_add([user.id, admin.id]) }++ it "removes multiple users and returns their IDs" do+ result = group.bulk_remove([user.id, admin.id])- group.bulk_remove([user.id, admin.id, admin.id + 1])+ expect(result).to contain_exactly(user.id, admin.id)+ expect(group.group_users.count).to eq(0)+ end- expect(group.group_users.count).to be_zero+ it "returns empty array for blank input" do+ expect(group.bulk_remove([])).to eq([])+ expect(group.bulk_remove(nil)).to eq([])endit "updates group user count" do- group.bulk_add([user.id, admin.id])- expect(group.reload.user_count).to eq(2)+ group.reload+ expect { group.bulk_remove([user.id]) }.to change { group.reload.user_count }.from(2).to(1)+ end++ it "ignores user_ids not in the group" do+ result = group.bulk_remove([user.id, admin.id, admin.id + 1000])- group.bulk_remove([user.id, admin.id])- expect(group.reload.user_count).to eq(0)+ expect(result).to contain_exactly(user.id, admin.id)+ end++ it "clears primary_group_id" do+ group.update!(primary_group: true)+ User.where(id: [user.id, admin.id]).update_all(primary_group_id: group.id)++ group.bulk_remove([user.id])++ expect(user.reload.primary_group_id).to be_nil+ expect(admin.reload.primary_group_id).to eq(group.id)+ end++ it "clears flair_group_id" do+ User.where(id: user.id).update_all(flair_group_id: group.id)++ group.bulk_remove([user.id])++ expect(user.reload.flair_group_id).to be_nil+ end++ context "when stripping title" do+ before { group.update!(title: "Awesome") }++ it "clears title when user has no other titled groups or badges" do+ user.update!(title: "Awesome")++ group.bulk_remove([user.id])++ expect(user.reload.title).to be_nil+ end++ it "does not clear title if it doesn't match the group title" do+ user.update!(title: "Something Else")++ group.bulk_remove([user.id])++ expect(user.reload.title).to eq("Something Else")+ end++ it "assigns title from remaining group when removed from current title group" do+ other_group = Fabricate(:group, title: "Other Title")+ other_group.bulk_add([user.id])+ user.update!(title: "Awesome")++ group.bulk_remove([user.id])++ expect(user.reload.title).to eq("Other Title")+ end+ end++ it "enqueues bulk_grant_trust_level job with recalculate flag" do+ group.update!(grant_trust_level: 2)++ expect_enqueued_with(+ job: :bulk_grant_trust_level,+ args: {+ recalculate: true,+ user_ids: [user.id],+ },+ ) { group.bulk_remove([user.id]) }+ end++ it "triggers user_removed_from_group event for each user" do+ events =+ DiscourseEvent.track_events(:user_removed_from_group) do+ group.bulk_remove([user.id, admin.id])+ end++ expect(events.size).to eq(2)+ expect(events.map { |e| e[:params][0] }).to contain_exactly(user, admin)+ end++ it "publishes category updates for removed users" do+ category = Fabricate(:category)+ group.update!(categories: [category])++ message =+ MessageBus.track_publish("/refresh_client") { group.bulk_remove([user.id, admin.id]) }.first++ expect(message.user_ids).to contain_exactly(user.id, admin.id)enddescribe "with webhook" dofab!(:group_user_web_hook)- it "Enqueues user_removed_from_group webhook events for each group_user" do- group.bulk_add([user.id, admin.id])-+ it "enqueues webhook events for each removed user" dogroup.bulk_remove([user.id, admin.id])- Jobs::EmitWebHookEvent- .jobs- .last(2)- .each do |event|- job_args = event["args"].first- expect(job_args["event_name"]).to eq("user_removed_from_group")- payload = JSON.parse(job_args["payload"])- expect(payload["group_id"]).to eq(group.id)- expect([user.id, admin.id]).to include(payload["user_id"])- end++ webhook_jobs = Jobs::EmitWebHookEvent.jobs.last(2)+ webhook_jobs.each do |event|+ job_args = event["args"].first+ expect(job_args["event_name"]).to eq("user_removed_from_group")+ payload = JSON.parse(job_args["payload"])+ expect(payload["group_id"]).to eq(group.id)+ expect([user.id, admin.id]).to include(payload["user_id"])+ endendendenddiff --git a/spec/models/group_user_spec.rb b/spec/models/group_user_spec.rbindex d5cb3ee73af..6f0aa0f5f69 100644--- a/spec/models/group_user_spec.rb+++ b/spec/models/group_user_spec.rb@@ -354,4 +354,124 @@ RSpec.describe GroupUser doPromotion.expects(:recalculate).nevergroup_user.destroy!end++ describe ".bulk_set_category_notifications" do+ fab!(:group)+ fab!(:user1, :user)+ fab!(:user2, :user)+ fab!(:category1, :category)+ fab!(:category2, :category)++ it "does nothing when group has no category notification defaults" do+ expect { GroupUser.bulk_set_category_notifications(group, [user1.id]) }.not_to change {+ CategoryUser.count+ }+ end++ it "sets category notifications for multiple users" do+ group.watching_category_ids = [category1.id]+ group.tracking_category_ids = [category2.id]+ group.save!++ GroupUser.bulk_set_category_notifications(group, [user1.id, user2.id])++ expect(+ CategoryUser.where(user_id: user1.id, category_id: category1.id).first.notification_level,+ ).to eq(CategoryUser.notification_levels[:watching])+ expect(+ CategoryUser.where(user_id: user1.id, category_id: category2.id).first.notification_level,+ ).to eq(CategoryUser.notification_levels[:tracking])+ expect(+ CategoryUser.where(user_id: user2.id, category_id: category1.id).first.notification_level,+ ).to eq(CategoryUser.notification_levels[:watching])+ expect(+ CategoryUser.where(user_id: user2.id, category_id: category2.id).first.notification_level,+ ).to eq(CategoryUser.notification_levels[:tracking])+ end++ it "keeps the semantically higher notification level on conflict" do+ CategoryUser.create!(+ user_id: user1.id,+ category_id: category1.id,+ notification_level: CategoryUser.notification_levels[:watching],+ )+ CategoryUser.create!(+ user_id: user2.id,+ category_id: category1.id,+ notification_level: CategoryUser.notification_levels[:watching],+ )++ group.tracking_category_ids = [category1.id]+ group.save!++ GroupUser.bulk_set_category_notifications(group, [user1.id, user2.id])++ expect(+ CategoryUser.where(user_id: user1.id, category_id: category1.id).first.notification_level,+ ).to eq(CategoryUser.notification_levels[:watching])+ expect(+ CategoryUser.where(user_id: user2.id, category_id: category1.id).first.notification_level,+ ).to eq(CategoryUser.notification_levels[:watching])+ end+ end++ describe ".bulk_set_tag_notifications" do+ fab!(:group)+ fab!(:user1, :user)+ fab!(:user2, :user)+ fab!(:tag1, :tag)+ fab!(:tag2, :tag)++ it "does nothing when group has no tag notification defaults" do+ expect { GroupUser.bulk_set_tag_notifications(group, [user1.id]) }.not_to change {+ TagUser.count+ }+ end++ it "sets tag notifications for multiple users" do+ group.watching_tags = [tag1.name]+ group.tracking_tags = [tag2.name]+ group.save!++ GroupUser.bulk_set_tag_notifications(group, [user1.id, user2.id])++ expect(TagUser.where(user_id: user1.id, tag_id: tag1.id).first.notification_level).to eq(+ TagUser.notification_levels[:watching],+ )+ expect(TagUser.where(user_id: user1.id, tag_id: tag2.id).first.notification_level).to eq(+ TagUser.notification_levels[:tracking],+ )+ expect(TagUser.where(user_id: user2.id, tag_id: tag1.id).first.notification_level).to eq(+ TagUser.notification_levels[:watching],+ )+ expect(TagUser.where(user_id: user2.id, tag_id: tag2.id).first.notification_level).to eq(+ TagUser.notification_levels[:tracking],+ )+ end++ it "keeps the semantically higher notification level on conflict" do+ TagUser.create!(+ user_id: user1.id,+ tag_id: tag1.id,+ notification_level: TagUser.notification_levels[:watching],+ )+ TagUser.create!(+ user_id: user2.id,+ tag_id: tag1.id,+ notification_level: TagUser.notification_levels[:watching],+ )++ group.tracking_tags = [tag1.name]+ group.save!++ GroupUser.bulk_set_tag_notifications(group, [user1.id, user2.id])++ expect(TagUser.where(user_id: user1.id, tag_id: tag1.id).first.notification_level).to eq(+ TagUser.notification_levels[:watching],+ )+ expect(TagUser.where(user_id: user2.id, tag_id: tag1.id).first.notification_level).to eq(+ TagUser.notification_levels[:watching],+ )+ end+ endenddiff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex c52d9cbb82f..254c5af4b11 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -2066,6 +2066,8 @@ RSpec.describe User doendit "get attributes from the group" do+ Jobs.run_immediately!+user =Fabricate.build(:user,diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rbindex a5896bd3bb8..25b9fbe82b1 100644--- a/spec/models/web_hook_spec.rb+++ b/spec/models/web_hook_spec.rb@@ -664,7 +664,7 @@ RSpec.describe WebHook doexpect(payload["group_id"]).to eq(group.id)expect(payload["user_id"]).to eq(user.id)expect(payload["notification_level"]).to eq(group.default_notification_level)- expect(Time.zone.parse(payload["created_at"]).to_f).to be_within(0.001).of(now.to_f)+ expect(Time.zone.parse(payload["created_at"])).to be_within_one_second_of(now)endit "should enqueue the right hooks for group user deletion" dodiff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rbindex a7b11fa824e..9d296d1fddf 100644--- a/spec/requests/groups_controller_spec.rb+++ b/spec/requests/groups_controller_spec.rb@@ -732,7 +732,7 @@ RSpec.describe GroupsController dofreeze_time 1.day.from_now4.times { group.add(Fabricate(:user)) }- usernames = group.users.map { |m| m.username }.sort+ usernames = group.reload.users.map { |m| m.username }.sortget "/groups/#{group.name}/members.json", params: { limit: 3, asc: true }@@ -753,7 +753,9 @@ RSpec.describe GroupsController doget "/groups/#{group.name}/members.json", params: { order: "added_at" }members = response.parsed_body["members"]- expect(members.last["added_at"]).to eq(first_user.created_at.as_json)+ expect(Time.zone.parse(members.last["added_at"])).to be_within_one_second_of(+ first_user.created_at,+ )endit "can sort items" dodiff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rbindex 5bdbcb30477..54cab2b2966 100644--- a/spec/serializers/current_user_serializer_spec.rb+++ b/spec/serializers/current_user_serializer_spec.rb@@ -108,18 +108,16 @@ RSpec.describe CurrentUserSerializer dodescribe "#groups" doit "should only show visible groups" do- Fabricate.build(:group, visibility_level: Group.visibility_levels[:public])- hidden_group = Fabricate.build(:group, visibility_level: Group.visibility_levels[:owners])+ Fabricate(:group, visibility_level: Group.visibility_levels[:public])+ hidden_group = Fabricate(:group, visibility_level: Group.visibility_levels[:owners])public_group =- Fabricate.build(+ Fabricate(:group,visibility_level: Group.visibility_levels[:public],name: "UppercaseGroupName",)hidden_group.add(user)- hidden_group.save!public_group.add(user)- public_group.save!payload = serializer.as_jsonexpect(payload[:groups]).to contain_exactly(