Add MessageVisitor, MessageVisitorEx, Message.accept and Message.acceptChildren as internal API

This commit is contained in:
Him188 2022-01-14 19:40:01 +00:00
parent c3f94a66d6
commit 3955546868
37 changed files with 624 additions and 97 deletions

View File

@ -5629,6 +5629,9 @@ public final class net/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder {
public static synthetic fun title$default (Lnet/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder;Ljava/lang/String;ILjava/lang/String;ILjava/lang/Object;)V
}
public final class net/mamoe/mirai/message/data/visitor/MessageVisitorKt {
}
public abstract class net/mamoe/mirai/network/CustomLoginFailedException : net/mamoe/mirai/network/LoginFailedException {
public fun <init> (Z)V
public fun <init> (ZLjava/lang/String;)V

View File

@ -5629,6 +5629,9 @@ public final class net/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder {
public static synthetic fun title$default (Lnet/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder;Ljava/lang/String;ILjava/lang/String;ILjava/lang/Object;)V
}
public final class net/mamoe/mirai/message/data/visitor/MessageVisitorKt {
}
public abstract class net/mamoe/mirai/network/CustomLoginFailedException : net/mamoe/mirai/network/LoginFailedException {
public fun <init> (Z)V
public fun <init> (ZLjava/lang/String;)V

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:JvmMultifileClass
@ -21,7 +21,9 @@ import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.UserOrBot
import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
/**
@ -68,6 +70,11 @@ public data class At(
}
return super<MessageContent>.followedBy(PlainText(" ")) + tail
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitAt(this, data)
}
}
/**

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:JvmMultifileClass
@ -16,7 +16,9 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
/**
* "@全体成员".
@ -68,4 +70,9 @@ public object AtAll :
}
return super<MessageContent>.followedBy(PlainText(" ")) + tail
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitAtAll(this, data)
}
}

View File

@ -20,6 +20,7 @@ import net.mamoe.mirai.contact.AudioSupported
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.MessageSerializers
import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
@ -101,6 +102,11 @@ public sealed interface Audio : MessageContent {
*/
public override fun toString(): String
public override fun contentToString(): String = "[语音消息]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitAudio(this, data)
}
}

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:OptIn(MiraiInternalApi::class)
@ -18,6 +18,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.protobuf.ProtoBuf
import kotlinx.serialization.protobuf.ProtoNumber
import net.mamoe.mirai.message.MessageSerializers
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import java.util.concurrent.ConcurrentLinkedQueue
@ -205,6 +206,11 @@ public abstract class CustomMessageMetadata : CustomMessage(), MessageMetadata {
final override fun toString(): String =
"[mirai:custom:${getFactory().typeName}:${String(customToString())}]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitCustomMessageMetadata(this, data)
}
public companion object
}

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("NOTHING_TO_INLINE")
@ -16,7 +16,9 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
import org.jetbrains.annotations.Range
import kotlin.random.Random
@ -55,6 +57,11 @@ public data class Dice(
override fun toString(): String = "[mirai:dice:$value]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitDice(this, data)
}
public companion object Key :
AbstractPolymorphicMessageKey<MarketFace, Dice>(MarketFace, { it.safeCast() }) {
public const val SERIAL_NAME: String = "Dice"

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:JvmMultifileClass
@ -16,7 +16,9 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
/**
* QQ 自带表情
@ -44,6 +46,11 @@ public data class Face(public val id: Int) : // used in delegation
override fun equals(other: Any?): Boolean = other is Face && other.id == this.id
override fun hashCode(): Int = id
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitFace(this, data)
}
//Auto generated
@Suppress("NonAsciiCharacters", "unused", "SpellCheckingInspection", "all", "ObjectPropertyName")
public companion object {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
@ -23,6 +23,7 @@ import net.mamoe.mirai.contact.file.AbsoluteFile
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.*
/**
@ -95,6 +96,11 @@ public interface FileMessage : MessageContent, ConstrainSingle, CodableMessage {
override val key: Key get() = Key
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitFileMessage(this, data)
}
/**
* 注意, baseKey [MessageContent] 不稳定. 未来可能会有变更.
*/

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("NOTHING_TO_INLINE")
@ -14,7 +14,9 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
/**
@ -60,6 +62,11 @@ public data class FlashImage(
override fun toString(): String = stringValue
override fun contentToString(): String = "[闪照]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitFlashImage(this, data)
}
public companion object Key :
AbstractPolymorphicMessageKey<HummerMessage, FlashImage>(HummerMessage, { it.safeCast() }) {
public const val SERIAL_NAME: String = "FlashImage"

View File

@ -1,28 +1,24 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("MemberVisibilityCanBePrivate", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "unused")
package net.mamoe.mirai.message.data
import io.ktor.http.*
import io.ktor.util.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.message.data.ForwardMessage.DisplayStrategy
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.currentTimeSeconds
import net.mamoe.mirai.utils.safeCast
import net.mamoe.mirai.utils.toLongUnsigned
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.*
/**
@ -118,6 +114,11 @@ public data class ForwardMessage(
override fun contentToString(): String = "[转发消息]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitForwardMessage(this, data)
}
// use data-class generated toString()

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
@ -13,7 +13,9 @@
package net.mamoe.mirai.message.data
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.castOrNull
@ -30,6 +32,11 @@ import net.mamoe.mirai.utils.castOrNull
@MiraiExperimentalApi
@NotStableForInheritance
public interface HummerMessage : MessageContent, ConstrainSingle {
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitHummerMessage(this, data)
}
public companion object Key :
AbstractPolymorphicMessageKey<MessageContent, HummerMessage>(MessageContent, { it.castOrNull() })
// has service type etc.

View File

@ -38,6 +38,7 @@ import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_1
import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_2
import net.mamoe.mirai.message.data.Image.Key.isUploaded
import net.mamoe.mirai.message.data.Image.Key.queryUrl
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
@ -166,6 +167,11 @@ public interface Image : Message, MessageContent, CodableMessage {
*/ // was an extension on Image before 2.9.0-M1.
public val md5: ByteArray get() = calculateImageMd5ByImageId(imageId)
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitImage(this, data)
}
public object AsStringSerializer : KSerializer<Image> by String.serializer().mapPrimitive(
SERIAL_NAME,
serialize = { imageId },

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
@ -9,7 +9,9 @@
package net.mamoe.mirai.message.data
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.safeCast
@ -37,6 +39,11 @@ public interface MarketFace : HummerMessage {
override fun contentToString(): String = name.ifEmpty { "[商城表情]" }
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitMarketFace(this, data)
}
public companion object Key :
AbstractPolymorphicMessageKey<HummerMessage, MarketFace>(HummerMessage, { it.safeCast() }) {
public const val SERIAL_NAME: String = "MarketFace"

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress(
@ -25,6 +25,8 @@ import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.code.MiraiCode
import net.mamoe.mirai.message.code.MiraiCode.serializeToMiraiCode
import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiInternalApi
import kotlin.internal.LowPriorityInOverloadResolution
/**
@ -273,6 +275,21 @@ public interface Message {
public operator fun plus(another: Sequence<Message>): MessageChain =
another.fold(this, Message::plus).toMessageChain()
/**
* @suppress 这是内部 API, 不要在任何情况下调用
* @since MESSAGE_VISITOR
*/
@MiraiInternalApi
public fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R = visitor.visitMessage(this, data)
/**
* @suppress 这是内部 API, 不要在任何情况下调用
* @since MESSAGE_VISITOR
*/
@MiraiInternalApi
public fun <D> acceptChildren(visitor: MessageVisitor<D, *>, data: D) {
}
public companion object
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
@ -14,7 +14,6 @@
package net.mamoe.mirai.message.data
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.serialization.*
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
@ -33,10 +32,8 @@ import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString
import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.data.MessageSource.Key.recall
import net.mamoe.mirai.message.data.MessageSource.Key.recallIn
import net.mamoe.mirai.utils.DeprecatedSinceMirai
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.safeCast
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.*
import java.util.stream.Stream
import kotlin.reflect.KProperty
import kotlin.streams.asSequence
@ -253,6 +250,14 @@ public sealed interface MessageChain :
forEach { it.safeCast<CodableMessage>()?.appendMiraiCodeTo(builder) }
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R = visitor.visitMessageChain(this, data)
@MiraiInternalApi
override fun <D> acceptChildren(visitor: MessageVisitor<D, *>, data: D) {
forEach { it.accept(visitor, data) }
}
/**
* [MessageChain] 作为 `List<SingleMessage>` 序列化. 使用 [多态序列化][Polymorphic].
*

View File

@ -15,7 +15,9 @@ import kotlinx.serialization.Polymorphic
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.IMirai
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
/**
@ -87,6 +89,8 @@ public class MessageOrigin(
return result
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R = visitor.visitMessageOrigin(this, data)
public companion object Key : AbstractMessageKey<MessageOrigin>({ it.safeCast() }) {
public const val SERIAL_NAME: String = "MessageOrigin"

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
@ -28,6 +28,7 @@ import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.action.AsyncRecallResult
import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.data.MessageSource.Key.recall
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.safeCast
@ -185,6 +186,11 @@ public sealed class MessageSource : Message, MessageMetadata, ConstrainSingle {
public final override fun toString(): String =
"[mirai:source:${ids.contentToString()},${internalIds.contentToString()}]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitMessageSource(this, data)
}
public object Serializer : MessageSourceSerializerImpl("MessageSource")
public companion object Key : AbstractMessageKey<MessageSource>({ it.safeCast() }) {

View File

@ -15,6 +15,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
@ -129,6 +130,10 @@ public data class MusicShare(
.append(']')
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitMusicShare(this, data)
}
/**
* 注意, baseKey [MessageContent] 不稳定. 未来可能会有变更.

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:JvmMultifileClass
@ -17,7 +17,9 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
/**
* 纯文本.
@ -48,6 +50,11 @@ public data class PlainText(
builder.appendStringAsMiraiCode(content)
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitPlainText(this, data)
}
public companion object {
public const val SERIAL_NAME: String = "PlainText"
}

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.message.data
@ -14,6 +14,7 @@ import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.action.Nudge
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.castOrNull
@ -57,6 +58,11 @@ public data class PokeMessage @MiraiInternalApi constructor(
//serviceType=0x00000002(2)
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitPokeMessage(this, data)
}
public companion object Key :
AbstractPolymorphicMessageKey<HummerMessage, PokeMessage>(HummerMessage, { it.castOrNull() }) {

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:JvmMultifileClass
@ -17,6 +17,8 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.data.MessageSource.Key.recall
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
@ -57,6 +59,11 @@ public data class QuoteReply(
public override fun equals(other: Any?): Boolean = other is QuoteReply && other.source == this.source
public override fun hashCode(): Int = source.hashCode()
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitQuoteReply(this, data)
}
public companion object Key : AbstractMessageKey<QuoteReply>({ it.safeCast() }) {
public const val SERIAL_NAME: String = "QuoteReply"
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
@ -17,7 +17,9 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.safeCast
import kotlin.annotation.AnnotationTarget.*
@ -48,6 +50,11 @@ public interface RichMessage : MessageContent, ConstrainSingle {
*/
public val content: String
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitRichMessage(this, data)
}
/**
* 一些模板
* @suppress API 不稳定, 可能在任意时刻被删除
@ -110,6 +117,11 @@ public data class LightApp(override val content: String) : RichMessage, CodableM
public override fun toString(): String = "[mirai:app:$content]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitLightApp(this, data)
}
@MiraiExperimentalApi
override fun appendMiraiCodeTo(builder: StringBuilder) {
builder.append("[mirai:app:").appendStringAsMiraiCode(content).append(']')
@ -148,6 +160,11 @@ public class SimpleServiceMessage(
return result
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitSimpleServiceMessage(this, data)
}
public companion object {
public const val SERIAL_NAME: String = "SimpleServiceMessage"
}
@ -175,6 +192,11 @@ public interface ServiceMessage : RichMessage, CodableMessage {
*/
public val serviceId: Int
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitServiceMessage(this, data)
}
@MiraiExperimentalApi
override fun appendMiraiCodeTo(builder: StringBuilder) {
builder.append("[mirai:service:").append(serviceId).append(',').appendStringAsMiraiCode(content).append(']')
@ -185,6 +207,11 @@ public interface ServiceMessage : RichMessage, CodableMessage {
@Serializable
public abstract class AbstractServiceMessage : ServiceMessage {
public override fun toString(): String = "[mirai:service:$serviceId,$content]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitAbstractServiceMessage(this, data)
}
}

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.message.data
@ -18,6 +18,8 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.decodeStructure
import kotlinx.serialization.encoding.encodeStructure
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
/**
@ -45,6 +47,11 @@ public object ShowImageFlag : MessageMetadata, ConstrainSingle, AbstractMessageK
override fun toString(): String = "ShowImageFlag"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitShowImageFlag(this, data)
}
/**
* @since 2.4
*/

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress(
@ -19,7 +19,9 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.KSerializer
import kotlinx.serialization.PolymorphicSerializer
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.DeprecatedSinceMirai
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
/**
@ -27,6 +29,8 @@ import net.mamoe.mirai.utils.safeCast
*/
// @Serializable(SingleMessage.Serializer::class)
public interface SingleMessage : Message {
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R = visitor.visitSingleMessage(this, data)
/**
* @suppress deprecated since 2.4.0
@ -66,6 +70,11 @@ public interface MessageMetadata : SingleMessage {
* 返回空字符串
*/
override fun contentToString(): String = ""
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitMessageMetadata(this, data)
}
}
/**
@ -85,6 +94,11 @@ public interface MessageMetadata : SingleMessage {
* @see MusicShare 音乐分享
*/
public interface MessageContent : SingleMessage {
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitMessageContent(this, data)
}
public companion object Key : AbstractMessageKey<MessageContent>({ it.safeCast() })
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
@ -19,6 +19,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.IMirai
import net.mamoe.mirai.Mirai
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.*
/**
@ -39,6 +40,11 @@ public interface UnsupportedMessage : MessageContent {
*/
public val struct: ByteArray
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitUnsupportedMessage(this, data)
}
public companion object {
public const val SERIAL_NAME: String = "UnsupportedMessage"

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("NOTHING_TO_INLINE")
@ -15,6 +15,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.VipFace.Kind
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.safeCast
@ -48,6 +49,11 @@ public data class VipFace @MiraiInternalApi constructor(
override fun toString(): String = "[mirai:vipface:$kind,$count]"
override fun contentToString(): String = "[${kind.name}]x$count"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitVipFace(this, data)
}
@Serializable
public data class Kind(
val id: Int,

View File

@ -17,6 +17,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.*
@ -176,6 +177,11 @@ public open class Voice @MiraiInternalApi constructor(
result = 15 * result + _url.hashCode()
return result
}
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitVoice(this, data)
}
}
/**

View File

@ -0,0 +1,168 @@
/*
* Copyright 2019-2022 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/dev/LICENSE
*/
package net.mamoe.mirai.message.data.visitor
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.MiraiInternalApi
/**
* @suppress 这是内部 API, 请不要调用
* @since MESSAGE_VISITOR
*/
@MiraiInternalApi
public interface MessageVisitor<in D, out R> {
public fun visitMessage(message: Message, data: D): R
public fun visitSingleMessage(message: SingleMessage, data: D): R {
return visitMessage(message, data)
}
public fun visitMessageChain(messageChain: MessageChain, data: D): R {
return visitMessage(messageChain, data)
}
public fun visitMessageContent(message: MessageContent, data: D): R {
return visitSingleMessage(message, data)
}
public fun visitMessageMetadata(message: MessageMetadata, data: D): R {
return visitSingleMessage(message, data)
}
public fun visitMessageOrigin(message: MessageOrigin, data: D): R {
return visitMessageMetadata(message, data)
}
public fun visitMessageSource(message: MessageSource, data: D): R {
return visitMessageMetadata(message, data)
}
public fun visitQuoteReply(message: QuoteReply, data: D): R {
return visitMessageMetadata(message, data)
}
public fun visitCustomMessageMetadata(message: CustomMessageMetadata, data: D): R {
return visitMessageMetadata(message, data)
}
public fun visitShowImageFlag(message: ShowImageFlag, data: D): R {
return visitMessageMetadata(message, data)
}
public fun visitPlainText(message: PlainText, data: D): R {
return visitMessageContent(message, data)
}
public fun visitAt(message: At, data: D): R {
return visitMessageContent(message, data)
}
public fun visitAtAll(message: AtAll, data: D): R {
return visitMessageContent(message, data)
}
@Suppress("DEPRECATION_ERROR")
public fun visitVoice(message: Voice, data: D): R {
return visitMessageContent(message, data)
}
public fun visitAudio(message: Audio, data: D): R {
return visitMessageContent(message, data)
}
public fun visitHummerMessage(message: HummerMessage, data: D): R {
return visitMessageContent(message, data)
}
public fun visitFlashImage(message: FlashImage, data: D): R {
return visitHummerMessage(message, data)
}
public fun visitPokeMessage(message: PokeMessage, data: D): R {
return visitHummerMessage(message, data)
}
public fun visitVipFace(message: VipFace, data: D): R {
return visitHummerMessage(message, data)
}
public fun visitMarketFace(message: MarketFace, data: D): R {
return visitHummerMessage(message, data)
}
public fun visitDice(message: Dice, data: D): R {
return visitMarketFace(message, data)
}
public fun visitFace(message: Face, data: D): R {
return visitMessageContent(message, data)
}
public fun visitFileMessage(message: FileMessage, data: D): R {
return visitMessageContent(message, data)
}
public fun visitImage(message: Image, data: D): R {
return visitMessageContent(message, data)
}
public fun visitForwardMessage(message: ForwardMessage, data: D): R {
return visitMessageContent(message, data)
}
public fun visitMusicShare(message: MusicShare, data: D): R {
return visitMessageContent(message, data)
}
public fun visitUnsupportedMessage(message: UnsupportedMessage, data: D): R {
return visitMessageContent(message, data)
}
public fun visitRichMessage(message: RichMessage, data: D): R {
return visitMessageContent(message, data)
}
public fun visitServiceMessage(message: ServiceMessage, data: D): R {
return visitRichMessage(message, data)
}
public fun visitSimpleServiceMessage(message: SimpleServiceMessage, data: D): R {
return visitServiceMessage(message, data)
}
public fun visitLightApp(message: LightApp, data: D): R {
return visitRichMessage(message, data)
}
public fun visitAbstractServiceMessage(message: AbstractServiceMessage, data: D): R {
return visitServiceMessage(message, data)
}
}
/**
* @suppress 这是内部 API, 请不要调用
* @since 2.11
*/
@MiraiInternalApi
public interface MessageVisitorUnit<in D> : MessageVisitor<D, Unit> {
override fun visitMessage(message: Message, data: D): Unit = Unit
}
/**
* @suppress 这是内部 API, 请不要调用
* @since 2.11
*/
@MiraiInternalApi
public fun <R> Message.accept(visitor: MessageVisitor<Unit, R>): R = this.accept(visitor, Unit)

View File

@ -1,17 +1,22 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("unused")
package net.mamoe.mirai.internal.message
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.internal.message.visitor.ex
import net.mamoe.mirai.message.data.AbstractMessageKey
import net.mamoe.mirai.message.data.ConstrainSingle
import net.mamoe.mirai.message.data.MessageKey
import net.mamoe.mirai.message.data.MessageMetadata
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.safeCast
/**
@ -22,6 +27,10 @@ internal object ForceAsLongMessage : MessageMetadata, ConstrainSingle, InternalF
override val key: MessageKey<ForceAsLongMessage> get() = this
override fun toString(): String = ""
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitForceAsLongMessage(this, data) ?: super<InternalFlagOnlyMessage>.accept(visitor, data)
}
}
/**
@ -32,6 +41,10 @@ internal object DontAsLongMessage : MessageMetadata, ConstrainSingle, InternalFl
override val key: MessageKey<DontAsLongMessage> get() = this
override fun toString(): String = ""
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitDontAsLongMessage(this, data) ?: super<InternalFlagOnlyMessage>.accept(visitor, data)
}
}
internal object IgnoreLengthCheck : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage,
@ -39,6 +52,10 @@ internal object IgnoreLengthCheck : MessageMetadata, ConstrainSingle, InternalFl
override val key: MessageKey<IgnoreLengthCheck> get() = this
override fun toString(): String = ""
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitIgnoreLengthCheck(this, data) ?: super<InternalFlagOnlyMessage>.accept(visitor, data)
}
}
/**
@ -48,9 +65,16 @@ internal object MiraiInternalMessageFlag : MessageMetadata, ConstrainSingle, Int
AbstractMessageKey<MiraiInternalMessageFlag>({ it.safeCast() }) {
override val key: MessageKey<MiraiInternalMessageFlag> get() = this
override fun toString(): String = ""
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitMiraiInternalMessageFlag(this, data) ?: super<InternalFlagOnlyMessage>.accept(
visitor,
data
)
}
}
/**
* Ignore on transformation
*/
internal interface InternalFlagOnlyMessage : SingleMessage
internal sealed interface InternalFlagOnlyMessage : MessageMetadata

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.internal.message
@ -13,9 +13,11 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.Mirai
import net.mamoe.mirai.internal.asQQAndroidBot
import net.mamoe.mirai.internal.getMiraiImpl
import net.mamoe.mirai.internal.message.visitor.ex
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgTransmit
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.safeCast
// internal runtime value, not serializable
@ -30,6 +32,10 @@ internal data class LongMessageInternal internal constructor(override val conten
return MessageOrigin(SimpleServiceMessage(serviceId, content), resId, MessageOriginKind.LONG) + long
}
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitLongMessageInternal(this, data) ?: super<AbstractServiceMessage>.accept(visitor, data)
}
companion object Key :
AbstractPolymorphicMessageKey<ServiceMessage, LongMessageInternal>(ServiceMessage, { it.safeCast() })
}
@ -118,6 +124,13 @@ internal data class ForwardMessageInternal(
)
}
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitForwardMessageInternal(this, data) ?: super<AbstractServiceMessage>.accept(
visitor,
data
)
}
companion object Key :
AbstractPolymorphicMessageKey<ServiceMessage, ForwardMessageInternal>(ServiceMessage, { it.safeCast() }) {

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@ -14,11 +14,13 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import net.mamoe.mirai.Bot
import net.mamoe.mirai.internal.message.visitor.ex
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.message.data.Dice
import net.mamoe.mirai.message.data.MarketFace
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.hexToBytes
@SerialName(MarketFace.SERIAL_NAME)
@ -33,6 +35,10 @@ internal data class MarketFaceImpl internal constructor(
override val id: Int = delegate.tabId
override fun toString() = "[mirai:marketface:$id,$name]"
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitMarketFaceImpl(this, data) ?: super.accept(visitor, data)
}
}
/**
@ -50,6 +56,10 @@ internal class MarketFaceInternal(
}
override fun toString(): String = "[mirai:marketface:$id,$name]"
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitMarketFaceInternal(this, data) ?: super<MarketFace>.accept(visitor, data)
}
}
// From https://github.com/mamoe/mirai/issues/1012

View File

@ -14,16 +14,19 @@ import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.internal.contact.SendMessageHandler
import net.mamoe.mirai.internal.message.LightMessageRefiner.dropMiraiInternalFlags
import net.mamoe.mirai.internal.message.LightMessageRefiner.refineLight
import net.mamoe.mirai.internal.message.visitor.ex
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.cast
import java.util.concurrent.atomic.AtomicBoolean
/**
* All [MessageSource] should implement this interface.
*/
internal interface MessageSourceInternal {
internal interface MessageSourceInternal : MessageMetadata {
@Transient
val sequenceIds: IntArray // ids
@ -38,6 +41,10 @@ internal interface MessageSourceInternal {
val isRecalledOrPlanned: AtomicBoolean
fun toJceData(): ImMsgBody.SourceMsg
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.ex()?.visitMessageSourceInternal(this.cast(), data) ?: super.accept(visitor, data)
}
}
/**

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_OVERRIDE", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
@ -30,6 +30,7 @@ import net.mamoe.mirai.internal.utils.structureToString
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSourceKind
import net.mamoe.mirai.message.data.OnlineMessageSource
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.utils.encodeBase64
import net.mamoe.mirai.utils.mapToIntArray
@ -59,6 +60,10 @@ internal class OnlineMessageSourceFromFriendImpl(
private val jceData: ImMsgBody.SourceMsg by lazy { msg.toJceDataPrivate(internalIds) }
override fun toJceData(): ImMsgBody.SourceMsg = jceData
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<IncomingMessageSourceInternal>.accept(visitor, data)
}
}
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@ -85,6 +90,10 @@ internal class OnlineMessageSourceFromStrangerImpl(
private val jceData: ImMsgBody.SourceMsg by lazy { msg.toJceDataPrivate(internalIds) }
override fun toJceData(): ImMsgBody.SourceMsg = jceData
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<IncomingMessageSourceInternal>.accept(visitor, data)
}
}
private fun List<MsgComm.Msg>.toJceDataPrivate(ids: IntArray): ImMsgBody.SourceMsg {
@ -154,6 +163,10 @@ internal class OnlineMessageSourceFromTempImpl(
private val jceData: ImMsgBody.SourceMsg by lazy { msg.toJceDataPrivate(internalIds) }
override fun toJceData(): ImMsgBody.SourceMsg = jceData
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<IncomingMessageSourceInternal>.accept(visitor, data)
}
}
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@ -216,4 +229,8 @@ internal class OnlineMessageSourceFromGroupImpl(
override fun toJceData(): ImMsgBody.SourceMsg {
return jceData
}
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<IncomingMessageSourceInternal>.accept(visitor, data)
}
}

View File

@ -1,10 +1,10 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
* Copyright 2019-2022 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.
* 此源代码的使用受 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
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@ -20,6 +20,7 @@ import net.mamoe.mirai.internal.utils.io.serialization.loadAs
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSourceKind
import net.mamoe.mirai.message.data.OfflineMessageSource
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.utils.mapToIntArray
import java.util.concurrent.atomic.AtomicBoolean
@ -105,6 +106,10 @@ internal class OfflineMessageSourceImplData(
result = 31 * result + internalIds.contentHashCode()
return result
}
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<MessageSourceInternal>.accept(visitor, data)
}
}
internal fun OfflineMessageSourceImplData(

View File

@ -27,6 +27,7 @@ import net.mamoe.mirai.internal.utils.io.serialization.toByteArray
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.OnlineMessageSource
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.toLongUnsigned
import java.util.concurrent.atomic.AtomicBoolean
@ -92,6 +93,10 @@ internal class OnlineMessageSourceToFriendImpl(
override var isRecalledOrPlanned: AtomicBoolean = AtomicBoolean(false)
private val jceData: ImMsgBody.SourceMsg by lazy { toJceDataImpl(subject) }
override fun toJceData(): ImMsgBody.SourceMsg = jceData
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<OutgoingMessageSourceInternal>.accept(visitor, data)
}
}
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@ -119,6 +124,10 @@ internal class OnlineMessageSourceToStrangerImpl(
override var isRecalledOrPlanned: AtomicBoolean = AtomicBoolean(false)
private val jceData: ImMsgBody.SourceMsg by lazy { toJceDataImpl(subject) }
override fun toJceData(): ImMsgBody.SourceMsg = jceData
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<OutgoingMessageSourceInternal>.accept(visitor, data)
}
}
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@ -145,6 +154,10 @@ internal class OnlineMessageSourceToTempImpl(
override var isRecalledOrPlanned: AtomicBoolean = AtomicBoolean(false)
private val jceData: ImMsgBody.SourceMsg by lazy { toJceDataImpl(subject) }
override fun toJceData(): ImMsgBody.SourceMsg = jceData
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<OutgoingMessageSourceInternal>.accept(visitor, data)
}
}
@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@ -236,4 +249,8 @@ internal class OnlineMessageSourceToGroupImpl(
}
override fun toJceData(): ImMsgBody.SourceMsg = jceData
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return super<OutgoingMessageSourceInternal>.accept(visitor, data)
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2019-2022 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/dev/LICENSE
*/
package net.mamoe.mirai.internal.message.visitor
import net.mamoe.mirai.internal.message.*
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.castOrNull
internal fun <D, R> MessageVisitor<D, R>?.ex(): MessageVisitorEx<D, R>? = castOrNull()
/**
* For mirai-core-specific types
*/
internal interface MessageVisitorEx<in D, out R> : MessageVisitor<D, R> {
fun <T> visitMessageSourceInternal(message: T, data: D): R where T : MessageSourceInternal, T : MessageSource {
return visitMessageSource(message, data)
}
fun visitForwardMessageInternal(message: ForwardMessageInternal, data: D): R {
return visitAbstractServiceMessage(message, data)
}
fun visitLongMessageInternal(message: LongMessageInternal, data: D): R {
return visitAbstractServiceMessage(message, data)
}
fun visitMarketFaceInternal(message: MarketFaceInternal, data: D): R {
return visitMarketFace(message, data)
}
fun visitMarketFaceImpl(message: MarketFaceImpl, data: D): R {
return visitMarketFace(message, data)
}
fun visitInternalFlagOnlyMessage(message: InternalFlagOnlyMessage, data: D): R {
return visitMessageMetadata(message, data)
}
fun visitForceAsLongMessage(message: ForceAsLongMessage, data: D): R {
return visitInternalFlagOnlyMessage(message, data)
}
fun visitDontAsLongMessage(message: DontAsLongMessage, data: D): R {
return visitInternalFlagOnlyMessage(message, data)
}
fun visitIgnoreLengthCheck(message: IgnoreLengthCheck, data: D): R {
return visitInternalFlagOnlyMessage(message, data)
}
fun visitMiraiInternalMessageFlag(message: MiraiInternalMessageFlag, data: D): R {
return visitInternalFlagOnlyMessage(message, data)
}
}