Skip to content

Add new method to fetch unread message counts for user's channels#185

Merged
jguz-pubnub merged 8 commits intomasterfrom
fix/getUnreadMessagesCounts
May 9, 2025
Merged

Add new method to fetch unread message counts for user's channels#185
jguz-pubnub merged 8 commits intomasterfrom
fix/getUnreadMessagesCounts

Conversation

@jguz-pubnub
Copy link
Contributor

@jguz-pubnub jguz-pubnub commented May 5, 2025

feat: Add new API method returning paginated unread message counts

@jguz-pubnub jguz-pubnub requested a review from marcin-cebo as a code owner May 5, 2025 15:13
channel = membershipMatchingChannel.channel,
membership = membershipMatchingChannel,
count = messageCount
count = messageCount,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding test.
We could add test similar to can_getUnreadMessageCounts_global
but with limit = 1 to test paging
e.g.
chat.getUnreadMessagesCounts(limit = 1).await()
Maybe there are better way to test paging?

My understanding is that we would like to support situation that there are more than 100 channels that user is member of.

    @Test
    fun can_getUnreadMessageCounts_withPagination() = runTest {
        val channelId01 = channel01.id
        val channelId02 = channel02.id

        // join two channels
        channel01.join { }.await()
        channel02.join { }.await()

        // send message
        channel01.sendText("message01In$channelId01").await()
        channel02.sendText("message01In$channelId02").await()
        delayForHistory()

        // read message count with limit=1 to get pagination
        val unreadMessagesCounts: List<GetUnreadMessagesCounts> = chat.getUnreadMessagesCounts(limit = 1).await()
        
        // Verify we got one result due to limit
        assertEquals(1, unreadMessagesCounts.size)
        
        // Verify first count is correct
        val firstCount: GetUnreadMessagesCounts = unreadMessagesCounts.first()
        assertEquals(1, firstCount.count)
        assertEquals(channelId01, firstCount.channel.id)

        // Verify we have next page but no prev page
        assertNotNull(firstCount.next, "Should have next page")
        assertNull(firstCount.prev, "Should not have prev page")

        // Get next page
        val nextPageCounts: List<GetUnreadMessagesCounts> = chat.getUnreadMessagesCounts(limit = 1, page = firstCount.next).await()
        
        // Verify next page results
        assertEquals(1, nextPageCounts.size)
        val nextPageCount = nextPageCounts.first()
        assertEquals(1, nextPageCount.count)
        assertEquals(channelId02, nextPageCount.channel.id)

        // remove messages
        chat.pubNub.deleteMessages(listOf(channelId01, channelId02))
    }

createJsObject<GetUnreadMessagesCountsJs> {
this.channel = unreadMessagesCount.channel.asJs(this@ChatJs)
this.membership = unreadMessagesCount.membership.asJs(this@ChatJs)
this.page = MetadataPage(unreadMessagesCount.next, unreadMessagesCount.prev)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add test for paging in JS.
There is already test for getUnreadMessagesCounts called : test("Should get the number of unread messages on channels"

located:
kmp-chat/js-chat/tests/channel.test.ts

}.asPromise()
}

private fun GetUnreadMessagesCounts.toJs() =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering why this method was not present before. Why it is needed now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the newly introduced UnreadMessagesCounts class stores an array of GetUnreadMessagesCounts.These stored items must be serialized into the type expected by the JS

}

override fun fetchUnreadMessagesCounts(
limit: Int?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding tests ?

@jguz-pubnub jguz-pubnub force-pushed the fix/getUnreadMessagesCounts branch from 06a8d16 to 4457a75 Compare May 9, 2025 09:05
}

fun fetchUnreadMessagesCounts(params: PubNub.GetMembershipsParametersv2?): Promise<FetchUnreadMessagesCountsJs> {
fun fetchUnreadMessagesCounts(params: PubNub.GetMembershipsParametersv2?): Promise<FetchUnreadMessagesCountsResponseJs> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have doc changes for this ?

@jguz-pubnub jguz-pubnub changed the title Add pagination tokens to GetUnreadMessagesCounts Add method to fetch unread message counts for user's channels May 9, 2025
@jguz-pubnub jguz-pubnub changed the title Add method to fetch unread message counts for user's channels Add new method to fetch unread message counts for user's channels May 9, 2025
@jguz-pubnub jguz-pubnub force-pushed the fix/getUnreadMessagesCounts branch from 4908d62 to 1cee446 Compare May 9, 2025 10:37
@jguz-pubnub
Copy link
Contributor Author

@pubnub-release-bot release kotlin as 0.13.0

@jguz-pubnub jguz-pubnub force-pushed the fix/getUnreadMessagesCounts branch from f1b110e to 6534c2c Compare May 9, 2025 11:03
@jguz-pubnub jguz-pubnub merged commit 9e71ed2 into master May 9, 2025
2 checks passed
@jguz-pubnub jguz-pubnub deleted the fix/getUnreadMessagesCounts branch May 9, 2025 11:25
@pubnub-release-bot
Copy link
Contributor

🚀 Release successfully completed 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants