Skip to content

Commit

Permalink
feat(ongoing): add raid and pinned messages event parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
outadoc committed Apr 6, 2023
1 parent dcfe0c5 commit 81c19b2
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 8 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/fr/outadoc/justchatting/di/ChatModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import fr.outadoc.justchatting.component.twitch.websocket.irc.recent.RecentMessa
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.client.PubSubWebSocket
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.broadcastsettingsupdate.PubSubBroadcastSettingsPlugin
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.channelpoints.PubSubChannelPointsPlugin
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.pinnedmessage.PubSubPinnedMessagePlugin
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.poll.PubSubPollPlugin
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.prediction.PubSubPredictionPlugin
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.raid.PubSubRaidPlugin
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.richembed.PubSubRichEmbedPlugin
import fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.viewercount.PubSubViewerCountPlugin
import fr.outadoc.justchatting.feature.chat.data.emotes.ChannelBttvEmotesSource
Expand Down Expand Up @@ -67,12 +69,14 @@ val chatModule = module {
}
}

single { PubSubBroadcastSettingsPlugin(get()) }
single { PubSubChannelPointsPlugin(get(), get(), get()) }
single { PubSubPinnedMessagePlugin(get()) }
single { PubSubPollPlugin(get()) }
single { PubSubPredictionPlugin(get()) }
single { PubSubBroadcastSettingsPlugin(get()) }
single { PubSubViewerCountPlugin(get()) }
single { PubSubRaidPlugin(get()) }
single { PubSubRichEmbedPlugin(get()) }
single { PubSubViewerCountPlugin(get()) }

