Find nick from source; fix #1137

This commit is contained in:
Karlatemp 2021-04-17 09:28:11 +08:00
parent 1dce29a154
commit 83fa78b50d
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
3 changed files with 48 additions and 7 deletions

View File

@ -14,12 +14,40 @@ import kotlinx.io.core.readBytes
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.AtAll
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.safeCast
internal fun At.toJceData(group: Group?): ImMsgBody.Text {
val text = "@${group?.members?.get(this.target)?.nameCardOrNick ?: target}"
internal fun At.toJceData(
group: Group?,
source: MessageSource?,
isForward: Boolean,
): ImMsgBody.Text {
fun findFromGroup(g: Group?): String? {
return g?.members?.get(this.target)?.nameCardOrNick
}
fun findFromSource(): String? {
return when (source) {
is OnlineMessageSource -> {
return findFromGroup(source.target.safeCast())
}
is OfflineMessageSource -> {
if (source.kind == MessageSourceKind.GROUP) {
return findFromGroup(group?.bot?.getGroup(source.targetId))
} else null
}
else -> null
}
}
val text = "@${
if (isForward) {
findFromSource() ?: findFromGroup(group)
} else {
findFromGroup(group) ?: findFromSource()
} ?: target
}"
return ImMsgBody.Text(
str = text,
attr6Buf = buildPacket {

View File

@ -34,7 +34,8 @@ internal val UNSUPPORTED_VOICE_MESSAGE_PLAIN = PlainText("收到语音消息,
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
internal fun MessageChain.toRichTextElems(
messageTarget: ContactOrBot?,
withGeneralFlags: Boolean
withGeneralFlags: Boolean,
isForward: Boolean = false,
): MutableList<ImMsgBody.Elem> {
val forGroup = messageTarget is Group
val elements = ArrayList<ImMsgBody.Elem>(this.size)
@ -114,7 +115,15 @@ internal fun MessageChain.toRichTextElems(
)
}
is At -> {
elements.add(ImMsgBody.Elem(text = currentMessage.toJceData(messageTarget.safeCast())))
elements.add(
ImMsgBody.Elem(
text = currentMessage.toJceData(
messageTarget.safeCast(),
this[MessageSource],
isForward,
)
)
)
// elements.add(ImMsgBody.Elem(text = ImMsgBody.Text(str = " ")))
// removed by https://github.com/mamoe/mirai/issues/524
// 发送 QuoteReply 消息时无可避免的产生多余空格 #524

View File

@ -86,7 +86,11 @@ internal fun Collection<ForwardMessage.INode>.calculateValidationData(
msgBody = ImMsgBody.MsgBody(
richText = ImMsgBody.RichText(
elems = chain.messageChain.toMessageChain()
.toRichTextElems(handler.contact, withGeneralFlags = false).toMutableList()
.toRichTextElems(
handler.contact,
withGeneralFlags = false,
isForward = true,
).toMutableList()
)
)
)