Fix issue #111: Entry sync on foundry keeps creating new journal entries
All checks were successful
Test / test (push) Successful in 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-16 16:57:28 +00:00
parent b5d5e570d5
commit eead3c9be8
4 changed files with 93 additions and 14 deletions

View File

@@ -138,9 +138,10 @@ note stands as a page of one JournalEntry named **Dragonroll**. The module creat
adopts one the GM already made under that name, marking it as the notes' journal — and renders each adopts one the GM already made under that name, marking it as the notes' journal — and renders each
note's markdown into page HTML with Foundry's own renderer. Notes are written in Dragonroll, so this note's markdown into page HTML with Foundry's own renderer. Notes are written in Dragonroll, so this
copy runs one way: a note made or edited in the campaign rewrites its page on the next reconcile, and copy runs one way: a note made or edited in the campaign rewrites its page on the next reconcile, and
a note deleted there takes its page with it. Pages the campaign does not own — the GM's own pages in a note deleted there takes its page with it. A page standing under a note's title but carrying no
that journal — are left alone, and nothing written in the journal travels back. Autosync off stops it Dragonroll mark is claimed as that note's page, so a lost pairing does not double it. Any other page in
too, like the rest of the sync. that journal — the GM's own — is left alone, and nothing written in the journal travels back. Autosync
off stops it too, like the rest of the sync.
The module calls these Dragonroll routes with the campaign sync token (no Dragonroll login): The module calls these Dragonroll routes with the campaign sync token (no Dragonroll login):

View File

@@ -1474,8 +1474,9 @@ async function initializeSceneSync() {
// stands as a page of one JournalEntry named "Dragonroll". Notes are written in // stands as a page of one JournalEntry named "Dragonroll". Notes are written in
// Dragonroll, so this copy runs one way — the reconcile writes each note into its // Dragonroll, so this copy runs one way — the reconcile writes each note into its
// page and drops the page of a note the campaign deleted. Nothing of the journal // page and drops the page of a note the campaign deleted. Nothing of the journal
// is read back, and the GM's own pages in that journal are left alone: only the // is read back. A page the module made carries the `dragonroll-foundry.noteId`
// ones this module made carry the `dragonroll-foundry.noteId` flag. // flag; one without it that stands under a note's title is claimed as that note's
// page rather than doubled, and any other page is the GM's own and left alone.
const NOTES_JOURNAL_NAME = "Dragonroll"; const NOTES_JOURNAL_NAME = "Dragonroll";
let noteSyncRunning = false; let noteSyncRunning = false;
@@ -1598,14 +1599,34 @@ async function reconcileDragonrollNotes() {
retainedPageIds.add(page.id); retainedPageIds.add(page.id);
await applyDragonrollNotePage(page, entry); await applyDragonrollNotePage(page, entry);
if (!entry.foundryPageId) await mapDragonrollNote(entry._id, page.id); if (!entry.foundryPageId) await mapDragonrollNote(entry._id, page.id);
} else {
// A page under the note's title that carries no Dragonroll mark — one the
// module made before the campaign kept its mapping, or one whose flag was
// lost — is claimed as that note's page instead of adding a second page of
// the same name. Like the item sync's same-name adoption, this is what keeps
// a note from being written as a new page on every reconcile.
const sameName = (journal.pages?.contents || []).find((candidate) => (
String(candidate.name ?? "") === notePageTitle(entry)
&& !dragonrollNoteId(candidate)
&& !retainedPageIds.has(candidate.id)
));
if (sameName) {
retainedPageIds.add(sameName.id);
await applyDragonrollNotePage(sameName, entry);
await mapDragonrollNote(entry._id, sameName.id);
} else { } else {
const created = await createDragonrollNotePage(journal, entry); const created = await createDragonrollNotePage(journal, entry);
if (created) { if (created) {
retainedPageIds.add(created.id); retainedPageIds.add(created.id);
// The create carries the note's flag, but a world that drops it would leave
// the page unclaimed and the next reconcile would make another: mark it
// again the way the rest of the sync marks its documents.
await applyDragonrollNotePage(created, entry);
await mapDragonrollNote(entry._id, created.id); await mapDragonrollNote(entry._id, created.id);
} }
} }
} }
}
// Pages whose note the campaign deleted go with it. Only pages this module // Pages whose note the campaign deleted go with it. Only pages this module
// made are its business: an unflagged page is the GM's own. // made are its business: an unflagged page is the GM's own.

View File

@@ -80,9 +80,10 @@ notice what is gone. Live edits never delete anything.
Notes are the exception that runs one way, because notes are written in Dragonroll: every note stands Notes are the exception that runs one way, because notes are written in Dragonroll: every note stands
as a page of a single journal called **Dragonroll**. A note you make or edit in the app appears on that as a page of a single journal called **Dragonroll**. A note you make or edit in the app appears on that
journal within a few seconds, and deleting one takes its page with it. Pages the campaign does not own journal within a few seconds, and deleting one takes its page with it. A page that stands under a note's
— one the GM made in that journal themselves — are left alone, and nothing written in the journal title but carries no Dragonroll mark is claimed as that note's page, so a pairing that was lost does not
travels back to the campaign. double it. Any other page — one the GM made in that journal themselves — is left alone, and nothing
written in the journal travels back to the campaign.
## Auto-sync, and sending one actor on purpose ## Auto-sync, and sending one actor on purpose

View File

