Search code examples
getstream-io

Query all channels with unread messages by an specific user?


I would like to recover all the channels that have unread messages by a specific user. From the docs, I was only able to find the count of unread messages and the count channels with unread messages for the current user.


Solution

  • If you want get all the channels where currentUser is member and sort them by unread_counts desc:

    const result = await client.queryChannels(
                { members: { $in: [currentUser] } },
                { unread_count: -1 },
            );
    

    it is also possible to sort by has_unread: (in this case it doesn't matter the number of unread messages, any channel with unread messages weight the same for sorting)

    const result = await client.queryChannels(
                { members: { $in: [currentUser] } },
                { has_unread: -1, last_message_at: -1 },
            );
    

    please take a look at our tests for more info