From 19bc7b80c5c1e6f4006725dad0d4d1553bee9466 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 4 Apr 2020 22:38:08 +0800 Subject: [PATCH] Rename `MessageSource.senderId` to `MessageSource.fromId` --- .../mirai/qqandroid/QQAndroidBot.common.kt | 6 +++-- .../qqandroid/message/MessageSourceImpl.kt | 19 +++++++++------- .../mirai/qqandroid/message/convension.kt | 2 +- .../mamoe/mirai/qqandroid/utils/numbers.kt | 4 ++-- .../net.mamoe.mirai/message/MessagePacket.kt | 18 --------------- .../message/data/MessageSource.kt | 22 +++++++++++-------- .../message/data/QuoteReply.kt | 15 +++++++++++-- 7 files changed, 44 insertions(+), 42 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.common.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.common.kt index e8f94beeb..71c626683 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.common.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.common.kt @@ -200,7 +200,7 @@ internal abstract class QQAndroidBotBase constructor( group.checkBotPermissionOperator() MessageRecallEvent.GroupRecall( this, - source.senderId, + source.fromId, source.id, source.time, null, @@ -222,7 +222,7 @@ internal abstract class QQAndroidBotBase constructor( -> network.run { PbMessageSvc.PbMsgWithDraw.createForFriendMessage( bot.client, - source.senderId, + source.fromId, source.sequenceId, source.id, source.time @@ -231,6 +231,8 @@ internal abstract class QQAndroidBotBase constructor( else -> error("stub!") } + + // 1001: No message meets the requirements (实际上是没权限, 管理员在尝试撤回群主的消息) check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #${source.id}: $response" } } diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceImpl.kt index f27917fae..b7af335ae 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceImpl.kt @@ -30,7 +30,6 @@ import net.mamoe.mirai.qqandroid.network.protocol.data.proto.SourceMsg import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.OnlinePush import net.mamoe.mirai.qqandroid.utils._miraiContentToString -import net.mamoe.mirai.qqandroid.utils.coerceAtMostOrFail import net.mamoe.mirai.qqandroid.utils.io.serialization.loadAs import net.mamoe.mirai.qqandroid.utils.io.serialization.toByteArray import net.mamoe.mirai.utils.MiraiExperimentalAPI @@ -146,6 +145,11 @@ internal class MessageSourceFromGroupImpl( internal class OfflineMessageSourceImpl( // from others' quotation val delegate: ImMsgBody.SourceMsg, override val bot: Bot ) : OfflineMessageSource(), MessageSourceImpl { + + init { + println(delegate._miraiContentToString()) + } + private val isRecalled: AtomicBoolean = atomic(false) override var isRecalledOrPlanned: Boolean get() = isRecalled.value @@ -164,11 +168,10 @@ internal class OfflineMessageSourceImpl( // from others' quotation */ override val id: Int - get() = delegate.pbReserve.loadAs(SourceMsg.ResvAttr.serializer()).origUids!! - .coerceAtMostOrFail(Int.MAX_VALUE.toLong()).toInt() + get() = delegate.pbReserve.loadAs(SourceMsg.ResvAttr.serializer()).origUids!!.toInt() // override val sourceMessage: MessageChain get() = delegate.toMessageChain() - override val senderId: Long get() = delegate.senderUin + override val fromId: Long get() = delegate.senderUin override val targetId: Long get() = Group.calculateGroupCodeByGroupUin(delegate.toUin) } @@ -196,7 +199,7 @@ internal class MessageSourceToFriendImpl( val messageUid: Long = sequenceId.toLong().shl(32) or id.toLong().and(0xffFFffFF) return ImMsgBody.SourceMsg( origSeqs = listOf(sequenceId), - senderUin = senderId, + senderUin = fromId, toUin = targetId, flag = 1, elems = elems, @@ -207,7 +210,7 @@ internal class MessageSourceToFriendImpl( ).toByteArray(SourceMsg.ResvAttr.serializer()), srcMsg = MsgComm.Msg( msgHead = MsgComm.MsgHead( - fromUin = senderId, // qq + fromUin = fromId, // qq toUin = targetId, // group msgType = 9, // 82? c2cCmd = 11, @@ -271,7 +274,7 @@ internal class MessageSourceToGroupImpl( fun toJceDataImplForGroup(): ImMsgBody.SourceMsg { return ImMsgBody.SourceMsg( origSeqs = listOf(sequenceId), - senderUin = senderId, + senderUin = fromId, toUin = Group.calculateGroupUinByGroupCode(targetId), flag = 1, elems = elems, @@ -282,7 +285,7 @@ internal class MessageSourceToGroupImpl( ).toByteArray(SourceMsg.ResvAttr.serializer()), srcMsg = MsgComm.Msg( msgHead = MsgComm.MsgHead( - fromUin = senderId, // qq + fromUin = fromId, // qq toUin = Group.calculateGroupUinByGroupCode(targetId), // group msgType = 82, // 82? c2cCmd = 1, diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/convension.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/convension.kt index de4f7cb73..b56bf8f21 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/convension.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/convension.kt @@ -241,7 +241,7 @@ internal inline fun Iterable<*>.firstIsInstance(): R { return it } } - throw NoSuchElementException("Collection contains no element matching the predicate.") + throw NoSuchElementException("Collection contains no element is ${R::class}") } internal inline fun Iterable<*>.firstIsInstanceOrNull(): R? { diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/numbers.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/numbers.kt index 856584958..e0ab1c2fa 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/numbers.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/numbers.kt @@ -38,10 +38,10 @@ internal fun Long.coerceAtLeastOrFail(min: Long): Long { */ @PublishedApi internal fun Int.coerceAtMostOrFail(max: Int): Int = - if (this >= max) error("value is greater than its expected maximum value $max") + if (this >= max) error("value $this is greater than its expected maximum value $max") else this @PublishedApi internal fun Long.coerceAtMostOrFail(max: Long): Long = - if (this >= max) error("value is greater than its expected maximum value $max") + if (this >= max) error("value $this is greater than its expected maximum value $max") else this \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt index f922808b0..e0ccaf11a 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt @@ -34,8 +34,6 @@ import net.mamoe.mirai.event.subscribingGetOrNull import net.mamoe.mirai.event.whileSelectMessages import net.mamoe.mirai.message.data.* import net.mamoe.mirai.qqandroid.network.Packet -import net.mamoe.mirai.recall -import net.mamoe.mirai.recallIn import net.mamoe.mirai.utils.* import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext @@ -124,23 +122,7 @@ abstract class MessagePacketBase : Pac // endregion // region 撤回 - suspend inline fun MessageChain.recall() = bot.recall(this) - suspend inline fun MessageSource.recall() = bot.recall(this) - suspend inline fun QuoteReply.recall() = bot.recall(this.source) - inline fun MessageChain.recallIn( - millis: Long, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ) = bot.recallIn(this, millis, coroutineContext) - inline fun MessageSource.recallIn( - millis: Long, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ) = bot.recallIn(this, millis, coroutineContext) - - inline fun QuoteReply.recallIn( - millis: Long, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ) = bot.recallIn(this.source, millis, coroutineContext) // endregion // region 上传图片 diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt index c884707de..be526522d 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt @@ -63,7 +63,7 @@ sealed class MessageSource : Message, MessageMetadata { /** * 发送人. 可能为机器人自己, 好友的 id, 或群 id */ - abstract val senderId: Long + abstract val fromId: Long /** * 发送目标. 可能为机器人自己, 好友的 id, 或群 id @@ -126,7 +126,7 @@ sealed class OnlineMessageSource : MessageSource() { abstract override val sender: Bot abstract override val target: Contact - final override val senderId: Long get() = sender.id + final override val fromId: Long get() = sender.id final override val targetId: Long get() = target.id abstract class ToFriend : Outgoing() { @@ -155,7 +155,7 @@ sealed class OnlineMessageSource : MessageSource() { abstract override val sender: QQ // out QQ abstract override val target: Bot - final override val senderId: Long get() = sender.id + final override val fromId: Long get() = sender.id final override val targetId: Long get() = target.id abstract class FromFriend : Incoming() { @@ -190,13 +190,10 @@ fun OnlineMessageSource.quote(): QuoteReply { */ fun MessageChain.quote(): QuoteReply { @OptIn(MiraiInternalAPI::class) - return QuoteReply(this.source) + return QuoteReply(this.source as? OnlineMessageSource ?: error("only online messages can be quoted")) } -/** - * 撤回这条消息 - */ -suspend inline fun OnlineMessageSource.recall() = bot.recall(this) +suspend inline fun MessageSource.recall() = bot.recall(this) /** * 撤回这条消息 @@ -237,4 +234,11 @@ inline val MessageChain.id: Int */ @get:JvmSynthetic inline val MessageChain.source: MessageSource - get() = this[MessageSource] \ No newline at end of file + get() = this[MessageSource] + +suspend inline fun MessageChain.recall() = this.source.recall() + +inline fun MessageChain.recallIn( + millis: Long, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Job = source.recallIn(millis, coroutineContext) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/QuoteReply.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/QuoteReply.kt index 9e62482b7..3f7394a0a 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/QuoteReply.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/QuoteReply.kt @@ -9,11 +9,15 @@ @file:JvmMultifileClass @file:JvmName("MessageUtils") +@file:Suppress("NOTHING_TO_INLINE") package net.mamoe.mirai.message.data +import kotlinx.coroutines.Job import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.utils.SinceMirai +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName @@ -27,9 +31,16 @@ import kotlin.jvm.JvmName * @see MessageSource 获取更多信息 */ @SinceMirai("0.33.0") -class QuoteReply(val source: MessageSource) : Message, MessageMetadata { +class QuoteReply(val source: OnlineMessageSource) : Message, MessageMetadata { // TODO: 2020/4/4 Metadata or Content? companion object Key : Message.Key override fun toString(): String = "[mirai:quote]" -} \ No newline at end of file +} + +suspend inline fun QuoteReply.recall() = this.source.recall() + +inline fun QuoteReply.recallIn( + millis: Long, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Job = this.source.recallIn(millis, coroutineContext) \ No newline at end of file