Commit 45c6c4db authored by Him188's avatar Him188

Adapt for concurrent events

parent a2c352d1
...@@ -15,6 +15,7 @@ import kotlinx.io.core.ByteReadPacket ...@@ -15,6 +15,7 @@ import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.readBytes import kotlinx.io.core.readBytes
import kotlinx.io.core.readUInt import kotlinx.io.core.readUInt
import net.mamoe.mirai.contact.MemberPermission import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.data.MultiPacket
import net.mamoe.mirai.data.NoPacket import net.mamoe.mirai.data.NoPacket
import net.mamoe.mirai.data.Packet import net.mamoe.mirai.data.Packet
import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.events.*
...@@ -141,10 +142,9 @@ internal class OnlinePush { ...@@ -141,10 +142,9 @@ internal class OnlinePush {
@UseExperimental(ExperimentalStdlibApi::class) @UseExperimental(ExperimentalStdlibApi::class)
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): Packet { override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): Packet {
val reqPushMsg = decodeUniPacket(OnlinePushPack.SvcReqPushMsg.serializer(), "req") val reqPushMsg = decodeUniPacket(OnlinePushPack.SvcReqPushMsg.serializer(), "req")
reqPushMsg.vMsgInfos.forEach { msgInfo: MsgInfo -> val packets = reqPushMsg.vMsgInfos.mapNotNull { msgInfo: MsgInfo ->
msgInfo.vMsg!!.read { msgInfo.vMsg!!.read {
// TODO: 2020/2/13 可能会同时收到多个事件. 使用 map 而不要直接 return
when { when {
msgInfo.shMsgType.toInt() == 732 -> { msgInfo.shMsgType.toInt() == 732 -> {
val group = bot.getGroup(this.readUInt().toLong()) val group = bot.getGroup(this.readUInt().toLong())
...@@ -154,7 +154,7 @@ internal class OnlinePush { ...@@ -154,7 +154,7 @@ internal class OnlinePush {
3073 -> { // mute 3073 -> { // mute
val operatorUin = this.readUInt().toLong() val operatorUin = this.readUInt().toLong()
if (operatorUin == bot.uin) { if (operatorUin == bot.uin) {
return NoPacket return@mapNotNull null
} }
val operator = group[operatorUin] val operator = group[operatorUin]
this.readUInt().toLong() // time this.readUInt().toLong() // time
...@@ -162,7 +162,7 @@ internal class OnlinePush { ...@@ -162,7 +162,7 @@ internal class OnlinePush {
val target = this.readUInt().toLong() val target = this.readUInt().toLong()
val time = this.readInt() val time = this.readInt()
return if (target == 0L) { return@mapNotNull if (target == 0L) {
if (time == 0) { if (time == 0) {
GroupMuteAllEvent( GroupMuteAllEvent(
origin = group.isMuteAll.also { group._muteAll = false }, origin = group.isMuteAll.also { group._muteAll = false },
...@@ -179,14 +179,16 @@ internal class OnlinePush { ...@@ -179,14 +179,16 @@ internal class OnlinePush {
) )
} }
} else { } else {
return if (target == bot.uin) { if (target == bot.uin) {
if (time == 0) { @Suppress("IMPLICIT_CAST_TO_ANY") // false positive
return@mapNotNull if (time == 0) {
BotUnmuteEvent(operator) BotUnmuteEvent(operator)
} else } else
BotMuteEvent(durationSeconds = time, operator = operator) BotMuteEvent(durationSeconds = time, operator = operator)
} else { } else {
val member = group[target] val member = group[target]
if (time == 0) { @Suppress("IMPLICIT_CAST_TO_ANY") // false positive
return@mapNotNull if (time == 0) {
MemberUnmuteEvent(operator = operator, member = member) MemberUnmuteEvent(operator = operator, member = member)
} else { } else {
MemberMuteEvent(operator = operator, member = member, durationSeconds = time) MemberMuteEvent(operator = operator, member = member, durationSeconds = time)
...@@ -197,7 +199,7 @@ internal class OnlinePush { ...@@ -197,7 +199,7 @@ internal class OnlinePush {
3585 -> { // 匿名 3585 -> { // 匿名
val operator = group[this.readUInt().toLong()] val operator = group[this.readUInt().toLong()]
val switch = this.readInt() == 0 val switch = this.readInt() == 0
return GroupAllowAnonymousChatEvent( return@mapNotNull GroupAllowAnonymousChatEvent(
origin = group.isAnonymousChatEnabled.also { group._anonymousChat = switch }, origin = group.isAnonymousChatEnabled.also { group._anonymousChat = switch },
new = switch, new = switch,
operator = operator, operator = operator,
...@@ -210,7 +212,7 @@ internal class OnlinePush { ...@@ -210,7 +212,7 @@ internal class OnlinePush {
// println(dataBytes.toUHexString()) // println(dataBytes.toUHexString())
if (dataBytes[0].toInt() != 59) { if (dataBytes[0].toInt() != 59) {
return GroupNameChangeEvent( return@mapNotNull GroupNameChangeEvent(
origin = group.name.also { group._name = message }, origin = group.name.also { group._name = message },
new = message, new = message,
group = group, group = group,
...@@ -220,7 +222,7 @@ internal class OnlinePush { ...@@ -220,7 +222,7 @@ internal class OnlinePush {
//println(message + ":" + dataBytes.toUHexString()) //println(message + ":" + dataBytes.toUHexString())
when (message) { when (message) {
"管理员已关闭群聊坦白说" -> { "管理员已关闭群聊坦白说" -> {
return GroupAllowConfessTalkEvent( return@mapNotNull GroupAllowConfessTalkEvent(
origin = group.isConfessTalkEnabled.also { group._confessTalk = false }, origin = group.isConfessTalkEnabled.also { group._confessTalk = false },
new = false, new = false,
group = group, group = group,
...@@ -228,7 +230,7 @@ internal class OnlinePush { ...@@ -228,7 +230,7 @@ internal class OnlinePush {
) )
} }
"管理员已开启群聊坦白说" -> { "管理员已开启群聊坦白说" -> {
return GroupAllowConfessTalkEvent( return@mapNotNull GroupAllowConfessTalkEvent(
origin = group.isConfessTalkEnabled.also { group._confessTalk = true }, origin = group.isConfessTalkEnabled.also { group._confessTalk = true },
new = true, new = true,
group = group, group = group,
...@@ -237,7 +239,7 @@ internal class OnlinePush { ...@@ -237,7 +239,7 @@ internal class OnlinePush {
} }
else -> { else -> {
bot.network.logger.debug { "Unknown server messages $message" } bot.network.logger.debug { "Unknown server messages $message" }
return NoPacket return@mapNotNull NoPacket
} }
} }
} }
...@@ -261,9 +263,10 @@ internal class OnlinePush { ...@@ -261,9 +263,10 @@ internal class OnlinePush {
} }
} }
} }
return@mapNotNull null
} }
return NoPacket return MultiPacket(packets)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment