mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-17 21:02:49 +00:00
fix deletion, update urls
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ app.get('/user/:username', async (req, res, next)=>{
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (const brew of brews) {
|
for (const brew of brews) {
|
||||||
const match = googleBrews.findIndex((b)=>b.editId === brew.editId);
|
const match = googleBrews?.findIndex((b)=>b.editId === brew.editId) ?? -1;
|
||||||
if(match !== -1) {
|
if(match !== -1) {
|
||||||
brew.googleId = googleBrews[match].googleId;
|
brew.googleId = googleBrews[match].googleId;
|
||||||
brew.stubbed = true;
|
brew.stubbed = true;
|
||||||
|
|||||||
@@ -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,22 @@ 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;
|
||||||
|
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)=>{
|
.catch((err)=>{
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(500).send(err);
|
res.status(500).send(err);
|
||||||
});
|
});
|
||||||
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 +287,12 @@ const deleteBrew = async (req, res)=>{
|
|||||||
throw { status: 500, message: 'Error while removing' };
|
throw { status: 500, message: 'Error while removing' };
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if(googleId && isOwner) {
|
||||||
|
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 +300,8 @@ const deleteBrew = async (req, res)=>{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const after = await afterSave();
|
||||||
|
if(!after) return;
|
||||||
|
|
||||||
res.status(204).send();
|
res.status(204).send();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user