Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions grouping.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let allowDirectIdSelectors = false;

// Publish admin and group for users that have it
Meteor.publish(null, function () {
return this.userId && Meteor.users._partitionerDirect.find(this.userId, {fields: {admin: 1, group: 1}});
return this.userId && Meteor.users._partitionerDirect.find(this.userId, {projection: {admin: 1, group: 1}});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return this.userId && Meteor.users._partitionerDirect.find(this.userId, {projection: {admin: 1, group: 1}});
// Since Meteor.[email protected] {projection: {}} has been preferred over the deprecated {fields: {}}
// Note that string comparison breaks since "2.16" is newer than "2.9" - so need to pad with zeros
let release = Meteor.release.split('@')[1].split('.')
release = release[0] + '.' + release[1].padStart(2, '0')
let defaultProjectionField = parseFloat(release) >= 2.16 ? 'projection' : 'fields';
// Publish admin and group for users that have it
Meteor.publish(null, function () {
return this.userId && Meteor.users._partitionerDirect.find(this.userId, {[defaultProjectionField]: {admin: 1, group: 1}});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the a reason you check for 2.16 instead of 2.6 here?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my mistake. I was testing this on a project on 2.16, so I had that number in my head when I wrote this. The test, when converted to floating point, should be parseFloat(release) >= 2.06.

Well spotted, thanks.

});

// Special hook for Meteor.users to scope for each group
Expand All @@ -21,7 +21,7 @@ function userFindHook(userId, selector /*, options*/) {
if (!userId && !groupId) return true;

if (!groupId) {
const user = Meteor.users._partitionerDirect.findOne(userId, {fields: {group: 1}});
const user = Meteor.users._partitionerDirect.findOne(userId, {projection: {group: 1}});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const user = Meteor.users._partitionerDirect.findOne(userId, {projection: {group: 1}});
const user = Meteor.users._partitionerDirect.findOne(userId, {[defaultProjectionField]: {group: 1}});


// user will be undefined inside reactive publish when user is deleted while subscribed
if (!user) return false;
Expand All @@ -30,7 +30,7 @@ function userFindHook(userId, selector /*, options*/) {

// If user is admin and not in a group, proceed as normal (select all users)
// do user2 findOne separately so that the findOne above can hit the cache
if (!groupId && Meteor.users._partitionerDirect.findOne(userId, {fields: {admin: 1}}).admin) return true;
if (!groupId && Meteor.users._partitionerDirect.findOne(userId, {projection: {admin: 1}}).admin) return true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!groupId && Meteor.users._partitionerDirect.findOne(userId, {projection: {admin: 1}}).admin) return true;
if (!groupId && Meteor.users._partitionerDirect.findOne(userId, {[defaultProjectionField]: {admin: 1}}).admin) return true;


// Normal users need to be in a group
if (!groupId) throw new Meteor.Error(403, ErrMsg.groupErr);
Expand Down Expand Up @@ -100,11 +100,11 @@ function findHook(userId, selector, options) {

// Adjust options to not return _groupId
if (options == null) {
this.args[1] = {fields: {_groupId: 0}};
this.args[1] = {projection: {_groupId: 0}};
} else {
// If options already exist, add {_groupId: 0} unless fields has {foo: 1} somewhere
if (options.fields == null) options.fields = {};
if (!Object.values(options.fields).some(v => v)) options.fields._groupId = 0;
// If options already exist, add {_groupId: 0} unless projection has {foo: 1} somewhere
if (options.projection == null) options.projection = {};
if (!Object.values(options.projection).some(v => v)) options.projection._groupId = 0;
Comment on lines +105 to +107
Copy link
Owner

@wildhart wildhart Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this whole block could be:

	// Adjust options to not return _groupId
	// Pre Meteor 2.16 we will get options?.fields, post Meteor 2.16 we might get either.
	let projectionField = options?.fields ? 'fields' : 'projection';
	if (options == null) {
		this.args[1] = {[defaultProjectionField]: {_groupId: 0}};
	} else if (!options[projectionField]) {
		options[defaultProjectionField] = {_groupId: 0};
	} else if (!Object.values(options[projectionField]).some(v => v)) {
		// If projection/fields already exist, add {_groupId: 0} unless it already has {foo: 1} somewhere
		options[projectionField]._groupId = 0;
	}

}

return true;
Expand Down Expand Up @@ -178,7 +178,7 @@ Partitioner = {
check(userId, String);
check(groupId, String);

if (Meteor.users._partitionerDirect.findOne(userId, {fields: {group: 1}}).group) {
if (Meteor.users._partitionerDirect.findOne(userId, {projection: {group: 1}}).group) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (Meteor.users._partitionerDirect.findOne(userId, {projection: {group: 1}}).group) {
if (Meteor.users._partitionerDirect.findOne(userId, {[defaultProjectionField]: {group: 1}}).group) {

throw new Meteor.Error(403, "User is already in a group");
}

Expand All @@ -187,7 +187,7 @@ Partitioner = {

getUserGroup(userId) {
check(userId, String);
return (Meteor.users._partitionerDirect.findOne(userId, {fields: {group: 1}}) || {}).group;
return (Meteor.users._partitionerDirect.findOne(userId, {projection: {group: 1}}) || {}).group;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (Meteor.users._partitionerDirect.findOne(userId, {projection: {group: 1}}) || {}).group;
return (Meteor.users._partitionerDirect.findOne(userId, {[defaultProjectionField]: {group: 1}}) || {}).group;

},

clearUserGroup(userId) {
Expand Down Expand Up @@ -227,15 +227,15 @@ Partitioner = {
},

_isAdmin(_id) {
return !!Meteor.users._partitionerDirect.findOne({_id, admin: true}, {fields: {_id: 1}});
return !!Meteor.users._partitionerDirect.findOne({_id, admin: true}, {projection: {_id: 1}});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return !!Meteor.users._partitionerDirect.findOne({_id, admin: true}, {projection: {_id: 1}});
return !!Meteor.users._partitionerDirect.findOne({_id, admin: true}, {[defaultProjectionField]: {_id: 1}});

},

addToGroup(collection, entityId, groupId) {
if (!multipleGroupCollections[collection._name]) {
throw new Meteor.Error(403, ErrMsg.multiGroupErr);
}

let currentGroupIds = collection._partitionerDirect.findOne(entityId, {fields: {_groupId: 1}})?._groupId;
let currentGroupIds = collection._partitionerDirect.findOne(entityId, {projection: {_groupId: 1}})?._groupId;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let currentGroupIds = collection._partitionerDirect.findOne(entityId, {projection: {_groupId: 1}})?._groupId;
let currentGroupIds = collection._partitionerDirect.findOne(entityId, {[defaultProjectionField]: {_groupId: 1}})?._groupId;

if (!currentGroupIds) {
currentGroupIds = [groupId];
} else if (typeof currentGroupIds == 'string') {
Expand All @@ -254,7 +254,7 @@ Partitioner = {
throw new Meteor.Error(403, ErrMsg.multiGroupErr);
}

let currentGroupIds = collection._partitionerDirect.findOne(entityId, {fields: {_groupId: 1}})?._groupId;
let currentGroupIds = collection._partitionerDirect.findOne(entityId, {projection: {_groupId: 1}})?._groupId;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let currentGroupIds = collection._partitionerDirect.findOne(entityId, {projection: {_groupId: 1}})?._groupId;
let currentGroupIds = collection._partitionerDirect.findOne(entityId, {[defaultProjectionField]: {_groupId: 1}})?._groupId;

if (!currentGroupIds) {
return [];
}
Expand Down