Skip to content

Commit

Permalink
新增 alias
Browse files Browse the repository at this point in the history
优化代码
  • Loading branch information
Nyayurin committed Oct 5, 2024
1 parent dae7ab9 commit 5badb79
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 131 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ suspend fun app() = coroutineScope {
// 路由消息发送
message.create {
// 找到对应 actions
val forwardActions = actionsList.first {
val forwardActions = yutori.actionsList.first {
it.platform == request.properties["platform"] && it.selfId == request.properties["selfId"]
}
// 转发, 并获取响应
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put
import kotlinx.serialization.json.putJsonObject

class YhChatAdapterActionService(val properties: YhChatProperties, val name: String) : AdapterActionService {
class YhChatAdapterActionService(
val properties: YhChatProperties,
val name: String
) : AdapterActionService() {
override suspend fun send(
resource: String,
method: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import cn.yurn.yutori.module.yhchat.YhChatProperties
import cn.yurn.yutori.module.yhchat.message.YhChatMessageBuilder
import kotlinx.atomicfu.atomic

val Adapter.Companion.YhChat: YhChatAdapter
get() = YhChatAdapter()
fun Adapter.Companion.yhchat(alias: String? = null) = YhChatAdapter(alias)

@BuilderMarker
class YhChatAdapter : Adapter(), Reinstallable {
class YhChatAdapter(alias: String?) : Adapter(alias), Reinstallable {
var host: String = "0.0.0.0"
var port: Int = 8080
var path: String = ""
Expand All @@ -40,7 +39,7 @@ class YhChatAdapter : Adapter(), Reinstallable {
}

override suspend fun start(yutori: Yutori) {
service = YhChatAdapterEventService(properties, yutori)
service = YhChatAdapterEventService(alias, properties, yutori)
service!!.onStart()
service!!.connect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json

class YhChatAdapterEventService(
alias: String?,
val properties: YhChatProperties,
val yutori: Yutori
) : AdapterEventService {
) : AdapterEventService(alias) {
val service = YhChatAdapterActionService(properties, yutori.name)
private var job by atomic<Job?>(null)
private val idMap = mapOf<Int, String>()
private var last = 0

init {
yutori.actionsList += RootActions("yhchat", properties.userId, service, yutori)
yutori.actionsList += RootActions(alias, "yhchat", properties.userId, service, yutori)
}

override suspend fun connect() {
Expand Down Expand Up @@ -125,6 +126,7 @@ class YhChatAdapterEventService(
timestamp: Long,
event: cn.yurn.yutori.module.yhchat.Event
) = Event<SigningEvent>(
alias = alias,
id = last++,
type = MessageEvents.CREATED,
platform = "yhchat",
Expand Down Expand Up @@ -212,6 +214,7 @@ class YhChatAdapterEventService(
timestamp: Long,
event: cn.yurn.yutori.module.yhchat.Event
) = Event<SigningEvent>(
alias = alias,
id = last++,
type = GuildMemberEvents.ADDED,
platform = "yhchat",
Expand Down Expand Up @@ -265,6 +268,7 @@ class YhChatAdapterEventService(
timestamp: Long,
event: cn.yurn.yutori.module.yhchat.Event
) = Event<SigningEvent>(
alias = alias,
id = last++,
type = GuildMemberEvents.REMOVED,
platform = "yhchat",
Expand Down
87 changes: 46 additions & 41 deletions yutori/src/commonMain/kotlin/cn/yurn/yutori/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import cn.yurn.yutori.message.message

abstract class Actions
abstract class Action(
val platform: String?, val userId: String?, val resource: String, val service: AdapterActionService
val platform: String?,
val userId: String?,
val resource: String,
val service: AdapterActionService
) {
protected suspend inline fun <reified T> send(
method: String, vararg content: Pair<String, Any?>
Expand All @@ -19,6 +22,7 @@ abstract class Action(
}

class RootActions(
val alias: String?,
val platform: String,
val userId: String,
val service: AdapterActionService,
Expand All @@ -34,13 +38,12 @@ class RootActions(
val upload = UploadAction(platform, userId, service)
val admin = AdminAction(service)
val containers = mutableMapOf<String, Actions>().apply {
for ((key, value) in yutori.actionsContainers) this[key] =
value(platform, userId, service)
for ((key, value) in yutori.actionsContainers) this[key] = value(platform, userId, service)
}

class ChannelAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "channel", service
) {
class ChannelAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "channel", service) {
suspend fun get(
channelId: String, vararg contents: Pair<String, Any> = arrayOf()
): Channel = send("get", "channel_id" to channelId, *contents)
Expand All @@ -66,9 +69,9 @@ class RootActions(
): Unit = send("mute", "channel_id" to channelId, "duration" to duration, *contents)
}

class GuildAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "guild", service
) {
class GuildAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "guild", service) {
val member = MemberAction(platform, userId, service)
val role = RoleAction(platform, userId, service)

Expand All @@ -93,9 +96,9 @@ class RootActions(
*contents
)

class MemberAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "guild.member", service
) {
class MemberAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "guild.member", service) {
val role = RoleAction(platform, userId, service)

suspend fun get(
Expand Down Expand Up @@ -148,9 +151,9 @@ class RootActions(
*contents
)

class RoleAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "guild.member.role", service
) {
class RoleAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "guild.member.role", service) {
suspend fun set(
guildId: String,
userId: String,
Expand Down Expand Up @@ -179,9 +182,9 @@ class RootActions(
}
}

class RoleAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "guild.role", service
) {
class RoleAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "guild.role", service) {
suspend fun list(
guildId: String,
next: String? = null,
Expand All @@ -208,19 +211,17 @@ class RootActions(
}
}

class LoginAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "login", service
) {
class LoginAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "login", service) {
suspend fun get(
vararg contents: Pair<String, Any> = arrayOf()
): Login = send("get", *contents)
}

class MessageAction(
private val yutori: Yutori, platform: String, userId: String, service: AdapterActionService
) : Action(
platform, userId, "message", service
) {
) : Action(platform, userId, "message", service) {
suspend fun create(
channelId: String,
content: List<MessageElement>,
Expand Down Expand Up @@ -280,9 +281,9 @@ class RootActions(
)
}

class ReactionAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "reaction", service
) {
class ReactionAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "reaction", service) {
suspend fun create(
channelId: String,
messageId: String,
Expand Down Expand Up @@ -340,18 +341,18 @@ class RootActions(
)
}

class UserAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "user", service
) {
class UserAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "user", service) {
val channel = ChannelAction(platform, userId, service)

suspend fun get(
userId: String, vararg contents: Pair<String, Any> = arrayOf()
): User = send("get", "user_id" to userId, *contents)

class ChannelAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "user.channel", service
) {
class ChannelAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "user.channel", service) {
suspend fun create(
userId: String,
guildId: String? = null,
Expand All @@ -360,9 +361,9 @@ class RootActions(
}
}

class FriendAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "friend", service
) {
class FriendAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "friend", service) {
suspend fun list(
next: String? = null, vararg contents: Pair<String, Any> = arrayOf()
): PagingList<User> = send("list", "next" to next, *contents)
Expand All @@ -381,9 +382,9 @@ class RootActions(
)
}

class UploadAction(platform: String, userId: String, service: AdapterActionService) : Action(
platform, userId, "upload", service
) {
class UploadAction(
platform: String, userId: String, service: AdapterActionService
) : Action(platform, userId, "upload", service) {
suspend fun create(
vararg contents: FormData
): Map<String, String> = upload("create", contents.toList())
Expand All @@ -393,13 +394,17 @@ class RootActions(
val login = LoginAction(service)
val webhook = WebhookAction(service)

class LoginAction(service: AdapterActionService) : Action(null, null, "login", service) {
class LoginAction(
service: AdapterActionService
) : Action(null, null, "login", service) {
suspend fun list(
vararg contents: Pair<String, Any> = arrayOf()
): List<Login> = send("list", *contents)
}

class WebhookAction(service: AdapterActionService) : Action(null, null, "webhook", service) {
class WebhookAction(
service: AdapterActionService
) : Action(null, null, "webhook", service) {
suspend fun create(
url: String, token: String? = null, vararg contents: Pair<String, Any> = arrayOf()
): Unit = send("list", "url" to url, "token" to token, *contents)
Expand Down
11 changes: 7 additions & 4 deletions yutori/src/commonMain/kotlin/cn/yurn/yutori/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ data class AdapterContext<T : SigningEvent>(
)

data class ServerContext<T : SigningRequest>(
val actionsList: List<RootActions>,
val request: Request<T>,
val response: Response,
val yutori: Yutori
)

class Event<T : SigningEvent>(val properties: Map<String, Any?> = mapOf()) {
class Event<T : SigningEvent>(val alias: String?, val properties: Map<String, Any?>) {
val id: Number by properties
val type: String by properties
val platform: String by properties
Expand Down Expand Up @@ -150,6 +149,7 @@ class Event<T : SigningEvent>(val properties: Map<String, Any?> = mapOf()) {
get() = properties["user"] as User?

constructor(
alias: String?,
id: Number,
type: String,
platform: String,
Expand All @@ -167,7 +167,8 @@ class Event<T : SigningEvent>(val properties: Map<String, Any?> = mapOf()) {
user: User? = null,
vararg pair: Pair<String, Any?> = arrayOf(),
) : this(
mapOf(
alias = alias,
properties = mapOf(
"id" to id,
"type" to type,
"platform" to platform,
Expand All @@ -189,8 +190,10 @@ class Event<T : SigningEvent>(val properties: Map<String, Any?> = mapOf()) {
}

class Request<T : SigningRequest>(
val alias: String?,
val api: String,
val properties: Map<String, Any?> = mapOf()
val header: Map<String, Any?>,
val body: Map<String, Any?>
)

class Response(private val onRespond: suspend (String) -> Unit) {
Expand Down
6 changes: 3 additions & 3 deletions yutori/src/commonMain/kotlin/cn/yurn/yutori/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package cn.yurn.yutori

abstract class Module {
abstract class Module(val alias: String?) {
abstract fun install(yutori: Yutori)
abstract fun uninstall(yutori: Yutori)

Expand All @@ -14,11 +14,11 @@ interface Startable {
fun stop(yutori: Yutori)
}

abstract class Adapter : Module(), Startable {
abstract class Adapter(alias: String?) : Module(alias), Startable {
companion object
}

abstract class Server : Module(), Startable {
abstract class Server(alias: String?) : Module(alias), Startable {
abstract suspend fun pushEvent(event: Event<SigningEvent>)
companion object
}
Expand Down
Loading

0 comments on commit 5badb79

Please sign in to comment.