From 98d09c90acc503051d02094a9f25113eb5fdf293 Mon Sep 17 00:00:00 2001 From: Maja Komel Date: Tue, 18 Dec 2018 08:41:42 +0100 Subject: [PATCH] Current user serializer groups (ef7f84b follow-up) --- .../javascripts/discourse/models/user.js.es6 | 10 ++++++++++ app/models/user.rb | 4 ++++ app/serializers/current_user_serializer.rb | 16 +++++---------- .../current_user_serializer_spec.rb | 20 +++++++++++++++++++ 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index f1f22752a71..d6ff8c300a6 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 69ec48cfc0e..1f47eaacb05 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 27eb3500a9a..1f65e474a32 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 45982c374a4..e0b6f7bed6b 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