From c4a875669e0ecaf8dcc47eab06d578e41a41dd71 Mon Sep 17 00:00:00 2001 From: Sebastien DUMETZ Date: Wed, 24 Jan 2024 13:50:52 +0100 Subject: [PATCH] dereference everywhere --- source/server/routes/scenes/put/document.ts | 29 ++----- source/server/utils/merge/types.ts | 83 +++++++++++++++++++-- source/server/utils/schema/setup.d.ts | 1 + 3 files changed, 83 insertions(+), 30 deletions(-) diff --git a/source/server/routes/scenes/put/document.ts b/source/server/routes/scenes/put/document.ts index f92f3dde..f7d26094 100644 --- a/source/server/routes/scenes/put/document.ts +++ b/source/server/routes/scenes/put/document.ts @@ -7,25 +7,6 @@ import { BadRequestError } from "../../../utils/errors.js"; import * as merge from "../../../utils/merge/index.js"; -/** - * Simple case of PUT /scenes/:scene/scene.svx.json - * Overwrites the current document. - * @param req - * @param res - * @returns - */ -async function overwritePutDocument(req: Request, res :Response){ - const uid = getUserId(req); - const {scene} = req.params; - const newDoc = req.body; - - let s = JSON.stringify(newDoc, null, 2); - if(s == "{}") throw new BadRequestError(`Invalid json document`); - const id = await getVfs(req).writeDoc(s, scene, uid); - res.cookie("docID", `${id}`, {sameSite: "strict", path: path.dirname(req.originalUrl)}); - return res.status(204).send(); -} - /** * Special handler for svx files to disallow the upload of invalid JSON. @@ -37,8 +18,12 @@ export default async function handlePutDocument(req :Request, res :Response){ const {scene} = req.params; const newDoc = req.body; const refId = newDoc?.asset?.id; - if(!refId) return await overwritePutDocument(req, res); - else delete newDoc.asset.id; //Don't write this to DB + if(!refId){ + await getVfs(req).writeDoc(JSON.stringify(newDoc), scene, uid); + return res.status(204).send(); + } + + delete newDoc.asset.id; //Don't write this to DB await getVfs(req).isolate(async (tr)=>{ // perform a diff of the document with the reference one @@ -61,6 +46,6 @@ export default async function handlePutDocument(req :Request, res :Response){ if(s == "{}") throw new BadRequestError(`Invalid json document`); let id = await tr.writeDoc(s, scene, uid); res.status(204).send(); - }) + }); }; diff --git a/source/server/utils/merge/types.ts b/source/server/utils/merge/types.ts index efb65d2c..903694f5 100644 --- a/source/server/utils/merge/types.ts +++ b/source/server/utils/merge/types.ts @@ -1,9 +1,9 @@ import { IDocumentAsset, IScene, INode, ICamera, ILight, Matrix4, Quaternion, Vector3 } from "../schema/document.js"; -import { IMeta } from "../schema/meta.js"; +import { IArticle, IAudioClip, IImage, IMeta } from "../schema/meta.js"; import { IModel, TUnitType } from "../schema/model.js"; -import { ISetup } from "../schema/setup.js"; -import { Index } from "../schema/types.js"; +import { IBackground, IEnvironment, IFloor, IGrid, IInterface, ILanguage, INavigation, IReader, ISetup, ISlicer, ISnapshots, ITape, ITour, ITourStep, ITours, IViewer } from "../schema/setup.js"; +import { Dictionary, Index } from "../schema/types.js"; /** * Special symbol to mark a field for deletion in a diff object @@ -19,9 +19,9 @@ export type Diff = { */ export interface DerefScene{ name?: string; - nodes: DerefNode[]; + nodes: NameMap; setup?: ISetup; - meta?: IMeta; + meta?: DerefMeta; units: TUnitType; } @@ -29,8 +29,8 @@ export interface DerefScene{ * INode where all indexed references were replaced by pointers to the actual objects. */ export interface DerefNode { - name?: string; - children?: DerefNode[]; + name: string; + children?: NameMap; matrix?: Matrix4; translation?: Vector3; @@ -40,7 +40,74 @@ export interface DerefNode { camera?: ICamera; light?: ILight; model?: IModel; - meta?: IMeta; + meta?: DerefMeta; +} + +export interface DerefTour{ + id?: string; //ID is optional because it does not exist on old scenes + title: string; + titles?: Dictionary; + //Tour steps are indirectly linked to Setup.snapshots through their ID. + //This linkage is not dereferenced because there is no known case where it needs to be. + steps: IdMap; + lead?: string; + leads?: Dictionary; + //Unimportant properties are kept as arrays + tags?: string[]; + //Unimportant properties are kept as arrays + taglist?: Dictionary; +} + +export interface DerefSnapshots{ + features: string[]; + targets: string[]; + states: IdMap<{ + id: string; + curve: string; + duration: number; + threshold: number; + values: any[]; + }>; +} + +export interface DerefMeta { + collection?: Dictionary; + process?: Dictionary; + images?: UriMap; + articles?: IdMap; + audio?: IdMap; + leadArticle?: Index; +} + +export interface DerefSetup +{ + interface?: IInterface; + viewer?: IViewer; + reader?: IReader; + navigation?: INavigation; + background?: IBackground; + environment?: IEnvironment, + language?: ILanguage, + floor?: IFloor; + grid?: IGrid; + tape?: ITape; + slicer?: ISlicer; + tours?: IdMap; + snapshots?: DerefSnapshots; +} + +//////// +// Maps are replacing arrays of identified nodes with a map-by-id +export type IdMap = { + [id: string]: T; +} + +export type NameMap = { + [name: string]: T; +} + +export type UriMap = { + [uri: string]: T; } /** diff --git a/source/server/utils/schema/setup.d.ts b/source/server/utils/schema/setup.d.ts index 29d32424..13ef59fe 100644 --- a/source/server/utils/schema/setup.d.ts +++ b/source/server/utils/schema/setup.d.ts @@ -182,6 +182,7 @@ export interface ISnapshots export interface ITour { + id?: string; //ID is optional because it does not exist on old scenes title: string; titles?: Dictionary; steps: ITourStep[];