Skip to content

Commit

Permalink
[JS] Fix method sendEvent for event types different from custom
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-cebo committed Feb 6, 2025
1 parent 2c46f05 commit 205d52b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 41 deletions.
109 changes: 85 additions & 24 deletions js-chat/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,37 +1276,98 @@ describe("Channel test", () => {
expect(result.channels.length).toBe(2)
})

test("send custom event", async () => {
const inviteCallback = jest.fn()
const unsubscribe = chat.listenForEvents({
test("send report event", async () => {
let receivedEvent
const reason = "rude"
const text = "reporting"
const unsubscribe = chat.listenForEvents({
channel: channel.id,
type: "report",
callback: (event) => {
receivedEvent = event
},
})

await sleep(1000)
await chat.emitEvent({
channel: channel.id,
type: 'report',
payload: {
text: text,
reason: reason
}
})
await sleep(500)
expect(receivedEvent.payload.text).toEqual(text)
expect(receivedEvent.payload.reason).toEqual(reason)

unsubscribe(); // Cleanup
})

test("send receipt event", async () => {
let receivedEvent
const messageTimetokenValue = "123"
const eventTypeReceipt = "receipt"

const unsubscribe = chat.listenForEvents({
type: eventTypeReceipt,
channel: channel.id,
callback: event => {
receivedEvent = event
}
})

await sleep(1000)
await chat.emitEvent({
type: eventTypeReceipt,
channel: channel.id,
payload: {
messageTimetoken: messageTimetokenValue
}

})

await sleep(500)
expect(receivedEvent.payload.messageTimetoken).toEqual(messageTimetokenValue)
expect(receivedEvent.type).toEqual(eventTypeReceipt)

unsubscribe() // cleanup
})

test("send custom event", async () => {
const inviteCallback = jest.fn()
const unsubscribe = chat.listenForEvents({
channel: channel.id,
type: "custom",
method: "publish",
callback: inviteCallback,
})

await sleep(1000)
await chat.emitEvent({
channel: channel.id,
type: 'custom',
method: 'publish',
payload: {
action: "action",
body: "payload"
}
})
await sleep(2000)
expect(inviteCallback).toHaveBeenCalledTimes(1)
expect(inviteCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
action: "action",
body: "payload"
}),
})
)

await sleep(1000)
await chat.emitEvent({
channel: channel.id,
type: 'custom',
method: 'publish',
payload: {
action: "action",
body: "payload"
}
})
await sleep(2000)
expect(inviteCallback).toHaveBeenCalledTimes(1)
expect(inviteCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
action: "action",
body: "payload"
}),
})
)

unsubscribe(); // Cleanup
})


