Skip to content

Commit

Permalink
More Chat JS test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Nov 14, 2024
1 parent c3f5ae1 commit 699b7a6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
@file:OptIn(ExperimentalJsExport::class)

package com.pubnub.chat.types

import kotlinx.serialization.Serializable
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

@Serializable
@JsExport
class MessageReferencedChannel(val id: String, val name: String)

typealias MessageReferencedChannels = Map<Int, MessageReferencedChannel>
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class MessageDraftImpl(
)
} else {
return channel.sendText(
text = render(getMessageElements()),
text = messageText.toString(),
meta = meta,
shouldStore = shouldStore,
usePost = usePost,
Expand Down Expand Up @@ -192,7 +192,6 @@ class MessageDraftImpl(
}

internal fun addMentionedUser(user: User, nameOccurrenceIndex: Int) {
println("before $messageText")
checkFormatV2()
val allUserMentions = findUserMentionMatches(messageText)
if (nameOccurrenceIndex >= allUserMentions.size) {
Expand All @@ -204,7 +203,6 @@ class MessageDraftImpl(
removeTextInternal(match.matchStart, match.value.length)
insertTextInternal(match.matchStart, "@" + user.name!!)
mentionedUsers[nameOccurrenceIndex] = MessageMentionedUser(user.id, user.name!!)
println("after $messageText")
fireMessageElementsChanged()
}

Expand Down
3 changes: 1 addition & 2 deletions pubnub-chat-impl/src/jsMain/kotlin/ChannelJs.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@file:OptIn(ExperimentalJsExport::class, ExperimentalJsStatic::class)

import com.pubnub.chat.Channel
import com.pubnub.chat.Message
import com.pubnub.chat.MessageDraft
import com.pubnub.chat.internal.MessageDraftImpl
import com.pubnub.chat.internal.channel.BaseChannel
Expand Down Expand Up @@ -132,7 +131,7 @@ open class ChannelJs internal constructor(internal val channel: Channel) : Chann
).then { result ->
createJsObject<HistoryResponseJs> {
this.isMore = result.isMore
this.messages = result.messages.map(Message::asJs).toTypedArray()
this.messages = result.messages.map { it.asJs() }.toTypedArray()
}
}.asPromise()
}
Expand Down
22 changes: 14 additions & 8 deletions pubnub-chat-impl/src/jsMain/kotlin/ChatJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.pubnub.api.PubNubImpl
import com.pubnub.api.createJsonElement
import com.pubnub.chat.Chat
import com.pubnub.chat.internal.ChatImpl
import com.pubnub.chat.internal.ChatInternal
import com.pubnub.chat.internal.serialization.PNDataEncoder
Expand All @@ -22,7 +21,7 @@ import kotlin.js.json

@JsExport
@JsName("Chat")
class ChatJs internal constructor(val chat: Chat, val config: ChatConfig) {
class ChatJs internal constructor(val chat: ChatInternal, val config: ChatConfig) {
val currentUser: UserJs get() = chat.currentUser.asJs()

val sdk: PubNub get() = (chat.pubNub as PubNubImpl).jsPubNub
Expand Down Expand Up @@ -302,12 +301,12 @@ class ChatJs internal constructor(val chat: Chat, val config: ChatConfig) {
}.asPromise()
}

fun getUnreadMessagesCounts(params: PubNub.GetMembershipsParametersv2): Promise<Array<GetUnreadMessagesCountsJs>> {
fun getUnreadMessagesCounts(params: PubNub.GetMembershipsParametersv2?): Promise<Array<GetUnreadMessagesCountsJs>> {
return chat.getUnreadMessagesCounts(
params.limit?.toInt(),
params.page?.toKmp(),
params.filter,
extractSortKeys(params.sort)
params?.limit?.toInt(),
params?.page?.toKmp(),
params?.filter,
extractSortKeys(params?.sort)
).then { result ->
result.map { unreadMessagesCount ->
createJsObject<GetUnreadMessagesCountsJs> {
Expand Down Expand Up @@ -365,11 +364,18 @@ class ChatJs internal constructor(val chat: Chat, val config: ChatConfig) {
return json("config" to config, "currentUser" to currentUser)
}

fun getUserSuggestions(text: String, options: dynamic?): Promise<Array<UserJs>> {
val limit = options?.limit as? Number
return chat.getUserSuggestions(text.substring(1), limit?.toInt() ?: 10).then { users ->
users.map { it.asJs() }.toTypedArray()
}.asPromise()
}

companion object {
@JsStatic
fun init(config: ChatConstructor): Promise<ChatJs> {
return ChatImpl(config.toChatConfiguration(), PubNubImpl(PubNub(config))).initialize().then {
ChatJs(it, config)
ChatJs(it as ChatInternal, config)
}.asPromise()
}
}
Expand Down
6 changes: 2 additions & 4 deletions pubnub-chat-impl/src/jsMain/kotlin/MessageDraftJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ class MessageDraftJs internal constructor(
val config: MessageDraftConfig?,
) {
val value: String get() = messageDraft.value.toString()
var quotedMessage: MessageJs? get() = messageDraft.quotedMessage?.asJs()
set(value) {
messageDraft.quotedMessage = value?.message
}
var quotedMessage: MessageJs? = null
var files: Any? = null

fun addReferencedChannel(channel: ChannelJs, channelNameOccurrenceIndex: Int) {
Expand Down Expand Up @@ -110,6 +107,7 @@ class MessageDraftJs internal constructor(
val name = file.name
messageDraft.files.add(InputFile(name ?: "", type ?: "", UploadableImpl(file)))
}
messageDraft.quotedMessage = quotedMessage?.message

return messageDraft.send(
options?.meta?.unsafeCast<JsMap<Any>>()?.toMap(),
Expand Down
11 changes: 8 additions & 3 deletions pubnub-chat-impl/src/jsMain/kotlin/MessageJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.pubnub.chat.Message
import com.pubnub.chat.internal.message.BaseMessage
import com.pubnub.chat.types.EventContent
import com.pubnub.chat.types.MessageMentionedUser
import com.pubnub.chat.types.MessageReferencedChannel
import com.pubnub.kmp.JsMap
import com.pubnub.kmp.createJsObject
import com.pubnub.kmp.then
import com.pubnub.kmp.toJsMap
Expand All @@ -28,9 +31,11 @@ open class MessageJs internal constructor(internal val message: Message) {
}?.toJsMap()
val meta get() = message.meta?.toJsMap() // todo recursive?

/*get mentionedUsers(): any;
get referencedChannels(): any;
get textLinks(): any;*/
val mentionedUsers: JsMap<MessageMentionedUser>?
get() = message.mentionedUsers?.mapKeys { it.key.toString() }?.toJsMap()
val referencedChannels: JsMap<MessageReferencedChannel>?
get() = message.referencedChannels?.mapKeys { it.key.toString() }?.toJsMap()
val textLinks get() = message.textLinks?.toTypedArray()
val type by message::type
val quotedMessage: QuotedMessageJs? get() = message.quotedMessage?.toJs()
val files get() = message.files.toTypedArray()
Expand Down

0 comments on commit 699b7a6

Please sign in to comment.