Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
add upvoting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
snwolak committed Feb 8, 2019
1 parent e51fb96 commit 41e5e57
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
14 changes: 8 additions & 6 deletions functions/upvotePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
Expand Down
51 changes: 51 additions & 0 deletions functions/upvoteSteemPost.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 41e5e57

Please sign in to comment.