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 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 MUSIC_SHARE 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;
}

View File

@ -98,6 +98,9 @@ public interface RichMessage : MessageContent, ConstrainSingle {
@Serializable
@SerialName(LightApp.SERIAL_NAME)
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 const val SERIAL_NAME: String = "LightApp"
}

View File

@ -108,8 +108,14 @@ public enum class RichMessageKind {
/**
* 合并转发
* @see ForwardMessage
*/
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 {
/**
* This message [RefinableMessage] will be replaced by return value of [refine]
*/
suspend fun refine(
contact: Contact,
context: MessageChain,

View File

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

View File

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