Commit 878d2ab8 authored by ryoii's avatar ryoii

fix serialize and rename

parent 4b9900e0
...@@ -21,7 +21,7 @@ inline fun <reified T : Any> String.jsonParseOrNull( ...@@ -21,7 +21,7 @@ inline fun <reified T : Any> String.jsonParseOrNull(
serializer: DeserializationStrategy<T>? = null serializer: DeserializationStrategy<T>? = null
): T? = try { ): T? = try {
if(serializer == null) MiraiJson.json.parse(this) else MiraiJson.json.parse(serializer, this) 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 ...@@ -7,7 +7,8 @@ import net.mamoe.mirai.api.http.AuthedSession
@Serializable @Serializable
abstract class VerifyDTO : DTO { abstract class VerifyDTO : DTO {
abstract val sessionKey: String abstract val sessionKey: String
@Transient lateinit var session: AuthedSession // 反序列化验证后传入 @Transient
lateinit var session: AuthedSession // 反序列化验证后传入
} }
@Serializable @Serializable
...@@ -16,15 +17,20 @@ data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO() ...@@ -16,15 +17,20 @@ data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO()
// 写成data class并继承DTO接口是为了返回时的形式统一 // 写成data class并继承DTO接口是为了返回时的形式统一
@Serializable @Serializable
open class StateCodeDTO(val code: Int, val msg: String) : DTO { sealed class StateCodeDTO(val code: Int, var msg: String) : DTO {
companion object { object Success : StateCodeDTO(0, "success") // 成功
val SUCCESS = StateCodeDTO(0, "success") // 成功 // object AUTH_WRONG : CodeDTO(1) // AuthKey错误, @see AuthResDTO
// val AUTH_WRONG = CodeDTO(1) // AuthKey错误, @see AuthResDTO object NoBot : StateCodeDTO(2, "指定Bot不存在")
val NO_BOT = StateCodeDTO(2, "指定Bot不存在") object IllegalSession : StateCodeDTO(3, "Session失效或不存在")
val ILLEGAL_SESSION = StateCodeDTO(3, "Session失效或不存在") object NotVerifySession : StateCodeDTO(3, "Session未认证")
val NOT_VERIFIED_SESSION = 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 @Serializable
......
...@@ -27,15 +27,15 @@ fun Application.authModule() { ...@@ -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.respondDTO(StateCodeDTO.Success)
} catch (e: NoSuchElementException) { } catch (e: NoSuchElementException) {
call.respondDTO(StateCodeDTO.NO_BOT) call.respondDTO(StateCodeDTO.NoBot)
} }
} }
miraiVerify<BindDTO>("/release") { miraiVerify<BindDTO>("/release") {
SessionManager.closeSession(it.sessionKey) 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 ...@@ -105,11 +105,11 @@ 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.ILLEGAL_SESSION) call.respondDTO(StateCodeDTO.IllegalSession)
} catch (e: NotVerifiedSessionException) { } catch (e: NotVerifiedSessionException) {
call.respondDTO(StateCodeDTO.NOT_VERIFIED_SESSION) call.respondDTO(StateCodeDTO.NotVerifySession)
} catch (e: IllegalAccessException) { } 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, ...@@ -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) = internal suspend fun ApplicationCall.respondJson(json: String, status: HttpStatusCode = HttpStatusCode.OK) =
respondText(json, defaultTextContentType(ContentType("application", "json")), status) 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 ...@@ -3,8 +3,6 @@ package net.mamoe.mirai.api.http.route
import io.ktor.application.Application import io.ktor.application.Application
import io.ktor.application.call import io.ktor.application.call
import io.ktor.routing.routing 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.* import net.mamoe.mirai.api.http.dto.*
fun Application.messageModule() { fun Application.messageModule() {
...@@ -20,12 +18,12 @@ fun Application.messageModule() { ...@@ -20,12 +18,12 @@ fun Application.messageModule() {
miraiVerify<SendDTO>("/sendFriendMessage") { miraiVerify<SendDTO>("/sendFriendMessage") {
it.session.bot.getQQ(it.target).sendMessage(it.messageChain.toMessageChain()) it.session.bot.getQQ(it.target).sendMessage(it.messageChain.toMessageChain())
call.respondDTO(StateCodeDTO.SUCCESS) call.respondDTO(StateCodeDTO.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.respondDTO(StateCodeDTO.Success)
} }
miraiVerify<VerifyDTO>("/event/message") { miraiVerify<VerifyDTO>("/event/message") {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment