Merge pull request 'Fix #67: Don't show all items on the items sidebar' (#75) from ai/issue-67 into master
All checks were successful
Test / test (push) Successful in 2s
Build and Deploy Nuxt / build (push) Successful in 1m27s

Reviewed-on: #75
This commit was merged in pull request #75.
This commit is contained in:
2026-10-15 16:40:39 +00:00
5 changed files with 103 additions and 37 deletions

View File

@@ -117,7 +117,7 @@ the JWT guard; CORS opened for it): `snapshot` (actors + world items, `fullSync`
`item-snapshot`, `item-delete`, `characters`, `items`, `status`, `map` / `map-item`, `delete`. Rules:
- **Identity** is `foundryActorId` / `foundryItemId` (unique per campaign, partial index); a Foundry document carrying the `dragonroll-foundry.actorId` / `.itemId` flag adopts the matching Dragonroll row.
- **Items are deduplicated by name** — Foundry keeps the same gear once per carrier, so one `Item` per name; `foundryNames` keeps Foundry's names so a Dragonroll rename does not resurrect a duplicate. `services/item-foundry.js` returns `null` for non-gear types and those are skipped.
- **Only the world's item directory reaches the item list** (Syndria98/dragonroll#67) — `snapshotItemSources` reads `body.items` (`game.items`) alone; gear embedded in a character is that actor's `inventory` (see the `Actor` model), not an `Item`. A world item still folds into a hand-made or renamed row it matches by name (`foundryNames`), so a rename does not resurrect a duplicate. `services/item-foundry.js` returns `null` for non-gear types and those are skipped.
- **Item folders mirror the world's item directory** (`Folder.foundryFolderId`, target `item`): every push carries the whole tree and the world owns its names and nesting. `color` is the one field the campaign sets too, so it travels back with the folders `GET /foundry/sync/items` answers with (`{items, folders}`) and the module writes it onto the world's folder.
- **`fullSync` preserves Dragonroll edits** (only new rows are overwritten); live Foundry events overwrite. Only a full snapshot can prune.
- **Autosync is a Foundry-side setting** (`autoSync`, on by default): off = no CRUD push, no periodic reconcile, no startup snapshot; the actor directory's *Dragonroll: Force Sync* still upserts the selection (`fullSync:false`).

View File

@@ -69,17 +69,13 @@ async function saveFoundryActor(campaign, source, fullSync = false) {
return { foundryActorId, actorId: actor.id };
}
// A snapshot carries world-level items plus the items embedded in every actor it
// sends. World items are listed first so they own a name before its copies.
// The campaign's item list mirrors the world's item directory and nothing else
// (Syndria98/dragonroll#67). The items embedded in an actor are that character's own
// inventory: they travel with the actor document (see services/actor-foundry.js), so a
// snapshot feeds Item rows from the world directory (`body.items`) alone and gear a
// character merely carries stays on its sheet instead of standing in the item list.
function snapshotItemSources(body) {
const sources = [];
if (Array.isArray(body.items)) sources.push(...body.items);
if (Array.isArray(body.actors)) {
for (const actor of body.actors) {
if (Array.isArray(actor?.items)) sources.push(...actor.items);
}
}
return sources;
return Array.isArray(body.items) ? body.items : [];
}
async function saveFoundryItem(campaign, source, fullSync = false, folders = null) {
@@ -95,9 +91,9 @@ async function saveFoundryItem(campaign, source, fullSync = false, folders = nul
if (flagged && (!flagged.foundryItemId || flagged.foundryItemId === foundryItemId)) item = flagged;
}
if (!item) {
// The same gear exists more than once in Foundry: once in the world
// directory and once inside each actor carrying it. Keep a single
// Dragonroll item per name so inventories do not flood the Item list.
// A world item can share its name with a Dragonroll row the campaign made by
// hand, or with one it renamed away from. Keep a single item per name so a
// piece of gear the world sends folds into that row instead of doubling it.
const sameName = await Item.find({
campaign: campaign._id,
$or: [{ name: fields.name }, { foundryNames: fields.name }]
@@ -117,10 +113,10 @@ async function saveFoundryItem(campaign, source, fullSync = false, folders = nul
item.foundryNames = [...item.foundryNames, fields.name].slice(-MAX_FOUNDRY_NAMES);
}
// 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 it reports lands on
// a full snapshot too and wins over one a campaign made from its item list.
// `undefined` means the push says nothing about folders — an older 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 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();
@@ -140,9 +136,9 @@ async function syncFoundryItems(campaign, sources, fullSync = false, folders = n
// A Foundry world groups its item directory into folders; the campaign mirrors that
// tree as Folders of target 'item' so its item list reads like the world's. Every
// push from a module that knows about folders carries the whole world tree, and
// names the folder each world item it sends stands in. Items embedded in an actor
// say nothing: the folders they carry belong to that actor's own inventory, and
// Dragonroll keeps one item per piece of gear anyway.
// names the folder each world item it sends stands in. Only the world's item
// directory reaches the campaign's item list, so only its folders are mirrored; the
// inventory folders an actor keeps for itself are none of the item list's business.
// The colour a pushed folder carries. `undefined` means the push says nothing about
// colours — an older module, which knows nothing about them — and the colour the
@@ -297,8 +293,8 @@ router.post('/sync/snapshot', async (req, res) => {
foundryActorId: { $exists: true, $nin: currentIds }
});
}
// Pruning needs a full snapshot: only that request sees the world
// directory and every actor's copies at the same time.
// Pruning needs a full snapshot: only that request sees the whole world
// directory, so it alone knows which synced rows the world has let go of.
if (fullSync && itemSources) {
const currentIds = snapshotItemSources(req.body)
.map((source) => String(source?._id || source?.id || '').trim())

View File

@@ -82,16 +82,16 @@ The optional read-only snapshot bridge is not affected: it keeps publishing the
### Items
The campaign's Item list is synchronized with the world's gear, both ways:
The campaign's Item list mirrors the world's item directory (Foundry's **items tab**) and nothing
else (Syndria98/dragonroll#67):
- The GM sends the world item directory (`game.items`) with every snapshot, and world items created,
changed or deleted in Foundry are pushed as they happen. Items embedded in a character travel with
that character's snapshot instead.
- Gear is deduplicated by name: the same sword in the world directory and in three characters'
inventories becomes one Dragonroll item. Names Dragonroll renames away from are remembered on the
item (`foundryNames`), so Foundry's copies keep folding into it rather than coming back as a
duplicate. Spells, classes, subclasses, backgrounds, feats and species are not gear and are not
imported.
changed or deleted in Foundry are pushed as they happen. The items embedded in a character are that
character's own inventory: they travel with its snapshot and stay on its sheet, so gear a character
merely carries does not stand in the campaign's item list.
- A world item folds into a Dragonroll row that already carries its name — one a campaign made by hand,
or one it renamed away from and remembers (`foundryNames`) — instead of doubling it. Spells, classes,
subclasses, backgrounds, feats and species are not gear and are not imported.
- Foundry owns the fields it sends (category from the item type, rarity, quantity, weight, weapon
properties, description). An item made by hand in Dragonroll is created in the world's item
directory on the next reconcile, mapped afterwards, and editing it in Dragonroll updates the Foundry
@@ -101,10 +101,9 @@ The campaign's Item list is synchronized with the world's gear, both ways:
what the GM colours in Foundry shows on the campaign's folder, and what the campaign colours lands on
the world's folder on the next reconcile (the world's document keeps its name and nesting either way).
- A full snapshot keeps Dragonroll edits on reconnect; live Foundry edits overwrite. Item removal is
pruned only by a full snapshot, so a live item push never deletes anything. Deleting a synced item in
Dragonroll removes its Foundry world copy on the next reconcile. Items that live only in an actor's
inventory are owned by that actor's snapshot: editing such an item in Dragonroll does not rewrite the
actor's copy.
pruned only by a full snapshot, so a live item push never deletes anything: only a full snapshot sees
the whole world directory and can tell which synced rows it has let go of. Deleting a synced item in
Dragonroll removes its Foundry world copy on the next reconcile.
The module calls these Dragonroll routes with the campaign sync token (no Dragonroll login):

View File

@@ -530,8 +530,9 @@ async function pushFoundryActors(actors, fullSync = false) {
const result = await postDragonroll("foundry/sync/snapshot", {
worldId: game.world.id,
actors: supportedActors.map(serializeFoundryActor),
// Dragonroll deduplicates by name, so sending the world directory next to
// the actor copies keeps a single Item per piece of gear.
// The world item directory is what the campaign lists as its items
// (Syndria98/dragonroll#67); the gear a character carries travels inside that
// actor's snapshot as its inventory, not as campaign items.
items: getWorldItemSnapshots(),
// The folder tree those world items stand in (see worldItemFolderSnapshots).
folders: worldItemFolderSnapshots(),

View File

@@ -299,6 +299,76 @@ test("an item the world moved out of a folder follows it to the root", async ()
assert.equal(sync.models["../models/Item"].rows[0].folder, null);
});
// --- Only the world's item directory is listed (Syndria98/dragonroll#67) --------------
// The campaign's item list equals the world's items tab: gear that only an actor
// carries is its inventory (kept on the actor document), not a Dragonroll item.
test("an actor's gear is kept off the campaign's item list", async () => {
const sync = loadSyncRoute();
await sync.call("POST /sync/snapshot", {
worldId: "world-1",
items: [worldItem()],
// Thalia carries a potion the world's item directory does not hold.
actors: [{
_id: "actor-1",
name: "Thalia",
items: [{ _id: "potion-1", name: "Potion of Healing", type: "consumable", system: { quantity: 2 } }],
}],
});
const names = sync.models["../models/Item"].rows.map((row) => row.name);
assert.deepEqual(names, ["Longsword"], "only the world's item reaches the list, not the actor's potion");
});
test("the campaign's item list is the world's whole item directory", async () => {
const sync = loadSyncRoute();
await sync.call("POST /sync/snapshot", {
worldId: "world-1",
items: [worldItem(), worldItem({ _id: "item-2", name: "Torch", type: "loot", system: { quantity: 1 } })],
actors: [],
});
assert.deepEqual(
sync.models["../models/Item"].rows.map((row) => row.name).sort(),
["Longsword", "Torch"],
);
});
test("an item carried by a character is listed once, as its world entry", async () => {
const sync = loadSyncRoute();
await sync.call("POST /sync/snapshot", {
worldId: "world-1",
items: [worldItem()],
// The same sword, a second copy embedded in an actor under another id.
actors: [{ _id: "actor-1", name: "Thalia", items: [worldItem({ _id: "sword-copy" })] }],
});
assert.deepEqual(sync.models["../models/Item"].rows.map((row) => row.name), ["Longsword"]);
});
test("a full snapshot prunes gear that only an actor carried", async () => {
const sync = loadSyncRoute({
seed: { items: [{ _id: "potion-1", name: "Potion of Healing", foundryItemId: "embedded-1" }] },
});
// The world's item directory is now empty: nothing but a character holds the potion.
await sync.call("POST /sync/snapshot", {
worldId: "world-1",
items: [],
actors: [{
_id: "actor-1",
name: "Thalia",
items: [{ _id: "embedded-1", name: "Potion of Healing", type: "consumable", system: { quantity: 2 } }],
}],
fullSync: true,
});
assert.deepEqual(sync.models["../models/Item"].rows, [], "the actor's own gear no longer stands in the item list");
});
test("a folder coloured in the world is coloured in the campaign", async () => {
const sync = loadSyncRoute();