Skip to content

Commit

Permalink
dereference everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Jan 24, 2024
1 parent b66652a commit c4a8756
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 30 deletions.
29 changes: 7 additions & 22 deletions source/server/routes/scenes/put/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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();
})
});

};
83 changes: 75 additions & 8 deletions source/server/utils/merge/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,18 +19,18 @@ export type Diff<T> = {
*/
export interface DerefScene{
name?: string;
nodes: DerefNode[];
nodes: NameMap<DerefNode>;
setup?: ISetup;
meta?: IMeta;
meta?: DerefMeta;
units: TUnitType;
}

/**
* INode where all indexed references were replaced by pointers to the actual objects.
*/
export interface DerefNode {
name?: string;
children?: DerefNode[];
name: string;
children?: NameMap<DerefNode>;

matrix?: Matrix4;
translation?: Vector3;
Expand All @@ -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<string>;
//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<ITourStep>;
lead?: string;
leads?: Dictionary<string>;
//Unimportant properties are kept as arrays
tags?: string[];
//Unimportant properties are kept as arrays
taglist?: Dictionary<string[]>;
}

export interface DerefSnapshots{
features: string[];
targets: string[];
states: IdMap<{
id: string;
curve: string;
duration: number;
threshold: number;
values: any[];
}>;
}

export interface DerefMeta {
collection?: Dictionary<any>;
process?: Dictionary<any>;
images?: UriMap<IImage>;
articles?: IdMap<IArticle>;
audio?: IdMap<IAudioClip>;
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<ITour & {id:string}>;
snapshots?: DerefSnapshots;
}

////////
// Maps are replacing arrays of identified nodes with a map-by-id
export type IdMap<T extends {id:string}> = {
[id: string]: T;
}

export type NameMap<T extends {name :string}> = {
[name: string]: T;
}

export type UriMap<T extends {uri :string}> = {
[uri: string]: T;
}

/**
Expand Down
1 change: 1 addition & 0 deletions source/server/utils/schema/setup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
steps: ITourStep[];
Expand Down

0 comments on commit c4a8756

Please sign in to comment.