diff --git a/functions/upvotePost.js b/functions/upvotePost.js index 018e0ff..1f4cbc8 100644 --- a/functions/upvotePost.js +++ b/functions/upvotePost.js @@ -16,13 +16,12 @@ const app = (data, context) => { if(checkToken) { if(data.weight === 0) { //weight 0 means user is unvoting the post - docRef.get().then(res => res.data()).then(res => { + return 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}) + return docRef.update({upvotes: upvotes}) }).catch(err => console.log(err)) } else if(data.weight > 0) { //upvoting the post @@ -36,10 +35,13 @@ const app = (data, context) => { weight: data.weight } ] - docRef.get().then(res => res.data()).then(res => { + return docRef.get().then(res => res.data()).then(res => { + const find = res.upvotes.find(obj => obj.uid === data.uid) + if(find) { + return void 0 + } 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}) + return docRef.update({upvotes: upvotes}) }).catch(err => console.log(err)) } } diff --git a/functions/upvoteSteemPost.js b/functions/upvoteSteemPost.js new file mode 100644 index 0000000..8da2423 --- /dev/null +++ b/functions/upvoteSteemPost.js @@ -0,0 +1,51 @@ +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 + return docRef.get().then(res => res.data()).then(res => { + const filtered = res.active_votes.filter(vote => { + return vote.voter !== data.voter + }) + const votes = res.active_votes !== undefined ? filtered : [] + return docRef.update({active_votes: votes}) + }).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, + weight: data.weight + } + ] + return docRef.get().then(res => res.data()).then(res => { + const find = res.active_votes.find(obj => obj.uid === data.uid) + if(find) { + return void 0 + } + const votes = res.active_votes !== undefined ? vote.concat(res.active_votes) : vote + return docRef.update({active_votes: votes}) + }).catch(err => console.log(err)) + } + } + +}; +module.exports = { + app +}