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

View File

@ -27,15 +27,15 @@ fun Application.authModule() {
closeSession(it.sessionKey) closeSession(it.sessionKey)
allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext) allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext)
} }
call.respondDTO(StateCodeDTO.Success) call.respondStateCode(StateCode.Success)
} catch (e: NoSuchElementException) { } catch (e: NoSuchElementException) {
call.respondDTO(StateCodeDTO.NoBot) call.respondStateCode(StateCode.NoBot)
} }
} }
miraiVerify<BindDTO>("/release") { miraiVerify<BindDTO>("/release") {
SessionManager.closeSession(it.sessionKey) 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 { try {
blk(this) blk(this)
} catch (e: IllegalSessionException) { } catch (e: IllegalSessionException) {
call.respondDTO(StateCodeDTO.IllegalSession) call.respondStateCode(StateCode.IllegalSession)
} catch (e: NotVerifiedSessionException) { } catch (e: NotVerifiedSessionException) {
call.respondDTO(StateCodeDTO.NotVerifySession) call.respondStateCode(StateCode.NotVerifySession)
} catch (e: NoSuchElementException) { } catch (e: NoSuchElementException) {
call.respondDTO(StateCodeDTO.NoElement) call.respondStateCode(StateCode.NoElement)
} catch (e: IllegalAccessException) { } catch (e: IllegalAccessException) {
call.respondDTO(StateCodeDTO.IllegalAccess(e.message), HttpStatusCode.BadRequest) call.respondStateCode(StateCode(400, e.message), HttpStatusCode.BadRequest)
} }
} }
/* /*
extend function 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) internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(dto: T, status: HttpStatusCode = HttpStatusCode.OK)
= respondJson(dto.toJson(), status) = respondJson(dto.toJson(), status)

View File

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