0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-04 23:22:42 +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; if(!this.props.brew.shareId) return;
let shareLink = this.props.brew.shareId; 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; shareLink = this.props.brew.googleId + shareLink;
} }

View File

@@ -210,9 +210,7 @@ const EditPage = createClass({
}); });
this.savedBrew = res.body; 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)=>({ this.setState((prevState)=>({
brew : _.merge({}, prevState.brew, { brew : _.merge({}, prevState.brew, {
@@ -340,7 +338,7 @@ const EditPage = createClass({
}, },
processShareId : function() { 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.googleId + this.state.brew.shareId :
this.state.brew.shareId; this.state.brew.shareId;
}, },

View File

@@ -49,7 +49,7 @@ const SharePage = createClass({
}, },
processShareId : function() { 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.googleId + this.props.brew.shareId :
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); 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) { 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] })); googleBrews = googleBrews.map((brew)=>({ ...brew, authors: [req.account.username] }));
brews = _.concat(brews, googleBrews); 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 we can't find the google brew and there is a google id for the brew, throw an error.
if(!googleBrew) throw googleError; if(!googleBrew) throw googleError;
// Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew // 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 // 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 deleteBrew = async (req, res)=>{
const { brew, account } = req; let brew = req.brew;
if(brew.googleId) { const { googleId, editId } = brew;
const googleDeleted = await deleteGoogleBrew(account, brew.googleId, brew.editId, res) const account = req.account;
.catch((err)=>{ const isOwner = account && (brew.authors.length === 0 || brew.authors[0] === account.username);
console.error(err); // If the user is the owner and the file is saved to google, mark the google brew for deletion
res.status(500).send(err); const deleteGoogleBrew = googleId && isOwner;
});
if(!googleDeleted) return;
}
if(brew._id) { if(brew._id) {
brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew);
if(account) { if(account) {
// Remove current user as author // Remove current user as author
brew.authors = _.pull(brew.authors, account.username); brew.authors = _.pull(brew.authors, account.username);
@@ -282,6 +280,13 @@ const deleteBrew = async (req, res)=>{
throw { status: 500, message: 'Error while removing' }; throw { status: 500, message: 'Error while removing' };
}); });
} else { } 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 // Otherwise, save the brew with updated author list
await brew.save() await brew.save()
.catch((err)=>{ .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(); res.status(204).send();
}; };