yedpodtrzitko
yedpodtrzitko

Reputation: 9359

Optional messages moderation in Firebase Realtime DB chat?

I'm implementing a chat using Firebase Realtime DB. There's a requirement, that some chatrooms can show new messages by default, but messages in different chatrooms have to be approved first before displaying them.

The DB structure in Firebase is nested like this:

/chat/$roomID/$messageID/

and each message object is structured as follows:

{
    user: string
    content: string
    isApproved: boolean
    clientStamp: number // client-side unix timestamp
    serverStamp: number // server-side unix timestamp, via `firebase.database.ServerValue.TIMESTAMP`
}

Having only the display-by-default chatrooms would be easy - I'd order the data by serverStamp:

let query = DBConn.ref('chat/' + roomID).orderByChild('serverStamp').limitToLast(50);
query.on("value", snapshot => {
     // set the list of messages to display
     this.appendSnapshot(snapshot);
});

The approve-first rule makes this approach difficult however. The obvious solution would be to show only messages with attribute isApproved set to true. But if I'd query for the last N messages ordered by timestamp, and someone would post N unapproved messages, then that would oust any of the approved messages from the query result and the chat would be empty. Realtime DB has the following limitation, which makes it impossible to query by both timestamp + isApproved:

You can only use one order-by method at a time

So right now I'm workarounding it the following way:

... but this is rather a quick workaround. Is there any way how to make this work nicely for both display-by-default and approve-first chatrooms?

Upvotes: 1

Views: 165

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

I like your approach with setting the timestamp as part of an approval flow. 👍

The more common solution is the one I documented in Query based on multiple where clauses in Firebase

Here that'd mean you have a property isApproved_serverStamp where you store the combined values of the isApproved and serverStamp properties, so like true_1632065222747. You can then query for the most recent approved messages with:

DBConn.ref('chat/' + roomID).orderByChild('isApproved_serverStamp').startAt("true_").limitToLast(50)

Upvotes: 1

Related Questions