single {
PubSubPluginsProvider {
Expand All @@ -83,6 +87,7 @@ val chatModule = module {
get<PubSubBroadcastSettingsPlugin>(),
get<PubSubViewerCountPlugin>(),
get<PubSubRichEmbedPlugin>(),
get<PubSubRaidPlugin>(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ sealed interface ChatEvent {
val prediction: Prediction,
) : ChatEvent

@Immutable
data class RaidUpdate(
val raid: Raid,
) : ChatEvent

@Immutable
data class PinnedMessageUpdate(
val pinnedMessage: PinnedMessage?,
) : ChatEvent

@Immutable
data class RichEmbed(
val messageId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.outadoc.justchatting.component.chatapi.common

import androidx.compose.runtime.Immutable
import kotlinx.datetime.Instant

@Immutable
data class PinnedMessage(
val pinId: String,
val pinnedBy: User,
val message: Message,
) {
@Immutable
data class User(
val userId: String,
val displayName: String,
)

@Immutable
data class Message(
val messageId: String,
val sender: User,
val content: Content,
val startsAt: Instant,
val endsAt: Instant,
) {
@Immutable
data class Content(
val text: String,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.outadoc.justchatting.component.chatapi.common

import androidx.compose.runtime.Immutable

@Immutable
data class Raid(
val targetId: String,
val targetLogin: String,
val targetDisplayName: String,
val targetProfileImageUrl: String?,
val transitionJitterSeconds: Int,
val forceRaidNowSeconds: Int,
val viewerCount: Int,
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.pinnedmessage

import fr.outadoc.justchatting.utils.core.InstantUnixEpochSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
internal sealed class PubSubPinnedMessage {

@Serializable
@SerialName("pin-message")
data class Pin(
@SerialName("data")
val data: Data,
) : PubSubPinnedMessage() {

@Serializable
data class Data(
@SerialName("id")
val pinId: String,
@SerialName("pinned_by")
val pinnedBy: User,
@SerialName("message")
val message: Message,
)

@Serializable
data class User(
@SerialName("id")
val userId: String,
@SerialName("display_name")
val displayName: String,
)

@Serializable
data class Message(
@SerialName("id")
val messageId: String,
@SerialName("sender")
val sender: User,
@SerialName("content")
val content: Content,
@SerialName("starts_at")
@Serializable(with = InstantUnixEpochSerializer::class)
val startsAt: Instant,
@SerialName("ends_at")
@Serializable(with = InstantUnixEpochSerializer::class)
val endsAt: Instant,
) {
@Serializable
data class Content(
@SerialName("text")
val text: String,
)
}
}

@Serializable
@SerialName("update-message")
object Update : PubSubPinnedMessage()

@Serializable
@SerialName("unpin-message")
object Unpin : PubSubPinnedMessage()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.pinnedmessage

import fr.outadoc.justchatting.component.chatapi.common.PinnedMessage

internal fun PubSubPinnedMessage.Pin.map(): PinnedMessage {
return PinnedMessage(
pinId = data.pinId,
pinnedBy = data.pinnedBy.map(),
message = data.message.map(),
)
}

private fun PubSubPinnedMessage.Pin.Message.map(): PinnedMessage.Message {
return PinnedMessage.Message(
messageId = messageId,
sender = sender.map(),
content = content.map(),
startsAt = startsAt,
endsAt = endsAt,
)
}

private fun PubSubPinnedMessage.Pin.Message.Content.map(): PinnedMessage.Message.Content {
return PinnedMessage.Message.Content(
text = text,
)
}

private fun PubSubPinnedMessage.Pin.User.map(): PinnedMessage.User {
return PinnedMessage.User(
userId = userId,
displayName = displayName,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.pinnedmessage

import fr.outadoc.justchatting.component.chatapi.common.ChatEvent
import fr.outadoc.justchatting.component.chatapi.common.pubsub.PubSubPlugin
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

class PubSubPinnedMessagePlugin(
private val json: Json,
) : PubSubPlugin<PubSubPinnedMessage> {

override fun getTopic(channelId: String): String =
"pinned-chat-updates-v1.$channelId"

override fun parseMessage(payload: String): List<ChatEvent> =
when (val message = json.decodeFromString<PubSubPinnedMessage>(payload)) {
is PubSubPinnedMessage.Pin -> {
listOf(
ChatEvent.PinnedMessageUpdate(
pinnedMessage = message.map(),
),
)
}

is PubSubPinnedMessage.Update -> emptyList()

is PubSubPinnedMessage.Unpin -> listOf(
ChatEvent.PinnedMessageUpdate(
pinnedMessage = null,
),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.raid

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
internal sealed class PubSubRaidMessage {

@Serializable
@SerialName("raid_update_v2")
data class Update(
@SerialName("raid")
val raid: Raid,
) : PubSubRaidMessage()

@Serializable
@SerialName("raid_go_v2")
data class Go(
@SerialName("raid")
val raid: Raid,
) : PubSubRaidMessage()

@Serializable
data class Raid(
@SerialName("id")
val raidId: String,
@SerialName("target_id")
val targetId: String,
@SerialName("target_login")
val targetLogin: String,
@SerialName("target_display_name")
val targetDisplayName: String,
@SerialName("target_profile_image")
val targetProfileImageUrl: String?,
@SerialName("transition_jitter_seconds")
val transitionJitterSeconds: Int,
@SerialName("force_raid_now_seconds")
val forceRaidNowSeconds: Int,
@SerialName("viewer_count")
val viewerCount: Int,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.raid

import fr.outadoc.justchatting.component.chatapi.common.Raid

internal fun PubSubRaidMessage.Raid.map(): Raid {
return Raid(
targetId = targetId,
targetLogin = targetLogin,
targetDisplayName = targetDisplayName,
targetProfileImageUrl = targetProfileImageUrl,
transitionJitterSeconds = transitionJitterSeconds,
forceRaidNowSeconds = forceRaidNowSeconds,
viewerCount = viewerCount,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fr.outadoc.justchatting.component.twitch.websocket.pubsub.feature.raid

import fr.outadoc.justchatting.component.chatapi.common.ChatEvent
import fr.outadoc.justchatting.component.chatapi.common.pubsub.PubSubPlugin
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

class PubSubRaidPlugin(
private val json: Json,
) : PubSubPlugin<PubSubRaidMessage> {

override fun getTopic(channelId: String): String =
"raid.$channelId"

override fun parseMessage(payload: String): List<ChatEvent> =
when (val message = json.decodeFromString<PubSubRaidMessage>(payload)) {
is PubSubRaidMessage.Update -> {
listOf(
ChatEvent.RaidUpdate(
raid = message.raid.map(),
),
)
}

is PubSubRaidMessage.Go -> {
listOf(
ChatEvent.RaidUpdate(
raid = message.raid.map(),
),
)
}
}
}
Loading

0 comments on commit 81c19b2

Please sign in to comment.