Skip to content

Commit

Permalink
Fixed bug rel. to setRestriction (#174)
Browse files Browse the repository at this point in the history
* [JS] Fixed bug rel. to setRestriction

When "Enforce referential integrity for memberships" is set then when setting restriction(channelMembers) we need to make sure that both channel and user exist in AppContext.

* PubNub js 0.11.6 release.

---------

Co-authored-by: PubNub Release Bot <[email protected]>
  • Loading branch information
marcin-cebo and pubnub-release-bot authored Feb 24, 2025
1 parent c8e159e commit 352d4e2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SONATYPE_HOST=DEFAULT
SONATYPE_AUTOMATIC_RELEASE=false
GROUP=com.pubnub
POM_PACKAGING=jar
VERSION_NAME=0.11.1
VERSION_NAME=0.11.6


POM_NAME=PubNub Chat SDK
Expand Down
7 changes: 6 additions & 1 deletion js-chat/.pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
name: pubnub-js-chat
version: 0.11.5
version: 0.11.6
scm: github.com/pubnub/js-chat
schema: 1
files:
- lib/dist/index.js
changelog:
- date: 2025-02-24
version: 0.11.6
changes:
- type: bug
text: "Fixed bug rel. to setRestriction. When "Enforce referential integrity for memberships" is enabled then when setting restriction(channelMembers) we need to make sure that both channel and user exist in AppContext."
- date: 2025-02-06
version: 0.11.5
changes:
Expand Down
2 changes: 1 addition & 1 deletion js-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
"react-native": "dist/index.es.js",
"version": "0.11.5",
"version": "0.11.6",
"name": "@pubnub/chat",
"dependencies": {
"pubnub": "8.6.0",
Expand Down
65 changes: 34 additions & 31 deletions js-chat/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ describe("Channel test", () => {

let chat: Chat
let chatPamServer: Chat
let chatPamServerWithRefIntegrity: Chat
let channel: Channel
let messageDraft: MessageDraft

beforeAll(async () => {
chat = await createChatInstance()
chatPamServer = await createChatInstance( { shouldCreateNewInstance: true, clientType: 'PamServer' })
chatPamServerWithRefIntegrity = await createChatInstance( { shouldCreateNewInstance: true, clientType: 'PamServerWithRefIntegrity' })
})

beforeEach(async () => {
Expand Down Expand Up @@ -1127,60 +1129,61 @@ describe("Channel test", () => {
})

test("should set (or lift) restrictions on a user", async () => {
const notExistingUserId = generateRandomString(10)
const moderationEventCallback = jest.fn()
const channelName = generateRandomString(10)
const notExistingChannelName = generateRandomString(10)

const removeModerationListener = chat.listenForEvents({
channel: "PUBNUB_INTERNAL_MODERATION." + chat.currentUser.id,
const removeModerationListener = chatPamServerWithRefIntegrity.listenForEvents({
channel: "PUBNUB_INTERNAL_MODERATION." + notExistingUserId,
type: "moderation",
callback: moderationEventCallback,
})

await chatPamServer.setRestrictions(chat.currentUser.id, channelName, { mute: true })
await chatPamServerWithRefIntegrity.setRestrictions(notExistingUserId, notExistingChannelName, {mute: true})
await sleep(250) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${channelName}`,
restriction: "muted",
}),
})
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${notExistingChannelName}`,
restriction: "muted",
}),
})
)
moderationEventCallback.mockReset()

await chatPamServer.setRestrictions(chat.currentUser.id, channelName, { ban: true })
await chatPamServerWithRefIntegrity.setRestrictions(notExistingUserId, notExistingChannelName, { ban: true })
await sleep(250) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${channelName}`,
restriction: "banned",
}),
})
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${notExistingChannelName}`,
restriction: "banned",
}),
})
)
moderationEventCallback.mockReset()

await chatPamServer.setRestrictions(chat.currentUser.id, channelName, { ban: true, mute: true })
await chatPamServerWithRefIntegrity.setRestrictions(notExistingUserId, notExistingChannelName, { ban: true, mute: true })
await sleep(250) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${channelName}`,
restriction: "banned",
}),
})
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${notExistingChannelName}`,
restriction: "banned",
}),
})
)
moderationEventCallback.mockReset()

await chatPamServer.setRestrictions(chat.currentUser.id, channelName, {})
await chatPamServerWithRefIntegrity.setRestrictions(notExistingUserId, notExistingChannelName, {})
await sleep(250) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${channelName}`,
restriction: "lifted",
}),
})
expect.objectContaining({
payload: expect.objectContaining({
channelId: `PUBNUB_INTERNAL_MODERATION_${notExistingChannelName}`,
restriction: "lifted",
}),
})
)

removeModerationListener()
Expand Down
14 changes: 10 additions & 4 deletions js-chat/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

type ClientType = 'PamClient' | 'PamServer' | 'NoPam';
type ClientType = 'PamClient' | 'PamServer' | 'NoPam' | 'PamServerWithRefIntegrity';

const createChat = async (
userId: string,
Expand Down Expand Up @@ -57,6 +57,12 @@ const createChat = async (
secretKey = process.env.PAM_SECRET_KEY;
break;

case 'PamServerWithRefIntegrity':
publishKey = process.env.PAM_WITH_INTEGRITY_PUBLISH_KEY;
subscribeKey = process.env.PAM_WITH_INTEGRITY_SUBSCRIBE_KEY;
secretKey = process.env.PAM_WITH_INTEGRITY_SECRET_KEY;
break;

case 'NoPam':
default:
publishKey = process.env.PUBLISH_KEY;
Expand All @@ -65,7 +71,7 @@ const createChat = async (
}

// Validate required keys
if (!publishKey || !subscribeKey || (clientType === 'PamServer' && !secretKey)) {
if (!publishKey || !subscribeKey || (clientType === 'PamServer' && !secretKey) || (clientType === 'PamServerWithRefIntegrity' && !secretKey)) {
throw keysetError
}
// Build the chat configuration
Expand All @@ -76,8 +82,8 @@ const createChat = async (
...config,
};

// Include secretKey only if clientType is 'PamServer'
if (clientType === 'PamServer' && secretKey) {
// Include secretKey only if clientType is 'PamServer' or 'PamServerWithRefIntegrity'
if ((clientType === 'PamServer' || clientType === 'PamServerWithRefIntegrity') && secretKey) {
chatConfig.secretKey = secretKey;
}

Expand Down

0 comments on commit 352d4e2

Please sign in to comment.