From 47f912750b2664b97f3d9496741d8071a2b6f27b Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sat, 13 Jul 2024 17:44:23 -0400 Subject: [PATCH] Extract getting userThemes from `getBrew()` `getBrew()` should do one thing only; retrieve a brew. UI elements like the list of themes available to the user are not part of a brew. Moved into the handers for the `/edit/` and `/new/` endpoints --- server/app.js | 7 +++++-- server/homebrew.api.js | 7 ------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/server/app.js b/server/app.js index 9680c64ca..6c791d634 100644 --- a/server/app.js +++ b/server/app.js @@ -9,7 +9,7 @@ const yaml = require('js-yaml'); const app = express(); const config = require('./config.js'); -const { homebrewApi, getBrew, getThemeBundle } = require('./homebrew.api.js'); +const { homebrewApi, getBrew, getThemeBundle, getUsersBrewThemes } = require('./homebrew.api.js'); const GoogleActions = require('./googleActions.js'); const serveCompressedStaticAssets = require('./static-assets.mv.js'); const sanitizeFilename = require('sanitize-filename'); @@ -274,6 +274,8 @@ app.get('/user/:username', async (req, res, next)=>{ app.get('/edit/:id', asyncHandler(getBrew('edit')), async(req, res, next)=>{ req.brew = req.brew.toObject ? req.brew.toObject() : req.brew; + req.userThemes = await(getUsersBrewThemes(req.account?.username)); + req.ogMeta = { ...defaultMetaTags, title : req.brew.title || 'Untitled Brew', description : req.brew.description || 'No description.', @@ -299,10 +301,11 @@ app.get('/new/:id', asyncHandler(getBrew('share')), (req, res, next)=>{ renderer : req.brew.renderer, theme : req.brew.theme, tags : req.brew.tags, - userThemes : req.brew.userThemes }; req.brew = _.defaults(brew, DEFAULT_BREW); + req.userThemes = await(getUsersBrewThemes(req.account?.username)); + req.ogMeta = { ...defaultMetaTags, title : 'New', description : 'Start crafting your homebrew on the Homebrewery!' diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 6da3d99af..ee361ea92 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -135,15 +135,11 @@ const api = { throw { name: 'BrewLoad Error', message: 'Brew not found', status: 404, HBErrorCode: '05', accessType: accessType, brewId: id }; } - const mainAuthor = stub.authors ? stub.authors[0] : ''; - const userID = req?.account?.username && (accessType === 'edit') ? req.account.username : mainAuthor; - // Clean up brew: fill in missing fields with defaults / fix old invalid values if(stub) { 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 - stub.userThemes = userThemes; } req.brew = stub ?? {}; @@ -382,9 +378,6 @@ const api = { brew.title = brew.title.trim(); brew.description = brew.description.trim() || ''; brew.text = api.mergeBrewText(brew); - const userID = req?.account?.username ? req.account.username : brew.authors.split(',')[0]; - brew.userThemes = await api.getUsersBrewThemes(userID, req, res, null); - if(brew.googleId && removeFromGoogle) { // If the google id exists and we're removing it from google, set afterSave to delete the google brew and mark the brew's google id as undefined