@@ -238,13 +238,15 @@ function writeFoundryPaths(target, data) {
return target; return target;
} }
function makeWorld({ markdown = true } = {}) { function makeWorld({ markdown = true, keepCreateFlags = true, failMapNote = false } = {}) {
const state = { const state = {
hooks: new Map(), hooks: new Map(),
registered: new Map(), registered: new Map(),
requests: [], requests: [],
timers: new Map(), timers: new Map(),
intervals: new Map(), intervals: new Map(),
keepCreateFlags,
failMapNote,
// What Dragonroll answers the notes reconcile with. Mapping a page answers // What Dragonroll answers the notes reconcile with. Mapping a page answers
// like the real backend: the note keeps the page id, so later reads carry it. // like the real backend: the note keeps the page id, so later reads carry it.
remoteNotes: [], remoteNotes: [],
@@ -289,7 +291,9 @@ function makeWorld({ markdown = true } = {}) {
assert.equal(documentName, "JournalEntryPage", "the notes' pages are journal pages"); assert.equal(documentName, "JournalEntryPage", "the notes' pages are journal pages");
// Foundry's own page validation refuses a page without a name. // Foundry's own page validation refuses a page without a name.
for (const doc of docs) assert.ok(doc.name, "a journal page needs a non-empty name"); for (const doc of docs) assert.ok(doc.name, "a journal page needs a non-empty name");
const created = docs.map((doc) => makePage(this, doc)); // A world that does not keep the flags a create was handed: the page
// exists but carries no mark, which is what a lost pairing looks like.
const created = docs.map((doc) => makePage(this, keepCreateFlags ? doc : { ...doc, flags: {} }));
pages.push(...created); pages.push(...created);
return created; return created;
}, },
@@ -326,6 +330,7 @@ function makeWorld({ markdown = true } = {}) {
const body = options.body ? JSON.parse(options.body) : null; const body = options.body ? JSON.parse(options.body) : null;
state.requests.push({ href, method: options.method, body }); state.requests.push({ href, method: options.method, body });
if (href.includes("foundry/sync/map-note")) { if (href.includes("foundry/sync/map-note")) {
if (state.failMapNote) return { ok: false, status: 500, json: async () => ({ status: "error", msg: "errors.internal" }) };
const note = state.remoteNotes.find((entry) => String(entry._id) === String(body.noteId)); const note = state.remoteNotes.find((entry) => String(entry._id) === String(body.noteId));
if (note) note.foundryPageId = body.foundryPageId; if (note) note.foundryPageId = body.foundryPageId;
return { ok: true, status: 200, json: async () => ({ status: "ok", note }) }; return { ok: true, status: 200, json: async () => ({ status: "ok", note }) };
@@ -524,6 +529,57 @@ test("the campaign's page mapping claims a page whose flag was lost", async () =
assert.equal(orphan.getFlag(MODULE_ID, "noteId"), "note-1", "and the page is marked again"); assert.equal(orphan.getFlag(MODULE_ID, "noteId"), "note-1", "and the page is marked again");
}); });
test("a page under the note's title with no mark is claimed, not doubled", async () => {
const world = await makeWorld().linkedSetup();
const journal = world.addJournal("Dragonroll");
const existing = world.addPage(journal, { name: "Session 1", text: { content: "an older copy" } });
// The campaign has no page id for the note, and the page carries no flag: this
// is a page the module made before the pairing was kept, or one whose flag was
// lost. It is claimed instead of a second page being made under the same title.
world.remoteNotes = [sessionNote()];
await world.runIntervals();
assert.equal(journal.pages.contents.length, 1, "no second page under the note's title");
assert.equal(existing.text.content, "<marked>**The start**</marked>", "the note's text lands on the page the world already had");
assert.equal(existing.getFlag(MODULE_ID, "noteId"), "note-1", "and the page is marked as the note's");
assert.deepEqual(world.mapNoteRequests().map((request) => request.body.foundryPageId), [existing.id],
"the campaign is told which page the note now stands as");
await world.runIntervals();
assert.equal(journal.pages.contents.length, 1, "reconciling again does not add another");
});
test("two notes of one title do not share a single page", async () => {
const world = await makeWorld().linkedSetup();
const journal = world.addJournal("Dragonroll");
const existing = world.addPage(journal, { name: "Session 1", text: { content: "an older copy" } });
world.remoteNotes = [sessionNote(), { _id: "note-9", title: "Session 1", content: "second" }];
await world.runIntervals();
assert.equal(journal.pages.contents.length, 2, "the second note gets a page of its own");
assert.equal(existing.getFlag(MODULE_ID, "noteId"), "note-1", "the world's page is claimed by the first note");
assert.equal(journal.pages.contents[1].getFlag(MODULE_ID, "noteId"), "note-9");
});
test("a page whose mark and mapping were both lost is claimed again, not doubled", async () => {
// Both ways of recognising the pairing are gone: the create's flag did not stick,
// and the campaign never got to keep the page id. Without claiming the page it
// already made, every reconcile would write another one.
const world = await makeWorld({ keepCreateFlags: false, failMapNote: true }).linkedSetup();
world.remoteNotes = [sessionNote()];
await world.runIntervals();
await world.runIntervals();
await world.runIntervals();
const journal = world.journals()[0];
assert.equal(journal.pages.contents.length, 1, "one note stands as the one page it was given");
assert.equal(journal.pages.contents[0].getFlag(MODULE_ID, "noteId"), "note-1",
"and the page keeps the note it stands for even though the create did not mark it");
});
test("without Foundry's markdown renderer the fallback keeps the text legible", async () => { test("without Foundry's markdown renderer the fallback keeps the text legible", async () => {
const world = await makeWorld({ markdown: false }).linkedSetup(); const world = await makeWorld({ markdown: false }).linkedSetup();
world.remoteNotes = [{ _id: "note-1", title: "Session 1", content: "a & b < c\nline two\n\nsecond para" }]; world.remoteNotes = [{ _id: "note-1", title: "Session 1", content: "a & b < c\nline two\n\nsecond para" }];