Deprecate Bot.qq, Bot.get, Bot.contains

This commit is contained in:
Him188 2020-03-03 08:32:39 +08:00
parent 28c0530268
commit cadbd7ff3a
6 changed files with 76 additions and 17 deletions

View File

@ -57,7 +57,14 @@ internal abstract class QQAndroidBotBase constructor(
) )
internal var firstLoginSucceed: Boolean = false internal var firstLoginSucceed: Boolean = false
override val uin: Long get() = client.uin override val uin: Long get() = client.uin
override val qqs: ContactList<QQ> = ContactList(LockFreeLinkedList()) @Deprecated(
"use friends instead",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.friends")
)
override val qqs: ContactList<QQ>
get() = friends
override val friends: ContactList<QQ> = ContactList(LockFreeLinkedList())
override val selfQQ: QQ by lazy { override val selfQQ: QQ by lazy {
QQ(object : FriendInfo { QQ(object : FriendInfo {

View File

@ -183,7 +183,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
check(bot.isActive) { "bot is dead therefore network can't init" } check(bot.isActive) { "bot is dead therefore network can't init" }
check(this@QQAndroidBotNetworkHandler.isActive) { "network is dead therefore can't init" } check(this@QQAndroidBotNetworkHandler.isActive) { "network is dead therefore can't init" }
bot.qqs.delegate.clear() bot.friends.delegate.clear()
bot.groups.delegate.clear() bot.groups.delegate.clear()
val friendListJob = launch { val friendListJob = launch {
@ -211,7 +211,14 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
totalFriendCount = data.totalFriendCount totalFriendCount = data.totalFriendCount
data.friendList.forEach { data.friendList.forEach {
// atomic add // atomic add
bot.qqs.delegate.addLast(QQImpl(bot, bot.coroutineContext, it.friendUin, FriendInfoImpl(it))).also { bot.friends.delegate.addLast(
QQImpl(
bot,
bot.coroutineContext,
it.friendUin,
FriendInfoImpl(it)
)
).also {
currentFriendCount++ currentFriendCount++
} }
} }

View File

@ -86,14 +86,28 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
/** /**
* 机器人的好友列表. 它将与服务器同步更新 * 机器人的好友列表. 它将与服务器同步更新
*/ */
@Deprecated(
"use friends instead",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.friends")
)
actual abstract val qqs: ContactList<QQ> actual abstract val qqs: ContactList<QQ>
/** /**
* 获取一个好友或一个群. * 机器人的好友列表. 它将与服务器同步更新
* 在一些情况下这可能会造成歧义. 请考虑后使用.
*/ */
actual abstract val friends: ContactList<QQ>
/**
* 获取一个好友或一个群.
*/
@Deprecated(
"use getFriend or getGroup instead",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.qqs.getOrNull(id) ?: this.groups.getOrNull(id) ?: throw NoSuchElementException(\"contact id \$id\")")
)
actual operator fun get(id: Long): Contact { actual operator fun get(id: Long): Contact {
return this.qqs.getOrNull(id) ?: this.groups.getOrNull(id) ?: throw NoSuchElementException("contact id $id") return this.friends.getOrNull(id) ?: this.groups.getOrNull(id) ?: throw NoSuchElementException("contact id $id")
} }
/** /**
@ -101,7 +115,7 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
* 在一些情况下这可能会造成歧义. 请考虑后使用. * 在一些情况下这可能会造成歧义. 请考虑后使用.
*/ */
actual operator fun contains(id: Long): Boolean { actual operator fun contains(id: Long): Boolean {
return this.qqs.contains(id) || this.groups.contains(id) return this.friends.contains(id) || this.groups.contains(id)
} }
/** /**
@ -109,7 +123,7 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
*/ */
actual fun getFriend(id: Long): QQ { actual fun getFriend(id: Long): QQ {
if (id == uin) return selfQQ if (id == uin) return selfQQ
return qqs.delegate.getOrNull(id) return friends.delegate.getOrNull(id)
?: throw NoSuchElementException("No such friend $id for bot ${this.uin}") ?: throw NoSuchElementException("No such friend $id for bot ${this.uin}")
} }

View File

@ -95,22 +95,39 @@ expect abstract class Bot() : CoroutineScope, LowLevelBotAPIAccessor {
// region contacts // region contacts
/**
* [QQ.id] [Bot.uin] 相同的 [QQ] 实例
*/
abstract val selfQQ: QQ abstract val selfQQ: QQ
/** /**
* 机器人的好友列表. 它将与服务器同步更新 * 机器人的好友列表. 它将与服务器同步更新
*/ */
@Deprecated(
"use friends instead",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.friends")
)
abstract val qqs: ContactList<QQ> abstract val qqs: ContactList<QQ>
/**
* 机器人的好友列表. 它将与服务器同步更新
*/
abstract val friends: ContactList<QQ>
/** /**
* 获取一个好友或一个群. * 获取一个好友或一个群.
* 在一些情况下这可能会造成歧义. 请考虑后使用. * 在一些情况下这可能会造成歧义. 请考虑后使用.
*/ */
@Deprecated(
"use getFriend or getGroup instead",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.qqs.getOrNull(id) ?: this.groups.getOrNull(id) ?: throw NoSuchElementException(\"contact id \$id\")")
)
operator fun get(id: Long): Contact operator fun get(id: Long): Contact
/** /**
* 判断是否有这个 id 的好友或群. * 判断是否有这个 id 的好友或群.
* 在一些情况下这可能会造成歧义. 请考虑后使用.
*/ */
operator fun contains(id: Long): Boolean operator fun contains(id: Long): Boolean
@ -294,10 +311,10 @@ suspend inline fun Bot.closeAndJoin(cause: Throwable? = null) {
coroutineContext[Job]?.join() coroutineContext[Job]?.join()
} }
inline fun Bot.containsFriend(id: Long): Boolean = this.qqs.contains(id) inline fun Bot.containsFriend(id: Long): Boolean = this.friends.contains(id)
inline fun Bot.containsGroup(id: Long): Boolean = this.groups.contains(id) inline fun Bot.containsGroup(id: Long): Boolean = this.groups.contains(id)
inline fun Bot.getFriendOrNull(id: Long): QQ? = this.qqs.getOrNull(id) inline fun Bot.getFriendOrNull(id: Long): QQ? = this.friends.getOrNull(id)
inline fun Bot.getGroupOrNull(id: Long): Group? = this.groups.getOrNull(id) inline fun Bot.getGroupOrNull(id: Long): Group? = this.groups.getOrNull(id)

View File

@ -189,7 +189,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
} }
} }
groups.delegate.clear() groups.delegate.clear()
qqs.delegate.clear() friends.delegate.clear()
instances.removeIf { it.get()?.uin == this.uin } instances.removeIf { it.get()?.uin == this.uin }
} }
} }

