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

Commit

Permalink
add upvotePost function
Browse files Browse the repository at this point in the history
  • Loading branch information
snwolak committed Jan 26, 2019
1 parent 7ad1a28 commit e51fb96
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
exports.editTheme = functions.https.onCall(editTheme.app)
exports.upvotePost = functions.https.onCall(upvotePost.app)
50 changes: 50 additions & 0 deletions functions/upvotePost.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e51fb96

Please sign in to comment.