Ismar Slomic
Ismar Slomic

Reputation: 5514

Timestamp calculations at Firebase server

Is it possible, and recommended, to define write rules at Firebase server which needs to consider calculations on timestamps?

Example

Scenario: User is trying to add new Comment to existing Thread.

Write Rule: Comments can only be added to existing Thread if current time is between Thread.openAtTimestamp and Thread.closesAtTimestamp.

This would be fairly easy to solve with use of momentjs, for instance. But I guess that momentjs lib is not available in Firebase rules?

Upvotes: 2

Views: 112

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599571

Say you have this data structure:

threads
    $threadid
        openAtTimestamp: 1453820367233
        closesAtTimestamp: 1454425139712
comments
    $threadid
        $commentid
            author: "Ismar Slomic"
            timestamp: 1454425139711

Then you can only have a comment in a thread if its timestamp is between the openAtTimestamp and closesAtTimestamp of that thread.

{
 "rules": {
  "comments: {
   "$threadid": {
    "$commentid": {
     "timestamp: {
      ".validate": "
       newData.val() > root.child('threads').child($threadid).child('openAtTimestamp') && 
       newData.val() < root.child('threads').child($threadid).child('closesAtTimestamp')
    }
   }
  }
 }
}

This is just a rough outline to get you started. The Firebase documentation on security rules has tons more information.

Upvotes: 3

Related Questions