Skip to content

Commit

Permalink
Fix FriendNickChangedEvent, fix #1536
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Sep 30, 2021
1 parent 6b63c32 commit b0e25a5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ internal class FriendNoticeProcessor(
bot.nick = to
} else {
val friend = bot.getFriend(body.uin)?.impl() ?: continue
val from = bot.nick
val from = friend.nick
if (from == to) continue
collect(FriendNickChangedEvent(friend, from, to))
friend.info.nick = to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
package net.mamoe.mirai.internal.network.protocol.data.jce

import kotlinx.serialization.Serializable
import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x27
import net.mamoe.mirai.internal.utils.io.JceStruct
import net.mamoe.mirai.internal.utils.io.NestedStructure
import net.mamoe.mirai.internal.utils.io.NestedStructureDesensitizer
import net.mamoe.mirai.internal.utils.io.ProtocolStruct
import net.mamoe.mirai.internal.utils.io.serialization.loadAs
import net.mamoe.mirai.internal.utils.io.serialization.tars.TarsId
import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY

Expand Down Expand Up @@ -72,8 +77,18 @@ internal class MsgType0x210(
@TarsId(7) @JvmField val stMsgInfo0x20: MsgType0x210SubMsgType0x20? = null,
@TarsId(8) @JvmField val stMsgInfo0x1d: MsgType0x210SubMsgType0x1d? = null,
@TarsId(9) @JvmField val stMsgInfo0x24: MsgType0x210SubMsgType0x24? = null,
@TarsId(10) @JvmField val vProtobuf: ByteArray = EMPTY_BYTE_ARRAY,
) : JceStruct
@NestedStructure(Deserializer::class) @TarsId(10) @JvmField val vProtobuf: ByteArray = EMPTY_BYTE_ARRAY,
) : JceStruct {
object Deserializer : NestedStructureDesensitizer<MsgType0x210, ProtocolStruct> {
override fun deserialize(context: MsgType0x210, byteArray: ByteArray): ProtocolStruct? {
return when (context.uSubMsgType) {
0x27L -> byteArray.loadAs(Submsgtype0x27.SubMsgType0x27.SubMsgType0x27MsgBody.serializer())
else -> null
}
}

}
}

@Serializable
internal class MsgType0x210SubMsgType0x13(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

@file:JvmBlockingBridge

package net.mamoe.mirai.internal.notice.processors

import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.event.events.FriendNickChangedEvent
import net.mamoe.mirai.internal.network.protocol.data.jce.MsgInfo
import net.mamoe.mirai.internal.network.protocol.data.jce.MsgType0x210
import net.mamoe.mirai.internal.network.protocol.data.jce.ShareData
import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x27.SubMsgType0x27
import net.mamoe.mirai.internal.utils.io.serialization.toByteArray
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs

internal class FriendNickChangeTest : AbstractNoticeProcessorTest() {

@Test
suspend fun `nick changed`() {
// FriendNickChangedEvent 内容异常 https://github.com/mamoe/mirai/issues/1356

suspend fun runTest() = use {

net.mamoe.mirai.internal.network.protocol.data.jce.OnlinePushPack.SvcReqPushMsg(
uin = 1230002,
uMsgTime = 1633037660,
vMsgInfos = mutableListOf(
MsgInfo(
lFromUin = 1230002,
shMsgType = 528,
shMsgSeq = 142,
strMsg = "",
uRealMsgTime = 160,
vMsg = MsgType0x210(
uSubMsgType = 39,
vProtobuf = SubMsgType0x27.SubMsgType0x27MsgBody(
msgModInfos = mutableListOf(
SubMsgType0x27.ForwardBody(
opType = 20,
msgModProfile = SubMsgType0x27.ModProfile(
uin = 1230001,
msgProfileInfos = mutableListOf(
SubMsgType0x27.ProfileInfo(
field = 20002,
value = "ABC",
)
),
),
)
),
)
.toByteArray(SubMsgType0x27.SubMsgType0x27MsgBody.serializer()),
).toByteArray(),
uAppShareID = 0,
vMsgCookies = "08 90 04 10 90 84 A0 81 80 80 80 80 02 18 00 20 E3 86 03".hexToBytes(),
lMsgUid = 14411,
lLastChangeTime = 1,
vCPicInfo = mutableListOf(),
stShareData = ShareData(
),
lFromInstId = 0,
vRemarkOfSender = net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY,
strFromMobile = "",
strFromName = "",
vNickName = mutableListOf(),
)
),
svrip = 1273521418,
)

}

setBot(1230002).apply {
addFriend(1230001, nick = "aaa")
}

runTest().toList().run {
assertEquals(1, size, toString())
get(0).run {
assertIs<FriendNickChangedEvent>(this)
assertEquals(1230001, friend.id)
assertEquals("aaa", from)
assertEquals("ABC", to)
}
}

}
}

1 comment on commit b0e25a5

@Him188
Copy link
Member Author

@Him188 Him188 commented on b0e25a5 Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.