`Shamrock`: fix #126

Signed-off-by: FQL <ydic001@vip.qq.com>
This commit is contained in:
FQL 2023-10-20 08:54:09 +08:00
parent 7b67c14642
commit e2901c216b
3 changed files with 21 additions and 21 deletions

View File

@ -254,17 +254,19 @@ internal object MsgConvert {
} }
MsgConstant.KELEMTYPEREPLY -> { MsgConstant.KELEMTYPEREPLY -> {
val reply = element.replyElement val reply = element.replyElement
var msgId = reply.sourceMsgIdInRecords val msgId = reply.replayMsgId
if (msgId == 0L) { val msgHash = if (msgId != 0L) {
msgId = reply.replayMsgId MessageHelper.generateMsgIdHash(chatType, msgId)
} else {
MessageDB.getInstance().messageMappingDao()
.queryByMsgSeq(chatType, peerId, reply.replayMsgSeq?.toInt() ?: 0)?.msgHashId
?: MessageHelper.generateMsgIdHash(chatType, reply.sourceMsgIdInRecords)
} }
val mapping = MessageDB.getInstance().messageMappingDao().queryByMsgSeq(chatType, peerId, reply.replayMsgSeq.toInt())
return hashMapOf( return hashMapOf(
"type" to "reply".json, "type" to "reply".json,
"data" to JsonObject(mapOf( "data" to JsonObject(mapOf(
"id" to (mapping?.msgHashId ?: MessageHelper.generateMsgIdHash(chatType, msgId)).json, "id" to msgHash.json,
)) ))
) )
} }

View File

@ -114,7 +114,7 @@ internal object MessageHelper {
fun generateMsgId(chatType: Int): Pair<Int, Long> { fun generateMsgId(chatType: Int): Pair<Int, Long> {
val msgId = createMessageUniseq(chatType, System.currentTimeMillis()) val msgId = createMessageUniseq(chatType, System.currentTimeMillis())
val hashCode: Int = convertMsgIdToMsgHash(chatType, msgId) val hashCode: Int = generateMsgIdHash(chatType, msgId)
return hashCode to msgId return hashCode to msgId
} }
@ -128,10 +128,6 @@ internal object MessageHelper {
return db.messageMappingDao().queryByMsgSeq(chatType, msgSeq) return db.messageMappingDao().queryByMsgSeq(chatType, msgSeq)
} }
inline fun convertMsgIdToMsgHash(chatType: Int, msgId: Long): Int {
return generateMsgIdHash(chatType, msgId)
}
fun removeMsgByHashCode(hashCode: Int) { fun removeMsgByHashCode(hashCode: Int) {
MessageDB.getInstance() MessageDB.getInstance()
.messageMappingDao() .messageMappingDao()

View File

@ -29,9 +29,7 @@ internal object AioListener: IKernelMsgListener {
private suspend fun handleMsg(record: MsgRecord) { private suspend fun handleMsg(record: MsgRecord) {
try { try {
val rawMsg = record.elements.toCQCode(record.chatType, record.peerUin.toString()) val msgHash = MessageHelper.generateMsgIdHash(record.chatType, record.msgId)
if (rawMsg.isEmpty()) return
val msgHash = MessageHelper.convertMsgIdToMsgHash(record.chatType, record.msgId)
MessageHelper.saveMsgMapping( MessageHelper.saveMsgMapping(
hash = msgHash, hash = msgHash,
@ -43,9 +41,12 @@ internal object AioListener: IKernelMsgListener {
time = record.msgTime time = record.msgTime
) )
val rawMsg = record.elements.toCQCode(record.chatType, record.peerUin.toString())
if (rawMsg.isEmpty()) return
when (record.chatType) { when (record.chatType) {
MsgConstant.KCHATTYPEGROUP -> { MsgConstant.KCHATTYPEGROUP -> {
LogCenter.log("群消息(group = ${record.peerName}(${record.peerUin}), uin = ${record.senderUin}, id = $msgHash, msg = $rawMsg)") LogCenter.log("群消息(group = ${record.peerName}(${record.peerUin}), uin = ${record.senderUin}, id = $msgHash|${record.msgSeq}, msg = $rawMsg)")
ShamrockConfig.getGroupMsgRule()?.let { rule -> ShamrockConfig.getGroupMsgRule()?.let { rule ->
if (rule.black?.contains(record.peerUin) == true) return if (rule.black?.contains(record.peerUin) == true) return
if (rule.white?.contains(record.peerUin) == false) return if (rule.white?.contains(record.peerUin) == false) return
@ -56,7 +57,7 @@ internal object AioListener: IKernelMsgListener {
} }
} }
MsgConstant.KCHATTYPEC2C -> { MsgConstant.KCHATTYPEC2C -> {
LogCenter.log("私聊消息(private = ${record.senderUin}, msg = $rawMsg)") LogCenter.log("私聊消息(private = ${record.senderUin}, id = $msgHash|${record.msgSeq}, msg = $rawMsg)")
ShamrockConfig.getPrivateRule()?.let { rule -> ShamrockConfig.getPrivateRule()?.let { rule ->
if (rule.black?.contains(record.peerUin) == true) return if (rule.black?.contains(record.peerUin) == true) return
if (rule.white?.contains(record.peerUin) == false) return if (rule.white?.contains(record.peerUin) == false) return
@ -76,11 +77,7 @@ internal object AioListener: IKernelMsgListener {
override fun onAddSendMsg(record: MsgRecord) { override fun onAddSendMsg(record: MsgRecord) {
GlobalScope.launch { GlobalScope.launch {
try { try {
val rawMsg = record.elements.toCQCode(record.chatType, record.peerUin.toString()) val msgHash = MessageHelper.generateMsgIdHash(record.chatType, record.msgId)
if (rawMsg.isEmpty()) return@launch
LogCenter.log("发送消息: $rawMsg")
val msgHash = MessageHelper.convertMsgIdToMsgHash(record.chatType, record.msgId)
MessageHelper.saveMsgMapping( MessageHelper.saveMsgMapping(
hash = msgHash, hash = msgHash,
qqMsgId = record.msgId, qqMsgId = record.msgId,
@ -91,6 +88,11 @@ internal object AioListener: IKernelMsgListener {
time = record.msgTime time = record.msgTime
) )
val rawMsg = record.elements.toCQCode(record.chatType, record.peerUin.toString())
if (rawMsg.isEmpty()) return@launch
LogCenter.log("发送消息($msgHash|${record.msgSeq}): $rawMsg")
if (!ShamrockConfig.enableSelfMsg()) if (!ShamrockConfig.enableSelfMsg())
return@launch return@launch