Vastly improved new character UX
Characters now can limit which libraries they allow
This commit is contained in:
39
app/imports/api/library/getCreatureLibraryIds.js
Normal file
39
app/imports/api/library/getCreatureLibraryIds.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import LibraryCollections from '/imports/api/library/LibraryCollections.js';
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||
import getUserLibraryIds from './getUserLibraryIds';
|
||||
import { intersection, union } from 'lodash';
|
||||
|
||||
export default function getCreatureLibraryIds(creature, userId) {
|
||||
if (!userId) return [];
|
||||
|
||||
// Get the ids of libraries the user is permitted to view
|
||||
const userLibIds = getUserLibraryIds(userId);
|
||||
|
||||
// If given a creature Id, get the creature document
|
||||
if (typeof creature === 'string') {
|
||||
creature = Creatures.findOne(creature, {
|
||||
fields: {
|
||||
allowedLibraries: 1,
|
||||
allowedLibraryCollections: 1,
|
||||
}
|
||||
});
|
||||
if (!creature) return [];
|
||||
}
|
||||
|
||||
// If the creature does not restrict the libraries, let it use them all
|
||||
if (!creature.allowedLibraryCollections && !creature.allowedLibraries) {
|
||||
return userLibIds;
|
||||
}
|
||||
|
||||
// Get the ids of the libraries that the creature allows
|
||||
const allowedCollections = creature.allowedLibraryCollections || [];
|
||||
let creatureLibIds = creature.allowedLibraries || [];
|
||||
LibraryCollections.find({
|
||||
_id: { $in: allowedCollections }
|
||||
}, { fields: { libraries: 1 } }).forEach(collection => {
|
||||
creatureLibIds = union(creatureLibIds, collection.libraries);
|
||||
});
|
||||
|
||||
// return all the ids that the creature allows and the user can view
|
||||
return intersection(userLibIds, creatureLibIds);
|
||||
}
|
||||
Reference in New Issue
Block a user