From fefadad3f4edf8a5c2161b79d25f3d1cf538fa0e Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 16 Dec 2020 18:33:56 +0800 Subject: [PATCH] Add blocking bridges to events --- .../commonMain/kotlin/event/events/friend.kt | 19 ++------- .../commonMain/kotlin/event/events/group.kt | 42 +++---------------- .../commonMain/kotlin/event/events/message.kt | 24 ++++++----- 3 files changed, 22 insertions(+), 63 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt b/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt index 44e7833a4..7c5ef00d0 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt @@ -13,8 +13,8 @@ package net.mamoe.mirai.event.events +import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.Bot -import net.mamoe.mirai.JavaFriendlyAPI import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.contact.Group @@ -23,8 +23,6 @@ import net.mamoe.mirai.event.AbstractEvent import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.message.action.Nudge import net.mamoe.mirai.utils.MiraiExperimentalApi - -import net.mamoe.mirai.utils.internal.runBlocking import java.util.concurrent.atomic.AtomicBoolean @@ -89,22 +87,11 @@ public data class NewFriendRequestEvent internal constructor( */ public val fromGroup: Group? = if (fromGroupId == 0L) null else bot.getGroup(fromGroupId) - @JvmSynthetic + @JvmBlockingBridge public suspend fun accept(): Unit = Mirai.acceptNewFriendRequest(this) - @JvmSynthetic + @JvmBlockingBridge public suspend fun reject(blackList: Boolean = false): Unit = Mirai.rejectNewFriendRequest(this, blackList) - - - @JavaFriendlyAPI - @JvmName("accept") - public fun __acceptBlockingForJava__(): Unit = runBlocking { accept() } - - @JavaFriendlyAPI - @JvmOverloads - @JvmName("reject") - public fun __rejectBlockingForJava__(blackList: Boolean = false): Unit = - runBlocking { reject(blackList) } } diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/group.kt b/mirai-core-api/src/commonMain/kotlin/event/events/group.kt index 290e8cb80..ee3285069 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/group.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/group.kt @@ -13,8 +13,8 @@ package net.mamoe.mirai.event.events +import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.Bot -import net.mamoe.mirai.JavaFriendlyAPI import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.contact.Group @@ -25,9 +25,7 @@ import net.mamoe.mirai.event.BroadcastControllable import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.message.action.Nudge import net.mamoe.mirai.utils.MiraiExperimentalApi -import net.mamoe.mirai.utils.internal.runBlocking import java.util.concurrent.atomic.AtomicBoolean -import kotlin.jvm.* /** * 机器人被踢出群或在其他客户端主动退出一个群. 在事件广播前 [Bot.groups] 就已删除这个群. @@ -332,21 +330,11 @@ public data class BotInvitedJoinGroupRequestEvent internal constructor( @JvmField internal val responded: AtomicBoolean = AtomicBoolean(false) - @JvmSynthetic + @JvmBlockingBridge public suspend fun accept(): Unit = Mirai.acceptInvitedJoinGroupRequest(this) - @JvmSynthetic + @JvmBlockingBridge public suspend fun ignore(): Unit = Mirai.ignoreInvitedJoinGroupRequest(this) - - @JavaFriendlyAPI - @JvmName("accept") - public fun __acceptBlockingForJava__(): Unit = - runBlocking { Mirai.acceptInvitedJoinGroupRequest(this@BotInvitedJoinGroupRequestEvent) } - - @JavaFriendlyAPI - @JvmName("ignore") - public fun __ignoreBlockingForJava__(): Unit = - runBlocking { Mirai.ignoreInvitedJoinGroupRequest(this@BotInvitedJoinGroupRequestEvent) } } /** @@ -380,34 +368,16 @@ public data class MemberJoinRequestEvent internal constructor( @PublishedApi internal val responded: AtomicBoolean = AtomicBoolean(false) - @JvmSynthetic + @JvmBlockingBridge public suspend fun accept(): Unit = Mirai.acceptMemberJoinRequest(this) - @JvmSynthetic + @JvmBlockingBridge @JvmOverloads public suspend fun reject(blackList: Boolean = false, message: String = ""): Unit = Mirai.rejectMemberJoinRequest(this, blackList, message) - @JvmSynthetic + @JvmBlockingBridge public suspend fun ignore(blackList: Boolean = false): Unit = Mirai.ignoreMemberJoinRequest(this, blackList) - - - @JavaFriendlyAPI - @JvmName("accept") - public fun __acceptBlockingForJava__(): Unit = - runBlocking { Mirai.acceptMemberJoinRequest(this@MemberJoinRequestEvent) } - - @JavaFriendlyAPI - @JvmOverloads - @JvmName("reject") - public fun __rejectBlockingForJava__(blackList: Boolean = false, message: String = ""): Unit = - runBlocking { Mirai.rejectMemberJoinRequest(this@MemberJoinRequestEvent, blackList, message) } - - @JavaFriendlyAPI - @JvmOverloads - @JvmName("ignore") - public fun __ignoreBlockingForJava__(blackList: Boolean = false): Unit = - runBlocking { Mirai.ignoreMemberJoinRequest(this@MemberJoinRequestEvent, blackList) } } // endregion diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/message.kt b/mirai-core-api/src/commonMain/kotlin/event/events/message.kt index 81e6cb9d6..8d474179a 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/message.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/message.kt @@ -13,6 +13,7 @@ package net.mamoe.mirai.event.events +import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.* import net.mamoe.mirai.event.* @@ -592,6 +593,7 @@ public interface MessageEvent : Event, Packet, BotEvent, MessageEventExtensions< } /** 消息事件的扩展函数 */ +@Suppress("UNCHECKED_CAST") public interface MessageEventExtensions : MessageEventPlatformExtensions { @@ -602,11 +604,11 @@ public interface MessageEventExtensions = subject.sendMessage(message.asMessageChain()) as MessageReceipt - @JvmSynthetic + @JvmBlockingBridge public suspend fun reply(plain: String): MessageReceipt = subject.sendMessage(PlainText(plain).asMessageChain()) as MessageReceipt @@ -633,15 +635,15 @@ public interface MessageEventExtensions = reply(this.message.quote() + message) - @JvmSynthetic + @JvmBlockingBridge public suspend fun quoteReply(message: Message): MessageReceipt = reply(this.message.quote() + message) - @JvmSynthetic + @JvmBlockingBridge public suspend fun quoteReply(plain: String): MessageReceipt = reply(this.message.quote() + plain) @JvmSynthetic @@ -670,24 +672,24 @@ public interface MessageEventPlatformExtensions = subject.sendImage(image) - @JvmSynthetic + @JvmBlockingBridge public suspend fun sendImage(image: InputStream): MessageReceipt = subject.sendImage(image) - @JvmSynthetic + @JvmBlockingBridge public suspend fun sendImage(image: File): MessageReceipt = subject.sendImage(image) // endregion