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

Commit

Permalink
add following function
Browse files Browse the repository at this point in the history
  • Loading branch information
snwolak committed Feb 8, 2019
1 parent 41e5e57 commit ea49a64
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
50 changes: 50 additions & 0 deletions functions/follow.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 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
}
6 changes: 5 additions & 1 deletion functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ 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);
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)
exports.upvotePost = functions.https.onCall(upvotePost.app)
exports.follow = functions.https.onCall(follow.app)
exports.upvoteSteemPost = functions.https.onCall(upvoteSteemPost.app)

0 comments on commit ea49a64

Please sign in to comment.