DbDiscordPreferences.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/database/DbDiscordPreferences.kt
1package eu.friedcutlet.yokan.database23import kotlinx.serialization.Serializable45@Serializable6enum class DbDiscordStatus(val gatewayValue: String) { Online("online"), Idle("idle"), DoNotDisturb("dnd") }78/** A user-owned Discord login. The token is installation-local: never exported, synced or logged. */9@Serializable10data class DbDiscordAccount(11 val id: String,12 val username: String,13 val displayName: String = username,14 val avatarUrl: String? = null,15 val token: String,16) {17 override fun toString(): String = "DbDiscordAccount(id=$id, username=$username)"18}1920@Serializable21data class DbDiscordButton(val label: String, val url: String)2223val DefaultDiscordButtons = listOf(24 DbDiscordButton("Get Yokan", "https://github.com/Schnitzel5/yokan"),25 DbDiscordButton("Join us", "https://discord.com/invite/EjfBuYahsP"),26)2728/** Discord presence customization and accounts; the legacy enable/title/cover toggles stay on [DbSettings]. */29@Serializable30data class DbDiscordPreferences(31 val accounts: List<DbDiscordAccount> = emptyList(),32 val activeAccountId: String? = null,33 val status: DbDiscordStatus = DbDiscordStatus.Online,34 val customMessage: String = "",35 val showTimestamp: Boolean = true,36 val showButtons: Boolean = true,37 val buttons: List<DbDiscordButton> = DefaultDiscordButtons,38 /** Never show what is being read/watched, whatever the title. */39 val alwaysPrivate: Boolean = false,40 /** Titles in these categories (or [UncategorizedActivityCategoryId]) appear without title/cover/progress. */41 val privateCategoryIds: Set<Long> = emptySet(),42) {43 val activeAccount: DbDiscordAccount? get() = accounts.firstOrNull { it.id == activeAccountId }4445 /** Adds or refreshes [account] (same Discord user id) and makes it the active one. */46 fun withAccount(account: DbDiscordAccount): DbDiscordPreferences =47 copy(accounts = accounts.filterNot { it.id == account.id } + account, activeAccountId = account.id)4849 /** Removing the active account activates the first remaining one. */50 fun withoutAccount(id: String): DbDiscordPreferences {51 val remaining = accounts.filterNot { it.id == id }52 val active = activeAccountId.takeIf { it != id && remaining.any { account -> account.id == it } } ?: remaining.firstOrNull()?.id53 return copy(accounts = remaining, activeAccountId = active)54 }5556 fun withActive(id: String): DbDiscordPreferences =57 if (accounts.any { it.id == id }) copy(activeAccountId = id) else this5859 /** Credentials stay on this installation. */60 fun forExport(): DbDiscordPreferences = copy(accounts = emptyList(), activeAccountId = null)6162 override fun toString(): String =63 "DbDiscordPreferences(accounts=${accounts.size}, active=${activeAccountId != null}, status=$status)"64}65