RichMessageOrigin support for MusicShare. fix #950

This commit is contained in:
Him188 2021-02-09 18:36:06 +08:00
parent 7d108d3222
commit 9a32b2690e
6 changed files with 49 additions and 30 deletions

View File

@ -5050,6 +5050,7 @@ public final class net/mamoe/mirai/message/data/RichMessage$Key : net/mamoe/mira
public final class net/mamoe/mirai/message/data/RichMessageKind : java/lang/Enum { public final class net/mamoe/mirai/message/data/RichMessageKind : java/lang/Enum {
public static final field FORWARD Lnet/mamoe/mirai/message/data/RichMessageKind; public static final field FORWARD Lnet/mamoe/mirai/message/data/RichMessageKind;
public static final field LONG Lnet/mamoe/mirai/message/data/RichMessageKind; public static final field LONG Lnet/mamoe/mirai/message/data/RichMessageKind;
public static final field MUSIC_SHARE Lnet/mamoe/mirai/message/data/RichMessageKind;
public static fun valueOf (Ljava/lang/String;)Lnet/mamoe/mirai/message/data/RichMessageKind; public static fun valueOf (Ljava/lang/String;)Lnet/mamoe/mirai/message/data/RichMessageKind;
public static fun values ()[Lnet/mamoe/mirai/message/data/RichMessageKind; public static fun values ()[Lnet/mamoe/mirai/message/data/RichMessageKind;
} }

View File

@ -98,6 +98,9 @@ public interface RichMessage : MessageContent, ConstrainSingle {
@Serializable @Serializable
@SerialName(LightApp.SERIAL_NAME) @SerialName(LightApp.SERIAL_NAME)
public data class LightApp(override val content: String) : RichMessage, CodableMessage { public data class LightApp(override val content: String) : RichMessage, CodableMessage {
// implementation notes: LightApp is always decoded as LightAppInternal
// which are transformed as RefinableMessage to LightApp
public companion object Key : AbstractMessageKey<LightApp>({ it.safeCast() }) { public companion object Key : AbstractMessageKey<LightApp>({ it.safeCast() }) {
public const val SERIAL_NAME: String = "LightApp" public const val SERIAL_NAME: String = "LightApp"
} }

View File

@ -108,8 +108,14 @@ public enum class RichMessageKind {
/** /**
* 合并转发 * 合并转发
* @see ForwardMessage
*/ */
FORWARD, FORWARD,
// TODO: 2021/2/3 MusicShare RichMessageKind /**
* 音乐分享
* @see MusicShare
* @since 2.4
*/
MUSIC_SHARE,
} }

View File

@ -87,6 +87,9 @@ internal data class ForwardMessageInternal(override val content: String, val res
internal interface RefinableMessage : SingleMessage { internal interface RefinableMessage : SingleMessage {
/**
* This message [RefinableMessage] will be replaced by return value of [refine]
*/
suspend fun refine( suspend fun refine(
contact: Contact, contact: Contact,
context: MessageChain, context: MessageChain,

View File

@ -278,7 +278,7 @@ private object ReceiveMessageTransformer {
} }
} }
list.add(LightApp(content).refine()) list.add(LightAppInternal(content))
} }
private fun decodeCustomElem( private fun decodeCustomElem(

View File

@ -12,42 +12,48 @@ package net.mamoe.mirai.internal.message
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import net.mamoe.mirai.message.data.LightApp import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.data.MusicKind import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.MusicShare import net.mamoe.mirai.utils.safeCast
import net.mamoe.mirai.message.data.SingleMessage
private val json = Json { internal data class LightAppInternal(
ignoreUnknownKeys = true override val content: String
} ) : RichMessage, RefinableMessage {
companion object Key :
AbstractPolymorphicMessageKey<RichMessage, LightAppInternal>(RichMessage, { it.safeCast() })
internal fun LightApp.tryDeserialize(): LightAppStruct? { override suspend fun refine(contact: Contact, context: MessageChain): Message {
return kotlin.runCatching { val struct = tryDeserialize() ?: return LightApp(content)
json.decodeFromString(LightAppStruct.serializer(), this.content) struct.run {
}.getOrNull() if (meta.music != null) {
} MusicKind.values().find { it.appId.toInt() == meta.music.appid }?.let { musicType ->
meta.music.run {
/** return RichMessageOrigin(
* 识别 app 内容, 如果有必要 LightApp(content),
*/ null,
internal fun LightApp.refine(): SingleMessage { RichMessageKind.MUSIC_SHARE
val struct = tryDeserialize() ?: return this ) + MusicShare(
struct.run { kind = musicType, title = title, summary = desc,
if (meta.music != null) { jumpUrl = jumpUrl, pictureUrl = preview, musicUrl = musicUrl, brief = prompt
MusicKind.values().find { it.appId.toInt() == meta.music.appid }?.let { musicType -> )
meta.music.run { }
return MusicShare(
kind = musicType, title = title, summary = desc,
jumpUrl = jumpUrl, pictureUrl = preview, musicUrl = musicUrl, brief = prompt
)
} }
} }
} }
return LightApp(content)
} }
}
return this private val json = Json {
ignoreUnknownKeys = true
isLenient = true
}
internal fun LightAppInternal.tryDeserialize(): LightAppStruct? {
return kotlin.runCatching {
json.decodeFromString(LightAppStruct.serializer(), this.content)
}.getOrNull()
} }
/* /*