Skip to content

Instantly share code, notes, and snippets.

@leblancmeneses
Forked from katowulf/rules.json
Last active April 4, 2017 13:23
Show Gist options
  • Save leblancmeneses/16274598b3a2da9c9566ef7e672a94be to your computer and use it in GitHub Desktop.
Save leblancmeneses/16274598b3a2da9c9566ef7e672a94be to your computer and use it in GitHub Desktop.
throttle messages to no more than one every 5,000 milliseconds, see http://jsfiddle.net/firebase/VBmA5/
type SharedWithInviteRequest {
........
createdDate: RateLimiting
}
// in order to write a message, I must first make an entry in users/${uid}/rate_limiting/{feature} is Number
// additionally, that message must be within 500ms of now, which means I can't
// just re-use the same one over and over, thus, we've effectively required messages
// to be 5 seconds apart
type RateLimiting extends Number {
validate() { this >= now - 500 && this === root['users'][auth.uid]['rate_limiting']['xyz'] }
}
path /users/{$urlUserId} {
// the new value must be at least 5000 milliseconds after the last (no more than one message every five seconds)
// the new value must be before now (it will be since `now` is when it reaches the server unless I try to cheat)
/rate_limiting/{$throttling} is Number {
read() { isOwner($urlUserId) }
write() { isOwner($urlUserId) }
validate() { $throttling.test(/^(xyz)$/) && this === now && (prior(this) == null || this > prior(this)+5000) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment