diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt index 85d84c16..97446deb 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt @@ -132,12 +132,12 @@ class AuthedSession internal constructor(val bot: Bot, originKey: String, corout internal var latestUsed = currentTimeSeconds() init { - _listener = bot.subscribeAlways{ + _listener = bot.eventChannel.subscribeAlways{ if (this.bot === this@AuthedSession.bot) { this.run(messageQueue::add) } } - _cache = bot.subscribeAlways { + _cache = bot.eventChannel.subscribeAlways { if (this.bot === this@AuthedSession.bot) { cacheQueue.add(this.source) } @@ -167,7 +167,7 @@ class AuthedSession internal constructor(val bot: Bot, originKey: String, corout fun disableWebSocket() { if (_listener.isCompleted) { - _listener = bot.subscribeAlways{ + _listener = bot.eventChannel.subscribeAlways{ if (this.bot === this@AuthedSession.bot) { this.run(messageQueue::add) } diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt index dc427fa8..cd94c83e 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt @@ -36,7 +36,7 @@ suspend fun BotEvent.toDTO() = when (this) { authorId, messageIds.firstOrNull() ?: 0, messageTime.toLong() and 0xFFFF, - operator + operatorId ) is BotGroupPermissionChangeEvent -> BotGroupPermissionChangeEventDTO( origin, diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt index 87c17eb6..7508addb 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt @@ -26,8 +26,11 @@ import net.mamoe.mirai.event.events.TempMessageEvent import net.mamoe.mirai.message.* import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.Image.Key.queryUrl +import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource +import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage +import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsVoice +import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiInternalApi -import net.mamoe.mirai.utils.uploadImage import java.net.URL /* @@ -155,7 +158,6 @@ suspend fun MessageEvent.toDTO() = when (this) { } } -@OptIn(ExperimentalMessageKey::class) suspend inline fun MessageChain.toMessageChainDTO(filter: (MessageDTO) -> Boolean): MessageChainDTO = // `foreachContent`会忽略`MessageSource`,手动添加 mutableListOf(this.getOrFail(MessageSource).toDTO()).apply { @@ -192,43 +194,43 @@ suspend fun Message.toDTO() = when (this) { else -> UnknownMessageDTO } -@OptIn(MiraiInternalApi::class) +@OptIn(MiraiInternalApi::class, MiraiExperimentalApi::class) suspend fun MessageDTO.toMessage(contact: Contact) = when (this) { is AtDTO -> (contact as Group).getOrFail(target).at() is AtAllDTO -> AtAll is FaceDTO -> when { faceId >= 0 -> Face(faceId) name.isNotEmpty() -> Face(FaceMap[name]) - else -> Face(Face.unknown) + else -> Face(255) } is PlainDTO -> PlainText(text) is ImageDTO -> when { !imageId.isNullOrBlank() -> Image(imageId) - !url.isNullOrBlank() -> contact.uploadImage(withContext(Dispatchers.IO) { URL(url).openStream() }) + !url.isNullOrBlank() -> withContext(Dispatchers.IO) { URL(url).openStream().uploadAsImage(contact) } !path.isNullOrBlank() -> with(HttpApiPluginBase.image(path)) { if (exists()) { - contact.uploadImage(this) + uploadAsImage(contact) } else throw NoSuchFileException(this) } else -> null } is FlashImageDTO -> when { !imageId.isNullOrBlank() -> Image(imageId) - !url.isNullOrBlank() -> contact.uploadImage(withContext(Dispatchers.IO) { URL(url).openStream() }) + !url.isNullOrBlank() -> withContext(Dispatchers.IO) { URL(url).openStream().uploadAsImage(contact) } !path.isNullOrBlank() -> with(HttpApiPluginBase.image(path)) { if (exists()) { - contact.uploadImage(this) + uploadAsImage(contact) } else throw NoSuchFileException(this) } else -> null }?.flash() is VoiceDTO -> when { contact !is Group -> null - !voiceId.isNullOrBlank() -> Voice(voiceId, voiceId.substringBefore(".").toHexArray(), 0, "") - !url.isNullOrBlank() -> contact.uploadVoice(withContext(Dispatchers.IO) { URL(url).openStream() }) + !voiceId.isNullOrBlank() -> Voice(voiceId, voiceId.substringBefore(".").toHexArray(), 0, 0, "") + !url.isNullOrBlank() -> withContext(Dispatchers.IO) { URL(url).openStream().toExternalResource().uploadAsVoice(contact) } !path.isNullOrBlank() -> with(HttpApiPluginBase.voice(path)) { if (exists()) { - contact.uploadVoice(this.inputStream()) + inputStream().toExternalResource().uploadAsVoice(contact) } else throw NoSuchFileException(this) } else -> null diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt index 5fd6e3da..6cef4977 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt @@ -28,7 +28,8 @@ import net.mamoe.mirai.message.data.MessageSource.Key.quote import net.mamoe.mirai.message.data.MessageSource.Key.recall import net.mamoe.mirai.message.data.OnlineMessageSource.Incoming import net.mamoe.mirai.message.data.OnlineMessageSource.Outgoing -import net.mamoe.mirai.utils.uploadImage +import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource +import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage import java.net.URL /** @@ -209,7 +210,7 @@ fun Application.messageModule() { it.group != null -> bot.getGroupOrFail(it.group) else -> throw IllegalParamException("target、qq、group不可全为null") } - val ls = it.urls.map { url -> contact.uploadImage(URL(url).openStream()) } + val ls = it.urls.map { url -> URL(url).openStream().uploadAsImage(contact) } val receipt = contact.sendMessage(buildMessageChain { addAll(ls) }) it.session.cacheQueue.add(receipt.source) @@ -231,10 +232,10 @@ fun Application.messageModule() { ) when (type) { - "group" -> session.bot.groups.firstOrNull()?.uploadImage(newFile.await()) + "group" -> session.bot.groups.firstOrNull()?.uploadImage(newFile.await().toExternalResource()) "friend", "temp" - -> session.bot.friends.firstOrNull()?.uploadImage(newFile.await()) + -> session.bot.friends.firstOrNull()?.uploadImage(newFile.await().toExternalResource()) else -> null }.apply { // 使用apply不影响when返回 @@ -269,7 +270,7 @@ fun Application.messageModule() { ) when (type) { - "group" -> session.bot.groups.firstOrNull()?.uploadVoice(newFile.await().inputStream()) + "group" -> session.bot.groups.firstOrNull()?.uploadVoice(newFile.await().toExternalResource()) else -> null }.apply { // 使用apply不影响when返回 diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/WebSocketRouteModule.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/WebSocketRouteModule.kt index d7949f66..89a56776 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/WebSocketRouteModule.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/WebSocketRouteModule.kt @@ -36,7 +36,7 @@ fun Application.websocketRouteModule() { * 广播通知消息 */ miraiWebsocket("/message") { session -> - val listener = session.bot.subscribeMessages { + val listener = session.bot.eventChannel.subscribeMessages { content { bot === session.bot }.invoke { this.toDTO().takeIf { dto -> dto != IgnoreEventDTO }?.apply { outgoing.send(Frame.Text(this.toJson())) @@ -57,7 +57,7 @@ fun Application.websocketRouteModule() { * 广播通知事件 */ miraiWebsocket("/event") { session -> - val listener = session.bot.subscribeAlways { + val listener = session.bot.eventChannel.subscribeAlways { if (it.bot === session.bot && this !is MessageEvent) { this.toDTO().takeIf { dto -> dto != IgnoreEventDTO }?.apply { outgoing.send(Frame.Text(this.toJson())) @@ -78,7 +78,7 @@ fun Application.websocketRouteModule() { * 广播通知所有信息(消息,事件) */ miraiWebsocket("/all") { session -> - val listener = session.bot.subscribeAlways { + val listener = session.bot.eventChannel.subscribeAlways { if (it.bot === session.bot) { this.toDTO().takeIf { dto -> dto != IgnoreEventDTO }?.apply { outgoing.send(Frame.Text(this.toJson())) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/service/report/ReportService.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/service/report/ReportService.kt index 2aaa19e1..66f90b9d 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/service/report/ReportService.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/service/report/ReportService.kt @@ -17,6 +17,7 @@ import net.mamoe.mirai.api.http.service.MiraiApiHttpService import net.mamoe.mirai.api.http.util.HttpClient import net.mamoe.mirai.api.http.util.toJson import net.mamoe.mirai.console.plugin.jvm.JvmPlugin +import net.mamoe.mirai.event.GlobalEventChannel import net.mamoe.mirai.event.Listener import net.mamoe.mirai.event.events.* import net.mamoe.mirai.event.subscribeAlways @@ -46,7 +47,7 @@ class ReportService( } override fun onEnable() { - subscription = console.subscribeAlways { + subscription = GlobalEventChannel.subscribeAlways { this.takeIf { reportConfig.enable } ?.apply { this.takeIf { reportConfig.eventMessage.report }