Skip to content

Commit

Permalink
同步 yutori 更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyayurin committed Oct 5, 2024
1 parent 430fac6 commit 6e9aad8
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 71 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ yutorix-satori-server = { module = "cn.yurn.yutorix:yutorix-satori-server", vers
安装模块

```kotlin
install(Adapter.Satori) {
install(Adapter.satori()) {
host = "127.0.0.1"
port = 8080
token = "token"
}
install(Server.Satori) {
install(Server.satori()) {
listen = "0.0.0.0"
port = 8080
token = "token"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import kotlinx.serialization.json.Json
class SatoriActionService(
val yutori: Yutori,
val properties: SatoriAdapterProperties
) : AdapterActionService {
) : AdapterActionService() {
override suspend fun send(
resource: String,
method: String,
Expand Down Expand Up @@ -113,30 +113,30 @@ class SatoriActionService(
val body = response.bodyAsText()
when (resource) {
"channel" -> when (method) {
"get" -> Json.decodeFromString<SerializableChannel>(body).toUniverse(yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableChannel, Channel>>(body).toUniverse(yutori)
"create" -> Json.decodeFromString<SerializableChannel>(body).toUniverse(yutori)
"get" -> Json.decodeFromString<SerializableChannel>(body).toUniverse(null, yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableChannel, Channel>>(body).toUniverse(null, yutori)
"create" -> Json.decodeFromString<SerializableChannel>(body).toUniverse(null, yutori)
"update" -> Unit
"delete" -> Unit
"mute" -> Unit
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"user.channel" -> when (method) {
"create" -> Json.decodeFromString<SerializableChannel>(body).toUniverse(yutori)
"create" -> Json.decodeFromString<SerializableChannel>(body).toUniverse(null, yutori)
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"guild" -> when (method) {
"get" -> Json.decodeFromString<SerializableGuild>(body).toUniverse(yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableGuild, Guild>>(body).toUniverse(yutori)
"get" -> Json.decodeFromString<SerializableGuild>(body).toUniverse(null, yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableGuild, Guild>>(body).toUniverse(null, yutori)
"approve" -> Unit
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"guild.member" -> when (method) {
"get" -> Json.decodeFromString<SerializableGuildMember>(body).toUniverse(yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableGuildMember, GuildMember>>(body).toUniverse(yutori)
"get" -> Json.decodeFromString<SerializableGuildMember>(body).toUniverse(null, yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableGuildMember, GuildMember>>(body).toUniverse(null, yutori)
"kick" -> Unit
"mute" -> Unit
"approve" -> Unit
Expand All @@ -150,42 +150,42 @@ class SatoriActionService(
}

"guild.role" -> when (method) {
"list" -> Json.decodeFromString<SerializablePagingList<SerializableGuildRole, GuildRole>>(body).toUniverse(yutori)
"create" -> Json.decodeFromString<SerializableGuildRole>(body).toUniverse(yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableGuildRole, GuildRole>>(body).toUniverse(null, yutori)
"create" -> Json.decodeFromString<SerializableGuildRole>(body).toUniverse(null, yutori)
"update" -> Unit
"delete" -> Unit
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"login" -> when (method) {
"get" -> Json.decodeFromString<SerializableLogin>(body).toUniverse(yutori)
"get" -> Json.decodeFromString<SerializableLogin>(body).toUniverse(null, yutori)
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"message" -> when (method) {
"create" -> Json.decodeFromString<List<SerializableMessage>>(body).map { it.toUniverse(yutori) }
"get" -> Json.decodeFromString<SerializableMessage>(body).toUniverse(yutori)
"create" -> Json.decodeFromString<List<SerializableMessage>>(body).map { it.toUniverse(null, yutori) }
"get" -> Json.decodeFromString<SerializableMessage>(body).toUniverse(null, yutori)
"delete" -> Unit
"update" -> Unit
"list" -> Json.decodeFromString<SerializableBidiPagingList<SerializableMessage, Message>>(body).toUniverse(yutori)
"list" -> Json.decodeFromString<SerializableBidiPagingList<SerializableMessage, Message>>(body).toUniverse(null, yutori)
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"reaction" -> when (method) {
"create" -> Unit
"delete" -> Unit
"clear" -> Unit
"list" -> Json.decodeFromString<SerializablePagingList<SerializableUser, User>>(body).toUniverse(yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableUser, User>>(body).toUniverse(null, yutori)
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"user" -> when (method) {
"get" -> Json.decodeFromString<SerializableUser>(body).toUniverse(yutori)
"get" -> Json.decodeFromString<SerializableUser>(body).toUniverse(null, yutori)
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}

"friend" -> when (method) {
"list" -> Json.decodeFromString<SerializablePagingList<SerializableUser, User>>(body).toUniverse(yutori)
"list" -> Json.decodeFromString<SerializablePagingList<SerializableUser, User>>(body).toUniverse(null, yutori)
"approve" -> Unit
else -> throw UnsupportedOperationException("Unsupported action: $resource.$method")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import co.touchlab.kermit.Logger
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.delay

val Adapter.Companion.Satori: SatoriAdapter
get() = SatoriAdapter()
fun Adapter.Companion.satori(alias: String? = null) = SatoriAdapter(alias)

@BuilderMarker
class SatoriAdapter : Adapter(), Reinstallable {
class SatoriAdapter(alias: String?) : Adapter(alias), Reinstallable {
var host: String = "127.0.0.1"
var port: Int = 8080
var path: String = ""
Expand Down Expand Up @@ -53,7 +52,7 @@ class SatoriAdapter : Adapter(), Reinstallable {
connecting = true
var sequence: Number? = null
do {
service = WebSocketEventService(properties, onConnect, onClose, onError, yutori, sequence)
service = WebSocketEventService(alias, properties, onConnect, onClose, onError, yutori, sequence)
service!!.onStart()
service!!.connect()
if (connecting) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json

class WebSocketEventService(
alias: String?,
val properties: SatoriAdapterProperties,
val onConnect: suspend WebSocketEventService.(List<Login>) -> Unit = { },
val onClose: suspend () -> Unit = { },
val onError: suspend () -> Unit = { },
val yutori: Yutori,
var sequence: Number? = null
) : AdapterEventService {
) : AdapterEventService(alias) {
private val actionsList = mutableListOf<RootActions>()
val service = SatoriActionService(yutori, properties)
private var job by atomic<Job?>(null)
Expand Down Expand Up @@ -94,6 +95,7 @@ class WebSocketEventService(
ready = true
for (login in signal.body.logins) {
val actions = RootActions(
alias = alias,
platform = login.platform!!,
userId = login.user!!.id,
service = service,
Expand All @@ -102,7 +104,7 @@ class WebSocketEventService(
actionsList += actions
yutori.actionsList += actions
}
onConnect(signal.body.logins.map { it.toUniverse(yutori) })
onConnect(signal.body.logins.map { it.toUniverse(null, yutori) })
Logger.i(name) { "成功建立事件推送服务: ${signal.body.logins}" }
launch {
do {
Expand All @@ -115,7 +117,7 @@ class WebSocketEventService(
}
}

is EventSignal -> launch { onEvent(signal.body.toUniverse(yutori)) }
is EventSignal -> launch { onEvent(signal.body.toUniverse(alias, yutori)) }
is PongSignal -> {
isReceivedPong = true
Logger.d(name) { "收到 PONG" }
Expand Down Expand Up @@ -168,6 +170,7 @@ class WebSocketEventService(
actions -> actions.platform == event.platform && actions.userId == event.selfId
} ?: run {
val actions = RootActions(
alias = alias,
platform = event.platform,
userId = event.selfId,
service = service,
Expand Down
63 changes: 32 additions & 31 deletions core/src/commonMain/kotlin/cn/yurn/yutori/module/satori/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ data class SatoriServerProperties(
)

interface Convertable<U> {
fun toUniverse(yutori: Yutori): U
fun toUniverse(alias: String?, yutori: Yutori): U
}

@Serializable
Expand All @@ -135,22 +135,23 @@ class SerializableEvent(
val role: SerializableGuildRole? = null,
val user: SerializableUser? = null
) : Convertable<Event<SigningEvent>> {
override fun toUniverse(yutori: Yutori) = Event<SigningEvent>(
override fun toUniverse(alias: String?, yutori: Yutori) = Event<SigningEvent>(
alias = alias,
id = id,
type = type,
platform = platform,
selfId = selfId,
timestamp = timestamp,
argv = argv?.toUniverse(yutori),
button = button?.toUniverse(yutori),
channel = channel?.toUniverse(yutori),
guild = guild?.toUniverse(yutori),
login = login?.toUniverse(yutori),
member = member?.toUniverse(yutori),
message = message?.toUniverse(yutori),
operator = operator?.toUniverse(yutori),
role = role?.toUniverse(yutori),
user = user?.toUniverse(yutori)
argv = argv?.toUniverse(alias, yutori),
button = button?.toUniverse(alias, yutori),
channel = channel?.toUniverse(alias, yutori),
guild = guild?.toUniverse(alias, yutori),
login = login?.toUniverse(alias, yutori),
member = member?.toUniverse(alias, yutori),
message = message?.toUniverse(alias, yutori),
operator = operator?.toUniverse(alias, yutori),
role = role?.toUniverse(alias, yutori),
user = user?.toUniverse(alias, yutori)
)

companion object {
Expand Down Expand Up @@ -182,7 +183,7 @@ sealed class SerializableInteraction {
@Serializable(DynamicLookupSerializer::class)
val options: Any
) : SerializableInteraction(), Convertable<Interaction.Argv> {
override fun toUniverse(yutori: Yutori) = Interaction.Argv(
override fun toUniverse(alias: String?, yutori: Yutori) = Interaction.Argv(
name = name,
arguments = arguments,
options = options
Expand All @@ -199,7 +200,7 @@ sealed class SerializableInteraction {

@Serializable
data class Button(val id: String) : SerializableInteraction(), Convertable<Interaction.Button> {
override fun toUniverse(yutori: Yutori) = Interaction.Button(id = id)
override fun toUniverse(alias: String?, yutori: Yutori) = Interaction.Button(id = id)

companion object {
fun fromUniverse(universe: Interaction.Button) = Button(id = universe.id)
Expand All @@ -215,7 +216,7 @@ data class SerializableChannel(
@SerialName("parent_id")
val parentId: String? = null
) : Convertable<Channel> {
override fun toUniverse(yutori: Yutori) = Channel(
override fun toUniverse(alias: String?, yutori: Yutori) = Channel(
id = id,
type = type,
name = name,
Expand All @@ -238,7 +239,7 @@ data class SerializableGuild(
val name: String? = null,
val avatar: String? = null
) : Convertable<Guild> {
override fun toUniverse(yutori: Yutori) = Guild(
override fun toUniverse(alias: String?, yutori: Yutori) = Guild(
id = id,
name = name,
avatar = avatar
Expand All @@ -263,10 +264,10 @@ data class SerializableLogin(
@SerialName("proxy_urls")
val proxyUrls: List<String> = listOf(),
) : Convertable<Login> {
override fun toUniverse(yutori: Yutori) = Login(
override fun toUniverse(alias: String?, yutori: Yutori) = Login(
adapter = adapter,
platform = platform,
user = user?.toUniverse(yutori),
user = user?.toUniverse(alias, yutori),
status = status,
features = features,
proxyUrls = proxyUrls
Expand All @@ -292,8 +293,8 @@ data class SerializableGuildMember(
@SerialName("joined_at")
val joinedAt: Long? = null
) : Convertable<GuildMember> {
override fun toUniverse(yutori: Yutori) = GuildMember(
user = user?.toUniverse(yutori),
override fun toUniverse(alias: String?, yutori: Yutori) = GuildMember(
user = user?.toUniverse(alias, yutori),
nick = nick,
avatar = avatar,
joinedAt = joinedAt
Expand Down Expand Up @@ -322,13 +323,13 @@ data class SerializableMessage(
@SerialName("updated_at")
val updatedAt: Long? = null
) : Convertable<Message> {
override fun toUniverse(yutori: Yutori) = Message(
override fun toUniverse(alias: String?, yutori: Yutori) = Message(
id = id,
content = content.deserialize(yutori),
channel = channel?.toUniverse(yutori),
guild = guild?.toUniverse(yutori),
member = member?.toUniverse(yutori),
user = user?.toUniverse(yutori),
channel = channel?.toUniverse(alias, yutori),
guild = guild?.toUniverse(alias, yutori),
member = member?.toUniverse(alias, yutori),
user = user?.toUniverse(alias, yutori),
createdAt = createdAt,
updatedAt = updatedAt
)
Expand Down Expand Up @@ -356,7 +357,7 @@ data class SerializableUser(
@SerialName("is_bot")
val isBot: Boolean? = null
) : Convertable<User> {
override fun toUniverse(yutori: Yutori) = User(
override fun toUniverse(alias: String?, yutori: Yutori) = User(
id = id,
name = name,
nick = nick,
Expand All @@ -380,7 +381,7 @@ data class SerializableGuildRole(
val id: String,
val name: String? = null
) : Convertable<GuildRole> {
override fun toUniverse(yutori: Yutori) = GuildRole(
override fun toUniverse(alias: String?, yutori: Yutori) = GuildRole(
id = id,
name = name
)
Expand All @@ -398,10 +399,10 @@ data class SerializablePagingList<T, U>(
val data: List<T>,
val next: String? = null
) : Convertable<PagingList<U>> {
override fun toUniverse(yutori: Yutori) = PagingList(
override fun toUniverse(alias: String?, yutori: Yutori) = PagingList(
data = data.map {
if (it is Convertable<*>) {
it.toUniverse(yutori)
it.toUniverse(alias, yutori)
} else {
it
} as U
Expand Down Expand Up @@ -437,10 +438,10 @@ data class SerializableBidiPagingList<T, U>(
override fun toString() = value
}

override fun toUniverse(yutori: Yutori) = BidiPagingList(
override fun toUniverse(alias: String?, yutori: Yutori) = BidiPagingList(
data = data.map {
if (it is Convertable<*>) {
it.toUniverse(yutori)
it.toUniverse(alias, yutori)
} else {
it
} as U
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ agp = "8.6.1"
atomicfu = "0.25.0"
ktor = "3.0.0-rc-1"
ksoup = "0.1.9"
yutori = "dae7ab90aa9b5e3b3ee88a5900635c3fd4b83e83"
yutori = "5badb79b61b360efaba89b8c97206bedc2e265d9"

[libraries]
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import cn.yurn.yutori.Yutori
import cn.yurn.yutori.module.satori.SatoriServerProperties
import kotlinx.atomicfu.atomic

val Server.Companion.Satori: SatoriServer
get() = SatoriServer()
fun Server.Companion.satori(alias: String? = null) = SatoriServer(alias)

@BuilderMarker
class SatoriServer : Server(), Reinstallable {
class SatoriServer(alias: String?) : Server(alias), Reinstallable {
var listen: String = "0.0.0.0"
var port: Int = 8080
var path: String = ""
Expand All @@ -30,7 +29,7 @@ class SatoriServer : Server(), Reinstallable {
override suspend fun start(yutori: Yutori) {
val properties = SatoriServerProperties(listen, port, path, token, version)
connecting = true
service = SatoriServerService(properties, yutori)
service = SatoriServerService(alias, properties, yutori)
service!!.start()
}

Expand Down
Loading

0 comments on commit 6e9aad8

Please sign in to comment.