Skip to content

Commit

Permalink
feat(lib): extend filter property for memberships
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-suwala committed Jun 5, 2024
1 parent 98bdadb commit ba0856b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 35 additions & 1 deletion lib/tests/channel.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Channel, Message, Chat, MessageDraft } from "../src"
import { Channel, Message, Chat, MessageDraft, INTERNAL_MODERATION_PREFIX } from "../src"
import {
sleep,
extractMentionedUserIds,
Expand Down Expand Up @@ -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}*')`,
})
})
})
92 changes: 91 additions & 1 deletion lib/tests/user.test.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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)
)
})
})

0 comments on commit ba0856b

Please sign in to comment.