0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 20:42:43 +00:00

Merge branch 'google-document-stubs' of https://github.com/jeddai/homebrewery into pr/2114

This commit is contained in:
Trevor Buckner
2022-05-28 23:10:53 -04:00
5 changed files with 39 additions and 28 deletions

View File

@@ -76,7 +76,7 @@ const BrewItem = createClass({
if(!this.props.brew.shareId) return;
let shareLink = this.props.brew.shareId;
if(this.props.brew.googleId) {
if(this.props.brew.googleId && !this.props.brew.stubbed) {
shareLink = this.props.brew.googleId + shareLink;
}

View File

@@ -210,9 +210,7 @@ const EditPage = createClass({
});
this.savedBrew = res.body;
if(transfer) {
history.replaceState(null, null, `/edit/${this.savedBrew.editId}`);
}
history.replaceState(null, null, `/edit/${this.savedBrew.editId}`);
this.setState((prevState)=>({
brew : _.merge({}, prevState.brew, {
@@ -340,7 +338,7 @@ const EditPage = createClass({
},
processShareId : function() {
return this.state.brew.googleId ?
return this.state.brew.googleId && !this.state.brew.stubbed ?
this.state.brew.googleId + this.state.brew.shareId :
this.state.brew.shareId;
},

View File

@@ -49,7 +49,7 @@ const SharePage = createClass({
},
processShareId : function() {
return this.props.brew.googleId ?
return this.props.brew.googleId && !this.props.brew.stubbed ?
this.props.brew.googleId + this.props.brew.shareId :
this.props.brew.shareId;
},

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);
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.filter((brew)=>brew.googleId)) {
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

@@ -48,7 +48,7 @@ const getBrew = (accessType)=>{
// If we can't find the google brew and there is a google id for the brew, throw an error.
if(!googleBrew) throw googleError;
// Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew
stub = stub ? _.assign(excludeStubProps(stub), excludeGoogleProps(googleBrew)) : googleBrew;
stub = stub ? _.assign({ ...excludeStubProps(stub), stubbed: true }, excludeGoogleProps(googleBrew)) : googleBrew;
}
// If after all of that we still don't have a brew, throw an exception
@@ -257,17 +257,15 @@ const deleteGoogleBrew = async (account, id, editId, res)=>{
};
const deleteBrew = async (req, res)=>{
const { brew, account } = req;
if(brew.googleId) {
const googleDeleted = await deleteGoogleBrew(account, brew.googleId, brew.editId, res)
.catch((err)=>{
console.error(err);
res.status(500).send(err);
});
if(!googleDeleted) return;
}
let brew = req.brew;
const { googleId, editId } = brew;
const account = req.account;
const isOwner = account && (brew.authors.length === 0 || brew.authors[0] === account.username);
// 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);
if(account) {
// Remove current user as author
brew.authors = _.pull(brew.authors, account.username);
@@ -282,6 +280,13 @@ const deleteBrew = async (req, res)=>{
throw { status: 500, message: 'Error while removing' };
});
} else {
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;
}
// Otherwise, save the brew with updated author list
await brew.save()
.catch((err)=>{
@@ -289,6 +294,14 @@ const deleteBrew = async (req, res)=>{
});
}
}
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();
};