From d29b3a47536ed85d7a7bc18b870e02ba8e3e6154 Mon Sep 17 00:00:00 2001 From: Piotr Suwala Date: Tue, 18 Jun 2024 12:46:48 +0200 Subject: [PATCH] fix(lib): take membership related events into account --- lib/src/entities/user.ts | 4 ++- lib/tests/channel.test.ts | 12 ++++++-- lib/tests/user.test.ts | 58 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/lib/src/entities/user.ts b/lib/src/entities/user.ts index 4c45d09..f6731c4 100644 --- a/lib/src/entities/user.ts +++ b/lib/src/entities/user.ts @@ -82,7 +82,9 @@ export class User { const listener = { objects: (event: PubNub.SetUUIDMetadataEvent) => { if (event.message.type !== "uuid") return - const user = users.find((c) => c.id === event.channel) + const user = + users.find((c) => c.id === event.channel) || + users.find((c) => c.id === event.message.data.id) if (!user) return const newUser = User.fromDTO(user.chat, event.message.data) const newUsers = users.map((user) => (user.id === newUser.id ? newUser : user)) diff --git a/lib/tests/channel.test.ts b/lib/tests/channel.test.ts index e926f9f..5424ec7 100644 --- a/lib/tests/channel.test.ts +++ b/lib/tests/channel.test.ts @@ -131,13 +131,14 @@ describe("Channel test", () => { }) test("should edit membership metadata", async () => { - const { membership } = await channel.join(() => null) + const { membership, disconnect } = await channel.join(() => null) const updatedMembership = await membership.update({ custom: { role: "admin" }, }) expect(updatedMembership.custom?.role).toBe("admin") await channel.leave() + disconnect() }) test("should create a direct, group and public chats with a predefined ID", async () => { @@ -365,7 +366,7 @@ describe("Channel test", () => { }) test("should stream membership updates and invoke the callback", async () => { - const { membership } = await channel.join(() => null) + const { membership, disconnect } = await channel.join(() => null) expect(membership).toBeDefined() let updatedMembership @@ -382,6 +383,7 @@ describe("Channel test", () => { await channel.leave() stopUpdates() + disconnect() }) test("should get unread messages count", async () => { @@ -392,7 +394,9 @@ describe("Channel test", () => { await channel.sendText(messageText2) await sleep(150) // history calls have around 130ms of cache time - let { membership } = await channel.join(() => null) + const channelJoinData = await channel.join(() => null) + let { membership } = channelJoinData + const { disconnect } = channelJoinData let unreadCount = await membership.getUnreadMessagesCount() expect(unreadCount).toBe(0) @@ -402,6 +406,7 @@ describe("Channel test", () => { expect(unreadCount).toBe(1) await channel.leave() + disconnect() }) test("should mention users in a message and validate mentioned users", async () => { @@ -873,6 +878,7 @@ describe("Channel test", () => { const nonExistentChannel = await chat.getChannel(nonExistentChannelId) const membership = await nonExistentChannel.join(() => null) await membership.update({ custom: { role: "admin" } }) + membership.disconnect() fail("Editing membership metadata of a non-existent channel should fail") } catch (error) { expect(error.message).toContain("Cannot read properties of null (reading 'join')") diff --git a/lib/tests/user.test.ts b/lib/tests/user.test.ts index a5c2343..7ca5a57 100644 --- a/lib/tests/user.test.ts +++ b/lib/tests/user.test.ts @@ -63,6 +63,64 @@ describe("User test", () => { stopUpdates() }) + test("should update the user even if they're a member of a particular channel", async () => { + let someUser = await chat.getUser("test-user-chatsdk0") + if (!someUser) { + someUser = await chat.createUser("test-user-chatsdk0", { name: "Chat SDK user 0" }) + } + let someChannel = await chat.getChannel("some-public-channel") + if (!someChannel) { + someChannel = await chat.createPublicConversation({ + channelId: "some-public-channel", + channelData: { name: "Public channel test" }, + }) + } + await chat.sdk.objects.setChannelMembers({ + channel: someChannel.id, + uuids: [someUser.id], + }) + + const stopUpdates = User.streamUpdatesOn([someUser], (updatedUsers) => { + someUser = updatedUsers[0] + }) + await someUser.update({ name: "update number 1" }) + await sleep(1000) + expect(someUser.name).toBe("update number 1") + await someUser.update({ name: "update number 2" }) + await sleep(1000) + expect(someUser.name).toBe("update number 2") + + stopUpdates() + }) + + test("should update the user even if they're not a member of a particular channel", async () => { + let someUser = await chat.getUser("test-user-chatsdk1") + if (!someUser) { + someUser = await chat.createUser("test-user-chatsdk1", { name: "Chat SDK user 1" }) + } + let someChannel = await chat.getChannel("some-public-channel-2") + if (!someChannel) { + someChannel = await chat.createPublicConversation({ + channelId: "some-public-channel-2", + channelData: { name: "Public channel test 2" }, + }) + } + const { members } = await someChannel.getMembers() + + expect(members.length).toBe(0) + const stopUpdates = User.streamUpdatesOn([someUser], (updatedUsers) => { + someUser = updatedUsers[0] + }) + await someUser.update({ name: "update number 1" }) + await sleep(1000) + expect(someUser.name).toBe("update number 1") + await someUser.update({ name: "update number 2" }) + await sleep(1000) + expect(someUser.name).toBe("update number 2") + + stopUpdates() + }) + test("should report a user", async () => { const reportReason = "Inappropriate behavior" await user.report(reportReason)