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);
|
||||
}
|
||||
31
app/imports/api/library/getUserLibraryIds.js
Normal file
31
app/imports/api/library/getUserLibraryIds.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import LibraryCollections from '/imports/api/library/LibraryCollections.js';
|
||||
import Libraries from '/imports/api/library/Libraries.js';
|
||||
import { union } from 'lodash';
|
||||
|
||||
export default function getUserLibraryIds(userId) {
|
||||
if (!userId) return [];
|
||||
const user = Meteor.users.findOne(userId);
|
||||
let subbedIds = user?.subscribedLibraries || [];
|
||||
const subCollections = user?.subscribedLibraryCollections || [];
|
||||
LibraryCollections.find({
|
||||
$or: [
|
||||
{ owner: userId },
|
||||
{ writers: userId },
|
||||
{ readers: userId },
|
||||
{ _id: { $in: subCollections }, public: true },
|
||||
]
|
||||
}, { fields: { libraries: 1 } }).forEach(collection => {
|
||||
subbedIds = union(subbedIds, collection.libraries);
|
||||
});
|
||||
const libraryIds = Libraries.find({
|
||||
$or: [
|
||||
{ owner: userId },
|
||||
{ writers: userId },
|
||||
{ readers: userId },
|
||||
{ _id: { $in: subbedIds }, public: true },
|
||||
]
|
||||
}, {
|
||||
fields: { _id: 1 }
|
||||
}).map(lib => lib._id);
|
||||
return libraryIds;
|
||||
}
|
||||
Reference in New Issue
Block a user