Commit 753f3261 authored by Him188's avatar Him188

Review: misc improvements

parent 1c95434e
...@@ -20,12 +20,14 @@ import kotlinx.coroutines.CoroutineScope ...@@ -20,12 +20,14 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.mamoe.mirai.api.http.route.mirai import net.mamoe.mirai.api.http.route.mirai
import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.MiraiLogger
import org.slf4j.helpers.NOPLoggerFactory import org.slf4j.helpers.NOPLoggerFactory
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
object MiraiHttpAPIServer : CoroutineScope { object MiraiHttpAPIServer : CoroutineScope {
var logger = DefaultLogger("Mirai HTTP API") var logger: MiraiLogger = DefaultLogger("Mirai HTTP API")
override val coroutineContext: CoroutineContext = override val coroutineContext: CoroutineContext =
CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
......
...@@ -74,17 +74,17 @@ internal object SessionManager { ...@@ -74,17 +74,17 @@ internal object SessionManager {
/** /**
* @author NaturalHG * 管理不同 Client 与 Mirai HTTP 的会话
* 这个用于管理不同Client与Mirai HTTP的会话
* *
* [Session]均为内部操作用类 * [Session] 均为内部操作用类
* 需使用[SessionManager] * @see [SessionManager]
* @author NaturalHG
*/ */
abstract class Session internal constructor( internal abstract class Session internal constructor(
coroutineContext: CoroutineContext, coroutineContext: CoroutineContext,
val key: String = generateSessionKey() val key: String = generateSessionKey()
) : CoroutineScope { ) : CoroutineScope {
val supervisorJob = SupervisorJob(coroutineContext[Job]) private val supervisorJob = SupervisorJob(coroutineContext[Job])
final override val coroutineContext: CoroutineContext = supervisorJob + coroutineContext final override val coroutineContext: CoroutineContext = supervisorJob + coroutineContext
internal open fun close() { internal open fun close() {
...@@ -98,13 +98,13 @@ abstract class Session internal constructor( ...@@ -98,13 +98,13 @@ abstract class Session internal constructor(
* *
* TempSession在建立180s内没有转变为[AuthedSession]应被清除 * TempSession在建立180s内没有转变为[AuthedSession]应被清除
*/ */
class TempSession internal constructor(coroutineContext: CoroutineContext) : Session(coroutineContext) internal class TempSession internal constructor(coroutineContext: CoroutineContext) : Session(coroutineContext)
/** /**
* 任何[TempSession]认证后转化为一个[AuthedSession] * 任何[TempSession]认证后转化为一个[AuthedSession]
* 在这一步[AuthedSession]应该已经有assigned的bot * 在这一步[AuthedSession]应该已经有assigned的bot
*/ */
class AuthedSession internal constructor(val bot: Bot, originKey: String, coroutineContext: CoroutineContext) : Session(coroutineContext, originKey) { internal class AuthedSession internal constructor(val bot: Bot, originKey: String, coroutineContext: CoroutineContext) : Session(coroutineContext, originKey) {
companion object { companion object {
const val CHECK_TIME = 1800L // 1800s aka 30min const val CHECK_TIME = 1800L // 1800s aka 30min
......
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.api.http.data package net.mamoe.mirai.api.http.data
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
......
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.api.http.data.common package net.mamoe.mirai.api.http.data.common
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import net.mamoe.mirai.contact.MemberPermission import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.MessagePacket import net.mamoe.mirai.message.MessagePacket
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
......
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.api.http.data.common package net.mamoe.mirai.api.http.data.common
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
...@@ -13,7 +22,7 @@ data class AuthDTO(val authKey: String) : DTO ...@@ -13,7 +22,7 @@ data class AuthDTO(val authKey: String) : DTO
abstract class VerifyDTO : DTO { abstract class VerifyDTO : DTO {
abstract val sessionKey: String abstract val sessionKey: String
@Transient @Transient
lateinit var session: AuthedSession // 反序列化验证成功后传入 internal lateinit var session: AuthedSession // 反序列化验证成功后传入
} }
@Serializable @Serializable
......
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.api.http.data package net.mamoe.mirai.api.http.data
/** /**
......
...@@ -37,11 +37,7 @@ import net.mamoe.mirai.api.http.data.common.VerifyDTO ...@@ -37,11 +37,7 @@ import net.mamoe.mirai.api.http.data.common.VerifyDTO
import net.mamoe.mirai.api.http.util.jsonParseOrNull import net.mamoe.mirai.api.http.util.jsonParseOrNull
import net.mamoe.mirai.api.http.util.toJson import net.mamoe.mirai.api.http.util.toJson
import net.mamoe.mirai.contact.PermissionDeniedException import net.mamoe.mirai.contact.PermissionDeniedException
import org.slf4j.Logger
import org.slf4j.helpers.NOPLogger
import org.slf4j.helpers.NOPLoggerFactory import org.slf4j.helpers.NOPLoggerFactory
import org.slf4j.impl.SimpleLogger
import org.slf4j.impl.SimpleLoggerFactory
fun Application.mirai() { fun Application.mirai() {
install(DefaultHeaders) install(DefaultHeaders)
...@@ -86,7 +82,7 @@ internal fun Route.miraiGet( ...@@ -86,7 +82,7 @@ internal fun Route.miraiGet(
val sessionKey = call.parameters["sessionKey"] ?: throw IllegalParamException("参数格式错误") val sessionKey = call.parameters["sessionKey"] ?: throw IllegalParamException("参数格式错误")
if (!SessionManager.containSession(sessionKey)) throw IllegalSessionException if (!SessionManager.containSession(sessionKey)) throw IllegalSessionException
when(val session = SessionManager[sessionKey]) { when (val session = SessionManager[sessionKey]) {
is TempSession -> throw NotVerifiedSessionException is TempSession -> throw NotVerifiedSessionException
is AuthedSession -> this.body(session) is AuthedSession -> this.body(session)
} }
...@@ -151,9 +147,11 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni ...@@ -151,9 +147,11 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
/* /*
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 : 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) internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(dto: T, status: HttpStatusCode = HttpStatusCode.OK) =
respondJson(dto.toJson(), status)
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)
......
...@@ -96,7 +96,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull( ...@@ -96,7 +96,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
/** /**
* 异步监听这个事件, 并尝试从这个事件中获取一个值. * 异步监听这个事件, 并尝试从这个事件中获取一个值.
* *
* 若 [filter] 抛出了一个异常, [Deferred.await] 会抛出这个异常或. * 若 [filter] 抛出的异常将会被传递给 [Deferred.await] 抛出.
* *
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制 * @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
* @param coroutineContext 额外的 [CoroutineContext] * @param coroutineContext 额外的 [CoroutineContext]
......
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