Current user serializer groups (ef7f84b follow-up)

Current user serializer groups (ef7f84b follow-up)
diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6
index f1f2275..d6ff8c3 100644
--- a/app/assets/javascripts/discourse/models/user.js.es6
+++ b/app/assets/javascripts/discourse/models/user.js.es6
@@ -716,6 +716,16 @@ User.reopenClass(Singleton, {
   // TODO: Use app.register and junk Singleton
   createCurrent() {
     const userJson = PreloadStore.get("currentUser");
+
+    if (userJson && userJson.primary_group_id) {
+      const primaryGroup = userJson.groups.find(
+        group => group.id === userJson.primary_group_id
+      );
+      if (primaryGroup) {
+        userJson.primary_group_name = primaryGroup.name;
+      }
+    }
+
     if (userJson) {
       const store = Discourse.__container__.lookup("service:store");
       return store.createRecord("user", userJson);
diff --git a/app/models/user.rb b/app/models/user.rb
index 69ec48c..1f47eaa 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -353,6 +353,10 @@ class User < ActiveRecord::Base
       .maximum("groups.grant_trust_level")
   end
 
+  def visible_groups
+    groups.visible_groups(self)
+  end
+
   def enqueue_welcome_message(message_type)
     return unless SiteSetting.send_welcome_message?
     Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb
index 27eb350..1f65e47 100644
--- a/app/serializers/current_user_serializer.rb
+++ b/app/serializers/current_user_serializer.rb
@@ -38,14 +38,16 @@ class CurrentUserSerializer < BasicUserSerializer
              :previous_visit_at,
              :seen_notification_id,
              :primary_group_id,
-             :primary_group_name,
              :can_create_topic,
              :link_posting_access,
              :external_id,
              :top_category_ids,
-             :hide_profile_and_presence
+             :hide_profile_and_presence,
+             :groups
 
-  has_many :groups, embed: :object, serializer: BasicGroupSerializer
+  def groups
+    object.visible_groups.pluck(:id, :name).map { |id, name| { id: id, name: name.downcase } }
+  end
 
   def link_posting_access
     scope.link_posting_access
@@ -210,14 +212,6 @@ class CurrentUserSerializer < BasicUserSerializer
     object.primary_group_id.present?
   end
 
-  def primary_group_name
-    object.primary_group.name.downcase
-  end
-
-  def include_primary_group_name?
-    object.primary_group&.name.present?
-  end
-
   def external_id
     object&.single_sign_on_record&.external_id
   end
diff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rb
index 45982c3..e0b6f7b 100644
--- a/spec/serializers/current_user_serializer_spec.rb
+++ b/spec/serializers/current_user_serializer_spec.rb
@@ -65,4 +65,24 @@ RSpec.describe CurrentUserSerializer do
       expect(payload[:top_category_ids]).to eq([category2.id, category1.id])
     end
   end
+
+  context "#groups" do
+    let(:member) { Fabricate(:user) }
+    let :serializer do
+      CurrentUserSerializer.new(member, scope: Guardian.new, root: false)
+    end
+
+    it "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])
+      public_group = Fabricate.build(:group, visibility_level: Group.visibility_levels[:public])
+      hidden_group.add(member)
+      hidden_group.save!
+      public_group.add(member)
+      public_group.save!
+      payload = serializer.as_json
+
+      expect(payload[:groups]).to eq([{ id: public_group.id, name: public_group.name }])
+    end
+  end
 end

GitHub
sha: 98d09c90

2 Likes

I guess we got to reduce the sha matching down from 8 chars to 7 :slight_smile: there is some chance of collision but usually it will be fine

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/plugin-development-list-a-user-s-groups-without-making-a-request/103710/7

1 Like