guilds, lifecycle gateway disconnector, base channels

This commit is contained in:
Cilly Leang 2026-03-21 18:53:24 +11:00
parent fcdd237809
commit 0781606a00
Signed by: cilly
GPG key ID: 6500251E087653C9
14 changed files with 292 additions and 6 deletions

View file

@ -26,6 +26,9 @@ kotlin {
implementation(project(":api:rest"))
implementation(project(":common"))
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.core)
implementation(project.dependencies.platform(libs.koin.bom))
implementation(libs.koin.core)

View file

@ -0,0 +1,28 @@
package moe.lava.neon.core.data.guild
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.map
import moe.lava.neon.api.gateway.Event
import moe.lava.neon.api.gateway.GatewayHandler
import moe.lava.neon.core.model.Guild
import moe.lava.neon.api.objects.Guild as ApiGuild
class GuildGatewayDataSource(
gateway: GatewayHandler,
) {
@OptIn(ExperimentalCoroutinesApi::class)
val flow = gateway.events
.filterIsInstance<Event.Ready>()
.flatMapConcat { it.guilds.asFlow() }
.filterIsInstance<ApiGuild.Gateway>()
.map { apiGuild ->
Guild(
id = apiGuild.id,
name = apiGuild.properties.name,
iconHash = apiGuild.properties.icon,
)
}
}

View file

@ -3,21 +3,27 @@ package moe.lava.neon.core.di
import moe.lava.neon.api.ApiClient
import moe.lava.neon.api.gateway.GatewayHandler
import moe.lava.neon.core.AppSettings
import moe.lava.neon.core.data.guild.GuildGatewayDataSource
import moe.lava.neon.core.repository.AuthRepository
import moe.lava.neon.core.repository.CaptchaRepository
import moe.lava.neon.core.repository.GatewayRepository
import moe.lava.neon.core.repository.GuildRepository
import moe.lava.neon.core.repository.UserRepository
import org.koin.core.module.dsl.createdAtStart
import org.koin.core.module.dsl.withOptions
import org.koin.dsl.module
import org.koin.plugin.module.dsl.single
val coreModule = module {
factory { ApiClient() }
single<AppSettings>()
single<GatewayHandler>()
single<GuildGatewayDataSource>()
single<AuthRepository>()
single<CaptchaRepository>()
single<GatewayRepository>()
single<GuildRepository>() withOptions { createdAtStart() }
single<UserRepository>()
single<GatewayHandler>()
}

View file

@ -0,0 +1,16 @@
package moe.lava.neon.core.model
import kotlinx.serialization.Serializable
import moe.lava.neon.api.objects.Snowflake
@Serializable
data class Guild(
val id: Snowflake,
val name: String,
val iconHash: String?,
)
@Serializable
data class UnavailableGuild(val e: String)

View file

@ -0,0 +1,28 @@
package moe.lava.neon.core.repository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import moe.lava.neon.api.objects.Snowflake
import moe.lava.neon.core.data.guild.GuildGatewayDataSource
import moe.lava.neon.core.model.Guild
class GuildRepository(
gatewaySource: GuildGatewayDataSource,
) {
init {
gatewaySource.flow
.onEach { guild ->
cache[guild.id] = guild
joined[guild.id] = guild
}
.launchIn(CoroutineScope(Dispatchers.IO))
}
private val cache = mutableMapOf<Snowflake, Guild>()
private val joined = mutableMapOf<Snowflake, Guild>()
fun get(id: Snowflake) = cache[id]
fun getJoined() = joined.toMap()
}