I'm trying to figure out a more elegant way of doing this...
I am storing message objects and want to get the total number of conversations that have new messages. My node structure is like this:
/messages/{userId}/{receipientId}/{messageId}/{message: '', senderId: '', status: 'new|read' }
*** The messageId is an auto generated key from firebase. userId & receipientId are id's from auth.
The first code I wrote to get a number in the badge was this:
firebase.database().ref()
.child(`/messages/${userId}/${receipientId}`)
.orderByChild('status')
.equalTo('new')
.once('value')
.then(innerSnapshot => {
if (innerSnapshot.val() !== null) {
if (newConversations.findIndex(item => item === entry) === -1) {
newConversations.push(entry);
}
}
})
.catch();
This requires me to wrap it in another query to get all of the receipientId's prior to running a separate query for each item found. It works...but is very brute-forcish.
Essentially, all I need to know is the number of receipientId nodes that has a "status: 'new'" entry somewhere under it.