A common request is to get the first or 'oldest' unread message a user has in a channel.
Since it is channels that are marked as read/unread, not specific messages, getting this message id requires some extra steps in your implementation. Luckily, it's a fairly straightforward process.
1. Retrieve the users unread count on the channel.
const unreadCount = channel.countUnread()
2. Query the channel messages based on this count. For this example, the unread count will be 7.
const channelWithFirstUnreadMessage = await channel.query({messages: {offset: unreadCount - 1, limit: 1}})
Edge Case: If the unreadCount is equal to the number of channel messages, the query will not return any messages. To account for this, set the offset to unreadCount - 1. If the unreadCount is zero, exit the function as there will be no need to retrieve an ID.
The query will return a channel instance with the messages array offset to the number you provided. So - in this case, the first message returned will be 7 messages before the most recent message (the oldest timestamped unread message)
Set the limit to 1 as only one message ID is required for this.
3. Lastly, extract the message id from the response
const firstUnreadMessageId = channelWithFirstUnreadMessage.messages[0].id
Now the ID can be used to render a message list near that message ID.
The SDKs have built-in methods to support this:
React Native - loadChannelAroundMessage docs
iOS - ChannelController.loadPageAroundMessageId()
Comments
0 comments
Please sign in to leave a comment.