Introduce AbstractEvent acting as all events' super class

This commit is contained in:
Him188 2020-05-01 14:44:12 +08:00
parent b47955a824
commit 16c440433f
9 changed files with 136 additions and 94 deletions

View File

@ -1,19 +0,0 @@
/*
* Copyright 2020 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/master/LICENSE
*/
package net.mamoe.mirai.qqandroid.event
import net.mamoe.mirai.qqandroid.network.Packet
import net.mamoe.mirai.event.AbstractCancellableEvent
import net.mamoe.mirai.event.Event
/**
* 接收到数据包
*/
data class PacketReceivedEvent(val packet: Packet) : Event, AbstractCancellableEvent()

View File

@ -26,7 +26,6 @@ import net.mamoe.mirai.network.UnsupportedSMSLoginException
import net.mamoe.mirai.network.WrongPasswordException
import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.contact.*
import net.mamoe.mirai.qqandroid.event.PacketReceivedEvent
import net.mamoe.mirai.qqandroid.network.protocol.data.jce.StTroopNum
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgSvc
import net.mamoe.mirai.qqandroid.network.protocol.packet.*
@ -511,10 +510,6 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
}
}
if (packet != null && PacketReceivedEvent(packet).broadcast().isCancelled) {
return
}
if (packet is Event) {
if (packet is BroadcastControllable) {
if (packet.shouldBroadcast) packet.broadcast()

View File

@ -23,6 +23,7 @@ import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.event.AbstractEvent
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.events.BotJoinGroupEvent
import net.mamoe.mirai.event.events.BotOfflineEvent
@ -36,7 +37,7 @@ import net.mamoe.mirai.qqandroid.contact.GroupImpl
import net.mamoe.mirai.qqandroid.contact.checkIsFriendImpl
import net.mamoe.mirai.qqandroid.contact.checkIsMemberImpl
import net.mamoe.mirai.qqandroid.message.*
import net.mamoe.mirai.qqandroid.network.MultiPacketByIterable
import net.mamoe.mirai.qqandroid.network.MultiPacket
import net.mamoe.mirai.qqandroid.network.Packet
import net.mamoe.mirai.qqandroid.network.QQAndroidClient
import net.mamoe.mirai.qqandroid.network.protocol.data.jce.RequestPushForceOffline
@ -50,7 +51,6 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.*
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.GroupInfoImpl
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.NewContact
import net.mamoe.mirai.qqandroid.network.protocol.packet.list.FriendList
import net.mamoe.mirai.qqandroid.utils._miraiContentToString
import net.mamoe.mirai.qqandroid.utils.io.serialization.readProtoBuf
import net.mamoe.mirai.qqandroid.utils.io.serialization.readUniPacket
import net.mamoe.mirai.qqandroid.utils.io.serialization.toByteArray
@ -129,7 +129,10 @@ internal class MessageSvc {
*/
@MiraiInternalAPI
open class Response(internal val syncFlagFromServer: MsgSvc.SyncFlag, delegate: List<Packet>) :
MultiPacketByIterable<Packet>(delegate) {
AbstractEvent(),
MultiPacket<Packet>,
Iterable<Packet> by (delegate) {
override fun toString(): String =
"MessageSvc.PbGetMsg.Response(syncFlagFromServer=$syncFlagFromServer, messages=<Iterable>))"
}

View File

@ -17,6 +17,7 @@ import net.mamoe.mirai.LowLevelAPI
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.data.FriendInfo
import net.mamoe.mirai.event.AbstractEvent
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.*
@ -57,7 +58,7 @@ internal class OnlinePush {
internal class SendGroupMessageReceipt(
val messageRandom: Int,
val sequenceId: Int
) : Packet, Event, Packet.NoLog {
) : Packet, Event, Packet.NoLog, AbstractEvent() {
override fun toString(): String {
return "OnlinePush.PbPushGroupMsg.SendGroupMessageReceipt(messageRandom=$messageRandom, sequenceId=$sequenceId)"
}

View File

@ -12,6 +12,7 @@ package net.mamoe.mirai.qqandroid.network.protocol.packet.login
import kotlinx.io.core.ByteReadPacket
import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoId
import net.mamoe.mirai.event.AbstractEvent
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.network.Packet
@ -39,7 +40,7 @@ internal class ConfigPushSvc {
) {
override val canBeCached: Boolean get() = false
sealed class PushReqResponse : Packet, Event {
sealed class PushReqResponse : Packet, Event, AbstractEvent() {
class Success(
val struct: PushReqJceStruct
) : PushReqResponse() {

View File

@ -11,14 +11,20 @@
package net.mamoe.mirai.event
import kotlinx.atomicfu.atomic
import net.mamoe.mirai.event.internal.broadcastInternal
import net.mamoe.mirai.utils.MiraiInternalAPI
import net.mamoe.mirai.utils.PlannedRemoval
import net.mamoe.mirai.utils.SinceMirai
import kotlin.jvm.JvmSynthetic
/**
* 可被监听的类, 可以是任何 class object.
*
* 若监听这个类, 监听器将会接收所有事件的广播.
*
* 所有 [Event] 都应继承 [AbstractEvent] 而不要直接实现 [Event]. 否则将无法广播也无法监听.
*
* @see subscribeAlways
* @see subscribeOnce
*
@ -27,12 +33,74 @@ import net.mamoe.mirai.utils.MiraiInternalAPI
* @see [broadcast] 广播事件
* @see [subscribe] 监听事件
*/
interface Event
interface Event {
@Deprecated("""
Don't implement Event but extend AbstractEvent instead.
""", level = DeprecationLevel.HIDDEN) // so Kotlin class won't be compiled.
@Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION", "PropertyName")
@get:JvmSynthetic // so Java user won't see it
internal val DoNotImplementThisClassButExtendAbstractEvent: Nothing
}
/**
* 所有实现了 [Event] 接口的类都应该继承的父类.
*/
@SinceMirai("1.0.0")
abstract class AbstractEvent : Event {
@Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION", "PropertyName")
@get:JvmSynthetic // so Java user won't see it
@Deprecated("", level = DeprecationLevel.HIDDEN)
final override val DoNotImplementThisClassButExtendAbstractEvent: Nothing
get() = throw Error("Shouldn't be reached")
private val _intercepted = atomic(false)
private val _cancelled = atomic(false)
/**
* 事件是否已被拦截.
*
* 所有事件都可以被拦截, 拦截后低优先级的监听器将不会处理到这个事件.
*/
@SinceMirai("1.0.0")
val isIntercepted: Boolean
get() = _intercepted.value
/**
* 拦截这个事件.
* 重复拦截时不会抛出异常.
*/
@SinceMirai("1.0.0")
fun intercept() {
_intercepted.value = true
}
/**
* 事件是否已取消.
*
* 事件需实现 [CancellableEvent] 接口才可以被取消,
* 否则此属性固定返回 false.
*/
val isCancelled: Boolean get() = _cancelled.value
/**
* 取消这个事件.
* 重复取消时不会抛出异常.
*/
fun cancel() {
check(this is CancellableEvent) {
"Event $this is not cancellable"
}
_cancelled.value = true
}
}
/**
* 可被取消的事件
*/
interface CancellableEvent {
interface CancellableEvent : Event {
/**
* 事件是否已取消.
*/
@ -44,25 +112,6 @@ interface CancellableEvent {
fun cancel()
}
/**
* 可被取消的事件的实现
*/
abstract class AbstractCancellableEvent : Event, CancellableEvent {
/**
* 事件是否已取消.
*/
override val isCancelled: Boolean get() = _cancelled
private var _cancelled: Boolean = false
/**
* 取消事件.
*/
override fun cancel() {
_cancelled = true
}
}
/**
* 广播一个事件的唯一途径.
*/
@ -81,9 +130,20 @@ suspend fun <E : Event> E.broadcast(): E = apply {
var EventDisabled = false
/**
* 可控制是否需要广播这个事件
* 可控制是否需要广播这个事件
*/
interface BroadcastControllable : Event {
/**
* 返回 `false` 时将不会广播这个事件.
*/
val shouldBroadcast: Boolean
get() = true
}
}
@PlannedRemoval("1.1.0")
@Deprecated(
"use AbstractEvent and implement CancellableEvent",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("AbstractEvent", "net.mamoe.mirai.event.AbstractEvent"))
abstract class AbstractCancellableEvent : AbstractEvent(), CancellableEvent

View File

@ -15,7 +15,7 @@ package net.mamoe.mirai.event.events
import net.mamoe.mirai.Bot
import net.mamoe.mirai.JavaFriendlyAPI
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.AbstractCancellableEvent
import net.mamoe.mirai.event.AbstractEvent
import net.mamoe.mirai.event.BroadcastControllable
import net.mamoe.mirai.event.CancellableEvent
import net.mamoe.mirai.event.events.ImageUploadEvent.Failed
@ -49,12 +49,12 @@ class EventCancelledException : RuntimeException {
/**
* [Bot] 登录完成, 好友列表, 群组列表初始化完成
*/
data class BotOnlineEvent(override val bot: Bot) : BotActiveEvent
data class BotOnlineEvent(override val bot: Bot) : BotActiveEvent, AbstractEvent()
/**
* [Bot] 离线.
*/
sealed class BotOfflineEvent : BotEvent {
sealed class BotOfflineEvent : BotEvent, AbstractEvent() {
/**
* 主动离线
@ -85,7 +85,7 @@ sealed class BotOfflineEvent : BotEvent {
data class BotReloginEvent(
override val bot: Bot,
val cause: Throwable?
) : BotEvent, BotActiveEvent
) : BotEvent, BotActiveEvent, AbstractEvent()
// endregion
@ -94,7 +94,7 @@ data class BotReloginEvent(
/**
* 主动发送消息
*/
sealed class MessageSendEvent : BotEvent, BotActiveEvent, AbstractCancellableEvent() {
sealed class MessageSendEvent : BotEvent, BotActiveEvent, AbstractEvent() {
abstract val target: Contact
final override val bot: Bot
get() = target.bot
@ -115,7 +115,7 @@ sealed class MessageSendEvent : BotEvent, BotActiveEvent, AbstractCancellableEve
/**
* 消息撤回事件. 可是任意消息被任意人撤回.
*/
sealed class MessageRecallEvent : BotEvent {
sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
/**
* 消息原发送人
*/
@ -197,7 +197,7 @@ val MessageRecallEvent.isByBot: Boolean
data class BeforeImageUploadEvent(
val target: Contact,
val source: ExternalImage
) : BotEvent, BotActiveEvent, AbstractCancellableEvent() {
) : BotEvent, BotActiveEvent, AbstractEvent(), CancellableEvent {
override val bot: Bot
get() = target.bot
}
@ -208,7 +208,7 @@ data class BeforeImageUploadEvent(
* @see Succeed
* @see Failed
*/
sealed class ImageUploadEvent : BotEvent, BotActiveEvent, AbstractCancellableEvent() {
sealed class ImageUploadEvent : BotEvent, BotActiveEvent, AbstractEvent() {
abstract val target: Contact
abstract val source: ExternalImage
override val bot: Bot
@ -218,14 +218,14 @@ sealed class ImageUploadEvent : BotEvent, BotActiveEvent, AbstractCancellableEve
override val target: Contact,
override val source: ExternalImage,
val image: Image
) : ImageUploadEvent(), CancellableEvent
) : ImageUploadEvent()
data class Failed(
override val target: Contact,
override val source: ExternalImage,
val errno: Int,
val message: String
) : ImageUploadEvent(), CancellableEvent
) : ImageUploadEvent()
}
// endregion
@ -236,7 +236,7 @@ sealed class ImageUploadEvent : BotEvent, BotActiveEvent, AbstractCancellableEve
* 机器人被踢出群或在其他客户端主动退出一个群. 在事件广播前 [Bot.groups] 就已删除这个群.
*/
@SinceMirai("0.36.0")
sealed class BotLeaveEvent : BotEvent, Packet {
sealed class BotLeaveEvent : BotEvent, Packet, AbstractEvent() {
abstract val group: Group
/**
@ -261,7 +261,7 @@ data class BotGroupPermissionChangeEvent(
override val group: Group,
val origin: MemberPermission,
val new: MemberPermission
) : BotPassiveEvent, GroupEvent, Packet
) : BotPassiveEvent, GroupEvent, Packet, AbstractEvent()
/**
* Bot 被禁言
@ -272,7 +272,7 @@ data class BotMuteEvent(
* 操作人.
*/
val operator: Member
) : GroupEvent, Packet, BotPassiveEvent {
) : GroupEvent, Packet, BotPassiveEvent, AbstractEvent() {
override val group: Group
get() = operator.group
}
@ -285,7 +285,7 @@ data class BotUnmuteEvent(
* 操作人.
*/
val operator: Member
) : GroupEvent, Packet, BotPassiveEvent {
) : GroupEvent, Packet, BotPassiveEvent, AbstractEvent() {
override val group: Group
get() = operator.group
}
@ -296,7 +296,7 @@ data class BotUnmuteEvent(
@MiraiExperimentalAPI
data class BotJoinGroupEvent(
override val group: Group
) : BotPassiveEvent, GroupEvent, Packet
) : BotPassiveEvent, GroupEvent, Packet, AbstractEvent()
// region 群设置
@ -323,7 +323,7 @@ data class GroupNameChangeEvent(
*/
@SinceMirai("0.37.3")
override val operator: Member?
) : GroupSettingChangeEvent<String>, Packet, GroupOperableEvent {
) : GroupSettingChangeEvent<String>, Packet, GroupOperableEvent, AbstractEvent() {
@Deprecated("for binary compatibility", level = DeprecationLevel.HIDDEN)
val isByBot: Boolean
get() = operator == null
@ -340,7 +340,7 @@ data class GroupEntranceAnnouncementChangeEvent(
* 操作人. null 时则是机器人操作
*/
override val operator: Member?
) : GroupSettingChangeEvent<String>, Packet, GroupOperableEvent
) : GroupSettingChangeEvent<String>, Packet, GroupOperableEvent, AbstractEvent()
/**
@ -354,7 +354,7 @@ data class GroupMuteAllEvent(
* 操作人. null 时则是机器人操作
*/
override val operator: Member?
) : GroupSettingChangeEvent<Boolean>, Packet, GroupOperableEvent
) : GroupSettingChangeEvent<Boolean>, Packet, GroupOperableEvent, AbstractEvent()
/**
@ -368,7 +368,7 @@ data class GroupAllowAnonymousChatEvent(
* 操作人. null 时则是机器人操作
*/
override val operator: Member?
) : GroupSettingChangeEvent<Boolean>, Packet, GroupOperableEvent
) : GroupSettingChangeEvent<Boolean>, Packet, GroupOperableEvent, AbstractEvent()
/**
@ -379,7 +379,7 @@ data class GroupAllowConfessTalkEvent(
override val new: Boolean,
override val group: Group,
val isByBot: Boolean // 无法获取操作人
) : GroupSettingChangeEvent<Boolean>, Packet
) : GroupSettingChangeEvent<Boolean>, Packet, AbstractEvent()
/**
* "允许群员邀请好友加群" 功能状态改变. 此事件广播前修改就已经完成.
@ -392,7 +392,7 @@ data class GroupAllowMemberInviteEvent(
* 操作人. null 时则是机器人操作
*/
override val operator: Member?
) : GroupSettingChangeEvent<Boolean>, Packet, GroupOperableEvent
) : GroupSettingChangeEvent<Boolean>, Packet, GroupOperableEvent, AbstractEvent()
// endregion
@ -405,7 +405,7 @@ data class GroupAllowMemberInviteEvent(
/**
* 成员已经加入群的事件
*/
sealed class MemberJoinEvent(override val member: Member) : GroupMemberEvent, BotPassiveEvent, Packet {
sealed class MemberJoinEvent(override val member: Member) : GroupMemberEvent, BotPassiveEvent, Packet, AbstractEvent() {
/**
* 被邀请加入群
*/
@ -422,7 +422,7 @@ sealed class MemberJoinEvent(override val member: Member) : GroupMemberEvent, Bo
/**
* 成员已经离开群的事件. 在事件广播前成员就已经从 [Group.members] 中删除
*/
sealed class MemberLeaveEvent : GroupMemberEvent {
sealed class MemberLeaveEvent : GroupMemberEvent, AbstractEvent() {
/**
* 成员被踢出群. 成员不可能是机器人自己.
*/
@ -468,7 +468,7 @@ data class BotInvitedJoinGroupRequestEvent(
* 邀请人昵称
*/
val invitorNick: String
) : BotEvent, Packet {
) : BotEvent, Packet, AbstractEvent() {
val invitor: Friend = this.bot.getFriend(invitorId)
@JvmField
@ -515,7 +515,7 @@ data class MemberJoinRequestEvent(
* 申请人昵称
*/
val fromNick: String
) : BotEvent, Packet {
) : BotEvent, Packet, AbstractEvent() {
val group: Group = this.bot.getGroup(groupId)
@JvmField
@ -574,8 +574,9 @@ data class MemberCardChangeEvent(
@PlannedRemoval("1.0.0")
@Deprecated("operator is always unknown", level = DeprecationLevel.ERROR)
override val operator: Member?
) : GroupMemberEvent, Packet,
@Deprecated("operator is always unknown", level = DeprecationLevel.ERROR) GroupOperableEvent
) : GroupMemberEvent, Packet, AbstractEvent(),
@Deprecated("operator is always unknown", level = DeprecationLevel.ERROR)
GroupOperableEvent
/**
* 成员群头衔改动. 一定为群主操作
@ -599,7 +600,7 @@ data class MemberSpecialTitleChangeEvent(
* null 时则是机器人操作.
*/
override val operator: Member?
) : GroupMemberEvent, GroupOperableEvent
) : GroupMemberEvent, GroupOperableEvent, AbstractEvent()
// endregion
@ -613,7 +614,7 @@ data class MemberPermissionChangeEvent(
override val member: Member,
val origin: MemberPermission,
val new: MemberPermission
) : GroupMemberEvent, BotPassiveEvent, Packet
) : GroupMemberEvent, BotPassiveEvent, Packet, AbstractEvent()
// endregion
@ -630,7 +631,7 @@ data class MemberMuteEvent(
* 操作人. null 则为机器人操作
*/
override val operator: Member?
) : GroupMemberEvent, Packet, GroupOperableEvent
) : GroupMemberEvent, Packet, GroupOperableEvent, AbstractEvent()
/**
* 群成员被取消禁言事件. 被禁言的成员都不可能是机器人本人
@ -641,7 +642,7 @@ data class MemberUnmuteEvent(
* 操作人. null 则为机器人操作
*/
override val operator: Member?
) : GroupMemberEvent, Packet, GroupOperableEvent
) : GroupMemberEvent, Packet, GroupOperableEvent, AbstractEvent()
// endregion
@ -659,8 +660,9 @@ data class FriendRemarkChangeEvent(
override val bot: Bot,
val friend: Friend,
val newName: String
) : BotEvent, Packet {
) : BotEvent, Packet, AbstractEvent() {
@PlannedRemoval("1.0.0")
@Deprecated("", level = DeprecationLevel.HIDDEN)
@get:JvmSynthetic
@get:JvmName("getFriend")
@ -678,9 +680,10 @@ data class FriendAddEvent(
* 新好友. 已经添加到 [Bot.friends]
*/
val friend: Friend
) : BotEvent, Packet {
) : BotEvent, Packet, AbstractEvent() {
override val bot: Bot get() = friend.bot
@PlannedRemoval("1.0.0")
@Deprecated("", level = DeprecationLevel.HIDDEN)
@get:JvmSynthetic
@get:JvmName("getFriend")
@ -695,9 +698,10 @@ data class FriendAddEvent(
@SinceMirai("0.36.0")
data class FriendDeleteEvent(
val friend: Friend
) : BotEvent, Packet {
) : BotEvent, Packet, AbstractEvent() {
override val bot: Bot get() = friend.bot
@PlannedRemoval("1.0.0")
@Deprecated("", level = DeprecationLevel.HIDDEN)
@get:JvmSynthetic
@get:JvmName("getFriend")
@ -732,7 +736,7 @@ data class NewFriendRequestEvent(
* 群名片或好友昵称
*/
val fromNick: String
) : BotEvent, Packet {
) : BotEvent, Packet, AbstractEvent() {
@JvmField
internal val responded: MiraiAtomicBoolean = MiraiAtomicBoolean(false)

View File

@ -24,11 +24,8 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.io.ByteReadChannel
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.*
import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.event.selectMessages
import net.mamoe.mirai.event.syncFromEvent
import net.mamoe.mirai.event.syncFromEventOrNull
import net.mamoe.mirai.event.whileSelectMessages
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.qqandroid.network.Packet
import net.mamoe.mirai.utils.*
@ -71,7 +68,7 @@ expect abstract class MessagePacket<TSender : User, TSubject : Contact> construc
replaceWith = ReplaceWith("ContactMessage", "net.mamoe.mirai.message.ContactMessage")
)
@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
abstract class MessagePacketBase<out TSender : User, out TSubject : Contact> : Packet, BotEvent {
abstract class MessagePacketBase<out TSender : User, out TSubject : Contact> : Packet, BotEvent, AbstractEvent() {
/**
* 接受到这条消息的
*/

View File

@ -15,7 +15,7 @@ import net.mamoe.mirai.utils.internal.runBlocking
import kotlin.test.Test
import kotlin.test.assertTrue
class TestEvent : Event {
class TestEvent : Event, AbstractEvent() {
var triggered = false
}
@ -44,7 +44,7 @@ class EventTests {
}
open class ParentEvent : Event {
open class ParentEvent : Event, AbstractEvent() {
var triggered = false
}