Merge remote-tracking branch 'origin/master'

This commit is contained in:
ryoii 2020-02-26 15:11:43 +08:00
commit 27b44563ad
13 changed files with 96 additions and 30 deletions

View File

@ -3,7 +3,7 @@ kotlin.code.style=official
# config # config
mirai_version=0.22.0 mirai_version=0.22.0
mirai_japt_version=1.1.0 mirai_japt_version=1.1.0
mirai_console_version=0.1.1 mirai_console_version=0.2.0
kotlin.incremental.multiplatform=true kotlin.incremental.multiplatform=true
kotlin.parallel.tasks.in.project=true kotlin.parallel.tasks.in.project=true
# kotlin # kotlin

View File

@ -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) }

View File

@ -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(
* *
* 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

View File

@ -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 package net.mamoe.mirai.api.http.data
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable

View File

@ -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 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

View File

@ -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 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
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

View File

@ -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 package net.mamoe.mirai.api.http.data
/** /**

View File

@ -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)
@ -153,9 +149,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)

View File

@ -102,12 +102,6 @@ abstract class Bot : CoroutineScope {
*/ */
abstract val qqs: ContactList<QQ> abstract val qqs: ContactList<QQ>
/**
* 获取一个好友对象. 若没有这个好友, 则会抛出异常 [NoSuchElementException]
*/
@Deprecated(message = "这个函数有歧义. 它获取的是好友, 却名为 getQQ", replaceWith = ReplaceWith("getFriend(id)"))
fun getQQ(id: Long): QQ = getFriend(id)
/** /**
* 获取一个好友或一个群. * 获取一个好友或一个群.
* 在一些情况下这可能会造成歧义. 请考虑后使用. * 在一些情况下这可能会造成歧义. 请考虑后使用.

View File

@ -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]

View File

@ -249,6 +249,36 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
operator fun invoke(onEvent: MessageListener<T>): Listener<T> { operator fun invoke(onEvent: MessageListener<T>): Listener<T> {
return content(filter, onEvent) return content(filter, onEvent)
} }
infix fun reply(toReply: String): Listener<T> {
return content(filter) { reply(toReply) }
}
infix fun reply(message: Message): Listener<T> {
return content(filter) { reply(message) }
}
infix fun reply(replier: (@MessageDsl suspend T.(String) -> Any?)): Listener<T> {
return content(filter) {
@Suppress("DSL_SCOPE_VIOLATION_WARNING")
executeAndReply(replier)
}
}
infix fun quoteReply(toReply: String): Listener<T> {
return content(filter) { quoteReply(toReply) }
}
infix fun quoteReply(message: Message): Listener<T> {
return content(filter) { quoteReply(message) }
}
infix fun quoteReply(replier: (@MessageDsl suspend T.(String) -> Any?)): Listener<T> {
return content(filter) {
@Suppress("DSL_SCOPE_VIOLATION_WARNING")
executeAndQuoteReply(replier)
}
}
} }
/** /**
@ -698,6 +728,17 @@ class MessageSubscribersBuilder<T : MessagePacket<*, *>>(
} }
} }
@PublishedApi
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") // false positive
internal suspend inline fun T.executeAndQuoteReply(replier: suspend T.(String) -> Any?) {
when (val message = replier(this, this.message.toString())) {
is Message -> this.quoteReply(message)
is Unit -> {
}
else -> this.quoteReply(message.toString())
}
}
/* 易产生迷惑感 /* 易产生迷惑感
fun replyCase(equals: String, trim: Boolean = true, replier: MessageReplier<T>) = case(equals, trim) { reply(replier(this)) } fun replyCase(equals: String, trim: Boolean = true, replier: MessageReplier<T>) = case(equals, trim) { reply(replier(this)) }
fun replyContains(value: String, replier: MessageReplier<T>) = content({ value in it }) { replier(this) } fun replyContains(value: String, replier: MessageReplier<T>) = content({ value in it }) { replier(this) }