mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-20 07:32:43 +00:00
update app, googleActions, and homebrew api based on PR feedback
This commit is contained in:
@@ -200,13 +200,16 @@ app.get('/user/:username', async (req, res, next)=>{
|
|||||||
});
|
});
|
||||||
|
|
||||||
if(ownAccount && req?.account?.googleId){
|
if(ownAccount && req?.account?.googleId){
|
||||||
const googleBrews = await GoogleActions.listGoogleBrews(req, res)
|
const auth = await GoogleActions.authCheck(req.account, res);
|
||||||
.catch((err)=>{
|
let googleBrews = await GoogleActions.listGoogleBrews(auth)
|
||||||
console.error(err);
|
.catch((err)=>{
|
||||||
});
|
console.error(err);
|
||||||
|
});
|
||||||
|
|
||||||
if(googleBrews)
|
if(googleBrews) {
|
||||||
|
googleBrews = googleBrews.map((brew)=>({ ...brew, authors: [req.account.username] }));
|
||||||
brews = _.concat(brews, googleBrews);
|
brews = _.concat(brews, googleBrews);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
req.brews = _.map(brews, (brew)=>{
|
req.brews = _.map(brews, (brew)=>{
|
||||||
|
|||||||
@@ -12,10 +12,7 @@ let serviceAuth;
|
|||||||
try {
|
try {
|
||||||
serviceAuth = google.auth.fromJSON(keys);
|
serviceAuth = google.auth.fromJSON(keys);
|
||||||
serviceAuth.scopes = [
|
serviceAuth.scopes = [
|
||||||
'https://www.googleapis.com/auth/drive',
|
'https://www.googleapis.com/auth/drive'
|
||||||
'https://www.googleapis.com/auth/drive.appdata',
|
|
||||||
'https://www.googleapis.com/auth/drive.file',
|
|
||||||
'https://www.googleapis.com/auth/drive.metadata'
|
|
||||||
];
|
];
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
console.warn(err);
|
||||||
@@ -94,15 +91,8 @@ const GoogleActions = {
|
|||||||
return folderId;
|
return folderId;
|
||||||
},
|
},
|
||||||
|
|
||||||
listGoogleBrews : async (req, res)=>{
|
listGoogleBrews : async (auth)=>{
|
||||||
|
const drive = google.drive({ version: 'v3', auth });
|
||||||
const oAuth2Client = GoogleActions.authCheck(req.account, res);
|
|
||||||
|
|
||||||
//TODO: Change to service account to allow non-owners to view published files.
|
|
||||||
// Requires a driveId parameter in the drive.files.list command
|
|
||||||
// Then remove the `auth` parameter from the drive object initialization
|
|
||||||
|
|
||||||
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
|
|
||||||
|
|
||||||
const obj = await drive.files.list({
|
const obj = await drive.files.list({
|
||||||
pageSize : 1000,
|
pageSize : 1000,
|
||||||
@@ -110,18 +100,18 @@ const GoogleActions = {
|
|||||||
q : 'mimeType != \'application/vnd.google-apps.folder\' and trashed = false'
|
q : 'mimeType != \'application/vnd.google-apps.folder\' and trashed = false'
|
||||||
})
|
})
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.log(`Error Listing Google Brews`);
|
console.log(`Error Listing Google Brews`);
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw (err);
|
throw (err);
|
||||||
//TODO: Should break out here, but continues on for some reason.
|
//TODO: Should break out here, but continues on for some reason.
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!obj.data.files.length) {
|
if(!obj.data.files.length) {
|
||||||
console.log('No files found.');
|
console.log('No files found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const brews = obj.data.files.map((file)=>{
|
const brews = obj.data.files.map((file)=>{
|
||||||
return {
|
return {
|
||||||
text : '',
|
text : '',
|
||||||
shareId : file.properties.shareId,
|
shareId : file.properties.shareId,
|
||||||
editId : file.properties.editId,
|
editId : file.properties.editId,
|
||||||
@@ -135,59 +125,41 @@ const GoogleActions = {
|
|||||||
views : parseInt(file.properties.views),
|
views : parseInt(file.properties.views),
|
||||||
tags : '',
|
tags : '',
|
||||||
published : file.properties.published ? file.properties.published == 'true' : false,
|
published : file.properties.published ? file.properties.published == 'true' : false,
|
||||||
authors : [req.account.username], //TODO: properly save and load authors to google drive
|
|
||||||
systems : []
|
systems : []
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return brews;
|
return brews;
|
||||||
},
|
},
|
||||||
|
|
||||||
existsGoogleBrew : async (id)=>{
|
|
||||||
const drive = google.drive({ version: 'v3' });
|
|
||||||
|
|
||||||
const result = await drive.files.get({ fileId: id })
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log('error checking file exists...');
|
|
||||||
console.error(err);
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
if(result){return true;}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
|
|
||||||
updateGoogleBrew : async (brew)=>{
|
updateGoogleBrew : async (brew)=>{
|
||||||
const drive = google.drive({ version: 'v3' });
|
const drive = google.drive({ version: 'v3' });
|
||||||
|
|
||||||
if(await GoogleActions.existsGoogleBrew(brew.googleId) == true) {
|
await drive.files.update({
|
||||||
await drive.files.update({
|
fileId : brew.googleId,
|
||||||
fileId : brew.googleId,
|
resource : {
|
||||||
resource : {
|
name : `${brew.title}.txt`,
|
||||||
name : `${brew.title}.txt`,
|
description : `${brew.description}`,
|
||||||
description : `${brew.description}`,
|
properties : {
|
||||||
properties : {
|
title : brew.title,
|
||||||
title : brew.title,
|
published : brew.published,
|
||||||
published : brew.published,
|
version : brew.version,
|
||||||
version : brew.version,
|
renderer : brew.renderer,
|
||||||
renderer : brew.renderer,
|
tags : brew.tags,
|
||||||
tags : brew.tags,
|
pageCount : brew.pageCount,
|
||||||
pageCount : brew.pageCount,
|
systems : brew.systems.join()
|
||||||
systems : brew.systems.join()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
media : {
|
|
||||||
mimeType : 'text/plain',
|
|
||||||
body : brew.text
|
|
||||||
}
|
}
|
||||||
})
|
},
|
||||||
.catch((err)=>{
|
media : {
|
||||||
console.log('Error saving to google');
|
mimeType : 'text/plain',
|
||||||
console.error(err);
|
body : brew.text
|
||||||
throw (err);
|
}
|
||||||
//return res.status(500).send('Error while saving');
|
})
|
||||||
});
|
.catch((err)=>{
|
||||||
}
|
console.log('Error saving to google');
|
||||||
|
console.error(err);
|
||||||
|
throw (err);
|
||||||
|
//return res.status(500).send('Error while saving');
|
||||||
|
});
|
||||||
|
|
||||||
return (brew);
|
return (brew);
|
||||||
},
|
},
|
||||||
@@ -322,9 +294,8 @@ const GoogleActions = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteGoogleBrew : async (req, res, id)=>{
|
deleteGoogleBrew : async (auth, id)=>{
|
||||||
const oAuth2Client = GoogleActions.authCheck(req.account, res);
|
const drive = google.drive({ version: 'v3', auth });
|
||||||
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
|
|
||||||
|
|
||||||
const googleId = id.slice(0, -12);
|
const googleId = id.slice(0, -12);
|
||||||
const accessId = id.slice(-12);
|
const accessId = id.slice(-12);
|
||||||
@@ -336,7 +307,6 @@ const GoogleActions = {
|
|||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.log('Error loading from Google');
|
console.log('Error loading from Google');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if(obj && obj.data.properties.editId != accessId) {
|
if(obj && obj.data.properties.editId != accessId) {
|
||||||
@@ -351,8 +321,6 @@ const GoogleActions = {
|
|||||||
console.log('Can\'t delete Google file');
|
console.log('Can\'t delete Google file');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).send();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
increaseView : async (id, accessId, accessType, brew)=>{
|
increaseView : async (id, accessId, accessType, brew)=>{
|
||||||
|
|||||||
@@ -185,6 +185,10 @@ router.put('/api/update/:id', updateBrew);
|
|||||||
router.put('/api/updateGoogle/:id', updateGoogleBrew);
|
router.put('/api/updateGoogle/:id', updateGoogleBrew);
|
||||||
router.delete('/api/:id', deleteBrew);
|
router.delete('/api/:id', deleteBrew);
|
||||||
router.get('/api/remove/:id', deleteBrew);
|
router.get('/api/remove/:id', deleteBrew);
|
||||||
router.get('/api/removeGoogle/:id', (req, res)=>{GoogleActions.deleteGoogleBrew(req, res, req.params.id);});
|
router.get('/api/removeGoogle/:id', async (req, res)=>{
|
||||||
|
const auth = await GoogleActions.authCheck(req.account, res);
|
||||||
|
await GoogleActions.deleteGoogleBrew(auth, req.params.id);
|
||||||
|
return res.status(200).send();
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Reference in New Issue
Block a user