Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSK-780] Respect eTag and updated fields in the Membership object #171

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"homepage": "https://github.com/pubnub/js-chat#readme",
"dependencies": {
"pubnub": "8.2.0"
"pubnub": "8.2.3"
},
"devDependencies": {
"@babel/core": "7.22.15",
Expand Down
12 changes: 11 additions & 1 deletion lib/src/entities/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import { Message } from "./message"
import { User } from "./user"
import { getErrorProxiedEntity } from "../error-logging"

export type MembershipFields = Pick<Membership, "channel" | "user" | "custom">
export type MembershipFields = Pick<Membership, "channel" | "user" | "custom" | "updated" | "eTag">

export class Membership {
private chat: Chat
readonly channel: Channel
readonly user: User
readonly custom: ObjectCustom | null | undefined
readonly updated: string
readonly eTag: string

/** @internal */
constructor(chat: Chat, params: MembershipFields) {
this.chat = chat
this.channel = params.channel
this.user = params.user
this.custom = params.custom
this.updated = params.updated
this.eTag = params.eTag
}

/** @internal */
Expand All @@ -31,6 +35,8 @@ export class Membership {
channel: Channel.fromDTO(chat, channelMembershipObject.channel),
user,
custom: channelMembershipObject.custom,
updated: channelMembershipObject.updated,
eTag: channelMembershipObject.eTag,
}

return getErrorProxiedEntity(new Membership(chat, data), chat.errorLogger)
Expand All @@ -46,6 +52,8 @@ export class Membership {
user: User.fromDTO(chat, userMembershipObject.uuid),
channel,
custom: userMembershipObject.custom,
updated: userMembershipObject.updated,
eTag: userMembershipObject.eTag,
}

return getErrorProxiedEntity(new Membership(chat, data), chat.errorLogger)
Expand Down Expand Up @@ -104,6 +112,8 @@ export class Membership {
user: membership.user,
channel: membership.channel,
custom: event.message.data.custom,
updated: event.message.data.updated,
eTag: event.message.data.eTag,
})
const newMemberships = memberships.map((membership) =>
membership.channel.id === newMembership.channel.id &&
Expand Down
61 changes: 57 additions & 4 deletions lib/tests/channel.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Channel, Message, Chat, MessageDraft, INTERNAL_MODERATION_PREFIX } from "../src"
import {
Channel,
Message,
Chat,
MessageDraft,
INTERNAL_MODERATION_PREFIX,
Membership,
} from "../src"
import {
sleep,
extractMentionedUserIds,
Expand Down Expand Up @@ -336,9 +343,6 @@ describe("Channel test", () => {
await channel.createThread()
} catch (error) {
errorOccurred = true

//Should to be clarified
// expect(error.message).toBe("Cannot create an empty thread")
}

expect(errorOccurred).toBe(true)
Expand Down Expand Up @@ -1162,4 +1166,53 @@ describe("Channel test", () => {
filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*')`,
})
})

test("should set 'eTag' and 'updated' on the Membership object", async () => {
const membershipsDTOMock = {
status: 200,
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",
},
],
totalCount: 309,
next: "MTAw",
}

jest.spyOn(chat.sdk.objects, "getMemberships").mockImplementation(() => membershipsDTOMock)

const memberships = await chat.currentUser.getMemberships()
expect(memberships.memberships[0].updated).toBe(membershipsDTOMock.data[0].updated)
expect(memberships.memberships[0].eTag).toBe(membershipsDTOMock.data[0].eTag)
})

test("should set 'eTag' and 'updated' on the Membership object in real time", async () => {
const groupConversation = await chat.createGroupConversation({ users: [chat.currentUser] })
const hostMembership = groupConversation.hostMembership
let updatedMembership: Membership | undefined

const removeListener = hostMembership.streamUpdates((updatedMembershipData) => {
updatedMembership = updatedMembershipData
})

await hostMembership.update({ custom: { hello: "world" } })
await sleep(2000)
expect(updatedMembership?.custom?.hello).toBe("world")
expect(updatedMembership?.updated).toBeDefined()
expect(updatedMembership?.eTag).toBeDefined()
removeListener()
})
})
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11152,10 +11152,10 @@ proxy-from-env@^1.1.0:
resolved "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

[email protected].0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.2.0.tgz#6de8ee52be0c03d48c66667397f1e84a08d54268"
integrity sha512-gXck3w/9es+w3/tdzipIjIpzKEl2CAcWnKSyMhBP/Q43Rbk0eAeS0JV6uzcTsJdi6v8nZ6TAqghjy/0Y73zqoQ==
[email protected].3:
version "8.2.3"
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.2.3.tgz#ad47e1636e5301cb3d55aa55ac166e8955fa9245"
integrity sha512-VqAyildzNmkTJCSCFzAiMTeNFTO10On6Ah2AX59LkKCrNDPcqS4jw+xnyyakwmB/R9gLZuCieeTxHqsSWrZD2g==
dependencies:
agentkeepalive "^3.5.2"
buffer "^6.0.3"
Expand Down