Created
April 23, 2017 09:19
-
-
Save Atrix1987/2983cff06edf89ecaac0875de6e01bae to your computer and use it in GitHub Desktop.
Firebase Function snippet for populating a user feed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| exports.updateFeed = functions.database.ref('/stories/{userId}/{storyId}').onWrite(event => { | |
| const userId = event.params.userId; | |
| const storyId = event.params.storyId; | |
| let followersRef = admin.database().ref('/followers/'+userId); | |
| if(!event.data.val()){ | |
| //post was deleted | |
| followersRef.once("value", function(snap) { | |
| snap.forEach(function(childSnapshot) { | |
| let followerId = childSnapshot.key; | |
| admin.database().ref('/feed/'+followerId+'/'+storyId).remove(); | |
| console.log('Removed post from feed of user: '+ followerId); | |
| }); | |
| }); | |
| }else{ | |
| //post was added | |
| followersRef.once("value", function(snap) { | |
| snap.forEach(function(childSnapshot) { | |
| let followerId = childSnapshot.key; | |
| admin.database().ref('/feed/'+followerId+'/'+storyId).set(event.data.val()); | |
| console.log('Added post to feed of user: '+ followerId); | |
| }); | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment