[Review] LowLevelApiAccessor:

- Rename method names, remove "_lowLevel"
- Add blocking bridges
This commit is contained in:
Him188 2021-01-06 14:58:06 +08:00
parent 1f0d8363f6
commit 1215a56775
7 changed files with 75 additions and 79 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
* Copyright 2019-2021 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.
@ -10,6 +10,7 @@
package net.mamoe.mirai
import kotlinx.coroutines.Job
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.contact.AnonymousMember
import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.Group
@ -33,14 +34,10 @@ import kotlin.annotation.AnnotationTarget.*
public annotation class LowLevelApi
/**
* [Bot] 相关协议层低级 API.
*
* **注意**: 不应该把这个类作为一个类型, 只应使用其中的方法
* [IMirai] 协议层低级 API.
*
* **警告**: 所有的低级 API 都可能在任意时刻不经过任何警告和迭代就被修改. 因此非常不建议在任何情况下使用这些 API.
*/
@MiraiExperimentalApi
@Suppress("FunctionName", "unused")
@LowLevelApi
public interface LowLevelApiAccessor {
/**
@ -50,7 +47,7 @@ public interface LowLevelApiAccessor {
* 因此, [Bot] 被关闭后, 这个对象也会被关闭.
*/
@LowLevelApi
public fun _lowLevelNewFriend(bot: Bot, friendInfo: FriendInfo): Friend
public fun newFriend(bot: Bot, friendInfo: FriendInfo): Friend
/**
* 构造一个 [Stranger] 对象. 它持有对 [Bot] 的弱引用([WeakRef]).
@ -59,13 +56,14 @@ public interface LowLevelApiAccessor {
* 因此, [Bot] 被关闭后, 这个对象也会被关闭.
*/
@LowLevelApi
public fun _lowLevelNewStranger(bot: Bot, strangerInfo: StrangerInfo): Stranger
public fun newStranger(bot: Bot, strangerInfo: StrangerInfo): Stranger
/**
* 向服务器查询群列表. 返回值高 32 bits uin, 32 bits groupCode
*/
@LowLevelApi
public suspend fun _lowLevelQueryGroupList(bot: Bot): Sequence<Long>
@JvmBlockingBridge
public suspend fun getRawGroupList(bot: Bot): Sequence<Long>
/**
* 向服务器查询群成员列表.
@ -73,10 +71,11 @@ public interface LowLevelApiAccessor {
*
* 这个函数很慢. 请不要频繁使用.
*
* @see Group.calculateGroupUinByGroupCode 使用 groupCode 计算 groupUin
* @see IMirai.calculateGroupUinByGroupCode 使用 groupCode 计算 groupUin
*/
@LowLevelApi
public suspend fun _lowLevelQueryGroupMemberList(
@JvmBlockingBridge
public suspend fun getRawGroupMemberList(
bot: Bot,
groupUin: Long,
groupCode: Long,
@ -89,9 +88,12 @@ public interface LowLevelApiAccessor {
*/
@LowLevelApi
@MiraiExperimentalApi
public suspend fun _lowLevelGetAnnouncements(
@JvmBlockingBridge
public suspend fun getRawGroupAnnouncements(
bot: Bot,
groupId: Long, page: Int = 1, amount: Int = 10
groupId: Long,
page: Int = 1,
amount: Int = 10
): GroupAnnouncementList
/**
@ -100,10 +102,12 @@ public interface LowLevelApiAccessor {
* @return 公告的fid
*/
@LowLevelApi
@JvmBlockingBridge
@MiraiExperimentalApi
public suspend fun _lowLevelSendAnnouncement(
public suspend fun sendGroupAnnouncement(
bot: Bot,
groupId: Long, announcement: GroupAnnouncement
groupId: Long,
announcement: GroupAnnouncement
): String
@ -112,10 +116,12 @@ public interface LowLevelApiAccessor {
* @param fid [GroupAnnouncement.fid]
*/
@LowLevelApi
@JvmBlockingBridge
@MiraiExperimentalApi
public suspend fun _lowLevelDeleteAnnouncement(
public suspend fun deleteGroupAnnouncement(
bot: Bot,
groupId: Long, fid: String
groupId: Long,
fid: String
)
/**
@ -123,10 +129,12 @@ public interface LowLevelApiAccessor {
* @param fid [GroupAnnouncement.fid]
*/
@LowLevelApi
@JvmBlockingBridge
@MiraiExperimentalApi
public suspend fun _lowLevelGetAnnouncement(
public suspend fun getGroupAnnouncement(
bot: Bot,
groupId: Long, fid: String
groupId: Long,
fid: String
): GroupAnnouncement
@ -136,8 +144,9 @@ public interface LowLevelApiAccessor {
* page从0开始传入可以得到发言列表
*/
@LowLevelApi
@JvmBlockingBridge
@MiraiExperimentalApi
public suspend fun _lowLevelGetGroupActiveData(bot: Bot, groupId: Long, page: Int = -1): GroupActiveData
public suspend fun getRawGroupActiveData(bot: Bot, groupId: Long, page: Int = -1): GroupActiveData
/**
@ -145,7 +154,8 @@ public interface LowLevelApiAccessor {
*/
@LowLevelApi
@MiraiExperimentalApi
public suspend fun _lowLevelGetGroupHonorListData(
@JvmBlockingBridge
public suspend fun getRawGroupHonorListData(
bot: Bot,
groupId: Long,
type: GroupHonorType
@ -155,8 +165,9 @@ public interface LowLevelApiAccessor {
/**
* 处理一个账号请求添加机器人为好友的事件
*/
@JvmBlockingBridge
@LowLevelApi
public suspend fun _lowLevelSolveNewFriendRequestEvent(
public suspend fun solveNewFriendRequestEvent(
bot: Bot,
eventId: Long,
fromId: Long,
@ -169,7 +180,8 @@ public interface LowLevelApiAccessor {
* 处理被邀请加入一个群请求事件
*/
@LowLevelApi
public suspend fun _lowLevelSolveBotInvitedJoinGroupRequestEvent(
@JvmBlockingBridge
public suspend fun solveBotInvitedJoinGroupRequestEvent(
bot: Bot,
eventId: Long,
invitorId: Long,
@ -181,7 +193,8 @@ public interface LowLevelApiAccessor {
* 处理账号请求加入群事件
*/
@LowLevelApi
public suspend fun _lowLevelSolveMemberJoinRequestEvent(
@JvmBlockingBridge
public suspend fun solveMemberJoinRequestEvent(
bot: Bot,
eventId: Long,
fromId: Long,
@ -196,30 +209,22 @@ public interface LowLevelApiAccessor {
* 查询语音的下载连接
*/
@LowLevelApi
public suspend fun _lowLevelQueryGroupVoiceDownloadUrl(
@JvmBlockingBridge
public suspend fun getGroupVoiceDownloadUrl(
bot: Bot,
md5: ByteArray,
groupId: Long,
dstUin: Long
): String
/**
* 查询语音的上传连接
*/
@LowLevelApi
public suspend fun _lowLevelUploadVoice(
bot: Bot,
md5: ByteArray,
groupId: Long,
)
/**
* 禁言一个匿名用户
*
* @param anonymousId [AnonymousMember.anonymousId]
*/
@LowLevelApi
public suspend fun _lowLevelMuteAnonymous(
@JvmBlockingBridge
public suspend fun muteAnonymousMember(
bot: Bot,
anonymousId: String,
anonymousNick: String,

View File

@ -127,7 +127,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
"the request $event is outdated: You had already responded it on another device."
}
_lowLevelSolveNewFriendRequestEvent(
solveNewFriendRequestEvent(
event.bot,
eventId = event.eventId,
fromId = event.fromId,
@ -147,7 +147,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
"the request $event is outdated: You had already responded it on another device."
}
_lowLevelSolveNewFriendRequestEvent(
solveNewFriendRequestEvent(
event.bot,
eventId = event.eventId,
fromId = event.fromId,
@ -167,7 +167,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
if (event.group?.contains(event.fromId) == true) return
_lowLevelSolveMemberJoinRequestEvent(
solveMemberJoinRequestEvent(
bot = event.bot,
eventId = event.eventId,
fromId = event.fromId,
@ -188,7 +188,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
if (event.group?.contains(event.fromId) == true) return
_lowLevelSolveMemberJoinRequestEvent(
solveMemberJoinRequestEvent(
bot = event.bot,
eventId = event.eventId,
fromId = event.fromId,
@ -238,7 +238,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
"the request $this has already been responded"
}
_lowLevelSolveMemberJoinRequestEvent(
solveMemberJoinRequestEvent(
bot = event.bot,
eventId = event.eventId,
fromId = event.fromId,
@ -266,7 +266,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
"the request $this is outdated: Bot has been already in the group."
}
_lowLevelSolveBotInvitedJoinGroupRequestEvent(
solveBotInvitedJoinGroupRequestEvent(
bot = event.bot,
eventId = event.eventId,
invitorId = event.invitorId,
@ -276,7 +276,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
}
@LowLevelApi
override fun _lowLevelNewFriend(bot: Bot, friendInfo: FriendInfo): Friend {
override fun newFriend(bot: Bot, friendInfo: FriendInfo): Friend {
return FriendImpl(
bot.asQQAndroidBot(),
bot.coroutineContext + SupervisorJob(bot.supervisorJob),
@ -285,7 +285,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
}
@LowLevelApi
override fun _lowLevelNewStranger(bot: Bot, strangerInfo: StrangerInfo): Stranger {
override fun newStranger(bot: Bot, strangerInfo: StrangerInfo): Stranger {
return StrangerImpl(
bot.asQQAndroidBot(),
bot.coroutineContext + SupervisorJob(bot.supervisorJob),
@ -295,7 +295,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@OptIn(LowLevelApi::class)
override suspend fun _lowLevelQueryGroupList(bot: Bot): Sequence<Long> {
override suspend fun getRawGroupList(bot: Bot): Sequence<Long> {
bot.asQQAndroidBot()
return bot.network.run {
FriendList.GetTroopListSimplify(bot.client)
@ -304,7 +304,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
}
@OptIn(LowLevelApi::class)
override suspend fun _lowLevelQueryGroupMemberList(
override suspend fun getRawGroupMemberList(
bot: Bot,
groupUin: Long,
groupCode: Long,
@ -445,7 +445,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelGetAnnouncements(
override suspend fun getRawGroupAnnouncements(
bot: Bot,
groupId: Long,
page: Int,
@ -477,7 +477,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelSendAnnouncement(bot: Bot, groupId: Long, announcement: GroupAnnouncement): String =
override suspend fun sendGroupAnnouncement(bot: Bot, groupId: Long, announcement: GroupAnnouncement): String =
bot.asQQAndroidBot().run {
val rep = withContext(network.coroutineContext) {
Mirai.Http.post<String> {
@ -514,7 +514,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelDeleteAnnouncement(bot: Bot, groupId: Long, fid: String) = bot.asQQAndroidBot().run {
override suspend fun deleteGroupAnnouncement(bot: Bot, groupId: Long, fid: String) = bot.asQQAndroidBot().run {
val data = withContext(network.coroutineContext) {
Mirai.Http.post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/del_feed")
@ -543,7 +543,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelGetAnnouncement(bot: Bot, groupId: Long, fid: String): GroupAnnouncement =
override suspend fun getGroupAnnouncement(bot: Bot, groupId: Long, fid: String): GroupAnnouncement =
bot.asQQAndroidBot().run {
val rep = network.run {
Mirai.Http.post<String> {
@ -570,7 +570,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelGetGroupActiveData(bot: Bot, groupId: Long, page: Int): GroupActiveData =
override suspend fun getRawGroupActiveData(bot: Bot, groupId: Long, page: Int): GroupActiveData =
bot.asQQAndroidBot().run {
val rep = network.run {
Mirai.Http.get<String> {
@ -593,7 +593,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelGetGroupHonorListData(
override suspend fun getRawGroupHonorListData(
bot: Bot,
groupId: Long,
type: GroupHonorType
@ -719,7 +719,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelSolveNewFriendRequestEvent(
override suspend fun solveNewFriendRequestEvent(
bot: Bot,
eventId: Long,
fromId: Long,
@ -736,13 +736,13 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
blackList = blackList
).sendWithoutExpect()
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
bot.friends.delegate.add(_lowLevelNewFriend(bot, FriendInfoImpl(fromId, fromNick, "")))
bot.friends.delegate.add(newFriend(bot, FriendInfoImpl(fromId, fromNick, "")))
}
}
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelSolveBotInvitedJoinGroupRequestEvent(
override suspend fun solveBotInvitedJoinGroupRequestEvent(
bot: Bot,
eventId: Long,
invitorId: Long,
@ -763,7 +763,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@LowLevelApi
@MiraiExperimentalApi
override suspend fun _lowLevelSolveMemberJoinRequestEvent(
override suspend fun solveMemberJoinRequestEvent(
bot: Bot,
eventId: Long,
fromId: Long,
@ -803,7 +803,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@OptIn(ExperimentalStdlibApi::class)
@LowLevelApi
override suspend fun _lowLevelQueryGroupVoiceDownloadUrl(
override suspend fun getGroupVoiceDownloadUrl(
bot: Bot,
md5: ByteArray,
groupId: Long,
@ -816,12 +816,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
}
}
@LowLevelApi
override suspend fun _lowLevelUploadVoice(bot: Bot, md5: ByteArray, groupId: Long) {
}
override suspend fun _lowLevelMuteAnonymous(
override suspend fun muteAnonymousMember(
bot: Bot,
anonymousId: String,
anonymousNick: String,

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
* Copyright 2019-2021 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.
@ -77,7 +77,7 @@ internal class QQAndroidBot constructor(
override val asFriend: Friend by lazy {
@OptIn(LowLevelApi::class)
Mirai._lowLevelNewFriend(this, FriendInfoImpl(uin, nick, ""))
Mirai.newFriend(this, FriendInfoImpl(uin, nick, ""))
}
override val groups: ContactList<Group> = ContactList()
@ -117,7 +117,7 @@ internal class QQAndroidBot constructor(
get() = client.wLoginSigInfo.sKey.data
.fold(5381) { acc: Int, b: Byte -> acc + acc.shl(5) + b.toInt() }
.and(Int.MAX_VALUE)
override val asStranger: Stranger by lazy { Mirai._lowLevelNewStranger(bot, StrangerInfoImpl(bot.id, bot.nick)) }
override val asStranger: Stranger by lazy { Mirai.newStranger(bot, StrangerInfoImpl(bot.id, bot.nick)) }
override val strangers: ContactList<Stranger> = ContactList()
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
* Copyright 2019-2021 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.
@ -33,7 +33,7 @@ internal class AnonymousMemberImpl(
override suspend fun mute(durationSeconds: Int) {
checkBotPermissionHigherThanThis("mute")
MiraiImpl._lowLevelMuteAnonymous(bot, anonymousId, nameCard, group.uin, durationSeconds)
MiraiImpl.muteAnonymousMember(bot, anonymousId, nameCard, group.uin, durationSeconds)
}
override fun toString(): String = "AnonymousMember($nameCard, $anonymousId)"

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
* Copyright 2019-2021 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.
@ -343,7 +343,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
coroutineContext = bot.coroutineContext,
id = groupCode,
groupInfo = GroupInfoImpl(this),
members = Mirai._lowLevelQueryGroupMemberList(
members = Mirai.getRawGroupMemberList(
bot,
groupUin,
groupCode,

View File

@ -527,7 +527,7 @@ internal suspend fun MsgComm.Msg.transform(bot: QQAndroidBot): Packet? {
val nick = sequenceOf(msgHead.fromNick, msgHead.authNick, pbNick).filter { it.isNotEmpty() }.firstOrNull()
?: return null
val id = sequenceOf(msgHead.fromUin, msgHead.authUin).filter { it != 0L }.firstOrNull() ?: return null//对方QQ
Mirai._lowLevelNewStranger(bot, StrangerInfoImpl(id, nick, fromGroup)).let {
Mirai.newStranger(bot, StrangerInfoImpl(id, nick, fromGroup)).let {
bot.getStranger(id)?.let { previous ->
bot.strangers.remove(id)
StrangerRelationChangeEvent.Deleted(previous).broadcast()
@ -566,7 +566,7 @@ internal suspend fun QQAndroidBot.getNewGroup(groupCode: Long): Group? {
coroutineContext = coroutineContext,
id = groupCode,
groupInfo = GroupInfoImpl(troopNum),
members = Mirai._lowLevelQueryGroupMemberList(
members = Mirai.getRawGroupMemberList(
this,
troopNum.groupUin,
troopNum.groupCode,

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
* Copyright 2019-2021 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.
@ -14,13 +14,9 @@
package net.mamoe.mirai.internal.network.protocol.packet.chat.receive
import kotlinx.io.core.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.withLock
import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.discardExact
import kotlinx.io.core.readBytes
import kotlinx.io.core.readUInt
import kotlinx.io.core.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoNumber
import net.mamoe.mirai.JavaFriendlyAPI
@ -494,7 +490,7 @@ internal object Transformers528 : Map<Long, Lambda528> by mapOf(
0xB3L to lambda528 { bot ->
// 08 01 12 52 08 A2 FF 8C F0 03 10 00 1D 15 3D 90 5E 22 2E E6 88 91 E4 BB AC E5 B7 B2 E7 BB 8F E6 98 AF E5 A5 BD E5 8F 8B E5 95 A6 EF BC 8C E4 B8 80 E8 B5 B7 E6 9D A5 E8 81 8A E5 A4 A9 E5 90 A7 21 2A 09 48 69 6D 31 38 38 6D 6F 65 30 07 38 03 48 DD F1 92 B7 07
val body = vProtobuf.loadAs(Submsgtype0xb3.SubMsgType0xb3.MsgBody.serializer())
val new = Mirai._lowLevelNewFriend(
val new = Mirai.newFriend(
bot, FriendInfoImpl(
uin = body.msgAddFrdNotify.fuin,
nick = body.msgAddFrdNotify.fuinNick,