fix StateCode serialization

This commit is contained in:
ryoii 2020-02-04 20:28:52 +08:00
parent 696889ba0c
commit 4ba2b34a81
4 changed files with 19 additions and 18 deletions

View File

@ -15,19 +15,17 @@ abstract class VerifyDTO : DTO {
data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO()
// 写成data class并继承DTO接口是为了返回时的形式统一
@Serializable
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(4, "Session未认证")
object NoElement : StateCodeDTO(5, "指定对象不存在")
open class StateCode(val code: Int, var msg: String) {
object Success : StateCode(0, "success") // 成功
object NoBot : StateCode(2, "指定Bot不存在")
object IllegalSession : StateCode(3, "Session失效或不存在")
object NotVerifySession : StateCode(4, "Session未认证")
object NoElement : StateCode(5, "指定对象不存在")
// KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
@Serializable
class IllegalAccess() : StateCodeDTO(400, "") { // 非法访问
class IllegalAccess() : StateCode(400, "") { // 非法访问
constructor(msg: String) : this() {
this.msg = msg
}

View File

@ -27,15 +27,15 @@ fun Application.authModule() {
closeSession(it.sessionKey)
allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext)
}
call.respondDTO(StateCodeDTO.Success)
call.respondStateCode(StateCode.Success)
} catch (e: NoSuchElementException) {
call.respondDTO(StateCodeDTO.NoBot)
call.respondStateCode(StateCode.NoBot)
}
}
miraiVerify<BindDTO>("/release") {
SessionManager.closeSession(it.sessionKey)
call.respondDTO(StateCodeDTO.Success)
call.respondStateCode(StateCode.Success)
}
}

View File

@ -105,19 +105,22 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
try {
blk(this)
} catch (e: IllegalSessionException) {
call.respondDTO(StateCodeDTO.IllegalSession)
call.respondStateCode(StateCode.IllegalSession)
} catch (e: NotVerifiedSessionException) {
call.respondDTO(StateCodeDTO.NotVerifySession)
call.respondStateCode(StateCode.NotVerifySession)
} catch (e: NoSuchElementException) {
call.respondDTO(StateCodeDTO.NoElement)
call.respondStateCode(StateCode.NoElement)
} catch (e: IllegalAccessException) {
call.respondDTO(StateCodeDTO.IllegalAccess(e.message), HttpStatusCode.BadRequest)
call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest)
}
}
/*
extend function
*/
internal suspend inline fun <reified T : StateCode> ApplicationCall.respondStateCode(code: T, status: HttpStatusCode = HttpStatusCode.OK)
= respondJson(code.toJson(StateCode.serializer()), status)
internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(dto: T, status: HttpStatusCode = HttpStatusCode.OK)
= respondJson(dto.toJson(), status)

View File

@ -18,12 +18,12 @@ fun Application.messageModule() {
miraiVerify<SendDTO>("/sendFriendMessage") {
it.session.bot.getFriend(it.target).sendMessage(it.messageChain.toMessageChain())
call.respondDTO(StateCodeDTO.Success)
call.respondStateCode(StateCode.Success)
}
miraiVerify<SendDTO>("/sendGroupMessage") {
it.session.bot.getGroup(it.target).sendMessage(it.messageChain.toMessageChain())
call.respondDTO(StateCodeDTO.Success)
call.respondStateCode(StateCode.Success)
}
miraiVerify<VerifyDTO>("/event/message") {