Skip to content

Commit

Permalink
[CSK-780] Respect eTag and updated fields in the Membership object (#171
Browse files Browse the repository at this point in the history
)

* feat(lib): respect eTag and updated fields in the Membership object

* feat(lib): update js sdk

* PubNub SDK v0.7.2 release.

---------

Co-authored-by: PubNub Release Bot <[email protected]>
  • Loading branch information
piotr-suwala and pubnub-release-bot authored Jun 10, 2024
1 parent 43eed79 commit c5778cc
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
7 changes: 6 additions & 1 deletion .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
---
name: pubnub-js-chat
version: v0.7.1
version: v0.7.2
scm: github.com/pubnub/js-chat
schema: 1
files:
- lib/dist/index.js
changelog:
- date: 2024-06-10
version: v0.7.2
changes:
- type: feature
text: "Respect eTag and updated fields in the Membership object."
- date: 2024-06-05
version: v0.7.1
changes:
Expand Down
4 changes: 2 additions & 2 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubnub/chat",
"version": "0.7.1",
"version": "0.7.2",
"description": "PubNub JavaScript Chat SDK",
"author": "PubNub <[email protected]>",
"license": "SEE LICENSE IN LICENSE",
Expand All @@ -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

0 comments on commit c5778cc

Please sign in to comment.