diff --git a/server/app.js b/server/app.js index dc4e0c8ac..c4c10a8cc 100644 --- a/server/app.js +++ b/server/app.js @@ -407,7 +407,10 @@ if(isLocalEnvironment){ //Render the page const templateFn = require('./../client/template.js'); app.use(asyncHandler(async (req, res, next)=>{ + + // Assuming we have not received a brew yet, populate with default values const brew = _.defaults(req.brew, DEFAULT_BREW); + // Create configuration object const configuration = { local : isLocalEnvironment, diff --git a/server/brewDefaults.js b/server/brewDefaults.js index 72dc37c0a..94243b275 100644 --- a/server/brewDefaults.js +++ b/server/brewDefaults.js @@ -1,30 +1,36 @@ const _ = require('lodash'); -// Default brew properties in most cases +// Default properties for newly-created brews const DEFAULT_BREW = { - text : '', - editId : null, - shareId : null, title : 'Untitled Brew', + text : '', + style : undefined, description : '', + editId : undefined, + shareId : undefined, + createdAt : undefined, + updatedAt : undefined, renderer : 'V3', + theme : '5ePHB', + authors : [], tags : [], systems : [], thumbnail : '', published : false, pageCount : 1, - theme : '5ePHB' + gDrive : false, + trashed : false + }; -// Default brew properties for loading -const DEFAULT_BREW_LOAD = {}; -_.defaults(DEFAULT_BREW_LOAD, +// Default values for older brews with missing properties +// e.g., missing "renderer" is assumed to be "legacy" +const DEFAULT_BREW_LOAD = _.defaults( { - renderer : 'legacy', - published : true + renderer : 'legacy', }, DEFAULT_BREW); module.exports = { DEFAULT_BREW, DEFAULT_BREW_LOAD -}; \ No newline at end of file +}; diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 77d19d638..506af9293 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -73,17 +73,13 @@ If you believe you should have access to this brew, ask the file owner to invite throw 'Brew not found in Homebrewery database or Google Drive'; } - if(typeof stub?.tags === 'string') { - stub.tags = []; - } + // Clean up brew: fill in missing fields with defaults / fix old invalid values + stub.tags = stub.tags || undefined; // Clear empty strings + stub.renderer = stub.renderer || undefined; // Clear empty strings + stub = _.defaults(stub, DEFAULT_BREW_LOAD); // Fill in blank fields + - // Use _.assignWith instead of _.defaults - does this need to be replicated at all other uses of _.defaults??? - _.assignWith(stub, DEFAULT_BREW_LOAD, (objValue, srcValue)=>{ - if(typeof objValue === 'boolean') return objValue; - return objValue || srcValue; - }); req.brew = stub; - next(); }; };