test("use PubNub SDK types from Chat SDK", async () => {
let channelMetadata = await chat.sdk.objects.getChannelMetadata({
channel: channel.id,
Expand Down
16 changes: 0 additions & 16 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -659,22 +659,6 @@ proxy-from-env@^1.1.0:
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

[email protected]:
version "8.4.1"
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.4.1.tgz#5f6f19e84d3187dc8aee0a458bd6b05e90d43e6a"
integrity sha512-mPlwVoHJDWPasZx52UfSMiPX5TATm5A+ficSogyqDqTQ4w5EQnwxH+PJdsWc0mPnlT051jM1vIISMeM0fQ30CQ==
dependencies:
agentkeepalive "^3.5.2"
buffer "^6.0.3"
cbor-js "^0.1.0"
cbor-sync "^1.0.4"
form-data "^4.0.0"
lil-uuid "^0.1.1"
node-fetch "^2.7.0"
proxy-agent "^6.3.0"
react-native-url-polyfill "^2.0.0"
text-encoding "^0.7.0"

[email protected]:
version "8.6.0"
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.6.0.tgz#75524e7ed3653090652d160ce83ac089362a0379"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pubnub.chat.types

import com.pubnub.api.JsonElement
import com.pubnub.chat.restrictions.RestrictionType
import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
Expand Down Expand Up @@ -110,8 +111,10 @@ abstract class EventContent(
* @property data A map containing key-value pairs of custom data associated with the event.
* @property method The method by which the event was emitted (PUBLISH, SIGNAL).
*/
@Serializable
@SerialName("custom")
class Custom(
val data: Map<String, Any?>,
val data: Map<String, @Contextual Any?>,
@Transient val method: EmitEventMethod = EmitEventMethod.PUBLISH
) : EventContent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ internal const val METADATA_TEXT_LINKS = "textLinks"
internal const val METADATA_LAST_READ_MESSAGE_TIMETOKEN = "lastReadMessageTimetoken"
internal const val TYPE_OF_MESSAGE = "type"
internal const val TYPE_OF_MESSAGE_IS_CUSTOM = "custom"
internal const val TYPE_OF_MESSAGE_IS_REPORT = "report"
internal const val TYPE_OF_MESSAGE_IS_TYPING = "typing"
internal const val TYPE_OF_MESSAGE_IS_RECEIPT = "receipt"
internal const val TYPE_OF_MESSAGE_IS_MENTION = "mention"
internal const val TYPE_OF_MESSAGE_IS_INVITE = "invite"
internal const val RESTRICTION_BAN = "ban"
internal const val RESTRICTION_MUTE = "mute"
internal const val RESTRICTION_REASON = "reason"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ val module = SerializersModule {
subclass(EventContent.Moderation::class)
subclass(EventContent.TextMessageContent::class)
subclass(EventContent.Invite::class)
subclass(EventContent.Custom::class)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ class ChatIntegrationTest : BaseChatIntegrationTest() {
}
}

delayForHistory()
delayForHistory()
val eventFromHistory = chat.getEventsHistory(channelId, tt + 1, tt).await().events.first()
val payload: EventContent.Custom = eventFromHistory.payload as EventContent.Custom
Expand Down
18 changes: 18 additions & 0 deletions pubnub-chat-impl/src/jsMain/kotlin/ChatJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import com.pubnub.chat.internal.ChatImpl
import com.pubnub.chat.internal.ChatInternal
import com.pubnub.chat.internal.PUBNUB_CHAT_VERSION
import com.pubnub.chat.internal.TYPE_OF_MESSAGE_IS_CUSTOM
import com.pubnub.chat.internal.TYPE_OF_MESSAGE_IS_INVITE
import com.pubnub.chat.internal.TYPE_OF_MESSAGE_IS_MENTION
import com.pubnub.chat.internal.TYPE_OF_MESSAGE_IS_RECEIPT
import com.pubnub.chat.internal.TYPE_OF_MESSAGE_IS_REPORT
import com.pubnub.chat.internal.TYPE_OF_MESSAGE_IS_TYPING
import com.pubnub.chat.internal.serialization.PNDataEncoder
import com.pubnub.chat.restrictions.Restriction
import com.pubnub.chat.types.ChannelMentionData
Expand Down Expand Up @@ -43,8 +48,21 @@ class ChatJs internal constructor(val chat: ChatInternal, val config: ChatConfig
EmitEventMethod.PUBLISH
}

// todo Instead of manually picking Custom for everything try:
// val eventContent = PNDataEncoder.decode(EventContent.serializer(), createJsonElement(payload))

val eventContent = if (type == TYPE_OF_MESSAGE_IS_CUSTOM) {
EventContent.Custom((payload as JsMap<Any?>).toMap(), method)
} else if (type == TYPE_OF_MESSAGE_IS_REPORT) {
PNDataEncoder.decode(EventContent.Report.serializer(), createJsonElement(payload))
} else if (type == TYPE_OF_MESSAGE_IS_TYPING) {
PNDataEncoder.decode(EventContent.Typing.serializer(), createJsonElement(payload))
} else if (type == TYPE_OF_MESSAGE_IS_RECEIPT) {
PNDataEncoder.decode(EventContent.Receipt.serializer(), createJsonElement(payload))
} else if (type == TYPE_OF_MESSAGE_IS_MENTION) {
PNDataEncoder.decode(EventContent.Mention.serializer(), createJsonElement(payload))
} else if (type == TYPE_OF_MESSAGE_IS_INVITE) {
PNDataEncoder.decode(EventContent.Invite.serializer(), createJsonElement(payload))
} else {
payload.type = type
PNDataEncoder.decode(createJsonElement(payload))
Expand Down

0 comments on commit 205d52b

Please sign in to comment.