Skip to content

Commit

Permalink
Add "moderation." prefix for delivering Moderation events to users
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Nov 26, 2024
1 parent a2fe6fa commit 84128af
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 183 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
**/*.podspec

### IntelliJ IDEA ###
.idea/modules.xml
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dokka = "1.9.20"
kotlinx_datetime = "0.6.0"
kotlinx_coroutines = "1.8.1"
kotlinx_serialization = "1.7.1"
pubnub = "10.1.0"
pubnub = "10.2.0"

[libraries]
pubnub-kotlin-api = { module = "com.pubnub:pubnub-kotlin-api", version.ref = "pubnub" }
Expand Down
3 changes: 3 additions & 0 deletions pubnub-chat-api/api/pubnub-chat-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ public final class com/pubnub/chat/types/EmitEventMethod : java/lang/Enum {
public abstract class com/pubnub/chat/types/EventContent {
public static final field Companion Lcom/pubnub/chat/types/EventContent$Companion;
public synthetic fun <init> (ILkotlinx/serialization/internal/SerializationConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getCustomMessageType ()Ljava/lang/String;
public static final synthetic fun write$Self (Lcom/pubnub/chat/types/EventContent;Lkotlinx/serialization/encoding/CompositeEncoder;Lkotlinx/serialization/descriptors/SerialDescriptor;)V
}

Expand Down
56 changes: 0 additions & 56 deletions pubnub-chat-api/pubnub_chat_api.podspec

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class File(
* This is a sealed class with different subclasses representing specific types of events.
*/
@Serializable
sealed class EventContent {
sealed class EventContent(
@Transient val customMessageType: String? = null
) {
/**
* Represents a typing event that indicates whether a user is typing.
*
Expand Down Expand Up @@ -120,7 +122,7 @@ sealed class EventContent {
@Serializable
@SerialName("moderation")
class Moderation(val channelId: String, val restriction: RestrictionType, val reason: String? = null) :
EventContent()
EventContent("moderation")

/**
* Represents a text message event, containing the message text and any associated files.
Expand Down
56 changes: 0 additions & 56 deletions pubnub-chat-impl/pubnub_chat_impl.podspec

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,17 @@ class ChatImpl(
): PNFuture<PNPublishResult> {
val emitMethod = payload::class.getEmitMethod() ?: (payload as? EventContent.Custom)?.method
return if (emitMethod == EmitEventMethod.SIGNAL) {
pubNub.signal(channel = channelId, message = payload.encodeForSending(mergePayloadWith))
pubNub.signal(
channel = channelId,
message = payload.encodeForSending(mergePayloadWith),
customMessageType = payload.customMessageType
)
} else {
pubNub.publish(channel = channelId, message = payload.encodeForSending(mergePayloadWith))
pubNub.publish(
channel = channelId,
message = payload.encodeForSending(mergePayloadWith),
customMessageType = payload.customMessageType
)
}
}

Expand Down Expand Up @@ -699,7 +707,7 @@ class ChatImpl(
pubNub.removeChannelMembers(channel = channel, uuids = listOf(userId))
.alsoAsync { _ ->
emitEvent(
channelId = userId,
channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId,
payload = EventContent.Moderation(
channelId = channel,
restriction = RestrictionType.LIFT,
Expand All @@ -710,16 +718,16 @@ class ChatImpl(
} else {
val custom = createCustomObject(
mapOf(
"ban" to restriction.ban,
"mute" to restriction.mute,
"reason" to restriction.reason
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 = userId,
channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId,
payload = EventContent.Moderation(
channelId = channel,
restriction = if (restriction.ban) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal const val ORIGINAL_PUBLISHER = "originalPublisher"
internal const val ORIGINAL_CHANNEL_ID = "originalChannelId"
internal const val HTTP_ERROR_404 = 404
internal const val INTERNAL_MODERATION_PREFIX = "PUBNUB_INTERNAL_MODERATION_"
internal const val INTERNAL_USER_MODERATION_CHANNEL_PREFIX = "moderation."
internal const val MESSAGE_THREAD_ID_PREFIX = "PUBNUB_INTERNAL_THREAD"
internal val MINIMAL_TYPING_INDICATOR_TIMEOUT: Duration = 1.seconds
internal const val THREAD_ROOT_ID = "threadRootId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ChannelIntegrationTest : BaseChatIntegrationTest() {
@Test
fun getPinnedMessage() = runTest {
val timetoken = channel01.sendText("Text text text").await()
delayInMillis(250)
val message = channel01.getMessage(timetoken.timetoken).await()!!

val updatedChannel = channel01.pinMessage(message).await()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.pubnub.chat.User
import com.pubnub.chat.config.ChatConfiguration
import com.pubnub.chat.config.PushNotificationsConfig
import com.pubnub.chat.internal.ChatImpl
import com.pubnub.chat.internal.INTERNAL_USER_MODERATION_CHANNEL_PREFIX
import com.pubnub.chat.internal.UserImpl
import com.pubnub.chat.internal.error.PubNubErrorMessage
import com.pubnub.chat.internal.utils.cyrb53a
Expand Down Expand Up @@ -619,10 +620,10 @@ class ChatIntegrationTest : BaseChatIntegrationTest() {
val restrictionUnban = Restriction(userId = userId, channelId = channelId, ban = false, mute = false, reason = "ok")
pubnub.test(backgroundScope, checkAllEvents = false) {
var removeListenerAndUnsubscribe: AutoCloseable? = null
pubnub.awaitSubscribe(channels = listOf(userId)) {
pubnub.awaitSubscribe(channels = listOf(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId)) {
removeListenerAndUnsubscribe = chat.listenForEvents(
type = EventContent.Moderation::class,
channelId = userId
channelId = INTERNAL_USER_MODERATION_CHANNEL_PREFIX + userId
) { event: Event<EventContent.Moderation> ->
val restrictionType: RestrictionType = event.payload.restriction
if (restrictionType == RestrictionType.BAN) {
Expand Down
31 changes: 29 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 @@ -56,10 +56,12 @@ import com.pubnub.chat.config.ChatConfiguration
import com.pubnub.chat.config.PushNotificationsConfig
import com.pubnub.chat.internal.ChatImpl
import com.pubnub.chat.internal.ChatInternal
import com.pubnub.chat.internal.INTERNAL_USER_MODERATION_CHANNEL_PREFIX
import com.pubnub.chat.internal.message.MessageImpl
import com.pubnub.chat.internal.timer.TimerManager
import com.pubnub.chat.message.GetUnreadMessagesCounts
import com.pubnub.chat.restrictions.Restriction
import com.pubnub.chat.restrictions.RestrictionType
import com.pubnub.chat.types.ChannelType
import com.pubnub.chat.types.EventContent
import com.pubnub.chat.types.GetEventsHistoryResult
Expand Down Expand Up @@ -650,6 +652,31 @@ class ChatTest : BaseTest() {
assertEquals(forwardedChannelId, actualForwardedChannelId)
}

@Test
fun shouldPassCustomMessageTypeOfEventContentToPublish() {
every { pubnub.publish(any(), any(), any(), any(), any(), any(), any(), any()) } returns publishEndpoint
every { publishEndpoint.async(any()) } calls { (callback1: Consumer<Result<PNPublishResult>>) ->
callback1.accept(Result.success(PNPublishResult(timetoken)))
}
val payload = EventContent.Moderation("a", RestrictionType.BAN, null)

objectUnderTest.emitEvent(
channelId = channelId,
payload = payload,
).async { result ->
assertTrue(result.isSuccess)
assertEquals(timetoken, result.getOrNull()?.timetoken)
}

verify {
pubnub.publish(
channel = channelId,
message = mapOf("type" to "moderation", "channelId" to "a", "restriction" to "banned", "reason" to null),
customMessageType = payload.customMessageType
)
}
}

@Test
fun shouldCalSignalWhenEmitEventWithMethodSignal() {
every { pubnub.signal(any(), any()) } returns signalEndpoint
Expand Down Expand Up @@ -1350,7 +1377,7 @@ class ChatTest : BaseTest() {
val actualModerationEventChannelId = userIdSlot.get()
assertEquals(restrictedUserId, actualRestrictedUserId)
assertEquals("PUBNUB_INTERNAL_MODERATION_$restrictedChannelId", actualRestrictedChannelId)
assertEquals(restrictedUserId, actualModerationEventChannelId)
assertEquals(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + restrictedUserId, actualModerationEventChannelId)
}

@Test
Expand Down Expand Up @@ -1409,7 +1436,7 @@ class ChatTest : BaseTest() {
assertEquals(reason, actualRestriction.get("reason"))
assertEquals("banned", actualEncodedMessageSlot["restriction"])
assertEquals("PUBNUB_INTERNAL_MODERATION_$restrictedChannelId", actualRestrictedChannelId)
assertEquals(restrictedUserId, actualModerationEventChannelId)
assertEquals(INTERNAL_USER_MODERATION_CHANNEL_PREFIX + restrictedUserId, actualModerationEventChannelId)
}

private fun createMessage(chId: String = channelId, uId: String = userId): Message {
Expand Down
2 changes: 1 addition & 1 deletion pubnub-kotlin
56 changes: 0 additions & 56 deletions pubnub_chat.podspec

This file was deleted.

0 comments on commit 84128af

Please sign in to comment.