How to set the first message as unread for the receiver?

Viewed 69

I have a conversation with two participants:

var createdConversation = await ConversationResource.CreateAsync(uniqueName: uniqueName);

var sender = await ParticipantResource.CreateAsync(pathConversationSid: createdConversation.Sid, identity: "sender");
var receiver = await ParticipantResource.CreateAsync(pathConversationSid: createdConversation.Sid, identity: "receiver");

And I want to send a initial message in this newly created conversation:

var message = await MessageResource.CreateAsync(
    pathConversationSid: createdConversation.Sid,
    author: sender.Identity,
    body: intitialMessage);

After that, I want this new message to be read for the sender and unread for receiver which seems not possible.
I can set the lastReadMessageIndex of the sender to the created message.Index but this won't create read horizon for the receiver. And as long as the lastReadMessageIndex of the receiver is null, the unreadMessagesCount will be also null, and it will look like the receiver has no new messages in this conversation.

Any suggestions on how I can create a new conversation with initial message, which will be displayed as unread for one of the users?

2 Answers

Currently it is not possible to do this. This has been raised internally at Twilio as a feature gap.

In the meantime you may need to workaround it. One thing I thought of would be to mark any conversations that have a lastReadMessageIndex of null as unread in your UI.

As there is no "easy way" to achieve this, I am loading the conversation and its messages to calculate the unreadMessagesCount.

var conversation = await UserConversationResource.FetchAsync(
    pathUserSid: userSid, 
    pathConversationSid: conversationSid);

var conversationHasNoReadHorizon = conversation.UnreadMessagesCount == null;
if (conversationHasNoReadHorizon)
{
    var messages = await MessageResource.ReadAsync(pathConversationSid: conversationSid);

    return messages.Count(message => message.Author != callerId);
}
else
{
    return conversation.UnreadMessagesCount ?? 0;
}
Related