How to Determine Message Status Text
Overview
When displaying Delivered
/ Read
status text on a Message, there are a few different fields which help to manage this:
message.statusesPerRecipient
: This is a list of MessageStatusPerRecipient objects. Each one contains an intended recipient of the message and also their individual message status.message.shouldEnsureRecipientStatus
: Iftrue
, themessage.statusesPerRecipient
information might not yet be fully populated for every member of the conversation.
P2P Conversations
If message.shouldEnsureRecipientStatus
is true
, and this is a P2P conversation, message statuses should be ensured (that is, downloaded only if they are missing), using client.messages.ensureRecipientStatus().
This check should be made whenever the message content is about to be displayed on screen.
When this check is complete, message.statusesPerRecipient[0].status
will contain the status text:
// Arguments: client, messageconst { statusesPerRecipient } = message;const { MessageRecipientStatus } = client.enums;const initialStatus = statusesPerRecipient.length > 0 ?statusesPerRecipient[0].status : null;let displayLabel = '';if (initialStatus === MessageRecipientStatus.NEW) {displayLabel = 'Sent';} else if (initialStatus === MessageRecipientStatus.DELIVERED) {displayLabel = 'Delivered';} else if (initialStatus === MessageRecipientStatus.READ) {displayLabel = 'Read';}// displayLabel is ready to render
NA
(which should usually be displayed as empty string), NEW
(which usually should be displayed as Sent
), DELIVERED
, or READ
.
Group Conversations
If this is a group conversation, you should call client.messages.findRecipientStatus() 100% of the time upon render/update of the message content. Do this instead of checking message.shouldEnsureRecipientStatus
.
The reason this is currently required is that we do not guarantee that every member of a conversation is notified when another member reads a message due to concerns about traffic volume.
When this check is complete, you will need to write further code like the following, in order to display a single representative message status for the message:
// Arguments: client, message, groupconst { statusesPerRecipient } = message;const { MessageRecipientStatus } = client.enums;let readCount = 0;let sentCount = 0;if (statusesPerRecipient && group && group.memberIds) {for (const { status, userId } of statusesPerRecipient) {if (!group.memberIds.includes(userId)) continue;sentCount++;if (status === MessageRecipientStatus.READ) readCount++;}}const displayLabel = readCount === 0 ?'Sent' :`${readCount}/${sentCount} Read`;// displayLabel is ready to render