Skip to content

Commit

Permalink
fix: isChildOf
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Aug 8, 2023
1 parent 14c34a4 commit 16b288f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1780,12 +1780,11 @@ public abstract interface class net/mamoe/mirai/console/permission/Permittee {
public abstract interface class net/mamoe/mirai/console/permission/PermitteeId {
public static final field Companion Lnet/mamoe/mirai/console/permission/PermitteeId$Companion;
public abstract fun asString ()Ljava/lang/String;
public fun contains (Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public static fun getAllParents (Lnet/mamoe/mirai/console/permission/PermitteeId;)Lkotlin/sequences/Sequence;
public static fun getAllParentsWithSelf (Lnet/mamoe/mirai/console/permission/PermitteeId;)Lkotlin/sequences/Sequence;
public abstract fun getDirectParents ()[Lnet/mamoe/mirai/console/permission/PermitteeId;
public static fun hasChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public static fun isChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public static fun isChildOf (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
}

public final class net/mamoe/mirai/console/permission/PermitteeId$Companion {
Expand All @@ -1798,7 +1797,7 @@ public final class net/mamoe/mirai/console/permission/PermitteeId$Companion {
public final synthetic fun getPermitteeId (Lnet/mamoe/mirai/contact/User;)Lnet/mamoe/mirai/console/permission/AbstractPermitteeId$ExactUser;
public final synthetic fun getPermitteeIdOnTemp (Lnet/mamoe/mirai/contact/Member;)Lnet/mamoe/mirai/console/permission/AbstractPermitteeId$ExactGroupTemp;
public final fun hasChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public final fun isChild (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
public final fun isChildOf (Lnet/mamoe/mirai/console/permission/PermitteeId;Lnet/mamoe/mirai/console/permission/PermitteeId;)Z
}

public abstract interface class net/mamoe/mirai/console/plugin/NotYetLoadedPlugin : net/mamoe/mirai/console/plugin/Plugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import net.mamoe.mirai.console.data.PluginDataExtensions
import net.mamoe.mirai.console.permission.*
import net.mamoe.mirai.console.permission.Permission.Companion.parentsWithSelf
import net.mamoe.mirai.console.permission.PermitteeId.Companion.allParentsWithSelf
import net.mamoe.mirai.console.permission.PermitteeId.Companion.isChild
import net.mamoe.mirai.console.permission.PermitteeId.Companion.isChildOf

internal abstract class AbstractConcurrentPermissionService<P : Permission> : PermissionService<P> {
protected abstract val permissions: MutableMap<PermissionId, P>
Expand Down Expand Up @@ -75,7 +75,7 @@ internal abstract class AbstractConcurrentPermissionService<P : Permission> : Pe
override fun getPermittedPermissions(permitteeId: PermitteeId): Sequence<P> = sequence {
for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) {

val granted = permissibleIdentifiers.any { permitteeId.isChild(it) }
val granted = permissibleIdentifiers.any { permitteeId.isChildOf(it) }

if (granted) get(permissionIdentifier)?.let { yield(it) }
}
Expand All @@ -84,7 +84,7 @@ internal abstract class AbstractConcurrentPermissionService<P : Permission> : Pe
internal fun getPermittedPermissionsAndSource(permitteeId: PermitteeId): Sequence<Pair<PermitteeId, P>> = sequence {
for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) {
permissibleIdentifiers.forEach { pid ->
if (permitteeId.isChild(pid)) {
if (permitteeId.isChildOf(pid)) {
get(permissionIdentifier)?.let { yield(pid to it) }
}
}
Expand Down
13 changes: 3 additions & 10 deletions mirai-console/backend/mirai-console/src/permission/PermitteeId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,20 @@ public interface PermitteeId {
*/
public fun asString(): String

/**
* @since 2.16
*/
public operator fun contains(child: PermitteeId): Boolean {
return child.isChild(this)
}

public companion object {
/**
* 当 [this] 或 [this] 的任意一个直接或间接父 [PermitteeId.asString] 与 `parent.asString` 相同时返回 `true`
*
* @since 2.16
*/
@JvmStatic
public fun PermitteeId.isChild(parent: PermitteeId): Boolean {
public fun PermitteeId.isChildOf(parent: PermitteeId): Boolean {
return allParentsWithSelf.any { it.asString() == parent.asString() } // asString is for compatibility issue with external implementations
}

@JvmStatic
@DeprecatedSinceMirai(warningSince = "2.16", hiddenSince = "2.17")
public fun PermitteeId.hasChild(child: PermitteeId): Boolean = isChild(child)
@DeprecatedSinceMirai(warningSince = "2.16")
public fun PermitteeId.hasChild(child: PermitteeId): Boolean = isChildOf(child)

/**
* 获取所有直接或间接父类的 [PermitteeId].
Expand Down

0 comments on commit 16b288f

Please sign in to comment.