Skip to content

Commit

Permalink
Perform Url decoding for channels history calls
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Jul 29, 2024
1 parent a04dc28 commit 8cc5aea
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release/chat-maven-build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
echo "Build Chat SDK module artifacts"
./gradlew jar
./gradlew jar --no-configuration-cache
2 changes: 1 addition & 1 deletion .github/workflows/release/chat-maven-publish.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./gradlew publishToSonatype closeSonatypeStagingRepository
./gradlew publishToSonatype closeSonatypeStagingRepository --no-configuration-cache
2 changes: 1 addition & 1 deletion .github/workflows/release/pre-github-pages-publish.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./gradlew :dokkaGfmMultiModule
./gradlew :dokkaGfmMultiModule --no-configuration-cache
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import com.pubnub.chat.internal.serialization.PNDataEncoder
import com.pubnub.chat.internal.timer.PlatformTimer
import com.pubnub.chat.internal.timer.PlatformTimer.Companion.runPeriodically
import com.pubnub.chat.internal.timer.PlatformTimer.Companion.runWithDelay
import com.pubnub.chat.internal.util.channelsUrlDecoded
import com.pubnub.chat.internal.util.getPhraseToLookFor
import com.pubnub.chat.internal.utils.cyrb53a
import com.pubnub.chat.membership.MembershipsResponse
Expand Down Expand Up @@ -858,7 +859,7 @@ class ChatImpl(
includeMessageActions = false,
includeMessageType = true
).then { pnFetchMessagesResult: PNFetchMessagesResult ->
val pnFetchMessageItems: List<PNFetchMessageItem> = pnFetchMessagesResult.channels[channelId] ?: emptyList()
val pnFetchMessageItems: List<PNFetchMessageItem> = pnFetchMessagesResult.channelsUrlDecoded[channelId] ?: emptyList()
val events: Set<Event<EventContent>> =
pnFetchMessageItems.map { pnFetchMessageItem: PNFetchMessageItem ->
EventImpl.fromDTO(chat = this, channelId = channelId, pnFetchMessageItem = pnFetchMessageItem)
Expand Down Expand Up @@ -922,9 +923,7 @@ class ChatImpl(
channelId: String,
pnFetchMessagesResult: PNFetchMessagesResult
): Long {
// todo in TS there is encodeURIComponent(channelId) do we need this?
// created CLEN-2183 for PubNub SDK
val relevantLastMessage: List<PNFetchMessageItem>? = pnFetchMessagesResult.channels[channelId]
val relevantLastMessage: List<PNFetchMessageItem>? = pnFetchMessagesResult.channelsUrlDecoded[channelId]
return relevantLastMessage?.firstOrNull()?.timetoken ?: 0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import com.pubnub.chat.internal.message.MessageImpl
import com.pubnub.chat.internal.restrictions.RestrictionImpl
import com.pubnub.chat.internal.serialization.PNDataEncoder
import com.pubnub.chat.internal.timer.PlatformTimer.Companion.runWithDelay
import com.pubnub.chat.internal.util.channelsUrlDecoded
import com.pubnub.chat.internal.util.getPhraseToLookFor
import com.pubnub.chat.internal.utils.ExponentialRateLimiter
import com.pubnub.chat.internal.uuidFilterString
Expand Down Expand Up @@ -691,10 +692,10 @@ abstract class BaseChannel<C : Channel, M : Message>(
includeMeta = true
).then { pnFetchMessagesResult: PNFetchMessagesResult ->
HistoryResponse(
messages = pnFetchMessagesResult.channels[channelId]?.map { messageItem: PNFetchMessageItem ->
messages = pnFetchMessagesResult.channelsUrlDecoded[channelId]?.map { messageItem: PNFetchMessageItem ->
messageFactory(chat, messageItem, channelId)
} ?: error("Unable to read messages"),
isMore = pnFetchMessagesResult.channels[channelId]?.size == count
isMore = pnFetchMessagesResult.channelsUrlDecoded[channelId]?.size == count
)
}.catch {
Result.failure(PubNubException(FAILED_TO_RETRIEVE_HISTORY_DATA, it))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pubnub.chat.internal.util

import com.pubnub.api.models.consumer.history.PNFetchMessagesResult

internal fun getPhraseToLookFor(text: String, separator: String): String? {
val lastAtIndex = text.lastIndexOf(separator)
if (lastAtIndex == -1) {
Expand All @@ -16,3 +18,7 @@ internal fun getPhraseToLookFor(text: String, separator: String): String? {
}
return splitWords.joinToString(" ")
}

expect fun urlDecode(encoded: String): String

internal val PNFetchMessagesResult.channelsUrlDecoded get() = channels.mapKeys { urlDecode(it.key) }
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class BaseChatIntegrationTest : BaseIntegrationTest() {
chatPam = ChatImpl(ChatConfiguration(), pubnubPam)
channel01 = ChannelImpl(
chat = chat,
id = randomString(),
id = randomString() + "!_=-@",
name = randomString(),
custom = mapOf(randomString() to randomString()),
description = randomString(),
Expand All @@ -51,7 +51,7 @@ abstract class BaseChatIntegrationTest : BaseIntegrationTest() {
)
channel02 = ChannelImpl(
chat = chat,
id = randomString(),
id = randomString() + "!_=-@",
name = randomString(),
custom = mapOf(randomString() to randomString()),
description = randomString(),
Expand Down Expand Up @@ -81,7 +81,7 @@ abstract class BaseChatIntegrationTest : BaseIntegrationTest() {
)
channelPam = ChannelImpl(
chat = chatPam,
id = randomString(),
id = randomString() + "!_=-@",
name = randomString(),
custom = mapOf(randomString() to randomString()),
description = randomString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ class UtilsTest {
val result = getPhraseToLookFor(phraseWithTwoWordsAfterHash, "#")
assertEquals("$firstWord $secondWord", result)
}

@Test
fun urlDecode() {
val input = "a_-%3D%40.%21%24%23%25%26%5E%3B"
val expected = "a_-=@.!\$#%&^;"

assertEquals(expected, com.pubnub.chat.internal.util.urlDecode(input))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pubnub.chat.internal.util

actual fun urlDecode(encoded: String): String {
TODO("Not yet implemented")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pubnub.chat.internal.util

actual fun urlDecode(encoded: String): String = decodeURIComponent(encoded)

external fun decodeURIComponent(encoded: String): String
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pubnub.chat.internal.util

import java.net.URLDecoder

actual fun urlDecode(encoded: String): String = URLDecoder.decode(encoded, Charsets.UTF_8.name())

0 comments on commit 8cc5aea

Please sign in to comment.