From 627c52e8457622c7ea3be71c322016b08df6effa Mon Sep 17 00:00:00 2001 From: Charlie Humphreys Date: Thu, 26 May 2022 04:40:33 +0000 Subject: [PATCH] Add comments and adjust google brew for loop --- server/app.js | 24 ++++++++++++------------ server/homebrew.api.js | 24 ++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/server/app.js b/server/app.js index 74c66508f..b1397ca13 100644 --- a/server/app.js +++ b/server/app.js @@ -189,19 +189,19 @@ app.get('/user/:username', async (req, res, next)=>{ console.error(err); }); - for (const brew of brews) { - const match = googleBrews?.findIndex((b)=>b.editId === brew.editId) ?? -1; - if(match !== -1) { - brew.googleId = googleBrews[match].googleId; - brew.stubbed = true; - brew.pageCount = googleBrews[match].pageCount; - brew.renderer = googleBrews[match].renderer; - brew.version = googleBrews[match].version; - googleBrews.splice(match, 1); - } - } - if(googleBrews && googleBrews.length > 0) { + for (const brew of brews) { + const match = googleBrews.findIndex((b)=>b.editId === brew.editId); + if(match !== -1) { + brew.googleId = googleBrews[match].googleId; + brew.stubbed = true; + brew.pageCount = googleBrews[match].pageCount; + brew.renderer = googleBrews[match].renderer; + brew.version = googleBrews[match].version; + googleBrews.splice(match, 1); + } + } + googleBrews = googleBrews.map((brew)=>({ ...brew, authors: [req.account.username] })); brews = _.concat(brews, googleBrews); } diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 460127b1b..a7225beab 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -261,15 +261,8 @@ const deleteBrew = async (req, res)=>{ const { googleId, editId } = brew; const account = req.account; const isOwner = account && (brew.authors.length === 0 || brew.authors[0] === account.username); - console.log(brew.authors, googleId, account.username); - let afterSave = async ()=>true; - if(googleId && isOwner) { - afterSave = async ()=>await deleteGoogleBrew(account, googleId, editId, res) - .catch((err)=>{ - console.error(err); - res.status(500).send(err); - }); - } + // If the user is the owner and the file is saved to google, mark the google brew for deletion + const deleteGoogleBrew = googleId && isOwner; if(brew._id) { brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew); @@ -287,7 +280,8 @@ const deleteBrew = async (req, res)=>{ throw { status: 500, message: 'Error while removing' }; }); } else { - if(googleId && isOwner) { + if(deleteGoogleBrew) { + // When there are still authors remaining, we delete the google brew but store the full brew in the Homebrewery database brew.googleId = undefined; brew.textBin = zlib.deflateRawSync(brew.text); brew.text = undefined; @@ -300,8 +294,14 @@ const deleteBrew = async (req, res)=>{ }); } } - const after = await afterSave(); - if(!after) return; + if(deleteGoogleBrew) { + const deleted = await deleteGoogleBrew(account, googleId, editId, res) + .catch((err)=>{ + console.error(err); + res.status(500).send(err); + }); + if(!deleted) return; + } res.status(204).send(); };