mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-11 11:12:44 +00:00
Fix metadata in Google docs
Update view counts via service account since modifying another users' file properties requires increased permission scope
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
font-family : ScalySans;
|
font-family : ScalySans;
|
||||||
font-size : 1.2em;
|
font-size : 1.2em;
|
||||||
&>span{
|
&>span{
|
||||||
margin-right : 15px;
|
margin-right : 12px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&:hover{
|
&:hover{
|
||||||
|
|||||||
@@ -22,9 +22,8 @@ const BrewItem = require('./brewItem/brewItem.jsx');
|
|||||||
const UserPage = createClass({
|
const UserPage = createClass({
|
||||||
getDefaultProps : function() {
|
getDefaultProps : function() {
|
||||||
return {
|
return {
|
||||||
username : '',
|
username : '',
|
||||||
brews : [],
|
brews : []
|
||||||
googleBrews : []
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -149,6 +149,10 @@ app.get('/share/:id', (req, res, next)=>{
|
|||||||
const googleId = req.params.id.slice(0, -12);
|
const googleId = req.params.id.slice(0, -12);
|
||||||
const shareId = req.params.id.slice(-12);
|
const shareId = req.params.id.slice(-12);
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
||||||
|
.then((brew)=>{
|
||||||
|
GoogleActions.increaseView(googleId, shareId, 'share', brew);
|
||||||
|
return brew;
|
||||||
|
})
|
||||||
.then((brew)=>{
|
.then((brew)=>{
|
||||||
req.brew = brew; //TODO Need to sanitize later
|
req.brew = brew; //TODO Need to sanitize later
|
||||||
return next();
|
return next();
|
||||||
|
|||||||
@@ -87,11 +87,17 @@ GoogleActions = {
|
|||||||
|
|
||||||
oAuth2Client = GoogleActions.authCheck(req.account, res);
|
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
|
||||||
|
// const keys = JSON.parse(config.get('service_account'));
|
||||||
|
// const auth = google.auth.fromJSON(keys);
|
||||||
|
// auth.scopes = ['https://www.googleapis.com/auth/drive'];
|
||||||
|
|
||||||
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
|
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
|
||||||
|
|
||||||
const obj = await drive.files.list({
|
const obj = await drive.files.list({
|
||||||
pageSize : 100,
|
pageSize : 100,
|
||||||
fields : 'nextPageToken, files(id, name, modifiedTime, properties)',
|
fields : 'nextPageToken, files(id, name, description, modifiedTime, properties)',
|
||||||
q : 'mimeType != \'application/vnd.google-apps.folder\' and trashed = false'
|
q : 'mimeType != \'application/vnd.google-apps.folder\' and trashed = false'
|
||||||
})
|
})
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
@@ -107,15 +113,16 @@ GoogleActions = {
|
|||||||
text : '',
|
text : '',
|
||||||
shareId : file.properties.shareId,
|
shareId : file.properties.shareId,
|
||||||
editId : file.properties.editId,
|
editId : file.properties.editId,
|
||||||
createdAt : null,
|
createdAt : file.createdTime,
|
||||||
updatedAt : file.modifiedTime,
|
updatedAt : file.modifiedTime,
|
||||||
gDrive : true,
|
gDrive : true,
|
||||||
googleId : file.id,
|
googleId : file.id,
|
||||||
|
|
||||||
title : file.properties.title,
|
title : file.properties.title,
|
||||||
description : '',
|
description : file.description,
|
||||||
|
views : file.properties.views,
|
||||||
tags : '',
|
tags : '',
|
||||||
published : false,
|
published : file.properties.published ? file.properties.published == 'true' : false,
|
||||||
authors : [req.account.username], //TODO: properly save and load authors to google drive
|
authors : [req.account.username], //TODO: properly save and load authors to google drive
|
||||||
systems : []
|
systems : []
|
||||||
};
|
};
|
||||||
@@ -129,6 +136,8 @@ GoogleActions = {
|
|||||||
|
|
||||||
const result = await drive.files.get({ fileId: id })
|
const result = await drive.files.get({ fileId: id })
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
|
console.log('error checking file exists...');
|
||||||
|
console.log(err);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -140,12 +149,24 @@ GoogleActions = {
|
|||||||
updateGoogleBrew : async (auth, brew)=>{
|
updateGoogleBrew : async (auth, brew)=>{
|
||||||
const drive = google.drive({ version: 'v3', auth: auth });
|
const drive = google.drive({ version: 'v3', auth: auth });
|
||||||
|
|
||||||
if(await GoogleActions.existsGoogleBrew(auth, brew.googleId) == true) {
|
console.log('trying to update a brew');
|
||||||
|
console.log(brew);
|
||||||
|
|
||||||
|
if(await GoogleActions.existsGoogleBrew(auth, brew.googleId) == true) {
|
||||||
|
console.log('the brew exists at least');
|
||||||
|
console.log('going to put this text:');
|
||||||
|
console.log(brew.text);
|
||||||
await drive.files.update({
|
await drive.files.update({
|
||||||
fileId : brew.googleId,
|
fileId : brew.googleId,
|
||||||
resource : { name : `${brew.title}.txt`,
|
resource : { name : `${brew.title}.txt`,
|
||||||
properties : { title: brew.title } //AppProperties is not accessible via API key
|
description : `${brew.description}`,
|
||||||
|
properties : { title : brew.title,
|
||||||
|
published : brew.published,
|
||||||
|
lastViewed : brew.lastViewed,
|
||||||
|
views : brew.views,
|
||||||
|
version : brew.version,
|
||||||
|
tags : brew.tags,
|
||||||
|
systems : brew.systems.join() }
|
||||||
},
|
},
|
||||||
media : { mimeType : 'text/plain',
|
media : { mimeType : 'text/plain',
|
||||||
body : brew.text }
|
body : brew.text }
|
||||||
@@ -171,12 +192,14 @@ GoogleActions = {
|
|||||||
const folderId = await GoogleActions.getGoogleFolder(auth);
|
const folderId = await GoogleActions.getGoogleFolder(auth);
|
||||||
|
|
||||||
const fileMetadata = {
|
const fileMetadata = {
|
||||||
'name' : `${brew.title}.txt`,
|
'name' : `${brew.title}.txt`,
|
||||||
'parents' : [folderId],
|
'description' : `${brew.description}`,
|
||||||
'properties' : { //AppProperties is not accessible
|
'parents' : [folderId],
|
||||||
|
'properties' : { //AppProperties is not accessible
|
||||||
'shareId' : nanoid(12),
|
'shareId' : nanoid(12),
|
||||||
'editId' : nanoid(12),
|
'editId' : nanoid(12),
|
||||||
'title' : brew.title,
|
'title' : brew.title,
|
||||||
|
'views' : '0'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -206,15 +229,15 @@ GoogleActions = {
|
|||||||
text : brew.text,
|
text : brew.text,
|
||||||
shareId : fileMetadata.properties.shareId,
|
shareId : fileMetadata.properties.shareId,
|
||||||
editId : fileMetadata.properties.editId,
|
editId : fileMetadata.properties.editId,
|
||||||
createdAt : null,
|
createdAt : new Date(),
|
||||||
updatedAt : null,
|
updatedAt : new Date(),
|
||||||
gDrive : true,
|
gDrive : true,
|
||||||
googleId : obj.data.id,
|
googleId : obj.data.id,
|
||||||
|
|
||||||
title : brew.title,
|
title : brew.title,
|
||||||
description : '',
|
description : brew.description,
|
||||||
tags : '',
|
tags : '',
|
||||||
published : false,
|
published : brew.published,
|
||||||
authors : [],
|
authors : [],
|
||||||
systems : []
|
systems : []
|
||||||
};
|
};
|
||||||
@@ -227,7 +250,7 @@ GoogleActions = {
|
|||||||
|
|
||||||
const obj = await drive.files.get({
|
const obj = await drive.files.get({
|
||||||
fileId : id,
|
fileId : id,
|
||||||
fields : 'properties'
|
fields : 'properties, createdTime, modifiedTime, description'
|
||||||
})
|
})
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
console.log('Error loading from Google');
|
console.log('Error loading from Google');
|
||||||
@@ -244,6 +267,7 @@ GoogleActions = {
|
|||||||
|
|
||||||
const file = await drive.files.get({
|
const file = await drive.files.get({
|
||||||
fileId : id,
|
fileId : id,
|
||||||
|
fields : 'description, properties',
|
||||||
alt : 'media'
|
alt : 'media'
|
||||||
})
|
})
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
@@ -252,20 +276,25 @@ GoogleActions = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const brew = {
|
const brew = {
|
||||||
text : file.data,
|
shareId : obj.data.properties.shareId,
|
||||||
shareId : obj.data.properties.shareId,
|
editId : obj.data.properties.editId,
|
||||||
editId : obj.data.properties.editId,
|
title : obj.data.properties.title,
|
||||||
createdAt : null,
|
text : file.data,
|
||||||
updatedAt : null,
|
|
||||||
gDrive : true,
|
|
||||||
googleId : id,
|
|
||||||
|
|
||||||
title : obj.data.properties.title,
|
description : obj.data.description,
|
||||||
description : '',
|
tags : obj.data.properties.tags ? obj.data.properties.tags.split(',') : [],
|
||||||
tags : '',
|
systems : obj.data.properties.systems ? obj.data.properties.systems.split(',') : [],
|
||||||
published : false,
|
|
||||||
authors : [],
|
authors : [],
|
||||||
systems : []
|
published : obj.data.properties.published ? obj.data.properties.published == 'true' : false,
|
||||||
|
|
||||||
|
createdAt : obj.data.createdTime,
|
||||||
|
updatedAt : obj.data.modifiedTime,
|
||||||
|
lastViewed : obj.data.properties.lastViewed,
|
||||||
|
views : parseInt(obj.data.properties.views) || 0, //brews with no view parameter will return undefined
|
||||||
|
version : parseInt(obj.data.properties.version) || 0,
|
||||||
|
|
||||||
|
gDrive : true,
|
||||||
|
googleId : id
|
||||||
};
|
};
|
||||||
|
|
||||||
return (brew);
|
return (brew);
|
||||||
@@ -303,6 +332,29 @@ GoogleActions = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).send();
|
return res.status(200).send();
|
||||||
|
},
|
||||||
|
|
||||||
|
increaseView : async (id, accessId, accessType, brew)=>{
|
||||||
|
//service account because this is modifying another user's file properties
|
||||||
|
//so we need extended scope
|
||||||
|
const keys = JSON.parse(config.get('service_account'));
|
||||||
|
const auth = google.auth.fromJSON(keys);
|
||||||
|
auth.scopes = ['https://www.googleapis.com/auth/drive'];
|
||||||
|
|
||||||
|
const drive = google.drive({ version: 'v3', auth: auth });
|
||||||
|
|
||||||
|
await drive.files.update({
|
||||||
|
fileId : brew.googleId,
|
||||||
|
resource : { properties : { views : brew.views + 1,
|
||||||
|
lastViewed : new Date() } }
|
||||||
|
})
|
||||||
|
.catch((err)=>{
|
||||||
|
console.log('Error updating Google views');
|
||||||
|
console.error(err);
|
||||||
|
//return res.status(500).send('Error while saving');
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user