Skip to content

Commit 1e6e9db

Browse files
committed
Refactor. Use non-deprecated Memberships and ChannelMembers method.
1 parent bab3347 commit 1e6e9db

File tree

12 files changed

+93
-15
lines changed

12 files changed

+93
-15
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ ktlint = "12.1.0"
66
dokka = "1.9.20"
77
kotlinx_serialization = "1.7.3"
88
kotlinx_coroutines = "1.9.0"
9-
pubnub = "10.3.1"
9+
#pubnub = "10.3.1" tu podać nowa wersję
10+
pubnub = "10.3.2-dev"
1011
pubnub_swift = "8.2.2"
1112

1213
[libraries]

kotlin-js-store/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,10 @@ proxy-from-env@^1.1.0:
659659
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
660660
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
661661

662-
pubnub@8.3.1:
663-
version "8.3.1"
664-
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.3.1.tgz#48b6c2468452427c9d1aa9a51e78781d25eec660"
665-
integrity sha512-+aFp84Jsqpmu9utci8uQhABTMoh1Uk8UafGS1QANSkceXX3HDRTQnpTw8itH0UchzCS9pSfUATTl+Ay3jvM5EA==
662+
pubnub@8.4.0:
663+
version "8.4.0"
664+
resolved "https://registry.yarnpkg.com/pubnub/-/pubnub-8.4.0.tgz#8a5fdd3af9783abb1de44ea4145107e296c8394d"
665+
integrity sha512-HvkFhn4XcfR1wdJv4paX94I0TT4UBHW/vIuYnS+6XuoSx0AunJMCk5wbKnSesmdbzYUZa8Ag3H1mJQZKuyRy8Q==
666666
dependencies:
667667
agentkeepalive "^3.5.2"
668668
buffer "^6.0.3"

