Skip to content

Commit

Permalink
Fix: Wrong user suggestion source for message draft created on Thread…
Browse files Browse the repository at this point in the history
…Channel (#143)

* ThreadChannel: use parent channel for user suggestions in message draft
  • Loading branch information
wkal-pubnub authored Dec 12, 2024
1 parent 3cc7416 commit 3090472
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import com.pubnub.chat.MessageDraft
import com.pubnub.chat.MessageDraftChangeListener
import com.pubnub.chat.MessageElement
import com.pubnub.chat.SuggestedMention
import com.pubnub.chat.ThreadChannel
import com.pubnub.chat.User
import com.pubnub.chat.internal.channel.ChannelImpl
import com.pubnub.chat.internal.error.PubNubErrorMessage
import com.pubnub.chat.internal.util.pnError
import com.pubnub.chat.types.ChannelType
Expand Down Expand Up @@ -53,6 +55,11 @@ class MessageDraftImpl(
private var messageText: StringBuilder = StringBuilder("")

internal val value get() = messageText as CharSequence
private val userSuggestionSourceChannel = if (channel is ThreadChannel) {
ChannelImpl(chat, id = channel.parentChannelId)
} else {
channel
}

override fun addChangeListener(listener: MessageDraftChangeListener) {
listeners.update {
Expand Down Expand Up @@ -256,9 +263,9 @@ class MessageDraftImpl(
return getMessageElements(messageText, mentions)
}

private fun getSuggestedUsers(searchText: String): PNFuture<Collection<User>> {
internal fun getSuggestedUsers(searchText: String): PNFuture<Collection<User>> {
return if (userSuggestionSource == MessageDraft.UserSuggestionSource.CHANNEL) {
channel.getUserSuggestions(searchText, userLimit).then { memberships ->
userSuggestionSourceChannel.getUserSuggestions(searchText, userLimit).then { memberships ->
memberships.map { it.user }
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.pubnub.kmp
import com.pubnub.api.models.consumer.PNPublishResult
import com.pubnub.api.v2.callbacks.Result
import com.pubnub.chat.Channel
import com.pubnub.chat.Membership
import com.pubnub.chat.MentionTarget
import com.pubnub.chat.MessageDraft
import com.pubnub.chat.MessageDraftChangeListener
import com.pubnub.chat.MessageElement
import com.pubnub.chat.SuggestedMention
import com.pubnub.chat.ThreadChannel
import com.pubnub.chat.User
import com.pubnub.chat.internal.ChatInternal
import com.pubnub.chat.internal.Mention
Expand All @@ -17,6 +19,7 @@ import com.pubnub.chat.internal.channel.ChannelImpl
import com.pubnub.chat.internal.findChannelMentionMatches
import com.pubnub.chat.internal.findUserMentionMatches
import com.pubnub.test.await
import com.pubnub.test.randomString
import dev.mokkery.MockMode
import dev.mokkery.answering.returns
import dev.mokkery.every
Expand Down Expand Up @@ -434,4 +437,31 @@ class MessageDraftTest {
assertEquals(it.second, findChannelMentionMatches(it.first).first().value)
}
}

@Test
fun thread_channel_user_suggestions_come_from_parent_channel() {
val threadChannel: ThreadChannel = mock(MockMode.strict)
every { threadChannel.chat } returns chat
every { threadChannel.parentChannelId } returns randomString()
every { threadChannel.getUserSuggestions(any(), any()) } returns listOf<Membership>().asFuture()

val md = MessageDraftImpl(threadChannel, MessageDraft.UserSuggestionSource.CHANNEL, false)
try {
md.getSuggestedUsers("aaa")
} catch (_: Throwable) {
// ignore
}
verifyNoMoreCalls()
}

@Test
fun channel_user_suggestions_come_from_this_channel() {
val channel: Channel = mock(MockMode.strict)
every { channel.chat } returns chat
every { channel.getUserSuggestions(any(), any()) } returns listOf<Membership>().asFuture()

val md = MessageDraftImpl(channel, MessageDraft.UserSuggestionSource.CHANNEL, false)
md.getSuggestedUsers("aaa")
verify { channel.getUserSuggestions(any(), any()) }
}
}

0 comments on commit 3090472

Please sign in to comment.