Created
October 1, 2016 08:52
-
-
Save andersonsantos/6ff752aa2b0140c6956bfc6c2d66c461 to your computer and use it in GitHub Desktop.
This file contains 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
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