Fix issue #89: Traits and items are mixed up
All checks were successful
Test / test (push) Successful in 2s
All checks were successful
Test / test (push) Successful in 2s
This commit is contained in:
@@ -9,6 +9,7 @@ const Item = require('../models/Item');
|
||||
const Folder = require('../models/Folder');
|
||||
const { foundryActorToCharacter } = require('../services/actor-foundry');
|
||||
const { foundryItemToItemData } = require('../services/item-foundry');
|
||||
const { joinFoundryAssetUrl } = require('../services/foundry-icons');
|
||||
|
||||
// Upper bound for the Foundry names remembered per synced item (see Item model).
|
||||
const MAX_FOUNDRY_NAMES = 20;
|
||||
@@ -41,10 +42,33 @@ function requestMatchesWorld(campaign, worldId) {
|
||||
return typeof worldId === 'string' && worldId === campaign.foundryWorldId;
|
||||
}
|
||||
|
||||
// Foundry serves its data directory from the world's own address, which is the one the
|
||||
// campaign keeps under `settings.integrations.foundryUrl` — the icon window already loads
|
||||
// the world's icons through it. The module sends the images it publishes as absolute URLs;
|
||||
// the bridge's snapshot and older modules carry Foundry's relative paths, and a sheet
|
||||
// reading one of those against Dragonroll's upload folder finds nothing, which is how the
|
||||
// icons stopped loading (Syndria98/dragonroll#89). An absolute URL travels as it is, and so
|
||||
// does a path when the campaign has no world address to read it against.
|
||||
function worldAssetUrl(image, worldUrl) {
|
||||
const value = String(image ?? '').trim();
|
||||
if (!value || /^(https?:|data:|\/\/)/i.test(value)) return value;
|
||||
return joinFoundryAssetUrl(worldUrl, value) || value;
|
||||
}
|
||||
|
||||
// The images a world document arrives with, read against its world before they land.
|
||||
function resolveWorldAssets(fields, campaign) {
|
||||
const worldUrl = campaign?.settings?.integrations?.foundryUrl;
|
||||
if (typeof fields.image === 'string') fields.image = worldAssetUrl(fields.image, worldUrl);
|
||||
if (Array.isArray(fields.inventory)) {
|
||||
for (const entry of fields.inventory) entry.icon = worldAssetUrl(entry.icon, worldUrl);
|
||||
}
|
||||
}
|
||||
|
||||
async function saveFoundryActor(campaign, source, fullSync = false) {
|
||||
const foundryActorId = String(source?._id || source?.id || '').trim().slice(0, 120);
|
||||
const fields = foundryActorToCharacter(source || {});
|
||||
if (!foundryActorId || !fields.name) return null;
|
||||
resolveWorldAssets(fields, campaign);
|
||||
|
||||
let actor = await Actor.findOne({ campaign: campaign._id, foundryActorId });
|
||||
const dragonrollActorId = source?.flags?.['dragonroll-foundry']?.actorId;
|
||||
@@ -85,6 +109,7 @@ async function saveFoundryItem(campaign, source, fullSync = false, folders = nul
|
||||
const foundryItemId = String(source?._id || source?.id || '').trim().slice(0, 120);
|
||||
const fields = foundryItemToItemData(source || {});
|
||||
if (!foundryItemId || !fields) return null;
|
||||
resolveWorldAssets(fields, campaign);
|
||||
|
||||
let item = await Item.findOne({ campaign: campaign._id, foundryItemId });
|
||||
const dragonrollItemId = source?.flags?.['dragonroll-foundry']?.itemId;
|
||||
|
||||
Reference in New Issue
Block a user