mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-13 06:30:13 +08:00
fix serialize and rename
This commit is contained in:
parent
4b9900e0bd
commit
878d2ab850
@ -21,7 +21,7 @@ inline fun <reified T : Any> String.jsonParseOrNull(
|
||||
serializer: DeserializationStrategy<T>? = null
|
||||
): T? = try {
|
||||
if(serializer == null) MiraiJson.json.parse(this) else MiraiJson.json.parse(serializer, this)
|
||||
} catch (e: Exception) { throw e }
|
||||
} catch (e: Exception) { null }
|
||||
|
||||
|
||||
|
||||
|
@ -7,7 +7,8 @@ import net.mamoe.mirai.api.http.AuthedSession
|
||||
@Serializable
|
||||
abstract class VerifyDTO : DTO {
|
||||
abstract val sessionKey: String
|
||||
@Transient lateinit var session: AuthedSession // 反序列化验证后传入
|
||||
@Transient
|
||||
lateinit var session: AuthedSession // 反序列化验证后传入
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@ -16,15 +17,20 @@ data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO()
|
||||
|
||||
// 写成data class并继承DTO接口是为了返回时的形式统一
|
||||
@Serializable
|
||||
open class StateCodeDTO(val code: Int, val msg: String) : DTO {
|
||||
companion object {
|
||||
val SUCCESS = StateCodeDTO(0, "success") // 成功
|
||||
// val AUTH_WRONG = CodeDTO(1) // AuthKey错误, @see AuthResDTO
|
||||
val NO_BOT = StateCodeDTO(2, "指定Bot不存在")
|
||||
val ILLEGAL_SESSION = StateCodeDTO(3, "Session失效或不存在")
|
||||
val NOT_VERIFIED_SESSION = StateCodeDTO(3, "Session未认证")
|
||||
sealed class StateCodeDTO(val code: Int, var msg: String) : DTO {
|
||||
object Success : StateCodeDTO(0, "success") // 成功
|
||||
// object AUTH_WRONG : CodeDTO(1) // AuthKey错误, @see AuthResDTO
|
||||
object NoBot : StateCodeDTO(2, "指定Bot不存在")
|
||||
object IllegalSession : StateCodeDTO(3, "Session失效或不存在")
|
||||
object NotVerifySession : StateCodeDTO(3, "Session未认证")
|
||||
|
||||
// KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
|
||||
@Serializable
|
||||
class IllegalAccess() : StateCodeDTO(400, "") { // 非法访问
|
||||
constructor(msg: String) : this() {
|
||||
this.msg = msg
|
||||
}
|
||||
}
|
||||
class ILLEGAL_ACCESS(msg: String) : StateCodeDTO(400, msg) // 非法访问
|
||||
}
|
||||
|
||||
@Serializable
|
||||
|
@ -27,15 +27,15 @@ fun Application.authModule() {
|
||||
closeSession(it.sessionKey)
|
||||
allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext)
|
||||
}
|
||||
call.respondDTO(StateCodeDTO.SUCCESS)
|
||||
call.respondDTO(StateCodeDTO.Success)
|
||||
} catch (e: NoSuchElementException) {
|
||||
call.respondDTO(StateCodeDTO.NO_BOT)
|
||||
call.respondDTO(StateCodeDTO.NoBot)
|
||||
}
|
||||
}
|
||||
|
||||
miraiVerify<BindDTO>("/release") {
|
||||
SessionManager.closeSession(it.sessionKey)
|
||||
call.respondDTO(StateCodeDTO.SUCCESS)
|
||||
call.respondDTO(StateCodeDTO.Success)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -105,11 +105,11 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
|
||||
try {
|
||||
blk(this)
|
||||
} catch (e: IllegalSessionException) {
|
||||
call.respondDTO(StateCodeDTO.ILLEGAL_SESSION)
|
||||
call.respondDTO(StateCodeDTO.IllegalSession)
|
||||
} catch (e: NotVerifiedSessionException) {
|
||||
call.respondDTO(StateCodeDTO.NOT_VERIFIED_SESSION)
|
||||
call.respondDTO(StateCodeDTO.NotVerifySession)
|
||||
} catch (e: IllegalAccessException) {
|
||||
call.respondDTO(StateCodeDTO.ILLEGAL_ACCESS(e.message), HttpStatusCode.BadRequest)
|
||||
call.respondDTO(StateCodeDTO.IllegalAccess(e.message), HttpStatusCode.BadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,7 +122,7 @@ internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(dto: T,
|
||||
internal suspend fun ApplicationCall.respondJson(json: String, status: HttpStatusCode = HttpStatusCode.OK) =
|
||||
respondText(json, defaultTextContentType(ContentType("application", "json")), status)
|
||||
|
||||
internal suspend inline fun <reified T : DTO> ApplicationCall.receiveDTO(): T? = receive<String>().apply(::println).jsonParseOrNull()
|
||||
internal suspend inline fun <reified T : DTO> ApplicationCall.receiveDTO(): T? = receive<String>().jsonParseOrNull()
|
||||
|
||||
|
||||
|
||||
|
@ -3,8 +3,6 @@ package net.mamoe.mirai.api.http.route
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.call
|
||||
import io.ktor.routing.routing
|
||||
import net.mamoe.mirai.api.http.AuthedSession
|
||||
import net.mamoe.mirai.api.http.SessionManager
|
||||
import net.mamoe.mirai.api.http.dto.*
|
||||
|
||||
fun Application.messageModule() {
|
||||
@ -20,12 +18,12 @@ fun Application.messageModule() {
|
||||
|
||||
miraiVerify<SendDTO>("/sendFriendMessage") {
|
||||
it.session.bot.getQQ(it.target).sendMessage(it.messageChain.toMessageChain())
|
||||
call.respondDTO(StateCodeDTO.SUCCESS)
|
||||
call.respondDTO(StateCodeDTO.Success)
|
||||
}
|
||||
|
||||
miraiVerify<SendDTO>("/sendGroupMessage") {
|
||||
it.session.bot.getGroup(it.target).sendMessage(it.messageChain.toMessageChain())
|
||||
call.respondDTO(StateCodeDTO.SUCCESS)
|
||||
call.respondDTO(StateCodeDTO.Success)
|
||||
}
|
||||
|
||||
miraiVerify<VerifyDTO>("/event/message") {
|
||||
|
Loading…
Reference in New Issue
Block a user