0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 14:12:40 +00:00

Add comments and adjust google brew for loop

This commit is contained in:
Charlie Humphreys
2022-05-26 04:40:33 +00:00
parent 55c91217ab
commit 627c52e845
2 changed files with 24 additions and 24 deletions

View File

@@ -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);
}

View File

@@ -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();
};