View File

@ -96,14 +96,28 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
/** /**
* 机器人的好友列表. 它将与服务器同步更新 * 机器人的好友列表. 它将与服务器同步更新
*/ */
@Deprecated(
"use friends instead",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.friends")
)
actual abstract val qqs: ContactList<QQ> actual abstract val qqs: ContactList<QQ>
/** /**
* 获取一个好友或一个群. * 机器人的好友列表. 它将与服务器同步更新
* 在一些情况下这可能会造成歧义. 请考虑后使用.
*/ */
actual abstract val friends: ContactList<QQ>
/**
* 获取一个好友或一个群.
*/
@Deprecated(
"use getFriend or getGroup instead",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("this.qqs.getOrNull(id) ?: this.groups.getOrNull(id) ?: throw NoSuchElementException(\"contact id \$id\")")
)
actual operator fun get(id: Long): Contact { actual operator fun get(id: Long): Contact {
return this.qqs.getOrNull(id) ?: this.groups.getOrNull(id) ?: throw NoSuchElementException("contact id $id") return this.friends.getOrNull(id) ?: this.groups.getOrNull(id) ?: throw NoSuchElementException("contact id $id")
} }
/** /**
@ -111,7 +125,7 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
* 在一些情况下这可能会造成歧义. 请考虑后使用. * 在一些情况下这可能会造成歧义. 请考虑后使用.
*/ */
actual operator fun contains(id: Long): Boolean { actual operator fun contains(id: Long): Boolean {
return this.qqs.contains(id) || this.groups.contains(id) return this.friends.contains(id) || this.groups.contains(id)
} }
/** /**
@ -119,7 +133,7 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
*/ */
actual fun getFriend(id: Long): QQ { actual fun getFriend(id: Long): QQ {
if (id == uin) return selfQQ if (id == uin) return selfQQ
return qqs.delegate.getOrNull(id) return friends.delegate.getOrNull(id)
?: throw NoSuchElementException("No such friend $id for bot ${this.uin}") ?: throw NoSuchElementException("No such friend $id for bot ${this.uin}")
} }