Skip to content

Commit

Permalink
Partial updates for membership information
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Aug 8, 2024
1 parent f5d37b8 commit e47ee8d
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.pubnub.internal.models.consumer.objects.member

import com.pubnub.api.models.consumer.objects.uuid.PNUUIDMetadata
import com.pubnub.api.utils.PatchValue

data class PNMember(
val uuid: PNUUIDMetadata?,
val custom: Map<String, Any>? = null,
val uuid: PNUUIDMetadata,
val custom: PatchValue<Map<String, Any>?>? = null,
val updated: String,
val eTag: String,
val status: String?,
val status: PatchValue<String?>? = null,
) {
data class Partial(
val uuidId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.pubnub.internal.models.consumer.objects.membership

import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata
import com.pubnub.api.utils.PatchValue

data class PNChannelMembership(
val channel: PNChannelMetadata,
val custom: Map<String, Any?>?,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: String?,
val status: PatchValue<String?>? = null,
) {
data class Partial(
val channelId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.pubnub.api.models.consumer.objects.uuid.PNUUIDMetadata

data class PNUUIDMetadataResult(
val status: Int,
val data: PNUUIDMetadata?,
val data: PNUUIDMetadata,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata
import com.pubnub.api.models.consumer.objects.uuid.PNUUIDMetadata
import com.pubnub.api.models.consumer.pubsub.BasePubSubResult
import com.pubnub.api.models.consumer.pubsub.PubSubResult
import com.pubnub.api.utils.PatchValue
import com.pubnub.internal.utils.PolymorphicDeserializer
import com.pubnub.internal.utils.UnwrapSingleField

Expand Down Expand Up @@ -95,10 +96,10 @@ data class PNSetMembershipEvent(
val channel: String,
@JsonAdapter(UnwrapSingleField::class)
val uuid: String,
val custom: Map<String, Any?>?,
val custom: PatchValue<Map<String, Any?>?>?,
val eTag: String,
val updated: String,
val status: String?,
val status: PatchValue<String?>?,
)

data class PNDeleteMembershipEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class WhenSteps(
val channels =
membershipState.memberships.map {
PNChannelMembership.Partial(
channelId = it.channel!!.id,
custom = it.custom,
status = it.status,
channelId = it.channel.id,
custom = it.custom?.value,
status = it.status?.value,
)
}

Expand All @@ -63,9 +63,9 @@ class WhenSteps(
val channels =
membershipState.memberships.map {
PNChannelMembership.Partial(
channelId = it.channel!!.id,
custom = it.custom,
status = it.status,
channelId = it.channel.id,
custom = it.custom?.value,
status = it.status?.value,
)
}

Expand All @@ -79,7 +79,7 @@ class WhenSteps(

@When("I remove the membership")
fun i_remove_the_membership() {
val channels = membershipState.memberships.map { it.channel!!.id }
val channels = membershipState.memberships.map { it.channel.id }
world.pubnub.pubNubCore.removeMemberships(
uuid = membershipState.uuid(),
channels = channels,
Expand All @@ -91,7 +91,7 @@ class WhenSteps(

@When("I remove the membership for current user")
fun i_remove_the_membership_for_current_user() {
val channels = membershipState.memberships.map { it.channel!!.id }
val channels = membershipState.memberships.map { it.channel.id }
world.pubnub.pubNubCore.removeMemberships(
channels = channels,
).sync().let {
Expand All @@ -105,12 +105,12 @@ class WhenSteps(
val channelsToSet =
membershipState.memberships.map {
PNChannelMembership.Partial(
channelId = it.channel!!.id,
custom = it.custom,
status = it.status,
channelId = it.channel.id,
custom = it.custom?.value,
status = it.status?.value,
)
}
val channelsToRemove = membershipState.membershipsToRemove.map { it.channel!!.id }
val channelsToRemove = membershipState.membershipsToRemove.map { it.channel.id }
world.pubnub.pubNubCore.manageMemberships(
channelsToSet = channelsToSet,
channelsToRemove = channelsToRemove,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public static PNMembers from(@Nullable PNMember member) {
}
return new PNMembers()
.setUuid(PNUUIDMetadata.from(member.getUuid()))
.setCustom(member.getCustom())
.setCustom(member.getCustom() != null ? member.getCustom().getValue() : null)
.setUpdated(member.getUpdated())
.setETag(member.getETag())
.setStatus(member.getStatus());
.setStatus(member.getStatus() != null ? member.getStatus().getValue() : null);
}

public static List<PNMembers> from(Collection<PNMember> members) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public static PNMembership from(@Nullable PNChannelMembership data) {
}
PNChannelMetadata metadata = PNChannelMetadata.from(data.getChannel());
PNMembership newData = new PNMembership(metadata);
newData.setCustom(data.getCustom());
newData.setCustom(data.getCustom() != null ? data.getCustom().getValue() : null);
// newData.setUuid(data.get) //TODO where to get this? does it even exist in server responses?
newData.setUpdated(data.getUpdated());
newData.setETag(data.getETag());
newData.setStatus(data.getStatus());
newData.setStatus(data.getStatus() != null ? data.getStatus().getValue() : null);
return newData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ static PNMembershipResult getSetMembershipResult(@NotNull PNObjectEventResult ob
new PNChannelMetadata(data.getChannel(), null)
);
membership.setUuid(data.getUuid());
membership.setCustom(data.getCustom());
membership.setStatus(data.getStatus());
membership.setCustom(data.getCustom() != null ? data.getCustom().getValue() : null);
membership.setStatus(data.getStatus() != null ? data.getStatus().getValue() : null);
membership.setUpdated(data.getUpdated());
membership.setETag(data.getETag());
return new PNMembershipResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ internal class DelegatingEventListenerTest {

@Test
fun getSetMembershipResult() {
val metadata = PNSetMembershipEvent(channel, uuid, custom, eTag, updated, status)
val metadata = PNSetMembershipEvent(channel, uuid, PatchValue.of(custom), eTag, updated, PatchValue.of(status))
val message = PNSetMembershipEventMessage(source, version, event, type, metadata)
val objectEvent = PNObjectEventResult(BasePubSubResult(channel, subscription, timetoken, userMetadata, publisher), message)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.pubnub.api.models.consumer.objects.member

import com.pubnub.api.models.consumer.objects.uuid.PNUUIDMetadata
import com.pubnub.api.utils.PatchValue
import com.pubnub.kmp.CustomObject

data class PNMember(
val uuid: PNUUIDMetadata?,
val custom: Map<String, Any?>? = null,
val uuid: PNUUIDMetadata,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: String?,
val status: PatchValue<String?>?,
) {
data class Partial(
val uuidId: String,
Expand All @@ -17,4 +18,15 @@ data class PNMember(
) : MemberInput {
override val uuid: String = uuidId
}

// let's not make this public for now, but keep the implementation around in case it's needed
private operator fun plus(update: PNMember): PNMember {
return PNMember(
uuid + update.uuid,
update.custom ?: custom,
update.updated,
update.eTag,
update.status ?: status
)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.pubnub.api.models.consumer.objects.membership

import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata
import com.pubnub.api.models.consumer.pubsub.objects.PNSetMembershipEvent
import com.pubnub.api.utils.PatchValue
import com.pubnub.kmp.CustomObject

data class PNChannelMembership(
val channel: PNChannelMetadata,
val custom: Map<String, Any?>?,
val custom: PatchValue<Map<String, Any?>?>? = null,
val updated: String,
val eTag: String,
val status: String?,
val status: PatchValue<String?>? = null,
) {
data class Partial(
val channelId: String,
Expand All @@ -17,4 +19,28 @@ data class PNChannelMembership(
) : ChannelMembershipInput {
override val channel: String = channelId
}

// let's not make this public for now, but keep the implementation around in case it's needed
private operator fun plus(update: PNChannelMembership): PNChannelMembership {
return PNChannelMembership(
channel + update.channel,
update.custom ?: custom,
update.updated,
update.eTag,
update.status ?: status
)
}

/**
* Merge information from this `PNChannelMembership` with new data from `update`, returning a new `PNChannelMembership` instance.
*/
operator fun plus(update: PNSetMembershipEvent): PNChannelMembership {
return PNChannelMembership(
channel,
update.custom ?: custom,
update.updated,
update.eTag,
update.status ?: status
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.pubnub.api.models.consumer.objects.uuid

data class PNUUIDMetadataResult(
val status: Int,
val data: PNUUIDMetadata?,
val data: PNUUIDMetadata,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata
import com.pubnub.api.models.consumer.objects.uuid.PNUUIDMetadata
import com.pubnub.api.models.consumer.pubsub.BasePubSubResult
import com.pubnub.api.models.consumer.pubsub.PubSubResult
import com.pubnub.api.utils.PatchValue

data class PNObjectEventResult(
private val result: BasePubSubResult,
Expand Down Expand Up @@ -68,10 +69,10 @@ data class PNDeleteUUIDMetadataEventMessage(
data class PNSetMembershipEvent(
val channel: String,
val uuid: String,
val custom: Map<String, Any?>?,
val custom: PatchValue<Map<String, Any?>?>?,
val eTag: String,
val updated: String,
val status: String?,
val status: PatchValue<String?>?,
)

data class PNDeleteMembershipEvent(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.pubnub.api.models.consumer.objects.membership

import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata
import com.pubnub.api.models.consumer.pubsub.objects.PNSetMembershipEvent
import com.pubnub.api.utils.PatchValue
import com.pubnub.test.randomString
import kotlin.test.Test
import kotlin.test.assertEquals

class PNChannelMembershipTest {
@Test
fun plus() {
val originalMembership = PNChannelMembership(
PNChannelMetadata(randomString()),
custom = PatchValue.of(mapOf(randomString() to randomString())),
updated = randomString(),
eTag = randomString(),
status = null
)

val updateMembership = PNSetMembershipEvent(
originalMembership.channel.id,
randomString(),
custom = null,
updated = randomString(),
eTag = randomString(),
status = PatchValue.of(randomString())
)
val expectedMembership = originalMembership.copy(
updated = updateMembership.updated,
eTag = updateMembership.eTag,
status = updateMembership.status
)

val actualMembership = originalMembership + updateMembership

assertEquals(expectedMembership, actualMembership)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MembersTest : BaseIntegrationTest() {
).await()

// then
val pnChannelDetails = result.data.single { it.uuid?.id == pubnub.configuration.userId.value }
assertEquals(customData, pnChannelDetails.custom)
val pnChannelDetails = result.data.single { it.uuid.id == pubnub.configuration.userId.value }
assertEquals(customData, pnChannelDetails.custom?.value)
}

@Test
Expand All @@ -50,8 +50,8 @@ class MembersTest : BaseIntegrationTest() {
).await()

// then
val pnChannelDetails = result.data.single { it.uuid?.id == pubnub.configuration.userId.value }
assertEquals(status, pnChannelDetails.status)
val pnChannelDetails = result.data.single { it.uuid.id == pubnub.configuration.userId.value }
assertEquals(status, pnChannelDetails.status?.value)
}

@Test
Expand All @@ -71,7 +71,7 @@ class MembersTest : BaseIntegrationTest() {
yield()
val result = pubnub.getMemberships(pubnub.configuration.userId.value, page = next).await()
next = result.next
assertFalse { result.data.any { it.channel?.id == channel } }
assertFalse { result.data.any { it.channel.id == channel } }
if (next == null || result.data.isEmpty()) {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class MembershipsTest : BaseIntegrationTest() {
).await()

// then
val pnChannelDetails = result.data.single { it.channel?.id == channel }
assertEquals(channel, pnChannelDetails.channel?.id)
assertEquals(customData, pnChannelDetails.custom)
val pnChannelDetails = result.data.single { it.channel.id == channel }
assertEquals(channel, pnChannelDetails.channel.id)
assertEquals(customData, pnChannelDetails.custom?.value)
}

@Test
Expand All @@ -60,7 +60,7 @@ class MembershipsTest : BaseIntegrationTest() {
message as PNSetMembershipEventMessage
assertEquals(channel, message.data.channel)
assertEquals(pubnub.configuration.userId.value, message.data.uuid)
assertEquals(customData, message.data.custom)
assertEquals(customData, message.data.custom?.value)
}
}

Expand All @@ -79,7 +79,7 @@ class MembershipsTest : BaseIntegrationTest() {
while (true) {
val result = pubnub.getChannelMembers(channel, page = next).await()
next = result.next
assertFalse { result.data.any { it.uuid?.id == pubnub.configuration.userId.value } }
assertFalse { result.data.any { it.uuid.id == pubnub.configuration.userId.value } }
if (next == null || result.data.isEmpty()) {
break
}
Expand Down
Loading

0 comments on commit e47ee8d

Please sign in to comment.