Current user serializer groups (ef7f84b follow-up)

This commit is contained in:
Maja Komel 2018-12-18 08:41:42 +01:00
parent 5c2e194d01
commit 98d09c90ac
4 changed files with 39 additions and 11 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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

View File

@ -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