Ver código fonte

Enable http action with ws session

ryoii 3 anos atrás
pai
commit
23e297989e

+ 1 - 9
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt

@@ -20,12 +20,9 @@ import kotlinx.serialization.InternalSerializationApi
 import kotlinx.serialization.serializer
 import net.mamoe.mirai.api.http.adapter.common.IllegalParamException
 import net.mamoe.mirai.api.http.adapter.common.IllegalSessionException
-import net.mamoe.mirai.api.http.adapter.common.NotVerifiedSessionException
 import net.mamoe.mirai.api.http.adapter.common.StateCode
 import net.mamoe.mirai.api.http.adapter.http.feature.auth.Authorization.headerSession
 import net.mamoe.mirai.api.http.adapter.http.feature.handler.HttpRouterAccessHandler.Feature.bodyContent
-import net.mamoe.mirai.api.http.adapter.http.session.asHttpSession
-import net.mamoe.mirai.api.http.adapter.http.session.isHttpSession
 import net.mamoe.mirai.api.http.adapter.http.util.KtorParameterFormat
 import net.mamoe.mirai.api.http.adapter.internal.consts.Paths
 import net.mamoe.mirai.api.http.adapter.internal.dto.AuthedDTO
@@ -142,13 +139,8 @@ internal inline fun Route.httpAuthedMultiPart(
  * 获取 session 并进行类型校验
  */
 private fun PipelineContext<*, ApplicationCall>.getAuthedSession(sessionKey: String): Session {
-    val session = headerSession ?: MahContextHolder[sessionKey]
+    return headerSession ?: MahContextHolder[sessionKey]
         ?: throw IllegalSessionException
-    return when {
-        session.isAuthed && session.isHttpSession() -> session
-        session.isAuthed -> session.asHttpSession()
-        else -> throw NotVerifiedSessionException
-    }
 }
 
 /**

+ 5 - 1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/verify.kt

@@ -14,6 +14,7 @@ import io.ktor.routing.*
 import net.mamoe.mirai.api.http.adapter.common.IllegalSessionException
 import net.mamoe.mirai.api.http.adapter.common.StateCode
 import net.mamoe.mirai.api.http.adapter.http.session.asHttpSession
+import net.mamoe.mirai.api.http.adapter.http.session.unloadHttpSession
 import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyRetDTO
 import net.mamoe.mirai.api.http.context.MahContextHolder
 import net.mamoe.mirai.api.http.util.getBotOrThrow
@@ -70,7 +71,10 @@ internal fun Application.authRouter() = routing {
         val bot = getBotOrThrow(it.qq)
         val session = MahContextHolder[it.sessionKey] ?: throw IllegalSessionException
         if (bot.id == session.bot.id) {
-            session.close()
+            session.apply {
+                unloadHttpSession()
+                close()
+            }
             call.respondStateCode(StateCode.Success)
         } else {
             throw NoSuchElementException()

+ 5 - 0
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/session/HttpAuthedSession.kt

@@ -32,6 +32,11 @@ internal fun Session.asHttpSession(): Session {
     return this
 }
 
+internal fun Session.unloadHttpSession(): Session {
+    removeExtElement(UnreadQueueKey)
+    return this
+}
+
 internal fun Session.isHttpSession(): Boolean {
     return getExtElement(UnreadQueueKey) != null
 }

+ 8 - 2
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/context/session/session.kt

@@ -12,6 +12,7 @@ package net.mamoe.mirai.api.http.context.session
 import kotlinx.atomicfu.atomic
 import kotlinx.coroutines.*
 import net.mamoe.mirai.Bot
+import net.mamoe.mirai.api.http.adapter.common.NotVerifiedSessionException
 import net.mamoe.mirai.api.http.context.session.manager.SessionManager
 import net.mamoe.mirai.api.http.spi.persistence.Persistence
 import net.mamoe.mirai.event.Listener
@@ -35,8 +36,8 @@ class StandardSession constructor(
     private var _closed = false
     private var _closing = false
 
-    override val bot: Bot get() = if (isAuthed) _bot else throw RuntimeException("Session is not authed")
-    override val sourceCache: Persistence get() = if (isAuthed) _cache else throw RuntimeException("Session is not authed")
+    override val bot: Bot get() = if (isAuthed) _bot else throw NotVerifiedSessionException
+    override val sourceCache: Persistence get() = if (isAuthed) _cache else throw NotVerifiedSessionException
     override val isAuthed get() = _isAuthed
     override val isClosed get() = _closed
 
@@ -148,6 +149,10 @@ abstract class AbstractSession : Session {
     override fun <T> putExtElement(key: Session.ExtKey<T>, element: T) {
         extElement[key] = element
     }
+
+    override fun removeExtElement(key: Session.ExtKey<out Any>) {
+        extElement.remove(key)
+    }
 }
 
 /**
@@ -185,6 +190,7 @@ interface Session : CoroutineScope {
 
     fun <T> getExtElement(key: ExtKey<T>): T?
     fun <T> putExtElement(key: ExtKey<T>, element: T)
+    fun removeExtElement(key: ExtKey<out Any>)
 
     interface ExtKey<T>
 }

+ 4 - 6
mirai-api-http/src/test/kotlin/net/mamoe/mirai/api/http/request/http/HttpAuthTest.kt

@@ -9,11 +9,6 @@
 
 package net.mamoe.mirai.api.http.request.http
 
-import test.core.annotation.ExtendWith
-import test.core.extenssion.SetupBotMock
-import kotlinx.coroutines.runBlocking
-import test.core.mock.BotMockStub
-import test.core.mock.withSession
 import net.mamoe.mirai.api.http.adapter.common.StateCode
 import net.mamoe.mirai.api.http.adapter.internal.consts.Paths
 import net.mamoe.mirai.api.http.adapter.internal.dto.*
@@ -22,7 +17,10 @@ import net.mamoe.mirai.api.http.adapter.internal.serializer.toJson
 import net.mamoe.mirai.api.http.context.MahContext
 import net.mamoe.mirai.api.http.request.env.AdapterOperation
 import net.mamoe.mirai.api.http.request.env.startAdapter
-import kotlin.test.BeforeTest
+import test.core.annotation.ExtendWith
+import test.core.extenssion.SetupBotMock
+import test.core.mock.BotMockStub
+import test.core.mock.withSession
 import kotlin.test.Test
 import kotlin.test.assertEquals
 import kotlin.test.assertNotNull