From 1e6e9db6707bddf1f2ccc6b1ae0a6b0be73fc728 Mon Sep 17 00:00:00 2001 From: "marcin.cebo" Date: Thu, 19 Dec 2024 15:07:44 +0100 Subject: [PATCH] Refactor. Use non-deprecated Memberships and ChannelMembers method. --- gradle/libs.versions.toml | 3 +- kotlin-js-store/yarn.lock | 8 ++-- pubnub-chat-api/api/pubnub-chat-api.api | 2 + .../kotlin/com/pubnub/chat/Membership.kt | 10 +++++ .../com/pubnub/chat/internal/ChatImpl.kt | 10 ++++- .../pubnub/chat/internal/MembershipImpl.kt | 12 +++++- .../pubnub/integration/ChatIntegrationTest.kt | 10 ++--- .../kotlin/com/pubnub/kmp/ChatTest.kt | 37 ++++++++++++++++++- .../kotlin/com/pubnub/kmp/MembershipTest.kt | 6 +++ .../src/jsMain/kotlin/MembershipJs.kt | 4 ++ settings.gradle.kts | 2 + src/jsMain/resources/index.d.ts | 4 +- 12 files changed, 93 insertions(+), 15 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8b3b706e..9b14dffb 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,7 +6,8 @@ ktlint = "12.1.0" dokka = "1.9.20" kotlinx_serialization = "1.7.3" kotlinx_coroutines = "1.9.0" -pubnub = "10.3.1" +#pubnub = "10.3.1" tu podać nowa wersję +pubnub = "10.3.2-dev" pubnub_swift = "8.2.2" [libraries] diff --git a/kotlin-js-store/yarn.lock b/kotlin-js-store/yarn.lock index e9b8dbb2..9f60bdd9 100644 --- a/kotlin-js-store/yarn.lock +++ b/kotlin-js-store/yarn.lock @@ -659,10 +659,10 @@ 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== -pubnub@8.3.1: - version "8.3.1" - resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.3.1.tgz#48b6c2468452427c9d1aa9a51e78781d25eec660" - integrity sha512-+aFp84Jsqpmu9utci8uQhABTMoh1Uk8UafGS1QANSkceXX3HDRTQnpTw8itH0UchzCS9pSfUATTl+Ay3jvM5EA== +pubnub@8.4.0: + version "8.4.0" + resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.4.0.tgz#8a5fdd3af9783abb1de44ea4145107e296c8394d" + integrity sha512-HvkFhn4XcfR1wdJv4paX94I0TT4UBHW/vIuYnS+6XuoSx0AunJMCk5wbKnSesmdbzYUZa8Ag3H1mJQZKuyRy8Q== dependencies: agentkeepalive "^3.5.2" buffer "^6.0.3" diff --git a/pubnub-chat-api/api/pubnub-chat-api.api b/pubnub-chat-api/api/pubnub-chat-api.api index 7e3582ec..409db660 100644 --- a/pubnub-chat-api/api/pubnub-chat-api.api +++ b/pubnub-chat-api/api/pubnub-chat-api.api @@ -128,6 +128,8 @@ public abstract interface class com/pubnub/chat/Membership { public abstract fun getCustom ()Ljava/util/Map; public abstract fun getETag ()Ljava/lang/String; public abstract fun getLastReadMessageTimetoken ()Ljava/lang/Long; + public abstract fun getStatus ()Ljava/lang/String; + public abstract fun getType ()Ljava/lang/String; public abstract fun getUnreadMessagesCount ()Lcom/pubnub/kmp/PNFuture; public abstract fun getUpdated ()Ljava/lang/String; public abstract fun getUser ()Lcom/pubnub/chat/User; diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt index 8b3b6bd8..9262a99e 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt @@ -39,6 +39,16 @@ interface Membership { */ val eTag: String? + /** + * Status of a Membership + */ + val status: String? + + /** + * Type of a Membership + */ + val type: String? + /** * Timetoken of the last message a user read on a given channel. */ diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt index a26859f0..b3d9da7c 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt @@ -63,6 +63,7 @@ import com.pubnub.chat.internal.error.PubNubErrorMessage.FAILED_TO_FORWARD_MESSA import com.pubnub.chat.internal.error.PubNubErrorMessage.FAILED_TO_RETRIEVE_WHO_IS_PRESENT_DATA import com.pubnub.chat.internal.error.PubNubErrorMessage.FAILED_TO_SOFT_DELETE_CHANNEL import com.pubnub.chat.internal.error.PubNubErrorMessage.ID_IS_REQUIRED +import com.pubnub.chat.internal.error.PubNubErrorMessage.MODERATION_CAN_BE_SET_ONLY_BY_CLIENT_HAVING_SECRET_KEY import com.pubnub.chat.internal.error.PubNubErrorMessage.ONLY_ONE_LEVEL_OF_THREAD_NESTING_IS_ALLOWED import com.pubnub.chat.internal.error.PubNubErrorMessage.STORE_USER_ACTIVITY_INTERVAL_SHOULD_BE_AT_LEAST_1_MIN import com.pubnub.chat.internal.error.PubNubErrorMessage.THERE_IS_NO_ACTION_TIMETOKEN_CORRESPONDING_TO_THE_THREAD @@ -711,6 +712,9 @@ class ChatImpl( override fun setRestrictions( restriction: Restriction ): PNFuture { + if (this.pubNub.configuration.secretKey.isEmpty()) { + return log.logErrorAndReturnException(MODERATION_CAN_BE_SET_ONLY_BY_CLIENT_HAVING_SECRET_KEY).asFuture() + } val channel: String = INTERNAL_MODERATION_PREFIX + restriction.channelId val userId = restriction.userId @@ -1173,7 +1177,11 @@ class ChatImpl( customMetadataToSet[PINNED_MESSAGE_TIMETOKEN] = message.timetoken.toString() customMetadataToSet[PINNED_MESSAGE_CHANNEL_ID] = message.channelId } - return pubNub.setChannelMetadata(channel.id, includeCustom = true, custom = createCustomObject(customMetadataToSet)) + return pubNub.setChannelMetadata( + channel = channel.id, + includeCustom = true, + custom = createCustomObject(customMetadataToSet) + ) } internal fun getThreadId(channelId: String, messageTimetoken: Long): String { diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MembershipImpl.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MembershipImpl.kt index 5fa3982a..808baace 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MembershipImpl.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MembershipImpl.kt @@ -35,6 +35,8 @@ data class MembershipImpl( override val custom: Map?, override val updated: String?, override val eTag: String?, + override val status: String?, + override val type: String?, ) : Membership { override val lastReadMessageTimetoken: Long? get() { @@ -116,7 +118,9 @@ data class MembershipImpl( user, update.custom?.value ?: custom, update.updated, - update.eTag + update.eTag, + update.status?.value, + update.type?.value ) } @@ -158,6 +162,8 @@ data class MembershipImpl( custom = message.data.custom?.value, updated = message.data.updated, eTag = message.data.eTag, + status = message.data.status?.value, + type = message.data.type?.value, ) } is PNDeleteMembershipEventMessage -> null @@ -191,6 +197,8 @@ data class MembershipImpl( channelMembership.custom?.value, channelMembership.updated, channelMembership.eTag, + channelMembership.status?.value, + channelMembership.type?.value, ) internal fun fromChannelMemberDTO(chat: ChatInternal, userMembership: PNMember, channel: Channel) = @@ -201,6 +209,8 @@ data class MembershipImpl( userMembership.custom?.value, userMembership.updated, userMembership.eTag, + userMembership.status?.value, + userMembership.type?.value, ) } } diff --git a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/integration/ChatIntegrationTest.kt b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/integration/ChatIntegrationTest.kt index 7e431aa4..7d9ed19e 100644 --- a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/integration/ChatIntegrationTest.kt +++ b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/integration/ChatIntegrationTest.kt @@ -627,10 +627,10 @@ class ChatIntegrationTest : BaseChatIntegrationTest() { val unbanned = CompletableDeferred() val restrictionBan = Restriction(userId = userId, channelId = channelId, ban = true, reason = "rude") val restrictionUnban = Restriction(userId = userId, channelId = channelId, ban = false, mute = false, reason = "ok") - pubnub.test(backgroundScope, checkAllEvents = false) { + pubnubPamServer.test(backgroundScope, checkAllEvents = false) { var removeListenerAndUnsubscribe: AutoCloseable? = null - pubnub.awaitSubscribe(channels = listOf(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId)) { - removeListenerAndUnsubscribe = chat.listenForEvents( + pubnubPamServer.awaitSubscribe(channels = listOf(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId)) { + removeListenerAndUnsubscribe = chatPamServer.listenForEvents( type = EventContent.Moderation::class, channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId ) { event: Event -> @@ -643,9 +643,9 @@ class ChatIntegrationTest : BaseChatIntegrationTest() { } } - chat.setRestrictions(restrictionBan).await() + chatPamServer.setRestrictions(restrictionBan).await() banned.await() - chat.setRestrictions(restrictionUnban).await() + chatPamServer.setRestrictions(restrictionUnban).await() unbanned.await() removeListenerAndUnsubscribe?.close() diff --git a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChatTest.kt b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChatTest.kt index a3965e2e..921b7657 100644 --- a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChatTest.kt +++ b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChatTest.kt @@ -134,12 +134,22 @@ class ChatTest : BaseTest() { @BeforeTest fun setUp() { - pnConfiguration = createPNConfiguration(UserId(userId), subscribeKey, publishKey, authToken = null) + objectUnderTest = ChatImpl(setupConfiguration(withSecretKey = false), pubnub, timerManager = timerManager) + } + + private fun setupConfiguration(withSecretKey: Boolean): ChatConfiguration { + pnConfiguration = if (withSecretKey) { + createPNConfiguration(UserId(userId), subscribeKey, publishKey, "secretKey", authToken = null) + } else { + createPNConfiguration(UserId(userId), subscribeKey, publishKey, authToken = null) + } + every { pubnub.configuration } returns pnConfiguration chatConfig = ChatConfiguration( typingTimeout = 2000.milliseconds ) - objectUnderTest = ChatImpl(chatConfig, pubnub, timerManager = timerManager) + + return chatConfig } @Test @@ -1319,6 +1329,8 @@ class ChatTest : BaseTest() { @Test fun shouldRemoveRestrictionWhenBanAndMuteIsFalse() { + objectUnderTest = ChatImpl(setupConfiguration(withSecretKey = true), pubnub, timerManager = timerManager) + val restrictedUserId = userId val restrictedChannelId = channelId val ban = false @@ -1374,8 +1386,29 @@ class ChatTest : BaseTest() { assertEquals(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + restrictedUserId, actualModerationEventChannelId) } + @Test + fun shouldThrowExceptionWhenSecretKeyIsNotSet() { + val restriction = Restriction( + userId = "userId", + channelId = "channelId", + ban = true, + mute = true, + reason = "rude" + ) + + objectUnderTest.setRestrictions(restriction).async { result: Result -> + assertTrue(result.isFailure) + assertEquals( + "Moderation restrictions can only be set by clients initialized with a Secret Key.", + result.exceptionOrNull()?.message + ) + } + } + @Test fun shouldAddRestrictionWhenBanIsTrue() { + objectUnderTest = ChatImpl(setupConfiguration(withSecretKey = true), pubnub, timerManager = timerManager) + val restrictedUserId = userId val restrictedChannelId = channelId val ban = true diff --git a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MembershipTest.kt b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MembershipTest.kt index de442542..3f4dfeae 100644 --- a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MembershipTest.kt +++ b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MembershipTest.kt @@ -41,6 +41,8 @@ class MembershipTest { mapOf("lastReadMessageTimetoken" to lastMessageTimetoken, "other_stuff" to "some string"), null, null, + null, + null ) assertEquals(lastMessageTimetoken, membership.lastReadMessageTimetoken) @@ -56,6 +58,8 @@ class MembershipTest { mapOf(), null, null, + null, + null ) assertNull(membership.getUnreadMessagesCount().await()) } @@ -70,6 +74,8 @@ class MembershipTest { mapOf("lastReadMessageTimetoken" to lastMessageTimetoken), null, null, + null, + null ) val messageCounts: MessageCounts = mock() diff --git a/pubnub-chat-impl/src/jsMain/kotlin/MembershipJs.kt b/pubnub-chat-impl/src/jsMain/kotlin/MembershipJs.kt index 1890228b..75575a9a 100644 --- a/pubnub-chat-impl/src/jsMain/kotlin/MembershipJs.kt +++ b/pubnub-chat-impl/src/jsMain/kotlin/MembershipJs.kt @@ -16,6 +16,8 @@ class MembershipJs internal constructor(internal val membership: Membership, int val custom get() = membership.custom?.toJsMap() val updated by membership::updated val eTag by membership::eTag + val status by membership::status + val type by membership::type val lastReadMessageTimetoken: String? get() = membership.lastReadMessageTimetoken?.toString() @@ -56,6 +58,8 @@ class MembershipJs internal constructor(internal val membership: Membership, int "custom" to custom, "updated" to updated, "eTag" to eTag, + "status" to status, + "type" to type, "lastReadMessageTimetoken" to lastReadMessageTimetoken ) } diff --git a/settings.gradle.kts b/settings.gradle.kts index 1e4eed44..7d8c5a3a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,6 +1,7 @@ pluginManagement { includeBuild("build-logic/gradle-plugins") repositories { + mavenLocal() // todo remove gradlePluginPortal() mavenCentral() } @@ -13,6 +14,7 @@ rootProject.name = "pubnub-chat" dependencyResolutionManagement { repositories { + mavenLocal() // todo remove mavenCentral() } } diff --git a/src/jsMain/resources/index.d.ts b/src/jsMain/resources/index.d.ts index 27ea9eb1..af39f0f9 100644 --- a/src/jsMain/resources/index.d.ts +++ b/src/jsMain/resources/index.d.ts @@ -1,7 +1,7 @@ /// import PubNub from "pubnub"; import { GetMembershipsParametersv2, GetChannelMembersParameters, ObjectCustom, SetMembershipsParameters, ChannelMetadataObject, PublishParameters, SendFileParameters } from "pubnub"; -type MembershipFields = Pick; +type MembershipFields = Pick; declare class Membership { private chat; readonly channel: Channel; @@ -9,6 +9,8 @@ declare class Membership { readonly custom: ObjectCustom | null | undefined; readonly updated: string; readonly eTag: string; + readonly status?: string; + readonly type?: string; update({ custom }: { custom: ObjectCustom; }): Promise;