Skip to content

Commit

Permalink
Refactor. Use non-deprecated Memberships and ChannelMembers method.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-cebo committed Dec 19, 2024
1 parent bab3347 commit 1e6e9db
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 15 deletions.
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions pubnub-chat-api/api/pubnub-chat-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -711,6 +712,9 @@ class ChatImpl(
override fun setRestrictions(
restriction: Restriction
): PNFuture<Unit> {
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

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ data class MembershipImpl(
override val custom: Map<String, Any?>?,
override val updated: String?,
override val eTag: String?,
override val status: String?,
override val type: String?,
) : Membership {
override val lastReadMessageTimetoken: Long?
get() {
Expand Down Expand Up @@ -116,7 +118,9 @@ data class MembershipImpl(
user,
update.custom?.value ?: custom,
update.updated,
update.eTag
update.eTag,
update.status?.value,
update.type?.value
)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) =
Expand All @@ -201,6 +209,8 @@ data class MembershipImpl(
userMembership.custom?.value,
userMembership.updated,
userMembership.eTag,
userMembership.status?.value,
userMembership.type?.value,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,10 @@ class ChatIntegrationTest : BaseChatIntegrationTest() {
val unbanned = CompletableDeferred<Unit>()
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<EventContent.Moderation> ->
Expand All @@ -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()
Expand Down
37 changes: 35 additions & 2 deletions pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChatTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Unit> ->
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class MembershipTest {
mapOf("lastReadMessageTimetoken" to lastMessageTimetoken, "other_stuff" to "some string"),
null,
null,
null,
null
)

assertEquals(lastMessageTimetoken, membership.lastReadMessageTimetoken)
Expand All @@ -56,6 +58,8 @@ class MembershipTest {
mapOf(),
null,
null,
null,
null
)
assertNull(membership.getUnreadMessagesCount().await())
}
Expand All @@ -70,6 +74,8 @@ class MembershipTest {
mapOf("lastReadMessageTimetoken" to lastMessageTimetoken),
null,
null,
null,
null
)

val messageCounts: MessageCounts = mock()
Expand Down
4 changes: 4 additions & 0 deletions pubnub-chat-impl/src/jsMain/kotlin/MembershipJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
)
}
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pluginManagement {
includeBuild("build-logic/gradle-plugins")
repositories {
mavenLocal() // todo remove
gradlePluginPortal()
mavenCentral()
}
Expand All @@ -13,6 +14,7 @@ rootProject.name = "pubnub-chat"

dependencyResolutionManagement {
repositories {
mavenLocal() // todo remove
mavenCentral()
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/jsMain/resources/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/// <reference types="pubnub" />
import PubNub from "pubnub";
import { GetMembershipsParametersv2, GetChannelMembersParameters, ObjectCustom, SetMembershipsParameters, ChannelMetadataObject, PublishParameters, SendFileParameters } from "pubnub";
type MembershipFields = Pick<Membership, "channel" | "user" | "custom" | "updated" | "eTag">;
type MembershipFields = Pick<Membership, "channel" | "user" | "custom" | "updated" | "eTag" | "status" | "type">;
declare class Membership {
private chat;
readonly channel: Channel;
readonly user: User;
readonly custom: ObjectCustom | null | undefined;
readonly updated: string;
readonly eTag: string;
readonly status?: string;
readonly type?: string;
update({ custom }: {
custom: ObjectCustom;
}): Promise<Membership>;
Expand Down

0 comments on commit 1e6e9db

Please sign in to comment.