diff --git a/.pubnub.yml b/.pubnub.yml index 304bb66..17b2758 100644 --- a/.pubnub.yml +++ b/.pubnub.yml @@ -1,11 +1,16 @@ --- name: pubnub-js-chat -version: v0.7.0 +version: v0.7.1 scm: github.com/pubnub/js-chat schema: 1 files: - lib/dist/index.js changelog: + - date: 2024-06-05 + version: v0.7.1 + changes: + - type: feature + text: "Extend "filter" property for getMemberships." - date: 2024-05-22 version: v0.7.0 changes: diff --git a/lib/package.json b/lib/package.json index 842789d..5279a77 100644 --- a/lib/package.json +++ b/lib/package.json @@ -1,6 +1,6 @@ { "name": "@pubnub/chat", - "version": "0.7.0", + "version": "0.7.1", "description": "PubNub JavaScript Chat SDK", "author": "PubNub ", "license": "SEE LICENSE IN LICENSE", diff --git a/lib/src/entities/user.ts b/lib/src/entities/user.ts index bae9329..4c45d09 100644 --- a/lib/src/entities/user.ts +++ b/lib/src/entities/user.ts @@ -124,13 +124,21 @@ export class User { customChannelFields: true, channelTypeField: true, statusField: true, + channelStatusField: true, + } + + const filter = params.filter + let combinedFilter = `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*')` + + if (filter) { + combinedFilter = `${combinedFilter} && (${filter})` } const membershipsResponse = await this.chat.sdk.objects.getMemberships({ ...params, uuid: this.id, include, - filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*')`, + filter: combinedFilter, }) return { diff --git a/lib/tests/channel.test.ts b/lib/tests/channel.test.ts index b30d0a5..8033126 100644 --- a/lib/tests/channel.test.ts +++ b/lib/tests/channel.test.ts @@ -1,4 +1,4 @@ -import { Channel, Message, Chat, MessageDraft } from "../src" +import { Channel, Message, Chat, MessageDraft, INTERNAL_MODERATION_PREFIX } from "../src" import { sleep, extractMentionedUserIds, @@ -1128,4 +1128,38 @@ describe("Channel test", () => { removeModerationListener() }) + + test("Should get the number of unread messages on channels", async () => { + jest.spyOn(chat.sdk.objects, "getMemberships") + const commonParams = { + include: { + totalCount: true, + customFields: true, + channelFields: true, + customChannelFields: true, + channelTypeField: true, + statusField: true, + channelStatusField: true, + }, + uuid: chat.currentUser.id, + } + + await chat.getUnreadMessagesCounts({ filter: "channel.id like 'hello*'" }) + expect(chat.sdk.objects.getMemberships).toHaveBeenCalledWith({ + ...commonParams, + filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*') && (channel.id like 'hello*')`, + }) + + await chat.getUnreadMessagesCounts({ filter: "channel.name like '*test-channel'" }) + expect(chat.sdk.objects.getMemberships).toHaveBeenCalledWith({ + ...commonParams, + filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*') && (channel.name like '*test-channel')`, + }) + + await chat.getUnreadMessagesCounts() + expect(chat.sdk.objects.getMemberships).toHaveBeenCalledWith({ + ...commonParams, + filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*')`, + }) + }) }) diff --git a/lib/tests/user.test.ts b/lib/tests/user.test.ts index d7dd240..a5c2343 100644 --- a/lib/tests/user.test.ts +++ b/lib/tests/user.test.ts @@ -1,4 +1,4 @@ -import { Chat, User } from "../src" +import { Chat, INTERNAL_MODERATION_PREFIX, User } from "../src" import { createChatInstance, createRandomUser, sleep } from "./utils" import { INTERNAL_ADMIN_CHANNEL } from "../src" @@ -122,4 +122,94 @@ describe("User test", () => { expect(error.message).toContain("Cannot read properties of null (reading 'delete')") } }) + + test("Should apply filter to 'getMemberships'", async () => { + jest.spyOn(chat.sdk.objects, "getMemberships") + const commonParams = { + include: { + totalCount: true, + customFields: true, + channelFields: true, + customChannelFields: true, + channelTypeField: true, + statusField: true, + channelStatusField: true, + }, + uuid: chat.currentUser.id, + } + + await chat.currentUser.getMemberships({ filter: "channel.id like 'hello*'" }) + expect(chat.sdk.objects.getMemberships).toHaveBeenCalledWith({ + ...commonParams, + filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*') && (channel.id like 'hello*')`, + }) + + await chat.currentUser.getMemberships({ filter: "channel.name like '*test-channel'" }) + expect(chat.sdk.objects.getMemberships).toHaveBeenCalledWith({ + ...commonParams, + filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*') && (channel.name like '*test-channel')`, + }) + + await chat.currentUser.getMemberships() + expect(chat.sdk.objects.getMemberships).toHaveBeenCalledWith({ + ...commonParams, + filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*')`, + }) + + const exampleResponse = { + prev: undefined, + status: 200, + totalCount: 307, + next: "MTAw", + data: [ + { + channel: { + id: "0053d903-62d5-4f14-91cc-50aa90b1ab30", + name: "0053d903-62d5-4f14-91cc-50aa90b1ab30", + description: null, + type: "group", + status: null, + custom: null, + updated: "2024-02-28T13:04:28.210319Z", + eTag: "41ba0b6a52df2cc52775a83674ad4ba1", + }, + status: null, + custom: null, + updated: "2024-02-28T13:04:28.645304Z", + eTag: "AZO/t53al7m8fw", + }, + { + channel: { id: "019b58bd-3592-4184-8bc9-ce4a3ea87b37" }, + status: null, + custom: null, + updated: "2024-02-29T09:06:21.629495Z", + eTag: "AZO/t53al7m8fw", + }, + { + channel: { id: "0336a32b-3568-42ec-8664-48f05f479928" }, + status: null, + custom: null, + updated: "2024-05-21T12:12:51.439348Z", + eTag: "AZO/t53al7m8fw", + }, + ], + } + + jest.spyOn(chat.sdk.objects, "getMemberships").mockImplementation(() => exampleResponse) + + const response = await chat.currentUser.getMemberships() + expect(response).toEqual( + expect.objectContaining({ + page: { + prev: exampleResponse.prev, + next: exampleResponse.next, + }, + total: exampleResponse.totalCount, + status: exampleResponse.status, + }) + ) + expect(response.memberships.map((m) => m.channel.id)).toEqual( + exampleResponse.data.map((m) => m.channel.id) + ) + }) })