From 37cea58a1929d620d3be622f88d3502a9a7d1ec9 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 20 Feb 2020 13:18:59 +0800 Subject: [PATCH 1/3] Fix ambiguous `eq` --- .../commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt index 4234275ce..7614f5d4c 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt @@ -57,7 +57,7 @@ interface Message { */ interface Key - infix fun eq(other: Message): Boolean = this == other + infix fun eq(other: Message): Boolean = this.toString() == other.toString() /** * 将 [toString] 与 [other] 比较 From f5a6040668cc3892cdccc14cabfb1336e23582f8 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 20 Feb 2020 13:20:36 +0800 Subject: [PATCH 2/3] Add additional `coroutineScope` parameter --- .../net.mamoe.mirai/event/Subscribers.kt | 55 +++++++++++++++---- .../event/internal/InternalEventListeners.kt | 8 ++- .../mirai/event/internal/EventInternalJvm.kt | 5 +- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt index c42584f55..eeca3a407 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt @@ -22,6 +22,7 @@ import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext import kotlin.jvm.JvmName /* @@ -98,6 +99,8 @@ interface Listener : CompletableJob { * * **注意:** 事件处理是 `suspend` 的, 请规范处理 JVM 阻塞方法. * + * @param coroutineContext 给事件监听协程的额外的 [CoroutineContext] + * * @see subscribeAlways 一直监听 * @see subscribeOnce 只监听一次 * @@ -106,8 +109,11 @@ interface Listener : CompletableJob { * @see subscribeFriendMessages 监听好友消息 DSL */ @UseExperimental(MiraiInternalAPI::class) -inline fun CoroutineScope.subscribe(noinline handler: suspend E.(E) -> ListeningStatus): Listener = - E::class.subscribeInternal(Handler { it.handler(it); }) +inline fun CoroutineScope.subscribe( + coroutineContext: CoroutineContext = EmptyCoroutineContext, + noinline handler: suspend E.(E) -> ListeningStatus +): Listener = + E::class.subscribeInternal(Handler(coroutineContext) { it.handler(it); }) /** * 在指定的 [CoroutineScope] 下订阅所有 [E] 及其子类事件. @@ -116,14 +122,19 @@ inline fun CoroutineScope.subscribe(noinline handler: suspen * 可在任意时候通过 [Listener.complete] 来主动停止监听. * [Bot] 被关闭后事件监听会被 [取消][Listener.cancel]. * + * @param coroutineContext 给事件监听协程的额外的 [CoroutineContext] + * * @see subscribe 获取更多说明 */ @UseExperimental(MiraiInternalAPI::class, ExperimentalContracts::class) -inline fun CoroutineScope.subscribeAlways(noinline listener: suspend E.(E) -> Unit): Listener { +inline fun CoroutineScope.subscribeAlways( + coroutineContext: CoroutineContext = EmptyCoroutineContext, + noinline listener: suspend E.(E) -> Unit +): Listener { contract { callsInPlace(listener, InvocationKind.UNKNOWN) } - return E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.LISTENING }) + return E::class.subscribeInternal(Handler(coroutineContext) { it.listener(it); ListeningStatus.LISTENING }) } /** @@ -133,11 +144,16 @@ inline fun CoroutineScope.subscribeAlways(noinline listener: * 可在任意时候通过 [Listener.complete] 来主动停止监听. * [Bot] 被关闭后事件监听会被 [取消][Listener.cancel]. * + * @param coroutineContext 给事件监听协程的额外的 [CoroutineContext] + * * @see subscribe 获取更多说明 */ @UseExperimental(MiraiInternalAPI::class) -inline fun CoroutineScope.subscribeOnce(noinline listener: suspend E.(E) -> Unit): Listener = - E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.STOPPED }) +inline fun CoroutineScope.subscribeOnce( + coroutineContext: CoroutineContext = EmptyCoroutineContext, + noinline listener: suspend E.(E) -> Unit +): Listener = + E::class.subscribeInternal(Handler(coroutineContext) { it.listener(it); ListeningStatus.STOPPED }) // @@ -153,12 +169,17 @@ inline fun CoroutineScope.subscribeOnce(noinline listener: s * 可在任意时候通过 [Listener.complete] 来主动停止监听. * [Bot] 被关闭后事件监听会被 [取消][Listener.cancel]. * + * @param coroutineContext 给事件监听协程的额外的 [CoroutineContext] + * * @see subscribe 获取更多说明 */ @JvmName("subscribeAlwaysForBot") @UseExperimental(MiraiInternalAPI::class) -inline fun Bot.subscribe(noinline handler: suspend E.(E) -> ListeningStatus): Listener = - E::class.subscribeInternal(Handler { if (it.bot === this) it.handler(it) else ListeningStatus.LISTENING }) +inline fun Bot.subscribe( + coroutineContext: CoroutineContext = EmptyCoroutineContext, + noinline handler: suspend E.(E) -> ListeningStatus +): Listener = + E::class.subscribeInternal(Handler(coroutineContext) { if (it.bot === this) it.handler(it) else ListeningStatus.LISTENING }) /** @@ -168,12 +189,17 @@ inline fun Bot.subscribe(noinline handler: suspend E.(E) * 可在任意时候通过 [Listener.complete] 来主动停止监听. * [Bot] 被关闭后事件监听会被 [取消][Listener.cancel]. * + * @param coroutineContext 给事件监听协程的额外的 [CoroutineContext] + * * @see subscribe 获取更多说明 */ @JvmName("subscribeAlwaysForBot1") @UseExperimental(MiraiInternalAPI::class) -inline fun Bot.subscribeAlways(noinline listener: suspend E.(E) -> Unit): Listener { - return E::class.subscribeInternal(Handler { if (it.bot === this) it.listener(it); ListeningStatus.LISTENING }) +inline fun Bot.subscribeAlways( + coroutineContext: CoroutineContext = EmptyCoroutineContext, + noinline listener: suspend E.(E) -> Unit +): Listener { + return E::class.subscribeInternal(Handler(coroutineContext) { if (it.bot === this) it.listener(it); ListeningStatus.LISTENING }) } /** @@ -183,12 +209,17 @@ inline fun Bot.subscribeAlways(noinline listener: suspend * 可在任意时候通过 [Listener.complete] 来主动停止监听. * [Bot] 被关闭后事件监听会被 [取消][Listener.cancel]. * + * @param coroutineContext 给事件监听协程的额外的 [CoroutineContext] + * * @see subscribe 获取更多说明 */ @JvmName("subscribeOnceForBot2") @UseExperimental(MiraiInternalAPI::class) -inline fun Bot.subscribeOnce(noinline listener: suspend E.(E) -> Unit): Listener = - E::class.subscribeInternal(Handler { +inline fun Bot.subscribeOnce( + coroutineContext: CoroutineContext = EmptyCoroutineContext, + noinline listener: suspend E.(E) -> Unit +): Listener = + E::class.subscribeInternal(Handler(coroutineContext) { if (it.bot === this) { it.listener(it) ListeningStatus.STOPPED diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt index fe2ff0f8c..fb604edaa 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt @@ -31,8 +31,12 @@ fun , E : Event> KClass.subscribeInternal(listener: L): L @PublishedApi @Suppress("FunctionName") -internal fun CoroutineScope.Handler(handler: suspend (E) -> ListeningStatus): Handler { - return Handler(coroutineContext[Job], coroutineContext, handler) +internal fun CoroutineScope.Handler( + coroutineContext: CoroutineContext, + handler: suspend (E) -> ListeningStatus +): Handler { + val context = this.newCoroutineContext(coroutineContext) + return Handler(context[Job], context, handler) } private inline fun inline(block: () -> Unit) = block() diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt index ccc2a718f..ddae0cdd2 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt @@ -16,15 +16,16 @@ import net.mamoe.mirai.event.ListeningStatus import net.mamoe.mirai.utils.MiraiInternalAPI import java.util.function.Consumer import java.util.function.Function +import kotlin.coroutines.EmptyCoroutineContext @MiraiInternalAPI @Suppress("FunctionName") fun Class._subscribeEventForJaptOnly(scope: CoroutineScope, onEvent: Function): Listener { - return this.kotlin.subscribeInternal(scope.Handler { onEvent.apply(it) }) + return this.kotlin.subscribeInternal(scope.Handler(EmptyCoroutineContext) { onEvent.apply(it) }) } @MiraiInternalAPI @Suppress("FunctionName") fun Class._subscribeEventForJaptOnly(scope: CoroutineScope, onEvent: Consumer): Listener { - return this.kotlin.subscribeInternal(scope.Handler { onEvent.accept(it); ListeningStatus.LISTENING; }) + return this.kotlin.subscribeInternal(scope.Handler(EmptyCoroutineContext) { onEvent.accept(it); ListeningStatus.LISTENING; }) } \ No newline at end of file From 3d4ff807af89edb0fa9a9518600f02172d2027ba Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 20 Feb 2020 13:24:27 +0800 Subject: [PATCH 3/3] Add `MessageSource.time` --- .../mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt | 2 ++ .../kotlin/net.mamoe.mirai/message/data/MessageSource.kt | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt index c8e77f7d3..fe4aab43f 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt @@ -21,6 +21,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.data.proto.SourceMsg internal inline class MessageSourceFromServer( val delegate: ImMsgBody.SourceMsg ) : MessageSource { + override val time: Long get() = delegate.time.toLong() and 0xFFFFFFFF override val messageUid: Long get() = delegate.pbReserve.loadAs(SourceMsg.ResvAttr.serializer()).origUids!! override val sourceMessage: MessageChain get() = delegate.toMessageChain() override val senderId: Long get() = delegate.senderUin @@ -32,6 +33,7 @@ internal inline class MessageSourceFromServer( internal inline class MessageSourceFromMsg( val delegate: MsgComm.Msg ) : MessageSource { + override val time: Long get() = delegate.msgHead.msgTime.toLong() and 0xFFFFFFFF override val messageUid: Long get() = delegate.msgBody.richText.attr!!.random.toLong() override val sourceMessage: MessageChain get() = delegate.toMessageChain() override val senderId: Long get() = delegate.msgHead.fromUin diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt index 3653cfabb..e5b831b17 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt @@ -31,6 +31,11 @@ interface MessageSource : Message { */ val messageUid: Long + /** + * 发送时间, 单位为秒 + */ + val time: Long + /** * 发送人号码 */