From ea49a64261fd60d4d4fb3d96be688bd4824f9088 Mon Sep 17 00:00:00 2001 From: snwolak Date: Fri, 8 Feb 2019 18:35:21 +0100 Subject: [PATCH] add following function --- functions/follow.js | 50 +++++++++++++++++++++++++++++++++++++++++++++ functions/index.js | 6 +++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 functions/follow.js diff --git a/functions/follow.js b/functions/follow.js new file mode 100644 index 0000000..e1fb1b8 --- /dev/null +++ b/functions/follow.js @@ -0,0 +1,50 @@ +const defaultApp = require('./defaultApp') +const db = defaultApp.app.firestore(); +const app = (data, context) => { + //Adding or removing follow + console.log(data) + const docRef = db.collection('users').doc(data.username) + 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.action === "unfollow") { + //removing follow + return docRef.get().then(res => res.data()).then(res => { + const filtered = res.following.filter(obj => { + return obj.username !== data.usernameToFollow + }) + const following = res.following !== undefined ? filtered : [] + return docRef.update({following: following}) + }).catch(err => console.log(err)) + } else if(data.action === "follow") { + //adding a follow + const user = [ + { + timestamp: Date.now(), + id: data.id, + username: data.usernameToFollow, + platform: data.platform, + } + ] + return docRef.get().then(res => res.data()).then(res => { + const find = res.following.find(obj => obj.username === data.usernameToFollow) + if(find) { + return void 0 + } + const following = res.following !== undefined ? user.concat(res.following) : vote + return docRef.update({following: following}) + }).catch(err => console.log(err)) + } + } + +}; +module.exports = { + app +} diff --git a/functions/index.js b/functions/index.js index ee17707..0e040d7 100644 --- a/functions/index.js +++ b/functions/index.js @@ -8,6 +8,8 @@ const createProfileSteem = require('./createProfileSteem') const saveComment = require('./saveComment') const editTheme = require('./editTheme') const upvotePost = require('./upvotePost') +const follow = require('./follow') +const upvoteSteemPost = require('./upvoteSteemPost') exports.reciveToken = functions.https.onRequest(reciveToken.app); exports.deletePost = functions.https.onRequest(deletePost.app); @@ -15,4 +17,6 @@ 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.upvotePost = functions.https.onCall(upvotePost.app) \ No newline at end of file +exports.upvotePost = functions.https.onCall(upvotePost.app) +exports.follow = functions.https.onCall(follow.app) +exports.upvoteSteemPost = functions.https.onCall(upvoteSteemPost.app) \ No newline at end of file