Skip to content
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.typetype.android.data.livechat

import dev.typetype.android.domain.livechat.ChatEmoteRange
import dev.typetype.android.data.network.dto.TwitchChatEventDto
import dev.typetype.android.domain.livechat.LiveChatEvent
import dev.typetype.android.domain.livechat.LiveChatMessage
Expand Down Expand Up @@ -32,6 +33,11 @@ internal class TwitchChatSseParser(private val json: Json) {
authorName = event.displayName?.ifBlank { null } ?: event.login,
authorAvatarUrl = null,
moderator = event.badges.any { it.startsWith("moderator/") || it.startsWith("broadcaster/") },
login = event.login,
color = event.color,
emotes = event.emotes.map { ChatEmoteRange(it.id, it.start, it.end) },
badges = event.badges,
action = event.action,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ data class TwitchVideoCommentDto(
val offset: Long = 0,
val login: String? = null,
val displayName: String? = null,
val color: String? = null,
val text: String = "",
val emotes: List<TwitchChatEmoteDto> = emptyList(),
val badges: List<String> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.typetype.android.data.twitch

import dev.typetype.android.domain.twitch.TwitchChannel
import dev.typetype.android.core.error.suspendRunCatching
import dev.typetype.android.data.account.ActiveAccountScope
import dev.typetype.android.data.network.TypeTypeApi
Expand Down Expand Up @@ -54,6 +55,12 @@ class RemoteTwitchAccountRepository @Inject constructor(
override suspend fun followedLives(cursor: String?): Result<TwitchPage<Video>> = call { twitchFollowedLives(cursor) }
.map { page -> TwitchPage(page.items.map { it.toVideo() }, page.cursor?.takeIf { page.hasMore && it.isNotBlank() }) }

override suspend fun followedVideos(cursor: String?): Result<TwitchPage<Video>> = call { twitchFollowedVideos(cursor) }
.map { page -> TwitchPage(page.items.map { it.toVideo() }, page.cursor?.takeIf { page.hasMore && it.isNotBlank() }) }

override suspend fun followedChannels(cursor: String?): Result<TwitchPage<TwitchChannel>> = call { twitchFollows(cursor) }
.map { page -> TwitchPage(page.items.map { it.toChannel() }, page.cursor?.takeIf { page.hasMore && it.isNotBlank() }) }

override suspend fun isFollowing(login: String): Result<Boolean> = call { twitchFollowState(login) }.map { it.following }

override suspend fun setFollowing(login: String, follow: Boolean): Result<Unit> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package dev.typetype.android.data.twitch

import dev.typetype.android.data.network.dto.TwitchChatAssetsDto
import dev.typetype.android.domain.twitch.TwitchChatAssets
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import dev.typetype.android.core.error.suspendRunCatching
import dev.typetype.android.data.account.ActiveAccountScope
import dev.typetype.android.data.network.TypeTypeApi
Expand Down Expand Up @@ -30,6 +34,9 @@ class RemoteTwitchRepository @Inject constructor(
private val activeAccountScope: ActiveAccountScope,
private val serverRepository: ServerRepository,
) : TwitchRepository {
@Volatile
private var globalChatAssets: TwitchChatAssetsDto? = null

override suspend fun popular(cursor: String?): Result<TwitchPage<Video>> =
call { twitchStreams(cursor) }.map { page -> page.map { it.toVideo() } }

Expand Down Expand Up @@ -101,6 +108,15 @@ class RemoteTwitchRepository @Inject constructor(
override suspend fun chatHighlights(login: String): Result<TwitchChatHighlights> =
call { twitchChatHighlights(login) }.map { it.toDomain() }

// The global set is the same for every channel and large: it is fetched once per app run.
override suspend fun chatAssets(login: String): Result<TwitchChatAssets> = suspendRunCatching {
coroutineScope {
val global = async { globalChatAssets ?: call { twitchChatAssets() }.getOrNull()?.also { globalChatAssets = it } }
val channel = async { call { twitchChannelChatAssets(segment(login)) }.getOrNull() }
mergeChatAssets(channel.await(), global.await())
}
}

override suspend fun videoComments(id: String, offsetSeconds: Long?, cursor: String?): Result<TwitchPage<TwitchVideoComment>> =
call { twitchVideoComments(segment(id), offset = offsetSeconds.takeIf { cursor == null }, cursor = cursor) }
.map { page -> page.map { it.toDomain() } }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package dev.typetype.android.data.twitch

import dev.typetype.android.domain.livechat.ChatEmoteRange
import dev.typetype.android.data.network.dto.TwitchChatAssetsDto
import dev.typetype.android.domain.livechat.ChatEmote
import dev.typetype.android.domain.livechat.ChatImage
import dev.typetype.android.domain.twitch.TwitchChatAssets
import dev.typetype.android.domain.twitch.TwitchLiveInfo
import dev.typetype.android.data.network.dto.TwitchChannelDto
import dev.typetype.android.data.network.dto.TwitchChatHighlightsDto
import dev.typetype.android.data.network.dto.TwitchClipDto
Expand Down Expand Up @@ -64,8 +70,24 @@ internal fun TwitchChannelDto.toChannel(): TwitchChannel = TwitchChannel(
bannerUrl = bannerUrl?.ifBlank { null },
followers = followers,
live = live?.toVideo(),
liveInfo = live?.let {
TwitchLiveInfo(it.title, it.viewers, twitchMillis(it.startedAt), it.game?.name?.ifBlank { null }, it.tags)
},
)

internal fun mergeChatAssets(channel: TwitchChatAssetsDto?, global: TwitchChatAssetsDto?): TwitchChatAssets {
val emotes = LinkedHashMap<String, ChatEmote>()
for (emote in channel?.emotes.orEmpty() + global?.emotes.orEmpty()) {
if (emote.name.isBlank() || emote.url.isBlank()) continue
emotes.putIfAbsent(emote.name, ChatEmote(emote.name, emote.url2x ?: emote.url, emote.zeroWidth))
}
val badges = HashMap<String, ChatImage>()
for ((id, badge) in global?.badges.orEmpty() + channel?.badges.orEmpty()) {
if (badge.url.isNotBlank()) badges[id] = ChatImage(badge.title, badge.url2x ?: badge.url)
}
return TwitchChatAssets(emotes, badges)
}

internal fun TwitchGameDto.toCategory(): TwitchCategory =
TwitchCategory(id = id, name = name, slug = slug, boxArtUrl = boxArtUrl?.ifBlank { null }, viewers = viewers)

Expand Down Expand Up @@ -145,4 +167,8 @@ internal fun TwitchVideoCommentDto.toDomain() = TwitchVideoComment(
author = displayName?.ifBlank { null } ?: login,
text = text,
moderator = badges.any { it.startsWith("moderator/") || it.startsWith("broadcaster/") },
login = login?.ifBlank { null },
color = color,
emotes = emotes.map { ChatEmoteRange(it.id, it.start, it.end) },
badges = badges,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.typetype.android.data.stream

import dev.typetype.android.domain.twitch.TwitchChatAssets
import dev.typetype.android.domain.feed.Video
import dev.typetype.android.domain.stream.Chapter
import dev.typetype.android.domain.twitch.TwitchCategory
Expand Down Expand Up @@ -32,4 +33,5 @@ internal class FakeTwitchRepository(
override suspend fun videoComments(id: String, offsetSeconds: Long?, cursor: String?): Result<TwitchPage<TwitchVideoComment>> =
Result.success(TwitchPage(emptyList(), null))
override suspend fun chatHighlights(login: String): Result<TwitchChatHighlights> = Result.success(TwitchChatHighlights(null, null))
override suspend fun chatAssets(login: String): Result<TwitchChatAssets> = Result.success(TwitchChatAssets())
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ private fun needsImageProxy(source: String): Boolean {
return host.endsWith("ggpht.com") ||
host.endsWith("googleusercontent.com") ||
host.endsWith("hdslb.com") ||
host.endsWith("ytimg.com")
host.endsWith("ytimg.com") ||
host == "static-cdn.jtvnw.net" ||
host in TWITCH_CHAT_IMAGE_HOSTS
}

// The web proxies the same Twitch hosts: avatars, previews and emotes, and the BTTV, FFZ and 7TV emote CDNs.
private val TWITCH_CHAT_IMAGE_HOSTS = setOf("cdn.7tv.app", "cdn.betterttv.net", "cdn.frankerfacez.com")

// Bilibili covers and avatars are several hundred kilobytes at full size; its image service scales
// them on request, to the sizes the web client asks for.
private fun sizedBilibiliImage(source: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ data class LiveChatMessage(
val authorName: String?,
val authorAvatarUrl: String?,
val moderator: Boolean,
val login: String? = null,
val color: String? = null,
val emotes: List<ChatEmoteRange> = emptyList(),
val badges: List<String> = emptyList(),
val action: Boolean = false,
)

sealed interface LiveChatEvent {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.typetype.android.domain.livechat

import kotlin.math.abs
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.roundToInt

// The web chat's rule: a viewer without a color gets one from their login, and a color too dark for a dark theme (or
// too light for a light one) is mixed toward white (or black) until it reads. Returns an opaque ARGB value.
object TwitchChatColor {
private val DEFAULTS = listOf(
"#FF0000", "#0000FF", "#008000", "#B22222", "#FF7F50", "#9ACD32", "#FF4500", "#2E8B57",
"#DAA520", "#D2691E", "#5F9EA0", "#1E90FF", "#FF69B4", "#8A2BE2", "#00FF7F",
)

private val FALLBACK = intArrayOf(0x91, 0x46, 0xFF)

fun name(color: String?, login: String, dark: Boolean): Int {
val rgb = rgb(color?.takeIf { it.isNotBlank() } ?: DEFAULTS[hash(login)]) ?: FALLBACK
val lum = luminance(rgb)
val mixed = when {
dark && lum < 0.18 -> mix(rgb, 255, min(0.75, (0.18 - lum) * 4 + 0.25))
!dark && lum > 0.45 -> mix(rgb, 0, min(0.6, (lum - 0.45) * 2 + 0.2))
else -> rgb
}
return (0xFF shl 24) or (mixed[0] shl 16) or (mixed[1] shl 8) or mixed[2]
}

private fun hash(text: String): Int {
var value = 0
codePoints(text).forEach { value = value * 31 + it }
return abs(value % DEFAULTS.size)
}

private fun rgb(hex: String): IntArray? {
val digits = hex.removePrefix("#")
if (digits.length != 6) return null
val n = digits.toIntOrNull(16) ?: return null
return intArrayOf((n shr 16) and 255, (n shr 8) and 255, n and 255)
}

private fun luminance(rgb: IntArray): Double {
fun channel(v: Int): Double {
val c = v / 255.0
return if (c <= 0.03928) c / 12.92 else ((c + 0.055) / 1.055).pow(2.4)
}
return 0.2126 * channel(rgb[0]) + 0.7152 * channel(rgb[1]) + 0.0722 * channel(rgb[2])
}

private fun mix(rgb: IntArray, target: Int, amount: Double): IntArray =
IntArray(3) { (rgb[it] + (target - rgb[it]) * amount).roundToInt() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package dev.typetype.android.domain.livechat

data class ChatEmoteRange(val id: String, val start: Int, val end: Int)

data class ChatImage(val name: String, val url: String)

data class ChatEmote(val name: String, val url: String, val zeroWidth: Boolean = false)

sealed interface ChatPart {
data class Text(val text: String) : ChatPart
data class Emote(val image: ChatImage, val overlays: List<ChatImage> = emptyList()) : ChatPart
data class Mention(val text: String, val self: Boolean) : ChatPart
data class Link(val text: String, val href: String) : ChatPart
}

// Same split as the web chat: Twitch's own emotes come as code point ranges, BTTV/FFZ/7TV ones as whole words, and a
// zero-width emote sits on top of the emote before it.
object TwitchChatParts {
private const val TWITCH_EMOTE_BASE = "https://static-cdn.jtvnw.net/emoticons/v2/"
private val LINK = Regex("""^(?:https?://)?(?:[a-z0-9-]+\.)+[a-z]{2,}(?::\d{2,5})?(?:[/?#]\S*)?$""", RegexOption.IGNORE_CASE)
private val TRAILING = Regex("""[),.!?:;'"\]]+$""")
private val SPACES = Regex("""(\s+)""")
private val BARE_TLDS = (
"com net org info biz xyz io co app dev me tv gg ly to sh so ai fm am " +
"video live stream games game fun art blog club online site store shop tech link page news wiki social media " +
"be fr de uk es it nl eu ru jp cn kr br ca us au ch at se no dk fi pl pt cz ie in mx ar"
).split(" ").toSet()

fun twitchEmote(id: String, name: String): ChatImage =
ChatImage(name, "$TWITCH_EMOTE_BASE$id/default/dark/2.0")

fun parts(
text: String,
native: List<ChatEmoteRange> = emptyList(),
emotes: Map<String, ChatEmote> = emptyMap(),
selfLogin: String = "",
): List<ChatPart> {
val points = codePoints(text)
fun slice(from: Int, to: Int) = String(points, from, to - from)
val ranges = native.filter { it.start >= 0 && it.end < points.size && it.start <= it.end }.sortedBy { it.start }
val builder = Builder(emotes, selfLogin.lowercase())
var cursor = 0
for (range in ranges) {
if (range.start < cursor) continue
builder.segment(slice(cursor, range.start))
builder.parts += ChatPart.Emote(twitchEmote(range.id, slice(range.start, range.end + 1)))
cursor = range.end + 1
}
builder.segment(slice(cursor, points.size))
return builder.parts
}

fun linkHref(word: String): String? {
if (!LINK.matches(word)) return null
val schemed = word.startsWith("http://", ignoreCase = true) || word.startsWith("https://", ignoreCase = true)
val host = word.substringAfter("://").substringBefore('/').substringBefore('?').substringBefore('#')
.substringBefore(':').lowercase()
if (!schemed && !host.startsWith("www.") && host.substringAfterLast('.') !in BARE_TLDS) return null
return if (schemed) word else "https://$word"
}

private class Builder(val emotes: Map<String, ChatEmote>, val self: String) {
val parts = mutableListOf<ChatPart>()

fun segment(text: String) {
var index = 0
for (match in SPACES.findAll(text)) {
word(text.substring(index, match.range.first))
pushText(match.value)
index = match.range.last + 1
}
word(text.substring(index))
}

private fun word(word: String) {
if (word.isEmpty()) return
val emote = emotes[word]
if (emote != null) {
val image = ChatImage(emote.name, emote.url)
val base = if (emote.zeroWidth) lastEmoteIndex() else -1
if (base >= 0) {
if (parts.lastOrNull() is ChatPart.Text) parts.removeAt(parts.lastIndex)
val target = parts[base] as ChatPart.Emote
parts[base] = target.copy(overlays = target.overlays + image)
} else {
parts += ChatPart.Emote(image)
}
return
}
if (word.startsWith("@") && word.length > 1) {
val name = word.drop(1).replace(TRAILING, "")
parts += ChatPart.Mention("@$name", name.lowercase() == self)
pushText(word.drop(name.length + 1))
return
}
val trimmed = word.replace(TRAILING, "")
val href = linkHref(trimmed)
if (href != null) {
parts += ChatPart.Link(trimmed, href)
pushText(word.drop(trimmed.length))
return
}
pushText(word)
}

private fun lastEmoteIndex(): Int {
val last = parts.lastIndex
val index = if (last >= 0 && (parts[last] as? ChatPart.Text)?.text?.isBlank() == true) last - 1 else last
return if (index >= 0 && parts[index] is ChatPart.Emote) index else -1
}

private fun pushText(text: String) {
if (text.isEmpty()) return
val last = parts.lastOrNull()
if (last is ChatPart.Text) parts[parts.lastIndex] = ChatPart.Text(last.text + text) else parts += ChatPart.Text(text)
}
}
}

// String.codePoints() needs API 24; the app still runs on 23.
internal fun codePoints(text: String): IntArray {
val out = IntArray(text.codePointCount(0, text.length))
var index = 0
var count = 0
while (index < text.length) {
val point = text.codePointAt(index)
out[count++] = point
index += Character.charCount(point)
}
return out
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ interface TwitchAccountRepository {

suspend fun followedLives(cursor: String? = null): Result<TwitchPage<Video>>

suspend fun followedVideos(cursor: String? = null): Result<TwitchPage<Video>>

suspend fun followedChannels(cursor: String? = null): Result<TwitchPage<TwitchChannel>>

suspend fun isFollowing(login: String): Result<Boolean>

suspend fun setFollowing(login: String, follow: Boolean): Result<Unit>
Expand Down
Loading