This commit is contained in:
@@ -35,6 +35,13 @@ const ActorSchema = new Schema({
|
||||
ref: 'Campaign',
|
||||
required: true
|
||||
},
|
||||
// The folder this actor stands in: a Folder of the same campaign whose target is
|
||||
// 'actor' (null = the root of the actor list).
|
||||
folder: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'Folder',
|
||||
default: null
|
||||
},
|
||||
foundryActorId: { type: String },
|
||||
date: { type: Date, default: Date.now }
|
||||
});
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
// Which campaign list a folder belongs to. Every sidebar section files its rows into
|
||||
// folders of its own, and all of them share this model: note folders are what it has
|
||||
// always held, and their rows carry no target at all, so anything reading the note tree
|
||||
// asks with `filterForTarget('note')` and keeps finding them.
|
||||
const FOLDER_TARGETS = ['note', 'actor', 'item', 'map'];
|
||||
|
||||
const FolderSchema = new Schema({
|
||||
name: { type: String, required: true },
|
||||
campaign: {
|
||||
@@ -12,10 +18,7 @@ const FolderSchema = new Schema({
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'Folder'
|
||||
},
|
||||
// Which campaign list a folder belongs to. Note folders are what this model has
|
||||
// always held, and their rows carry no target at all, so everything reading the
|
||||
// note tree asks for `{ target: { $ne: 'item' } }` and keeps finding them.
|
||||
target: { type: String, enum: ['note', 'item'], default: 'note' },
|
||||
target: { type: String, enum: FOLDER_TARGETS, default: 'note' },
|
||||
// The Foundry item folder this row mirrors, set by the item sync only
|
||||
// (see routes/foundry-link.js).
|
||||
foundryFolderId: { type: String },
|
||||
@@ -28,4 +31,33 @@ FolderSchema.index(
|
||||
{ unique: true, partialFilterExpression: { foundryFolderId: { $type: 'string' } } }
|
||||
);
|
||||
|
||||
// The sections a folder can file rows of, and whether a request names one of them.
|
||||
FolderSchema.statics.isTarget = function isTarget(target) {
|
||||
return FOLDER_TARGETS.includes(target);
|
||||
};
|
||||
|
||||
// The filter matching one section's folders, to spread into a `find` along with whatever
|
||||
// else the listing asks for. The notes ask for "none of the other sections" because the
|
||||
// rows saved before `target` existed carry none at all and are all note folders.
|
||||
FolderSchema.statics.filterForTarget = function filterForTarget(target) {
|
||||
if (target === 'note') {
|
||||
return { target: { $nin: FOLDER_TARGETS.filter((other) => other !== 'note') } };
|
||||
}
|
||||
return { target };
|
||||
};
|
||||
|
||||
// The folder an entry of a section may stand in: `null` is the root of that list, and
|
||||
// anything else has to be a folder of the same campaign *and* the same section, so a
|
||||
// move never files an entry where its own list would not show it again. Answers whether
|
||||
// it does — an id that is none of those says `false` rather than throwing.
|
||||
FolderSchema.statics.belongsToSection = async function belongsToSection(folderId, campaignId, target) {
|
||||
if (folderId === null || folderId === undefined || folderId === '') return true;
|
||||
if (!mongoose.isValidObjectId(folderId)) return false;
|
||||
|
||||
const folder = await this
|
||||
.findOne({ _id: folderId, campaign: campaignId, ...this.filterForTarget(target) })
|
||||
.lean();
|
||||
return Boolean(folder);
|
||||
};
|
||||
|
||||
module.exports = mongoose.model('Folder', FolderSchema);
|
||||
|
||||
@@ -32,6 +32,13 @@ const MapSchema = new Schema({
|
||||
ref: 'Campaign',
|
||||
required: true
|
||||
},
|
||||
// The folder this map stands in: a Folder of the same campaign whose target is
|
||||
// 'map' (null = the root of the map list).
|
||||
folder: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'Folder',
|
||||
default: null
|
||||
},
|
||||
date: { type: Date, default: Date.now }
|
||||
});
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ const upload = require('../services/storage');
|
||||
|
||||
const Campaign = require('../models/Campaign');
|
||||
const Actor = require('../models/Actor');
|
||||
const Folder = require('../models/Folder');
|
||||
const Map = require('../models/Map');
|
||||
|
||||
async function userOwnsCampaign(campaignId, userId) {
|
||||
@@ -12,6 +13,15 @@ async function userOwnsCampaign(campaignId, userId) {
|
||||
return Boolean(campaign);
|
||||
}
|
||||
|
||||
// Folders the campaign's actors are grouped into, in the shape the sidebar groups by
|
||||
// (the notes, items and maps each keep a tree of their own, see models/Folder.js).
|
||||
async function actorFolders(campaignId) {
|
||||
return Folder.find({ campaign: campaignId, target: 'actor' })
|
||||
.select('_id name parentFolder date color')
|
||||
.sort({ date: -1 })
|
||||
.lean();
|
||||
}
|
||||
|
||||
router.get('/list', async (req, res) => {
|
||||
try {
|
||||
const { campaign } = req.query;
|
||||
@@ -19,7 +29,7 @@ router.get('/list', async (req, res) => {
|
||||
if (!(await userOwnsCampaign(campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
const actors = await Actor.find({ campaign }).sort({ name: 1 }).lean();
|
||||
res.json({ status: 'ok', actors });
|
||||
res.json({ status: 'ok', actors, folders: await actorFolders(campaign) });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
@@ -46,13 +56,22 @@ router.post('/create', async (req, res) => {
|
||||
|
||||
router.post('/update', async (req, res) => {
|
||||
try {
|
||||
const { id, name, species, className, subclass, level, abilityScores, armorClass, speed, proficiencies, inventory, traits, spells, image } = req.body;
|
||||
const { id, name, species, className, subclass, level, abilityScores, armorClass, speed, proficiencies, inventory, traits, spells, image, folder } = req.body;
|
||||
if (!id) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const actor = await Actor.findById(id);
|
||||
if (!actor) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
if (!(await userOwnsCampaign(actor.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
// `null` moves the actor to the root of the actor list, an id files it in one of
|
||||
// this campaign's actor folders; anything else is refused.
|
||||
if (folder !== undefined) {
|
||||
if (!(await Folder.belongsToSection(folder || null, actor.campaign, 'actor'))) {
|
||||
return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
}
|
||||
actor.folder = folder || null;
|
||||
}
|
||||
|
||||
if (name !== undefined) actor.name = String(name).trim();
|
||||
if (species !== undefined) actor.species = species;
|
||||
if (className !== undefined) actor.className = className;
|
||||
|
||||
@@ -2,7 +2,9 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const Campaign = require('../models/Campaign');
|
||||
const Actor = require('../models/Actor');
|
||||
const Folder = require('../models/Folder');
|
||||
const Map = require('../models/Map');
|
||||
const Note = require('../models/Note');
|
||||
const Item = require('../models/Item');
|
||||
|
||||
@@ -13,14 +15,16 @@ async function userOwnsCampaign(campaignId, userId) {
|
||||
|
||||
router.get('/list', async (req, res) => {
|
||||
try {
|
||||
const { campaign } = req.query;
|
||||
if (!campaign) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
// Every sidebar section has its own folders; a request that names none means the
|
||||
// notes, like every caller of this route has until the other sections got theirs.
|
||||
const { campaign, target = 'note' } = req.query;
|
||||
if (!campaign || !Folder.isTarget(target)) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const hasAccess = await userOwnsCampaign(campaign, req.user.id);
|
||||
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
const folders = await Folder.find({ campaign, target: { $ne: 'item' } })
|
||||
.select('_id name date color')
|
||||
const folders = await Folder.find({ campaign, ...Folder.filterForTarget(target) })
|
||||
.select('_id name parentFolder date color')
|
||||
.sort({ date: -1 })
|
||||
.lean();
|
||||
|
||||
@@ -33,15 +37,16 @@ router.get('/list', async (req, res) => {
|
||||
|
||||
router.post('/create', async (req, res) => {
|
||||
try {
|
||||
const { name, campaign } = req.body;
|
||||
if (!name || !campaign) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
const { name, campaign, target = 'note' } = req.body;
|
||||
if (!name || !campaign || !Folder.isTarget(target)) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const hasAccess = await userOwnsCampaign(campaign, req.user.id);
|
||||
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
const newFolder = new Folder({
|
||||
name: name.trim(),
|
||||
campaign
|
||||
campaign,
|
||||
target
|
||||
});
|
||||
await newFolder.save();
|
||||
res.json({ status: 'ok', folder: newFolder });
|
||||
@@ -51,6 +56,7 @@ router.post('/create', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Renaming and colouring works the same for the folders of every campaign section.
|
||||
router.post('/update', async (req, res) => {
|
||||
try {
|
||||
const { id, name, color } = req.body;
|
||||
@@ -86,16 +92,25 @@ router.post('/delete', async (req, res) => {
|
||||
|
||||
async function moveRecursive(folderId) {
|
||||
const subfolders = await Folder.find({ parentFolder: folderId }).select('_id').lean();
|
||||
// Every section files rows into a folder of its own, so each of them moves what
|
||||
// stands in this one to the root of its own list instead of leaving it pointing
|
||||
// at a folder that is gone.
|
||||
await Note.updateMany(
|
||||
{ folder: folderId },
|
||||
{ $set: { folder: null, date: Date.now() } }
|
||||
);
|
||||
// Item folders share this model: the items inside move to the root of
|
||||
// the item list rather than point at a folder that is gone.
|
||||
await Actor.updateMany(
|
||||
{ folder: folderId },
|
||||
{ $set: { folder: null } }
|
||||
);
|
||||
await Item.updateMany(
|
||||
{ folder: folderId },
|
||||
{ $set: { folder: null } }
|
||||
);
|
||||
await Map.updateMany(
|
||||
{ folder: folderId },
|
||||
{ $set: { folder: null } }
|
||||
);
|
||||
await Folder.deleteOne({ _id: folderId });
|
||||
for (const sub of subfolders) {
|
||||
await moveRecursive(sub._id);
|
||||
|
||||
@@ -119,8 +119,8 @@ async function saveFoundryItem(campaign, source, fullSync = false, folders = nul
|
||||
// Which folder of the campaign's item tree the world says this item stands in.
|
||||
// `undefined` means the push says nothing about folders — an item embedded in an
|
||||
// actor, or a module that knows nothing about them — and the item stays where it
|
||||
// is. The world owns the folder of its own directory, so a move lands on a full
|
||||
// snapshot too: nothing on the Dragonroll side files items by hand.
|
||||
// is. The world owns the folder of its own directory, so a move it reports lands on
|
||||
// a full snapshot too and wins over one a campaign made from its item list.
|
||||
const folder = resolveFoundryItemFolder(source, folders);
|
||||
if (folder !== undefined) item.folder = folder;
|
||||
await item.save();
|
||||
|
||||
@@ -16,7 +16,7 @@ async function userOwnsCampaign(campaignId, userId) {
|
||||
// (the world's own item folders arrive through the sync, see routes/foundry-link.js).
|
||||
async function itemFolders(campaignId) {
|
||||
return Folder.find({ campaign: campaignId, target: 'item' })
|
||||
.select('_id name parentFolder date')
|
||||
.select('_id name parentFolder date color')
|
||||
.sort({ date: -1 })
|
||||
.lean();
|
||||
}
|
||||
@@ -56,13 +56,23 @@ router.post('/create', async (req, res) => {
|
||||
|
||||
router.post('/update', async (req, res) => {
|
||||
try {
|
||||
const { id, name, category, rarity, quantity, weight, properties, description, image } = req.body;
|
||||
const { id, name, category, rarity, quantity, weight, properties, description, image, folder } = req.body;
|
||||
if (!id) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const item = await Item.findById(id);
|
||||
if (!item) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
if (!(await userOwnsCampaign(item.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
// `null` moves the item to the root of the item list, an id files it in one of
|
||||
// this campaign's item folders; anything else is refused. The linked world owns
|
||||
// its own folder tree, so a move it reports later wins over one made here.
|
||||
if (folder !== undefined) {
|
||||
if (!(await Folder.belongsToSection(folder || null, item.campaign, 'item'))) {
|
||||
return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
}
|
||||
item.folder = folder || null;
|
||||
}
|
||||
|
||||
if (name !== undefined) item.name = String(name).trim();
|
||||
if (category !== undefined) item.category = category;
|
||||
if (rarity !== undefined) item.rarity = rarity;
|
||||
|
||||
@@ -5,6 +5,7 @@ const upload = require('../services/storage');
|
||||
|
||||
const Campaign = require('../models/Campaign');
|
||||
const Map = require('../models/Map');
|
||||
const Folder = require('../models/Folder');
|
||||
const Note = require('../models/Note');
|
||||
const Actor = require('../models/Actor');
|
||||
|
||||
@@ -13,6 +14,15 @@ async function userOwnsCampaign(campaignId, userId) {
|
||||
return Boolean(campaign);
|
||||
}
|
||||
|
||||
// Folders the campaign's maps are grouped into, in the shape the sidebar groups by
|
||||
// (the notes, actors and items each keep a tree of their own, see models/Folder.js).
|
||||
async function mapFolders(campaignId) {
|
||||
return Folder.find({ campaign: campaignId, target: 'map' })
|
||||
.select('_id name parentFolder date color')
|
||||
.sort({ date: -1 })
|
||||
.lean();
|
||||
}
|
||||
|
||||
// Grid values arrive as strings when the request is a multipart form upload.
|
||||
function toNumber(value, fallback) {
|
||||
if (value === undefined || value === null || value === '') return fallback;
|
||||
@@ -27,7 +37,7 @@ router.get('/list', async (req, res) => {
|
||||
if (!(await userOwnsCampaign(campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
const maps = await Map.find({ campaign }).sort({ name: 1 }).lean();
|
||||
res.json({ status: 'ok', maps });
|
||||
res.json({ status: 'ok', maps, folders: await mapFolders(campaign) });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
@@ -57,13 +67,22 @@ router.post('/create', upload.single('image'), async (req, res) => {
|
||||
|
||||
router.post('/update', upload.single('image'), async (req, res) => {
|
||||
try {
|
||||
const { id, name, gridSize, offsetX, offsetY, scale } = req.body;
|
||||
const { id, name, gridSize, offsetX, offsetY, scale, folder } = req.body;
|
||||
if (!id) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const map = await Map.findById(id);
|
||||
if (!map) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
if (!(await userOwnsCampaign(map.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
// `null` moves the map to the root of the map list, an id files it in one of this
|
||||
// campaign's map folders; anything else is refused.
|
||||
if (folder !== undefined) {
|
||||
if (!(await Folder.belongsToSection(folder || null, map.campaign, 'map'))) {
|
||||
return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
}
|
||||
map.folder = folder || null;
|
||||
}
|
||||
|
||||
if (name !== undefined) map.name = String(name).trim();
|
||||
if (gridSize !== undefined) map.gridSize = toNumber(gridSize, map.gridSize);
|
||||
if (offsetX !== undefined) map.offsetX = toNumber(offsetX, map.offsetX);
|
||||
|
||||
@@ -19,9 +19,9 @@ router.get('/list', async (req, res) => {
|
||||
const hasAccess = await userOwnsCampaign(campaign, req.user.id);
|
||||
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
// The note tree only: item folders share this model and would otherwise
|
||||
// turn up as empty folders between the notes.
|
||||
const folders = await Folder.find({ campaign, target: { $ne: 'item' } })
|
||||
// The note tree only: the folders of the actors, items and maps share this model
|
||||
// and would otherwise turn up as empty folders between the notes.
|
||||
const folders = await Folder.find({ campaign, ...Folder.filterForTarget('note') })
|
||||
.select('_id name parentFolder date color')
|
||||
.sort({ date: -1 })
|
||||
.lean();
|
||||
@@ -50,7 +50,7 @@ router.get('/subfolder/list', async (req, res) => {
|
||||
const folderDoc = await Folder.findOne({ _id: folder, campaign }).lean();
|
||||
if (!folderDoc) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
const subfolders = await Folder.find({ campaign, parentFolder: folder, target: { $ne: 'item' } })
|
||||
const subfolders = await Folder.find({ campaign, parentFolder: folder, ...Folder.filterForTarget('note') })
|
||||
.select('_id name date color')
|
||||
.sort({ date: -1 })
|
||||
.lean();
|
||||
@@ -82,7 +82,7 @@ router.get('/all', async (req, res) => {
|
||||
.sort({ title: 1 })
|
||||
.lean();
|
||||
|
||||
const folders = await Folder.find({ campaign, target: { $ne: 'item' } })
|
||||
const folders = await Folder.find({ campaign, ...Folder.filterForTarget('note') })
|
||||
.select('_id name parentFolder')
|
||||
.lean();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user