Skip to content

Commit

Permalink
hide document merge behind a feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Feb 12, 2024
1 parent 12405fc commit 4e78ed3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions source/server/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ describe("Web Server Integration", function(){
.set("Content-Type", "application/si-dpo-3d.document+json")
.send(c)
.expect(204);

let {text} = await this.agent.get("/scenes/foo/foo.svx.json")
.expect(200)
.expect("Content-Type", "application/si-dpo-3d.document+json");
Expand Down
2 changes: 1 addition & 1 deletion source/server/migrations/004-node-ids.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WITH src AS (
json_group_array(
json_set(
json_extract(data,'$.nodes[' || key || ']'),
'$.id', hex(randomBlob(6))
'$.id', lower(hex(randomBlob(6)))
)
) as data
FROM src, json_each(src.nodes)
Expand Down
2 changes: 1 addition & 1 deletion source/server/routes/scenes/put/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("PUT /scenes/:scene/scene.svx.json", function(){
let titleSlug :string, scene_id :number, sampleDoc :any;

this.beforeAll(async function(){
let locals = await createIntegrationContext(this);
let locals = await createIntegrationContext(this, {enable_document_merge: true});
vfs = locals.vfs;
userManager = locals.userManager;
user = await userManager.addUser("bob", "12345678");
Expand Down
9 changes: 4 additions & 5 deletions source/server/routes/scenes/put/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@ import {inspect} from "util";
import path from "path";
import { Request, Response } from "express";

import { getUserId, getVfs } from "../../../utils/locals.js";
import { getLocals, getUserId, getVfs } from "../../../utils/locals.js";
import { BadRequestError } from "../../../utils/errors.js";

import * as merge from "../../../utils/merge/index.js";


/**
* Special handler for svx files to disallow the upload of invalid JSON.
* @todo Should check against the official json schema using ajv
* If the user provides a reference document ID and the document has been updated since, a diff is performed to try to merge the changes.
*/
export default async function handlePutDocument(req :Request, res :Response){
const {config} = getLocals(req);
const uid = getUserId(req);
const {scene} = req.params;
const newDoc = req.body;
const refId = newDoc?.asset?.id;
if(typeof refId !== "undefined") delete newDoc.asset.id; //Don't write this to DB

if(typeof newDoc !== "object"|| !Object.keys(newDoc).length) throw new BadRequestError(`Invalid json document`);

if(!refId){
if(!refId || !config.enable_document_merge){
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
const {id: scene_id} = await tr.getScene(scene);
Expand Down
4 changes: 4 additions & 0 deletions source/server/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const values = {
hot_reload:[false, toBool],
smart_host: ["smtp://localhost", toString],
verbose: [false, toBool],

/// FEATURE FLAGS ///
enable_document_merge: [false, toBool],

} as const;


Expand Down

0 comments on commit 4e78ed3

Please sign in to comment.