pubnub-chat-api/api/pubnub-chat-api.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public abstract interface class com/pubnub/chat/Membership {
128128
public abstract fun getCustom ()Ljava/util/Map;
129129
public abstract fun getETag ()Ljava/lang/String;
130130
public abstract fun getLastReadMessageTimetoken ()Ljava/lang/Long;
131+
public abstract fun getStatus ()Ljava/lang/String;
132+
public abstract fun getType ()Ljava/lang/String;
131133
public abstract fun getUnreadMessagesCount ()Lcom/pubnub/kmp/PNFuture;
132134
public abstract fun getUpdated ()Ljava/lang/String;
133135
public abstract fun getUser ()Lcom/pubnub/chat/User;

pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Membership.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ interface Membership {
3939
*/
4040
val eTag: String?
4141

42+
/**
43+
* Status of a Membership
44+
*/
45+
val status: String?
46+
47+
/**
48+
* Type of a Membership
49+
*/
50+
val type: String?
51+
4252
/**
4353
* Timetoken of the last message a user read on a given channel.
4454
*/

pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/ChatImpl.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import com.pubnub.chat.internal.error.PubNubErrorMessage.FAILED_TO_FORWARD_MESSA
6363
import com.pubnub.chat.internal.error.PubNubErrorMessage.FAILED_TO_RETRIEVE_WHO_IS_PRESENT_DATA
6464
import com.pubnub.chat.internal.error.PubNubErrorMessage.FAILED_TO_SOFT_DELETE_CHANNEL
6565
import com.pubnub.chat.internal.error.PubNubErrorMessage.ID_IS_REQUIRED
66+
import com.pubnub.chat.internal.error.PubNubErrorMessage.MODERATION_CAN_BE_SET_ONLY_BY_CLIENT_HAVING_SECRET_KEY
6667
import com.pubnub.chat.internal.error.PubNubErrorMessage.ONLY_ONE_LEVEL_OF_THREAD_NESTING_IS_ALLOWED
6768
import com.pubnub.chat.internal.error.PubNubErrorMessage.STORE_USER_ACTIVITY_INTERVAL_SHOULD_BE_AT_LEAST_1_MIN
6869
import com.pubnub.chat.internal.error.PubNubErrorMessage.THERE_IS_NO_ACTION_TIMETOKEN_CORRESPONDING_TO_THE_THREAD
@@ -711,6 +712,9 @@ class ChatImpl(
711712
override fun setRestrictions(
712713
restriction: Restriction
713714
): PNFuture<Unit> {
715+
if (this.pubNub.configuration.secretKey.isEmpty()) {
716+
return log.logErrorAndReturnException(MODERATION_CAN_BE_SET_ONLY_BY_CLIENT_HAVING_SECRET_KEY).asFuture()
717+
}
714718
val channel: String = INTERNAL_MODERATION_PREFIX + restriction.channelId
715719
val userId = restriction.userId
716720

@@ -1173,7 +1177,11 @@ class ChatImpl(
11731177
customMetadataToSet[PINNED_MESSAGE_TIMETOKEN] = message.timetoken.toString()
11741178
customMetadataToSet[PINNED_MESSAGE_CHANNEL_ID] = message.channelId
11751179
}
1176-
return pubNub.setChannelMetadata(channel.id, includeCustom = true, custom = createCustomObject(customMetadataToSet))
1180+
return pubNub.setChannelMetadata(
1181+
channel = channel.id,
1182+
includeCustom = true,
1183+
custom = createCustomObject(customMetadataToSet)
1184+
)
11771185
}
11781186

11791187
internal fun getThreadId(channelId: String, messageTimetoken: Long): String {

pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MembershipImpl.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ data class MembershipImpl(
3535
override val custom: Map<String, Any?>?,
3636
override val updated: String?,
3737
override val eTag: String?,
38+
override val status: String?,
39+
override val type: String?,
3840
) : Membership {
3941
override val lastReadMessageTimetoken: Long?
4042
get() {
@@ -116,7 +118,9 @@ data class MembershipImpl(
116118
user,
117119
update.custom?.value ?: custom,
118120
update.updated,
119-
update.eTag
121+
update.eTag,
122+
update.status?.value,
123+
update.type?.value
120124
)
121125
}
122126

@@ -158,6 +162,8 @@ data class MembershipImpl(
158162
custom = message.data.custom?.value,
159163
updated = message.data.updated,
160164
eTag = message.data.eTag,
165+
status = message.data.status?.value,
166+
type = message.data.type?.value,
161167
)
162168
}
163169
is PNDeleteMembershipEventMessage -> null
@@ -191,6 +197,8 @@ data class MembershipImpl(
191197
channelMembership.custom?.value,
192198
channelMembership.updated,
193199
channelMembership.eTag,
200+
channelMembership.status?.value,
201+
channelMembership.type?.value,
194202
)
195203

196204
internal fun fromChannelMemberDTO(chat: ChatInternal, userMembership: PNMember, channel: Channel) =
@@ -201,6 +209,8 @@ data class MembershipImpl(
201209
userMembership.custom?.value,
202210
userMembership.updated,
203211
userMembership.eTag,
212+
userMembership.status?.value,
213+
userMembership.type?.value,
204214
)
205215
}
206216
}

pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/integration/ChatIntegrationTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,10 @@ class ChatIntegrationTest : BaseChatIntegrationTest() {
627627
val unbanned = CompletableDeferred<Unit>()
628628
val restrictionBan = Restriction(userId = userId, channelId = channelId, ban = true, reason = "rude")
629629
val restrictionUnban = Restriction(userId = userId, channelId = channelId, ban = false, mute = false, reason = "ok")
630-
pubnub.test(backgroundScope, checkAllEvents = false) {
630+
pubnubPamServer.test(backgroundScope, checkAllEvents = false) {
631631
var removeListenerAndUnsubscribe: AutoCloseable? = null
632-
pubnub.awaitSubscribe(channels = listOf(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId)) {
633-
removeListenerAndUnsubscribe = chat.listenForEvents(
632+
pubnubPamServer.awaitSubscribe(channels = listOf(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId)) {
633+
removeListenerAndUnsubscribe = chatPamServer.listenForEvents(
634634
type = EventContent.Moderation::class,
635635
channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId
636636
) { event: Event<EventContent.Moderation> ->
@@ -643,9 +643,9 @@ class ChatIntegrationTest : BaseChatIntegrationTest() {
643643
}
644644
}
645645

646-
chat.setRestrictions(restrictionBan).await()
646+
chatPamServer.setRestrictions(restrictionBan).await()
647647
banned.await()
648-
chat.setRestrictions(restrictionUnban).await()
648+
chatPamServer.setRestrictions(restrictionUnban).await()
649649
unbanned.await()
650650

651651
removeListenerAndUnsubscribe?.close()

pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChatTest.kt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,22 @@ class ChatTest : BaseTest() {
134134

135135
@BeforeTest
136136
fun setUp() {
137-
pnConfiguration = createPNConfiguration(UserId(userId), subscribeKey, publishKey, authToken = null)
137+
objectUnderTest = ChatImpl(setupConfiguration(withSecretKey = false), pubnub, timerManager = timerManager)
138+
}
139+
140+
private fun setupConfiguration(withSecretKey: Boolean): ChatConfiguration {
141+
pnConfiguration = if (withSecretKey) {
142+
createPNConfiguration(UserId(userId), subscribeKey, publishKey, "secretKey", authToken = null)
143+
} else {
144+
createPNConfiguration(UserId(userId), subscribeKey, publishKey, authToken = null)
145+
}
146+
138147
every { pubnub.configuration } returns pnConfiguration
139148
chatConfig = ChatConfiguration(
140149
typingTimeout = 2000.milliseconds
141150
)
142-
objectUnderTest = ChatImpl(chatConfig, pubnub, timerManager = timerManager)
151+
152+
return chatConfig
143153
}
144154

145155
@Test
@@ -1319,6 +1329,8 @@ class ChatTest : BaseTest() {
13191329

13201330
@Test
13211331
fun shouldRemoveRestrictionWhenBanAndMuteIsFalse() {
1332+
objectUnderTest = ChatImpl(setupConfiguration(withSecretKey = true), pubnub, timerManager = timerManager)
1333+
13221334
val restrictedUserId = userId
13231335
val restrictedChannelId = channelId
13241336
val ban = false
@@ -1374,8 +1386,29 @@ class ChatTest : BaseTest() {
13741386
assertEquals(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + restrictedUserId, actualModerationEventChannelId)
13751387
}
13761388

1389+
@Test
1390+
fun shouldThrowExceptionWhenSecretKeyIsNotSet() {
1391+
val restriction = Restriction(
1392+
userId = "userId",
1393+
channelId = "channelId",
1394+
ban = true,
1395+
mute = true,
1396+
reason = "rude"
1397+
)
1398+
1399+
objectUnderTest.setRestrictions(restriction).async { result: Result<Unit> ->
1400+
assertTrue(result.isFailure)
1401+
assertEquals(
1402+
"Moderation restrictions can only be set by clients initialized with a Secret Key.",
1403+
result.exceptionOrNull()?.message
1404+
)
1405+
}
1406+
}
1407+
13771408
@Test
13781409
fun shouldAddRestrictionWhenBanIsTrue() {
1410+
objectUnderTest = ChatImpl(setupConfiguration(withSecretKey = true), pubnub, timerManager = timerManager)
1411+
13791412
val restrictedUserId = userId
13801413
val restrictedChannelId = channelId
13811414
val ban = true

pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MembershipTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class MembershipTest {
4141
mapOf("lastReadMessageTimetoken" to lastMessageTimetoken, "other_stuff" to "some string"),
4242
null,
4343
null,
44+
null,
45+
null
4446
)
4547

4648
assertEquals(lastMessageTimetoken, membership.lastReadMessageTimetoken)
@@ -56,6 +58,8 @@ class MembershipTest {
5658
mapOf(),
5759
null,
5860
null,
61+
null,
62+
null
5963
)
6064
assertNull(membership.getUnreadMessagesCount().await())
6165
}
@@ -70,6 +74,8 @@ class MembershipTest {
7074
mapOf("lastReadMessageTimetoken" to lastMessageTimetoken),
7175
null,
7276
null,
77+
null,
78+
null
7379
)
7480

7581
val messageCounts: MessageCounts = mock()

pubnub-chat-impl/src/jsMain/kotlin/MembershipJs.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class MembershipJs internal constructor(internal val membership: Membership, int
1616
val custom get() = membership.custom?.toJsMap()
1717
val updated by membership::updated
1818
val eTag by membership::eTag
19+
val status by membership::status
20+
val type by membership::type
1921

2022
val lastReadMessageTimetoken: String? get() = membership.lastReadMessageTimetoken?.toString()
2123

@@ -56,6 +58,8 @@ class MembershipJs internal constructor(internal val membership: Membership, int
5658
"custom" to custom,
5759
"updated" to updated,
5860
"eTag" to eTag,
61+
"status" to status,
62+
"type" to type,
5963
"lastReadMessageTimetoken" to lastReadMessageTimetoken
6064
)
6165
}

0 commit comments

Comments
 (0)