[core] fix up 2e4cab43: Rename MiraiInternalMessageFlag to SkipEventBroadcast; Do not broadcast pre- and post-message events for FileMessages that sent internally. #2195

full rev: 2e4cab4365
This commit is contained in:
Him188 2022-08-16 11:31:40 +08:00
parent 9dc25bb1f3
commit 7ff61becee
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
5 changed files with 27 additions and 20 deletions

View File

@ -18,7 +18,7 @@ import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.message.contextualBugReportException
import net.mamoe.mirai.internal.message.flags.MiraiInternalMessageFlag
import net.mamoe.mirai.internal.message.flags.SkipEventBroadcast
import net.mamoe.mirai.internal.message.image.*
import net.mamoe.mirai.internal.message.protocol.MessageProtocolFacade
import net.mamoe.mirai.internal.message.protocol.outgoing.HighwayUploader
@ -252,13 +252,13 @@ internal suspend fun <C : AbstractContact> C.sendMessageImpl(
preSendEventConstructor: (C, Message) -> MessagePreSendEvent,
postSendEventConstructor: (C, MessageChain, Throwable?, MessageReceipt<C>?) -> MessagePostSendEvent<C>,
): MessageReceipt<C> {
val isMiraiInternal = if (message is MessageChain) {
message.anyIsInstance<MiraiInternalMessageFlag>()
val skipEvent = if (message is MessageChain) {
message.anyIsInstance<SkipEventBroadcast>()
} else false
require(!message.isContentEmpty()) { "message is empty" }
val chain = broadcastMessagePreSendEvent(message, isMiraiInternal, preSendEventConstructor)
val chain = broadcastMessagePreSendEvent(message, skipEvent, preSendEventConstructor)
val result = kotlin.runCatching {
MessageProtocolFacade.preprocessAndSendOutgoing(this, message, buildComponentStorage {
@ -273,7 +273,7 @@ internal suspend fun <C : AbstractContact> C.sendMessageImpl(
bot.logger.verbose("$this <- $chain".replaceMagicCodes())
}
if (!isMiraiInternal) {
if (!skipEvent) {
postSendEventConstructor(this, chain, result.exceptionOrNull(), result.getOrNull()).broadcast()
}

View File

@ -24,10 +24,10 @@ import net.mamoe.mirai.message.data.toMessageChain
*/
internal suspend fun <C : Contact> C.broadcastMessagePreSendEvent(
message: Message,
isMiraiInternal: Boolean,
skipEvent: Boolean,
eventConstructor: (C, Message) -> MessagePreSendEvent,
): MessageChain {
if (isMiraiInternal) return message.toMessageChain()
if (skipEvent) return message.toMessageChain()
var eventName: String? = null
return kotlin.runCatching {
eventConstructor(this, message).also {

View File

@ -11,6 +11,7 @@
package net.mamoe.mirai.internal.message.flags
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.internal.message.visitor.ex
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.visitor.MessageVisitor
@ -79,25 +80,28 @@ internal object IgnoreLengthCheck : MessageMetadata, ConstrainSingle, InternalFl
}
/**
* 代表来自 mirai 内部
* Skip broadcasting events. Used for [Contact.sendMessage]
*/
internal object MiraiInternalMessageFlag : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage,
AbstractMessageKey<MiraiInternalMessageFlag>({ it.safeCast() }) {
override val key: MessageKey<MiraiInternalMessageFlag> get() = this
override fun toString(): String = ""
internal sealed interface SkipEventBroadcast : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage {
override val key: MessageKey<SkipEventBroadcast> get() = Companion
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitMiraiInternalMessageFlag(this, data) ?: super<InternalFlagOnlyMessage>.accept(
return visitor.ex()?.visitSkipEventBroadcast(this, data) ?: super<InternalFlagOnlyMessage>.accept(
visitor,
data
)
}
companion object : AbstractMessageKey<SkipEventBroadcast>({ it.safeCast() }), SkipEventBroadcast {
// instance
override fun toString(): String = ""
}
}
internal object AllowSendFileMessage : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage,
AbstractMessageKey<AllowSendFileMessage>({ it.safeCast() }) {
override val key: MessageKey<AllowSendFileMessage> get() = this
override fun toString(): String = ""
internal sealed interface AllowSendFileMessage : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage,
SkipEventBroadcast {
override val key: MessageKey<AllowSendFileMessage> get() = Companion
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitAllowSendFileMessage(this, data) ?: super<InternalFlagOnlyMessage>.accept(
@ -105,4 +109,8 @@ internal object AllowSendFileMessage : MessageMetadata, ConstrainSingle, Interna
data
)
}
companion object : AbstractMessageKey<AllowSendFileMessage>({ it.safeCast() }), AllowSendFileMessage {
override fun toString(): String = ""
}
}

View File

@ -61,11 +61,11 @@ internal interface MessageVisitorEx<in D, out R> : MessageVisitor<D, R> {
return visitInternalFlagOnlyMessage(message, data)
}
fun visitMiraiInternalMessageFlag(message: MiraiInternalMessageFlag, data: D): R {
fun visitSkipEventBroadcast(message: SkipEventBroadcast, data: D): R {
return visitInternalFlagOnlyMessage(message, data)
}
fun visitAllowSendFileMessage(message: AllowSendFileMessage, data: D): R {
return visitInternalFlagOnlyMessage(message, data)
return visitSkipEventBroadcast(message, data)
}
}

View File

@ -21,7 +21,6 @@ import net.mamoe.mirai.internal.asQQAndroidBot
import net.mamoe.mirai.internal.contact.groupCode
import net.mamoe.mirai.internal.message.data.FileMessageImpl
import net.mamoe.mirai.internal.message.flags.AllowSendFileMessage
import net.mamoe.mirai.internal.message.flags.MiraiInternalMessageFlag
import net.mamoe.mirai.internal.network.highway.Highway
import net.mamoe.mirai.internal.network.highway.ResourceKind
import net.mamoe.mirai.internal.network.protocol