mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-13 14:50:43 +08:00
Review: misc improvements
This commit is contained in:
parent
1c95434e65
commit
753f3261e0
@ -20,12 +20,14 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import net.mamoe.mirai.api.http.route.mirai
|
||||
import net.mamoe.mirai.utils.DefaultLogger
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import org.slf4j.helpers.NOPLoggerFactory
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
object MiraiHttpAPIServer : CoroutineScope {
|
||||
|
||||
var logger = DefaultLogger("Mirai HTTP API")
|
||||
var logger: MiraiLogger = DefaultLogger("Mirai HTTP API")
|
||||
|
||||
override val coroutineContext: CoroutineContext =
|
||||
CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
|
||||
|
||||
|
@ -74,17 +74,17 @@ internal object SessionManager {
|
||||
|
||||
|
||||
/**
|
||||
* @author NaturalHG
|
||||
* 这个用于管理不同Client与Mirai HTTP的会话
|
||||
* 管理不同 Client 与 Mirai HTTP 的会话
|
||||
*
|
||||
* [Session]均为内部操作用类
|
||||
* 需使用[SessionManager]
|
||||
* [Session] 均为内部操作用类
|
||||
* @see [SessionManager]
|
||||
* @author NaturalHG
|
||||
*/
|
||||
abstract class Session internal constructor(
|
||||
internal abstract class Session internal constructor(
|
||||
coroutineContext: CoroutineContext,
|
||||
val key: String = generateSessionKey()
|
||||
) : CoroutineScope {
|
||||
val supervisorJob = SupervisorJob(coroutineContext[Job])
|
||||
private val supervisorJob = SupervisorJob(coroutineContext[Job])
|
||||
final override val coroutineContext: CoroutineContext = supervisorJob + coroutineContext
|
||||
|
||||
internal open fun close() {
|
||||
@ -98,13 +98,13 @@ abstract class Session internal constructor(
|
||||
*
|
||||
* TempSession在建立180s内没有转变为[AuthedSession]应被清除
|
||||
*/
|
||||
class TempSession internal constructor(coroutineContext: CoroutineContext) : Session(coroutineContext)
|
||||
internal class TempSession internal constructor(coroutineContext: CoroutineContext) : Session(coroutineContext)
|
||||
|
||||
/**
|
||||
* 任何[TempSession]认证后转化为一个[AuthedSession]
|
||||
* 在这一步[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 {
|
||||
const val CHECK_TIME = 1800L // 1800s aka 30min
|
||||
|
@ -1,3 +1,12 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
@ -1,9 +1,17 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.mamoe.mirai.contact.MemberPermission
|
||||
import net.mamoe.mirai.event.events.BotEvent
|
||||
import net.mamoe.mirai.event.events.*
|
||||
import net.mamoe.mirai.message.MessagePacket
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
||||
|
@ -1,3 +1,12 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
@ -13,7 +22,7 @@ data class AuthDTO(val authKey: String) : DTO
|
||||
abstract class VerifyDTO : DTO {
|
||||
abstract val sessionKey: String
|
||||
@Transient
|
||||
lateinit var session: AuthedSession // 反序列化验证成功后传入
|
||||
internal lateinit var session: AuthedSession // 反序列化验证成功后传入
|
||||
}
|
||||
|
||||
@Serializable
|
||||
|
@ -1,3 +1,12 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
/**
|
@ -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.toJson
|
||||
import net.mamoe.mirai.contact.PermissionDeniedException
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.helpers.NOPLogger
|
||||
import org.slf4j.helpers.NOPLoggerFactory
|
||||
import org.slf4j.impl.SimpleLogger
|
||||
import org.slf4j.impl.SimpleLoggerFactory
|
||||
|
||||
fun Application.mirai() {
|
||||
install(DefaultHeaders)
|
||||
@ -86,7 +82,7 @@ internal fun Route.miraiGet(
|
||||
val sessionKey = call.parameters["sessionKey"] ?: throw IllegalParamException("参数格式错误")
|
||||
if (!SessionManager.containSession(sessionKey)) throw IllegalSessionException
|
||||
|
||||
when(val session = SessionManager[sessionKey]) {
|
||||
when (val session = SessionManager[sessionKey]) {
|
||||
is TempSession -> throw NotVerifiedSessionException
|
||||
is AuthedSession -> this.body(session)
|
||||
}
|
||||
@ -151,9 +147,11 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
|
||||
/*
|
||||
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) =
|
||||
respondText(json, defaultTextContentType(ContentType("application", "json")), status)
|
||||
|
File diff suppressed because one or more lines are too long
@ -96,7 +96,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
|
||||
/**
|
||||
* 异步监听这个事件, 并尝试从这个事件中获取一个值.
|
||||
*
|
||||
* 若 [filter] 抛出了一个异常, [Deferred.await] 会抛出这个异常或.
|
||||
* 若 [filter] 抛出的异常将会被传递给 [Deferred.await] 抛出.
|
||||
*
|
||||
* @param timeoutMillis 超时. 单位为毫秒. `-1` 为不限制
|
||||
* @param coroutineContext 额外的 [CoroutineContext]
|
||||
|
Loading…
Reference in New Issue
Block a user