Rename MessageSource.senderId to MessageSource.fromId

This commit is contained in:
Him188 2020-04-04 22:38:08 +08:00
parent 039b3d7517
commit 19bc7b80c5
7 changed files with 44 additions and 42 deletions

View File

@ -200,7 +200,7 @@ internal abstract class QQAndroidBotBase constructor(
group.checkBotPermissionOperator() group.checkBotPermissionOperator()
MessageRecallEvent.GroupRecall( MessageRecallEvent.GroupRecall(
this, this,
source.senderId, source.fromId,
source.id, source.id,
source.time, source.time,
null, null,
@ -222,7 +222,7 @@ internal abstract class QQAndroidBotBase constructor(
-> network.run { -> network.run {
PbMessageSvc.PbMsgWithDraw.createForFriendMessage( PbMessageSvc.PbMsgWithDraw.createForFriendMessage(
bot.client, bot.client,
source.senderId, source.fromId,
source.sequenceId, source.sequenceId,
source.id, source.id,
source.time source.time
@ -231,6 +231,8 @@ internal abstract class QQAndroidBotBase constructor(
else -> error("stub!") else -> error("stub!")
} }
// 1001: No message meets the requirements (实际上是没权限, 管理员在尝试撤回群主的消息)
check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #${source.id}: $response" } check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #${source.id}: $response" }
} }

View File

@ -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.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.OnlinePush import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.OnlinePush
import net.mamoe.mirai.qqandroid.utils._miraiContentToString 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.loadAs
import net.mamoe.mirai.qqandroid.utils.io.serialization.toByteArray import net.mamoe.mirai.qqandroid.utils.io.serialization.toByteArray
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
@ -146,6 +145,11 @@ internal class MessageSourceFromGroupImpl(
internal class OfflineMessageSourceImpl( // from others' quotation internal class OfflineMessageSourceImpl( // from others' quotation
val delegate: ImMsgBody.SourceMsg, override val bot: Bot val delegate: ImMsgBody.SourceMsg, override val bot: Bot
) : OfflineMessageSource(), MessageSourceImpl { ) : OfflineMessageSource(), MessageSourceImpl {
init {
println(delegate._miraiContentToString())
}
private val isRecalled: AtomicBoolean = atomic(false) private val isRecalled: AtomicBoolean = atomic(false)
override var isRecalledOrPlanned: Boolean override var isRecalledOrPlanned: Boolean
get() = isRecalled.value get() = isRecalled.value
@ -164,11 +168,10 @@ internal class OfflineMessageSourceImpl( // from others' quotation
*/ */
override val id: Int override val id: Int
get() = delegate.pbReserve.loadAs(SourceMsg.ResvAttr.serializer()).origUids!! get() = delegate.pbReserve.loadAs(SourceMsg.ResvAttr.serializer()).origUids!!.toInt()
.coerceAtMostOrFail(Int.MAX_VALUE.toLong()).toInt()
// override val sourceMessage: MessageChain get() = delegate.toMessageChain() // 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) 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) val messageUid: Long = sequenceId.toLong().shl(32) or id.toLong().and(0xffFFffFF)
return ImMsgBody.SourceMsg( return ImMsgBody.SourceMsg(
origSeqs = listOf(sequenceId), origSeqs = listOf(sequenceId),
senderUin = senderId, senderUin = fromId,
toUin = targetId, toUin = targetId,
flag = 1, flag = 1,
elems = elems, elems = elems,
@ -207,7 +210,7 @@ internal class MessageSourceToFriendImpl(
).toByteArray(SourceMsg.ResvAttr.serializer()), ).toByteArray(SourceMsg.ResvAttr.serializer()),
srcMsg = MsgComm.Msg( srcMsg = MsgComm.Msg(
msgHead = MsgComm.MsgHead( msgHead = MsgComm.MsgHead(
fromUin = senderId, // qq fromUin = fromId, // qq
toUin = targetId, // group toUin = targetId, // group
msgType = 9, // 82? msgType = 9, // 82?
c2cCmd = 11, c2cCmd = 11,
@ -271,7 +274,7 @@ internal class MessageSourceToGroupImpl(
fun toJceDataImplForGroup(): ImMsgBody.SourceMsg { fun toJceDataImplForGroup(): ImMsgBody.SourceMsg {
return ImMsgBody.SourceMsg( return ImMsgBody.SourceMsg(
origSeqs = listOf(sequenceId), origSeqs = listOf(sequenceId),
senderUin = senderId, senderUin = fromId,
toUin = Group.calculateGroupUinByGroupCode(targetId), toUin = Group.calculateGroupUinByGroupCode(targetId),
flag = 1, flag = 1,
elems = elems, elems = elems,
@ -282,7 +285,7 @@ internal class MessageSourceToGroupImpl(
).toByteArray(SourceMsg.ResvAttr.serializer()), ).toByteArray(SourceMsg.ResvAttr.serializer()),
srcMsg = MsgComm.Msg( srcMsg = MsgComm.Msg(
msgHead = MsgComm.MsgHead( msgHead = MsgComm.MsgHead(
fromUin = senderId, // qq fromUin = fromId, // qq
toUin = Group.calculateGroupUinByGroupCode(targetId), // group toUin = Group.calculateGroupUinByGroupCode(targetId), // group
msgType = 82, // 82? msgType = 82, // 82?
c2cCmd = 1, c2cCmd = 1,

View File

@ -241,7 +241,7 @@ internal inline fun <reified R> Iterable<*>.firstIsInstance(): R {
return it return it
} }
} }
throw NoSuchElementException("Collection contains no element matching the predicate.") throw NoSuchElementException("Collection contains no element is ${R::class}")
} }
internal inline fun <reified R> Iterable<*>.firstIsInstanceOrNull(): R? { internal inline fun <reified R> Iterable<*>.firstIsInstanceOrNull(): R? {

View File

@ -38,10 +38,10 @@ internal fun Long.coerceAtLeastOrFail(min: Long): Long {
*/ */
@PublishedApi @PublishedApi
internal fun Int.coerceAtMostOrFail(max: Int): Int = 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 else this
@PublishedApi @PublishedApi
internal fun Long.coerceAtMostOrFail(max: Long): Long = 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 else this

View File

@ -34,8 +34,6 @@ import net.mamoe.mirai.event.subscribingGetOrNull
import net.mamoe.mirai.event.whileSelectMessages import net.mamoe.mirai.event.whileSelectMessages
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.qqandroid.network.Packet import net.mamoe.mirai.qqandroid.network.Packet
import net.mamoe.mirai.recall
import net.mamoe.mirai.recallIn
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext import kotlin.coroutines.EmptyCoroutineContext
@ -124,23 +122,7 @@ abstract class MessagePacketBase<out TSender : QQ, out TSubject : Contact> : Pac
// endregion // endregion
// region 撤回 // 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 // endregion
// region 上传图片 // region 上传图片

View File

@ -63,7 +63,7 @@ sealed class MessageSource : Message, MessageMetadata {
/** /**
* 发送人. 可能为机器人自己, 好友的 id, 或群 id * 发送人. 可能为机器人自己, 好友的 id, 或群 id
*/ */
abstract val senderId: Long abstract val fromId: Long
/** /**
* 发送目标. 可能为机器人自己, 好友的 id, 或群 id * 发送目标. 可能为机器人自己, 好友的 id, 或群 id
@ -126,7 +126,7 @@ sealed class OnlineMessageSource : MessageSource() {
abstract override val sender: Bot abstract override val sender: Bot
abstract override val target: Contact 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 final override val targetId: Long get() = target.id
abstract class ToFriend : Outgoing() { abstract class ToFriend : Outgoing() {
@ -155,7 +155,7 @@ sealed class OnlineMessageSource : MessageSource() {
abstract override val sender: QQ // out QQ abstract override val sender: QQ // out QQ
abstract override val target: Bot 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 final override val targetId: Long get() = target.id
abstract class FromFriend : Incoming() { abstract class FromFriend : Incoming() {
@ -190,13 +190,10 @@ fun OnlineMessageSource.quote(): QuoteReply {
*/ */
fun MessageChain.quote(): QuoteReply { fun MessageChain.quote(): QuoteReply {
@OptIn(MiraiInternalAPI::class) @OptIn(MiraiInternalAPI::class)
return QuoteReply(this.source) return QuoteReply(this.source as? OnlineMessageSource ?: error("only online messages can be quoted"))
} }
/** suspend inline fun MessageSource.recall() = bot.recall(this)
* 撤回这条消息
*/
suspend inline fun OnlineMessageSource.recall() = bot.recall(this)
/** /**
* 撤回这条消息 * 撤回这条消息
@ -238,3 +235,10 @@ inline val MessageChain.id: Int
@get:JvmSynthetic @get:JvmSynthetic
inline val MessageChain.source: MessageSource inline val MessageChain.source: MessageSource
get() = this[MessageSource] 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)

View File

@ -9,11 +9,15 @@
@file:JvmMultifileClass @file:JvmMultifileClass
@file:JvmName("MessageUtils") @file:JvmName("MessageUtils")
@file:Suppress("NOTHING_TO_INLINE")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import kotlinx.coroutines.Job
import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.utils.SinceMirai import net.mamoe.mirai.utils.SinceMirai
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName import kotlin.jvm.JvmName
@ -27,9 +31,16 @@ import kotlin.jvm.JvmName
* @see MessageSource 获取更多信息 * @see MessageSource 获取更多信息
*/ */
@SinceMirai("0.33.0") @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? // TODO: 2020/4/4 Metadata or Content?
companion object Key : Message.Key<QuoteReply> companion object Key : Message.Key<QuoteReply>
override fun toString(): String = "[mirai:quote]" override fun toString(): String = "[mirai:quote]"
} }
suspend inline fun QuoteReply.recall() = this.source.recall()
inline fun QuoteReply.recallIn(
millis: Long,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Job = this.source.recallIn(millis, coroutineContext)