Unified MessageSource.toJceData

This commit is contained in:
Him188 2020-04-12 01:41:27 +08:00
parent e279e79b63
commit d97aa7bd94
2 changed files with 36 additions and 19 deletions

View File

@ -39,6 +39,8 @@ internal interface MessageSourceImpl {
val sequenceId: Int
var isRecalledOrPlanned: Boolean // // TODO: 2020/4/7 实现 isRecalledOrPlanned
fun toJceData(): ImMsgBody.SourceMsg
}
internal suspend inline fun MessageSource.ensureSequenceIdAvailable() {
@ -76,7 +78,7 @@ internal class MessageSourceFromFriendImpl(
}
}
internal fun toJceDataImplForFriend(): ImMsgBody.SourceMsg {
override fun toJceData(): ImMsgBody.SourceMsg {
return ImMsgBody.SourceMsg(
origSeqs = listOf(msg.msgHead.msgSeq),
senderUin = msg.msgHead.fromUin,
@ -131,9 +133,10 @@ internal class MessageSourceFromTempImpl(
)
}
override val target: Bot get() = bot
override val sender: Member get() = with(msg.msgHead) {
bot.getGroup(c2cTmpMsgHead!!.groupUin)[fromUin]
}
override val sender: Member
get() = with(msg.msgHead) {
bot.getGroup(c2cTmpMsgHead!!.groupUin)[fromUin]
}
private val elems by lazy {
msg.msgBody.richText.elems.toMutableList().also {
@ -141,7 +144,7 @@ internal class MessageSourceFromTempImpl(
}
}
internal fun toJceDataImplForTemp(): ImMsgBody.SourceMsg {
override fun toJceData(): ImMsgBody.SourceMsg {
return ImMsgBody.SourceMsg(
origSeqs = listOf(msg.msgHead.msgSeq),
senderUin = msg.msgHead.fromUin,
@ -204,7 +207,7 @@ internal class MessageSourceFromGroupImpl(
?: error("cannot find member for MessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}")
fun toJceDataImplForGroup(): ImMsgBody.SourceMsg {
override fun toJceData(): ImMsgBody.SourceMsg {
return ImMsgBody.SourceMsg(
origSeqs = listOf(msg.msgHead.msgSeq),
senderUin = msg.msgHead.fromUin,
@ -219,7 +222,8 @@ internal class MessageSourceFromGroupImpl(
}
}
internal class OfflineMessageSourceImplByMsg( // from other sources' originalMessage
internal class OfflineMessageSourceImplByMsg(
// from other sources' originalMessage
val delegate: MsgComm.Msg,
override val bot: Bot
) : OfflineMessageSource(), MessageSourceImpl {
@ -248,9 +252,24 @@ internal class OfflineMessageSourceImplByMsg( // from other sources' originalMes
set(value) {
isRecalled.value = value
}
override fun toJceData(): ImMsgBody.SourceMsg {
return ImMsgBody.SourceMsg(
origSeqs = listOf(delegate.msgHead.msgSeq),
senderUin = delegate.msgHead.fromUin,
toUin = 0,
flag = 1,
elems = delegate.msgBody.richText.elems,
type = 0,
time = delegate.msgHead.msgTime,
pbReserve = EMPTY_BYTE_ARRAY,
srcMsg = EMPTY_BYTE_ARRAY
)
}
}
internal class OfflineMessageSourceImplBySourceMsg( // from others' quotation
internal class OfflineMessageSourceImplBySourceMsg(
// from others' quotation
val delegate: ImMsgBody.SourceMsg,
override val bot: Bot,
groupIdOrZero: Long
@ -295,6 +314,10 @@ internal class OfflineMessageSourceImplBySourceMsg( // from others' quotation
)*/
}
}
override fun toJceData(): ImMsgBody.SourceMsg {
return delegate
}
}
internal class MessageSourceToFriendImpl(
@ -317,7 +340,7 @@ internal class MessageSourceToFriendImpl(
originalMessage.toRichTextElems(forGroup = false, withGeneralFlags = true)
}
fun toJceDataImplForFriend(): ImMsgBody.SourceMsg {
override fun toJceData(): ImMsgBody.SourceMsg {
val messageUid: Long = sequenceId.toLong().shl(32) or id.toLong().and(0xffFFffFF)
return ImMsgBody.SourceMsg(
origSeqs = listOf(sequenceId),
@ -374,7 +397,7 @@ internal class MessageSourceToTempImpl(
originalMessage.toRichTextElems(forGroup = false, withGeneralFlags = true)
}
fun toJceDataImplForTemp(): ImMsgBody.SourceMsg {
override fun toJceData(): ImMsgBody.SourceMsg {
val messageUid: Long = sequenceId.toLong().shl(32) or id.toLong().and(0xffFFffFF)
return ImMsgBody.SourceMsg(
origSeqs = listOf(sequenceId),
@ -449,7 +472,7 @@ internal class MessageSourceToGroupImpl(
}
fun toJceDataImplForGroup(): ImMsgBody.SourceMsg {
override fun toJceData(): ImMsgBody.SourceMsg {
return ImMsgBody.SourceMsg(
origSeqs = listOf(sequenceId),
senderUin = fromId,

View File

@ -40,14 +40,8 @@ internal fun MessageChain.toRichTextElems(forGroup: Boolean, withGeneralFlags: B
if (this.anyIsInstance<QuoteReply>()) {
when (val source = this[QuoteReply].source) {
is OfflineMessageSourceImplBySourceMsg -> elements.add(ImMsgBody.Elem(srcMsg = source.delegate))
is MessageSourceToFriendImpl -> elements.add(ImMsgBody.Elem(srcMsg = source.toJceDataImplForFriend()))
is MessageSourceToGroupImpl -> elements.add(ImMsgBody.Elem(srcMsg = source.toJceDataImplForGroup()))
is MessageSourceToTempImpl -> elements.add(ImMsgBody.Elem(srcMsg = source.toJceDataImplForTemp()))
is MessageSourceFromFriendImpl -> elements.add(ImMsgBody.Elem(srcMsg = source.toJceDataImplForFriend()))
is MessageSourceFromGroupImpl -> elements.add(ImMsgBody.Elem(srcMsg = source.toJceDataImplForGroup()))
is MessageSourceFromTempImpl -> elements.add(ImMsgBody.Elem(srcMsg = source.toJceDataImplForTemp()))
else -> error("unsupported MessageSource implementation: ${source::class.simpleName}")
is MessageSourceImpl -> elements.add(ImMsgBody.Elem(srcMsg = source.toJceData()))
else -> error("unsupported MessageSource implementation: ${source::class.simpleName}. Don't implement your own MessageSource.")
}
}