diff --git a/functions/index.js b/functions/index.js index c082252..ee17707 100644 --- a/functions/index.js +++ b/functions/index.js @@ -2,17 +2,17 @@ const functions = require('firebase-functions'); const reciveToken = require('./reciveToken') -const syncPosts = require('./syncPosts') const deletePost = require('./deletePost') const createProfile = require('./createProfile') const createProfileSteem = require('./createProfileSteem') const saveComment = require('./saveComment') const editTheme = require('./editTheme') +const upvotePost = require('./upvotePost') -exports.syncPosts = functions.https.onRequest(syncPosts.app); exports.reciveToken = functions.https.onRequest(reciveToken.app); exports.deletePost = functions.https.onRequest(deletePost.app); exports.createProfile = functions.https.onCall(createProfile.app); exports.createProfileSteem = functions.https.onCall(createProfileSteem.app); exports.saveComment = functions.https.onCall(saveComment.app) -exports.editTheme = functions.https.onCall(editTheme.app) \ No newline at end of file +exports.editTheme = functions.https.onCall(editTheme.app) +exports.upvotePost = functions.https.onCall(upvotePost.app) \ No newline at end of file diff --git a/functions/upvotePost.js b/functions/upvotePost.js new file mode 100644 index 0000000..018e0ff --- /dev/null +++ b/functions/upvotePost.js @@ -0,0 +1,50 @@ +const defaultApp = require('./defaultApp') +const db = defaultApp.app.firestore(); +const app = (data, context) => { + //Adding an upvote to the post.upvotes + console.log(data) + const docRef = db.collection('posts').doc(data.permlink) + const checkToken = defaultApp.app.auth().verifyIdToken(data.token) + .then((decodedToken) => { + const uid = decodedToken.uid; + if(uid === data.uid) { + return true + } else { + return false; + } + }).catch(err => console.log(err)) + if(checkToken) { + if(data.weight === 0) { + //weight 0 means user is unvoting the post + docRef.get().then(res => res.data()).then(res => { + const filtered = res.upvotes.filter(vote => { + return vote.voter !== data.voter + }) + const upvotes = res.upvotes !== undefined ? filtered : [] + const actions = res.actions !== undefined ? res.actions - 1 : 0 + return docRef.update({upvotes: upvotes, actions: actions}) + }).catch(err => console.log(err)) + } else if(data.weight > 0) { + //upvoting the post + const vote = [ + { + timestamp: new Date(), + id: data.id, + voter: data.voter, + uid: data.uid, + platform: data.platform, + weight: data.weight + } + ] + docRef.get().then(res => res.data()).then(res => { + const upvotes = res.upvotes !== undefined ? vote.concat(res.upvotes) : vote + const actions = res.actions !== undefined ? res.actions + 1 : 1 + return docRef.update({upvotes: upvotes, actions: actions}) + }).catch(err => console.log(err)) + } + } + +}; +module.exports = { + app +}