From ba0856b769f676cb36437a184bb02d1e54375ee4 Mon Sep 17 00:00:00 2001 From: Piotr Suwala Date: Wed, 5 Jun 2024 08:58:14 +0200 Subject: [PATCH] feat(lib): extend filter property for memberships --- lib/src/entities/user.ts | 10 ++++- lib/tests/channel.test.ts | 36 ++++++++++++++- lib/tests/user.test.ts | 92 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 135 insertions(+), 3 deletions(-) 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..b1a121f 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.only("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) + ) + }) })