Fix issue #98: Sync notes from dragonroll to foundry
All checks were successful
Test / test (push) Successful in 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-15 23:19:43 +00:00
parent c9ee16f2f2
commit 07dcf08616
11 changed files with 780 additions and 4 deletions

View File

@@ -14,7 +14,16 @@ const NoteSchema = new Schema({
ref: 'Folder'
},
color: { type: String },
// The journal page this note is written as in the linked Foundry world: the
// page of the one "Dragonroll" journal whose rows carry this note's id in
// their `dragonroll-foundry.noteId` flag (see the module's notes sync).
foundryPageId: { type: String },
date: { type: Date, default: Date.now }
});
NoteSchema.index(
{ campaign: 1, foundryPageId: 1 },
{ unique: true, partialFilterExpression: { foundryPageId: { $type: 'string' } } }
);
module.exports = mongoose.model('Note', NoteSchema);

View File

@@ -7,6 +7,7 @@ const Campaign = require('../models/Campaign');
const Actor = require('../models/Actor');
const Item = require('../models/Item');
const Folder = require('../models/Folder');
const Note = require('../models/Note');
const { foundryActorToCharacter } = require('../services/actor-foundry');
const { foundryItemToItemData } = require('../services/item-foundry');
const { joinFoundryAssetUrl } = require('../services/foundry-icons');
@@ -477,6 +478,57 @@ router.post('/sync/map-item', async (req, res) => {
}
});
// --- Note pages ------------------------------------------------------------
// The campaign's notes are read where their authors' table sits: every note
// stands as a page of the one journal the world calls "Dragonroll". Notes are
// written in Dragonroll, so this copy runs one way — the module reads the notes
// and rewrites their pages; nothing of the journal travels back. Its pages keep
// the note's id in a `dragonroll-foundry.noteId` flag, and the note keeps the
// page's id here, so either side recognises the pairing again after a restart.
// Every note of the campaign, content included: enough for the module to write
// each one as a page. Folders of the notes do not travel — the journal is flat.
router.get('/sync/notes', async (req, res) => {
try {
const campaign = await syncCampaign(req, res);
if (!campaign) return;
const notes = await Note.find({ campaign: campaign._id })
.select('_id title content foundryPageId date')
.sort({ date: -1 })
.lean();
res.json({ status: 'ok', notes });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
// The page the module made for a note, kept on the note so the next reconcile —
// or a world whose page flags were lost — writes that page instead of a second
// one under the same title.
router.post('/sync/map-note', async (req, res) => {
try {
const campaign = await syncCampaign(req, res);
if (!campaign) return;
const { noteId, foundryPageId, worldId } = req.body;
if (!requestMatchesWorld(campaign, worldId) || !noteId || !foundryPageId) {
return res.json({ status: 'error', msg: 'errors.missing-data' });
}
const note = await Note.findOne({ _id: noteId, campaign: campaign._id });
if (!note) return res.json({ status: 'error', msg: 'errors.notfound' });
const existing = await Note.findOne({ campaign: campaign._id, foundryPageId, _id: { $ne: note._id } });
if (existing) return res.json({ status: 'error', msg: 'foundry.errors.note-already-synced' });
note.foundryPageId = String(foundryPageId).trim().slice(0, 120);
await note.save();
res.json({ status: 'ok', note });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
module.exports = router;
// The writers every Foundry document goes through: the sync pushes above, and the
// force import a campaign asks for from its sidebar (routes/foundry.js). One set of