Implement tweets and similar / upvote removal features in Firebase

Continuing this thread, on HN: https://news.ycombinator.com/item?id=5462769

Reading the fire extinguishing rules file answered a lot of questions, except for two:

  • Editing an existing tweet is prohibited (".write": "! Data.exists ()"). How can you make it inaccessible for editing, but delete it by the author?
  • How would you safely handle sympathy / non-coverage or knocking / downvoting? write, if authenticated, check the increase / decrease by one, if the user has not changed it before? How will it work? Should there be a list of children who edited this? I'm just really interested to know about this particular use case, since it seems quite common in many applications, but it seems to me to be very difficult to implement in firebase?
+5
source share
2 answers

1. Not edited, but deleted by the author

".write": "!data.exists() || (!newData.exists() && data.child('author') === auth.id)"

2. Sympathy / Upvoting

On the client, use a transaction that allows you to safely increase the value:

ref.transaction(function(currentValue) {
   return (currentValue||0)+1;
}, function(error) {
   if( error ) /* failed too many times */
   else /* it worked */
});

Security is also simple:

".validate": "newData.isNumber() && newData.val() === data.val()+1"

2.5 Securing Unique Votes

, ; , , ; "" : " ? ?"

, , . , .

, , - (, /), , .

:

"votes": {
    "$spark_id": {
       "$vote": {
          ".read": "$vote === auth.id",
          ".write": "$vote === auth.id", 
          // to allow downvoting in addition to up or delete, just add -1 here
          ".validate": "newData.val() === 1 || newData.val() === null"
       }
    }
}

:

".validate": "!root.child('votes').child($spark_id).child(auth.id).exists() && newData.isNumber() && newData.val() === data.val()+1"
+8

, Firebase ( -) , : https://firebase.googleblog.com/2017/03/introducing-cloud-functions-for-firebase.html

, "upvoters" . -   , .

"upvote count", upvoters . , , Firebase, , upvote.

, , "upvote count" . , , , , . , , upvoters, upvote.

0

All Articles