Fix issue #67: Don't show all items on the items sidebar
All checks were successful
Test / test (push) Successful in 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-15 16:40:15 +00:00
parent 0caacf434f
commit 495e528124
5 changed files with 103 additions and 37 deletions

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())