Support anonymous in network

This commit is contained in:
Karlatemp 2020-12-20 09:56:42 +08:00
parent c6bef602b5
commit c96e7b609e
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
4 changed files with 17 additions and 6 deletions

View File

@ -21,4 +21,6 @@ public interface MemberInfo : FriendInfo {
public val specialTitle: String
public val muteTimestamp: Int
public val anonymousId: String? get() = null
}

View File

@ -230,6 +230,12 @@ internal class GroupImpl(
}
override fun newMember(memberInfo: MemberInfo): Member {
memberInfo.anonymousId?.let { anId ->
return AnonymousMemberImpl(
this, this.coroutineContext,
memberInfo, anId
)
}
return MemberImpl(
Mirai._lowLevelNewFriend(bot, memberInfo) as FriendImpl,
this,
@ -238,7 +244,7 @@ internal class GroupImpl(
)
}
internal fun newAnonymous(name: String): Member = newMember(
internal fun newAnonymous(name: String, id: String): Member = newMember(
object : MemberInfo {
override val nameCard = name
override val permission = MemberPermission.MEMBER
@ -247,6 +253,7 @@ internal class GroupImpl(
override val uin = 80000000L
override val nick = name
override val remark: String = "匿名"
override val anonymousId: String get() = id
}
)

View File

@ -20,6 +20,7 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm
import net.mamoe.mirai.internal.network.protocol.data.proto.SourceMsg
import net.mamoe.mirai.internal.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.internal.utils._miraiContentToString
import net.mamoe.mirai.internal.utils.encodeToBase64
import net.mamoe.mirai.internal.utils.encodeToString
import net.mamoe.mirai.internal.utils.io.serialization.toByteArray
import net.mamoe.mirai.message.data.Message
@ -154,7 +155,7 @@ internal data class MessageSourceFromGroupImpl(
) as GroupImpl).run {
get(msg.msgHead.fromUin)
?: msg.msgBody.richText.elems.firstOrNull { it.anonGroupMsg != null }?.run {
newAnonymous(anonGroupMsg!!.anonNick.encodeToString())
newAnonymous(anonGroupMsg!!.anonNick.encodeToString(), anonGroupMsg.anonId.encodeToBase64())
}
?: error("cannot find member for MessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}")
}

View File

@ -29,6 +29,7 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.MsgOnlinePush
import net.mamoe.mirai.internal.network.protocol.data.proto.Oidb0x8fc
import net.mamoe.mirai.internal.network.protocol.packet.IncomingPacketFactory
import net.mamoe.mirai.internal.utils._miraiContentToString
import net.mamoe.mirai.internal.utils.encodeToBase64
import net.mamoe.mirai.internal.utils.encodeToString
import net.mamoe.mirai.internal.utils.io.serialization.loadAs
import net.mamoe.mirai.internal.utils.io.serialization.readProtoBuf
@ -74,10 +75,10 @@ internal object OnlinePushPbPushGroupMsg : IncomingPacketFactory<Packet?>("Onlin
val group =
bot.getGroup(pbPushMsg.msg.msgHead.groupInfo!!.groupCode) as GroupImpl? ?: return null // 机器人还正在进群
val sender = if (anonymous != null) {
group.newAnonymous(anonymous.anonNick.encodeToString())
group.newAnonymous(anonymous.anonNick.encodeToString(), anonymous.anonId.encodeToBase64())
} else {
group[pbPushMsg.msg.msgHead.fromUin]
} as MemberImpl
group[pbPushMsg.msg.msgHead.fromUin] as MemberImpl
}
val name = if (anonymous != null) {
sender.nameCard
@ -99,7 +100,7 @@ internal object OnlinePushPbPushGroupMsg : IncomingPacketFactory<Packet?>("Onlin
val flags = extraInfo?.flags ?: 0
return GroupMessageEvent(
senderName = name.also {
if (it != sender.nameCard) {
if (sender is MemberImpl && it != sender.nameCard) {
val origin = sender._nameCard
sender._nameCard = name
MemberCardChangeEvent(origin, name, sender).broadcast()