diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt index 5b093038..c2def44b 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt @@ -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 @@ -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 { @@ -256,9 +263,9 @@ class MessageDraftImpl( return getMessageElements(messageText, mentions) } - private fun getSuggestedUsers(searchText: String): PNFuture> { + internal fun getSuggestedUsers(searchText: String): PNFuture> { return if (userSuggestionSource == MessageDraft.UserSuggestionSource.CHANNEL) { - channel.getUserSuggestions(searchText, userLimit).then { memberships -> + userSuggestionSourceChannel.getUserSuggestions(searchText, userLimit).then { memberships -> memberships.map { it.user } } } else { diff --git a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt index d26046dc..e14e257a 100644 --- a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt +++ b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt @@ -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 @@ -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 @@ -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().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().asFuture() + + val md = MessageDraftImpl(channel, MessageDraft.UserSuggestionSource.CHANNEL, false) + md.getSuggestedUsers("aaa") + verify { channel.getUserSuggestions(any(), any()) } + } }