From 8eb597470fe2a33ee073ad05edf7beabc7612937 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 24 Mar 2020 15:35:03 +0800 Subject: [PATCH] Extract group settings to `interface GroupSettings` --- .../net/mamoe/mirai/qqandroid/ContactImpl.kt | 163 +++++++++--------- .../kotlin/net/mamoe/mirai/contact/Group.kt | 49 +----- .../kotlin/net.mamoe.mirai/contact/Group.kt | 122 ++++++------- .../kotlin/net/mamoe/mirai/contact/Group.kt | 50 +----- 4 files changed, 141 insertions(+), 243 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt index f402a8c89..5601d77d3 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt @@ -412,7 +412,7 @@ internal class GroupImpl( @OptIn(MiraiExperimentalAPI::class) override val botAsMember: Member by lazy { - Member(object : MemberInfo { + newMember(object : MemberInfo { override val nameCard: String get() = bot.nick // TODO: 2020/2/21 机器人在群内的昵称获取 override val permission: MemberPermission @@ -447,7 +447,7 @@ internal class GroupImpl( owner = botAsMember } null - } else Member(it).also { member -> + } else newMember(it).also { member -> if (member.permission == MemberPermission.OWNER) { owner = member } @@ -465,7 +465,7 @@ internal class GroupImpl( override var name: String get() = _name set(newValue) { - this.checkBotPermissionOperator() + checkBotPermissionOperator() if (_name != newValue) { val oldValue = _name _name = newValue @@ -482,101 +482,104 @@ internal class GroupImpl( } } - override var entranceAnnouncement: String - get() = _announcement - set(newValue) { - this.checkBotPermissionOperator() - if (_announcement != newValue) { - val oldValue = _announcement - _announcement = newValue - launch { - bot.network.run { - TroopManagement.GroupOperation.memo( - client = bot.client, - groupCode = id, - newMemo = newValue - ).sendWithoutExpect() + override val settings: GroupSettings = object : GroupSettings{ + + override var entranceAnnouncement: String + get() = _announcement + set(newValue) { + checkBotPermissionOperator() + if (_announcement != newValue) { + val oldValue = _announcement + _announcement = newValue + launch { + bot.network.run { + TroopManagement.GroupOperation.memo( + client = bot.client, + groupCode = id, + newMemo = newValue + ).sendWithoutExpect() + } + GroupEntranceAnnouncementChangeEvent(oldValue, newValue, this@GroupImpl, null).broadcast() } - GroupEntranceAnnouncementChangeEvent(oldValue, newValue, this@GroupImpl, null).broadcast() } } - } - override var isAllowMemberInvite: Boolean - get() = _allowMemberInvite - set(newValue) { - this.checkBotPermissionOperator() - if (_allowMemberInvite != newValue) { - val oldValue = _allowMemberInvite - _allowMemberInvite = newValue - launch { - bot.network.run { - TroopManagement.GroupOperation.allowMemberInvite( - client = bot.client, - groupCode = id, - switch = newValue - ).sendWithoutExpect() + override var isAllowMemberInvite: Boolean + get() = _allowMemberInvite + set(newValue) { + checkBotPermissionOperator() + if (_allowMemberInvite != newValue) { + val oldValue = _allowMemberInvite + _allowMemberInvite = newValue + launch { + bot.network.run { + TroopManagement.GroupOperation.allowMemberInvite( + client = bot.client, + groupCode = id, + switch = newValue + ).sendWithoutExpect() + } + GroupAllowMemberInviteEvent(oldValue, newValue, this@GroupImpl, null).broadcast() } - GroupAllowMemberInviteEvent(oldValue, newValue, this@GroupImpl, null).broadcast() } } - } - override var isAutoApproveEnabled: Boolean - get() = _autoApprove - @Suppress("UNUSED_PARAMETER") - set(newValue) { - TODO() - } + override var isAutoApproveEnabled: Boolean + get() = _autoApprove + @Suppress("UNUSED_PARAMETER") + set(newValue) { + TODO() + } - override var isAnonymousChatEnabled: Boolean - get() = _anonymousChat - @Suppress("UNUSED_PARAMETER") - set(newValue) { - TODO() - } + override var isAnonymousChatEnabled: Boolean + get() = _anonymousChat + @Suppress("UNUSED_PARAMETER") + set(newValue) { + TODO() + } - override var isConfessTalkEnabled: Boolean - get() = _confessTalk - set(newValue) { - this.checkBotPermissionOperator() - if (_confessTalk != newValue) { - val oldValue = _confessTalk - _confessTalk = newValue - launch { - bot.network.run { - TroopManagement.GroupOperation.confessTalk( - client = bot.client, - groupCode = id, - switch = newValue - ).sendWithoutExpect() + override var isConfessTalkEnabled: Boolean + get() = _confessTalk + set(newValue) { + checkBotPermissionOperator() + if (_confessTalk != newValue) { + val oldValue = _confessTalk + _confessTalk = newValue + launch { + bot.network.run { + TroopManagement.GroupOperation.confessTalk( + client = bot.client, + groupCode = id, + switch = newValue + ).sendWithoutExpect() + } + GroupAllowConfessTalkEvent(oldValue, newValue, this@GroupImpl, true).broadcast() } - GroupAllowConfessTalkEvent(oldValue, newValue, this@GroupImpl, true).broadcast() } } - } - override var isMuteAll: Boolean - get() = _muteAll - set(newValue) { - this.checkBotPermissionOperator() - if (_muteAll != newValue) { - val oldValue = _muteAll - _muteAll = newValue - launch { - bot.network.run { - TroopManagement.GroupOperation.muteAll( - client = bot.client, - groupCode = id, - switch = newValue - ).sendWithoutExpect() + override var isMuteAll: Boolean + get() = _muteAll + set(newValue) { + checkBotPermissionOperator() + if (_muteAll != newValue) { + val oldValue = _muteAll + _muteAll = newValue + launch { + bot.network.run { + TroopManagement.GroupOperation.muteAll( + client = bot.client, + groupCode = id, + switch = newValue + ).sendWithoutExpect() + } + GroupMuteAllEvent(oldValue, newValue, this@GroupImpl, null).broadcast() } - GroupMuteAllEvent(oldValue, newValue, this@GroupImpl, null).broadcast() } } - } + } @MiraiExperimentalAPI override suspend fun quit(): Boolean { @@ -585,7 +588,7 @@ internal class GroupImpl( } @OptIn(MiraiExperimentalAPI::class) - override fun Member(memberInfo: MemberInfo): Member { + override fun newMember(memberInfo: MemberInfo): Member { return MemberImpl( @OptIn(LowLevelAPI::class) bot._lowLevelNewQQ(memberInfo) as QQImpl, diff --git a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt index ed3ad1d80..f2f400aad 100644 --- a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt +++ b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt @@ -38,54 +38,9 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract var name: String /** - * 入群公告, 没有时为空字符串. - * - * 在修改时将会异步上传至服务器. - * - * @see GroupEntranceAnnouncementChangeEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 + * 群设置 */ - actual abstract var entranceAnnouncement: String - - /** - * 全体禁言状态. `true` 为开启. - * - * 当前仅能修改状态. - * - * @see GroupMuteAllEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - actual abstract var isMuteAll: Boolean - - /** - * 坦白说状态. `true` 为允许. - * - * 在修改时将会异步上传至服务器. - * - * @see GroupAllowConfessTalkEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - actual abstract var isConfessTalkEnabled: Boolean - - /** - * 允许群员邀请好友入群的状态. `true` 为允许 - * - * 在修改时将会异步上传至服务器. - * - * @see GroupAllowMemberInviteEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - actual abstract var isAllowMemberInvite: Boolean - - /** - * 自动加群审批 - */ - actual abstract val isAutoApproveEnabled: Boolean - - /** - * 匿名聊天 - */ - actual abstract val isAnonymousChatEnabled: Boolean + actual abstract val settings: GroupSettings /** * 同为 groupCode, 用户看到的群号码. diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt index 8e7d7dab3..b38201552 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt @@ -40,50 +40,11 @@ expect abstract class Group() : Contact, CoroutineScope { * @throws PermissionDeniedException 无权限修改时将会抛出异常 */ abstract var name: String + /** - * 入群公告, 没有时为空字符串. - * - * 在修改时将会异步上传至服务器. - * - * @see GroupEntranceAnnouncementChangeEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 + * 群设置 */ - abstract var entranceAnnouncement: String - /** - * 全体禁言状态. `true` 为开启. - * - * 当前仅能修改状态. - * - * @see GroupMuteAllEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - abstract var isMuteAll: Boolean - /** - * 坦白说状态. `true` 为允许. - * - * 在修改时将会异步上传至服务器. - * - * @see GroupAllowConfessTalkEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - abstract var isConfessTalkEnabled: Boolean - /** - * 允许群员邀请好友入群的状态. `true` 为允许 - * - * 在修改时将会异步上传至服务器. - * - * @see GroupAllowMemberInviteEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - abstract var isAllowMemberInvite: Boolean - /** - * 自动加群审批 - */ - abstract val isAutoApproveEnabled: Boolean - /** - * 匿名聊天 - */ - abstract val isAnonymousChatEnabled: Boolean + abstract val settings: GroupSettings /** * 同为 groupCode, 用户看到的群号码. @@ -200,38 +161,63 @@ expect abstract class Group() : Contact, CoroutineScope { fun toFullString(): String } -internal object CommonGroupCalculations { +/** + * 群设置 + * + * @see Group.settings 获取群设置 + */ +@SinceMirai("0.30.0") +interface GroupSettings { /** - * by @kar98k + * 入群公告, 没有时为空字符串. + * + * 在修改时将会异步上传至服务器. + * + * @see GroupEntranceAnnouncementChangeEvent + * @throws PermissionDeniedException 无权限修改时将会抛出异常 */ - fun calculateGroupUinByGroupCode(groupCode: Long): Long { - var left: Long = groupCode / 1000000L + var entranceAnnouncement: String - when (left) { - in 0..10 -> left += 202 - in 11..19 -> left += 480 - 11 - in 20..66 -> left += 2100 - 20 - in 67..156 -> left += 2010 - 67 - in 157..209 -> left += 2147 - 157 - in 210..309 -> left += 4100 - 210 - in 310..499 -> left += 3800 - 310 - } + /** + * 全体禁言状态. `true` 为开启. + * + * 当前仅能修改状态. + * + * @see GroupMuteAllEvent + * @throws PermissionDeniedException 无权限修改时将会抛出异常 + */ + var isMuteAll: Boolean - return left * 1000000L + groupCode % 1000000L - } + /** + * 坦白说状态. `true` 为允许. + * + * 在修改时将会异步上传至服务器. + * + * @see GroupAllowConfessTalkEvent + * @throws PermissionDeniedException 无权限修改时将会抛出异常 + */ + var isConfessTalkEnabled: Boolean - fun calculateGroupCodeByGroupUin(groupUin: Long): Long { - var left: Long = groupUin / 1000000L + /** + * 允许群员邀请好友入群的状态. `true` 为允许 + * + * 在修改时将会异步上传至服务器. + * + * @see GroupAllowMemberInviteEvent + * @throws PermissionDeniedException 无权限修改时将会抛出异常 + */ + var isAllowMemberInvite: Boolean - when (left) { - in 0 + 202..10 + 202 -> left -= 202 - in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11 - in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20 - in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67 - in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157 - in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210 - in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310 - } + /** + * 自动加群审批 + */ + val isAutoApproveEnabled: Boolean + + /** + * 匿名聊天 + */ + val isAnonymousChatEnabled: Boolean +} /** diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt index 50ef5c1f0..82b1e9e3e 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt @@ -39,54 +39,9 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract var name: String /** - * 入群公告, 没有时为空字符串. - * - * 在修改时将会异步上传至服务器. - * - * @see GroupEntranceAnnouncementChangeEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 + * 群设置 */ - actual abstract var entranceAnnouncement: String - - /** - * 全体禁言状态. `true` 为开启. - * - * 当前仅能修改状态. - * - * @see GroupMuteAllEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - actual abstract var isMuteAll: Boolean - - /** - * 坦白说状态. `true` 为允许. - * - * 在修改时将会异步上传至服务器. - * - * @see GroupAllowConfessTalkEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - actual abstract var isConfessTalkEnabled: Boolean - - /** - * 允许群员邀请好友入群的状态. `true` 为允许 - * - * 在修改时将会异步上传至服务器. - * - * @see GroupAllowMemberInviteEvent - * @throws PermissionDeniedException 无权限修改时将会抛出异常 - */ - actual abstract var isAllowMemberInvite: Boolean - - /** - * 自动加群审批 - */ - actual abstract val isAutoApproveEnabled: Boolean - - /** - * 匿名聊天 - */ - actual abstract val isAnonymousChatEnabled: Boolean + actual abstract val settings: GroupSettings /** * 同为 groupCode, 用户看到的群号码. @@ -205,5 +160,4 @@ actual abstract class Group : Contact(), CoroutineScope { actual fun toFullString(): String { return "Group(id=${this.id}, name=$name, owner=${owner.id}, members=${members.idContentString})" } - } \ No newline at end of file