Skip to content

Instantly share code, notes, and snippets.

@andersonsantos
Created October 1, 2016 08:52
Show Gist options
  • Save andersonsantos/6ff752aa2b0140c6956bfc6c2d66c461 to your computer and use it in GitHub Desktop.
Save andersonsantos/6ff752aa2b0140c6956bfc6c2d66c461 to your computer and use it in GitHub Desktop.
async function onNewMessage(newMessage) {
const [
seenTimestamp,
previousMaxMessageTimestamp
] = await Promise.all([
r.table('RoomUser').get(['r31', 'liron-shapira'])('seenTimestamp'),
r.table('Message').filter({roomId: 'r31'}).max('timestamp')
])
if (previousMaxMessageTimestamp < seenTimestamp && seenTimestamp < newMessage.timestamp) {
// r31's status is flipping from "read" to "unread" as a result of inserting newMessage,
// so we know that if we were to recalculate numUnreadRooms, it would end up incrementing
// the previous value by 1
await r.table('User').get('liron-shapira').update({
numUnreadRooms: r.row('numUnreadRooms').add(1)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment