From df2dcb3031f47a0909a65a14867f5ec47213ce2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Kalici=C5=84ski?= Date: Fri, 20 Dec 2024 11:09:57 +0100 Subject: [PATCH] Fix setRestrictions with referential integrity enabled fix: Make setRestrictions work with `Enforce referential integrity for memberships` enabled on keyset --- .github/workflows/release/versions.json | 7 ++ .../com/pubnub/chat/internal/ChatImpl.kt | 83 ++++++++++--------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/.github/workflows/release/versions.json b/.github/workflows/release/versions.json index baa8396f..6c136ebf 100644 --- a/.github/workflows/release/versions.json +++ b/.github/workflows/release/versions.json @@ -6,6 +6,13 @@ "clearedSuffix": false } ], + "../gradle.properties": [ + { + "pattern": "^VERSION_NAME=(v?(\\d+\\.?){2,}([a-zA-Z0-9-]+(\\.?\\d+)?)?)$", + "clearedPrefix": true, + "clearedSuffix": false + } + ], "README.md": [ { "pattern": "^\\s{2,}(v?(\\d+\\.?){2,}([a-zA-Z0-9-]+(\\.?\\d+)?)?)<\\/version>$", 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 04ea75c6..15c3cf38 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 @@ -701,46 +701,53 @@ class ChatImpl( ): PNFuture { val channel: String = INTERNAL_MODERATION_PREFIX + restriction.channelId val userId = restriction.userId - - val moderationEvent: PNFuture = - if (!restriction.ban && !restriction.mute) { - pubNub.removeChannelMembers(channel = channel, uuids = listOf(userId)) - .alsoAsync { _ -> - emitEvent( - channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId, - payload = EventContent.Moderation( - channelId = channel, - restriction = RestrictionType.LIFT, - reason = restriction.reason - ), - ) - } + return createChannel(channel).catch { exception -> + if (exception.message == CHANNEL_ID_ALREADY_EXIST) { + Result.success(Unit) } else { - val custom = createCustomObject( - mapOf( - RESTRICTION_BAN to restriction.ban, - RESTRICTION_MUTE to restriction.mute, - RESTRICTION_REASON to restriction.reason - ) - ) - val uuids = listOf(PNMember.Partial(uuidId = userId, custom = custom, null)) - pubNub.setChannelMembers(channel = channel, uuids = uuids) - .alsoAsync { _ -> - emitEvent( - channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId, - payload = EventContent.Moderation( - channelId = channel, - restriction = if (restriction.ban) { - RestrictionType.BAN - } else { - RestrictionType.MUTE - }, - reason = restriction.reason - ), - ) - } + Result.failure(exception) } - return moderationEvent.then { } + }.thenAsync { + val moderationEvent: PNFuture = + if (!restriction.ban && !restriction.mute) { + pubNub.removeChannelMembers(channel = channel, uuids = listOf(userId)) + .alsoAsync { _ -> + emitEvent( + channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId, + payload = EventContent.Moderation( + channelId = channel, + restriction = RestrictionType.LIFT, + reason = restriction.reason + ), + ) + } + } else { + val custom = createCustomObject( + mapOf( + RESTRICTION_BAN to restriction.ban, + RESTRICTION_MUTE to restriction.mute, + RESTRICTION_REASON to restriction.reason + ) + ) + val uuids = listOf(PNMember.Partial(uuidId = userId, custom = custom, null)) + pubNub.setChannelMembers(channel = channel, uuids = uuids) + .alsoAsync { _ -> + emitEvent( + channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId, + payload = EventContent.Moderation( + channelId = channel, + restriction = if (restriction.ban) { + RestrictionType.BAN + } else { + RestrictionType.MUTE + }, + reason = restriction.reason + ), + ) + } + } + moderationEvent.then { } + } } override fun registerPushChannels(channels: List): PNFuture {