1

I want to retrieve the list of channels for a user containing unread messages. The best solution I have found so far (please correct me if I'm wrong) is using channel descriptors.

// Example for a single page
client.getUserChannelDescriptors().then(function(paginator) {

  for (var i = 0; i < paginator.items.length; i++) {
    var descriptor = paginator.items[i].descriptor;

    if (descriptor.unread_messages_count > 0) {
      console.log("Channel found, id: " + descriptor.uniqueName);
    }
  }
});

My question: is there a way to order in the paginator object so I could retrieve channels with unread messages first so I wouldn't have to go through the whole list of channels?

1 Answers1

0

Twilio developer evangelist here.

You can sort channels, but not with getUserChannelDescriptors. Instead, you need to make sure you've loaded all the subscribed channels and then you can sort them with getLocalChannels.

From the docs:

getLocalChannels( [sortingOptions])

Get array of Channels locally known to Client in provided sorting order. Locally known channels are the ones created and/or joined during client runtime and currently logged in User subscribed Channels. To ensure full list of subscribed Channels fetched - call the Client#getSubscribedChannels method and fetch all pages with help of Paginator#nextPage method.

The sorting options then allow you to sort by lastmessage.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • 1
    Thank you @philnash, you answered the question. But if I have to load ALL the channels before being able to sort, with hundreds of channels, it will be more performant to use the above code page per page I think. – Jean-Pierre Fortin Nov 19 '18 at 17:00
  • You don't have to load all the channels, just the ones your user is subscribed to. – philnash Nov 19 '18 at 23:56
  • 1
    @philnash But if your user is subscribed to hundreds of channels, then yes you do. I can't believe the ability to pass sorting options isn't supported for any method that fetches a paginated list. I'd love to understand what the technical decision for this was because at the moment it makes no sense at all and we're having to jump through hoops just to get an ordered list. My current issue being that I want to paginate the list of channels (so I don't have to load hundreds!), but the returned list is not in the correct order so latest unread messages aren't guaranteed to come first in the list – James Mar 20 '20 at 00:09
  • @James I am unaware of the technical decisions myself as I'm not part of that team. The best thing to do to get this addressed is to [raise a support ticket](https://www.twilio.com/help/contact) with your use case as that helps the teams to prioritise work like this. – philnash Mar 20 '20 at 03:13
  • 1
    Thanks @philnash, I did contact them. They said it was "a restriction of the underlying datastore". Doesn't help me much, but at least I know it's not possible so I'll just have to go down the much less performant route. – James Mar 20 '20 at 03:44