From 19ae78b23b78f1b777a0d6a2c42f21a63378d4fc Mon Sep 17 00:00:00 2001 From: ThaumRystra Date: Sat, 9 Nov 2024 16:43:18 +0200 Subject: [PATCH] closed Imported characters need to be recalculated before adding to a tabletop #375 --- .../importCharacterFromDiceCloudInstance.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/imports/api/creature/creatures/methods/importCharacterFromDiceCloudInstance.js b/app/imports/api/creature/creatures/methods/importCharacterFromDiceCloudInstance.js index 6901ef04..8f4fac22 100644 --- a/app/imports/api/creature/creatures/methods/importCharacterFromDiceCloudInstance.js +++ b/app/imports/api/creature/creatures/methods/importCharacterFromDiceCloudInstance.js @@ -16,7 +16,8 @@ if (Meteor.isServer) { function importApiCreature(apiCreature, userId) { const apiVersion = apiCreature.meta?.schemaVersion ?? 2; - const creatureId = apiCreature.creatures[0]._id; + const creature = apiCreature.creatures[0]; + const creatureId = creature._id; if (SCHEMA_VERSION < apiVersion) { throw new Meteor.Error('Incompatible', 'The creature on the remote server is from a newer version of DiceCloud') @@ -25,16 +26,17 @@ function importApiCreature(apiCreature, userId) { // Migrate and verify the archive meets the current schema migrateApiCreature(apiCreature); + // Asset that the api creature is (mildly) safe verifyArchiveSafety({ - creature: apiCreature.creatures[0], + creature, properties: apiCreature.creatureProperties ?? [], experiences: apiCreature.experiences ?? [], logs: apiCreature.logs ?? [], }); // Don't upload creatures twice - const existingCreature = Creatures.findOne(apiCreature.creatures[0]._id, { + const existingCreature = Creatures.findOne(creature._id, { fields: { _id: 1 } }); @@ -42,7 +44,13 @@ function importApiCreature(apiCreature, userId) { 'The creature you are trying to import already exists in this database.') // Ensure the user owns the restored creature - apiCreature.creatures[0].owner = userId; + creature.owner = userId; + // Remove the sharing permissions, the ids of users on this instance aren't going to match + creature.readers = []; + creature.writers = []; + + // Mark the creature as dirty so that it recomputes + creature.dirty = true; // Ensure there is only 1 creature being imported if (apiCreature.creatures.length !== 1) { @@ -53,7 +61,7 @@ function importApiCreature(apiCreature, userId) { // Insert the creature sub documents // They still have their original _id's - Creatures.insert(apiCreature.creatures[0]); + Creatures.insert(creature); try { // Add all the properties if (apiCreature.creatureProperties && apiCreature.creatureProperties.length) {