Skip to content

Commit

Permalink
Fix setRestrictions with referential integrity enabled
Browse files Browse the repository at this point in the history
fix: Make setRestrictions work with `Enforce referential integrity for memberships` enabled on keyset
  • Loading branch information
wkal-pubnub committed Dec 20, 2024
1 parent bbef18b commit df2dcb3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/release/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,}<version>(v?(\\d+\\.?){2,}([a-zA-Z0-9-]+(\\.?\\d+)?)?)<\\/version>$",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,46 +701,53 @@ class ChatImpl(
): PNFuture<Unit> {
val channel: String = INTERNAL_MODERATION_PREFIX + restriction.channelId
val userId = restriction.userId

val moderationEvent: PNFuture<PNMemberArrayResult> =
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<PNMemberArrayResult> =
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<String>): PNFuture<PNPushAddChannelResult> {
Expand Down

0 comments on commit df2dcb3

Please sign in to comment.