This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 412
/
PermitteeId.kt
413 lines (376 loc) · 14.4 KB
/
PermitteeId.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("unused")
package net.mamoe.mirai.console.permission
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import net.mamoe.mirai.console.command.BuiltInCommands
import net.mamoe.mirai.console.internal.data.map
import net.mamoe.mirai.console.internal.permission.parseFromStringImpl
import net.mamoe.mirai.console.permission.AbstractPermitteeId.*
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.utils.MiraiExperimentalApi
/**
* [被许可人][Permittee] 的标识符
*
* 一个这样的标识符即可代表特定的单个 [Permittee], 也可以表示多个同类 [Permittee].
*
* ### 获取 [PermitteeId]
* 总是通过 [Permittee.permitteeId].
*/
@PermissionImplementation
public interface PermitteeId {
/**
* 直接父 [PermitteeId]. 在检查权限时会首先检查自己, 再递归检查父类.
*
* @see allParentsWithSelf
* @see allParents
*/
public val directParents: Array<out PermitteeId>
/**
* 转换为字符串表示. 用于权限服务识别和指令的解析.
*/
public fun asString(): String
public companion object {
/**
* 当 [this] 或 [this] 的任意一个直接或间接父 [PermitteeId.asString] 与 `this.asString` 相同时返回 `true`
*/
@JvmStatic
public fun PermitteeId.hasChild(child: PermitteeId): Boolean {
return allParentsWithSelf.any { it.asString() == child.asString() } // asString is for compatibility issue with external implementations
}
/**
* 获取所有直接或间接父类的 [PermitteeId].
*/
@get:JvmStatic
public val PermitteeId.allParentsWithSelf: Sequence<PermitteeId>
get() = sequence {
yield(this@allParentsWithSelf)
yieldAll(allParents)
}
/**
* 获取所有直接或间接父类的 [PermitteeId], 返回包含 `this` + 这些父类 的 [Sequence]
*/
@get:JvmStatic
public val PermitteeId.allParents: Sequence<PermitteeId>
get() = directParents.asSequence().flatMap { it.allParentsWithSelf }
/**
* 创建 [AbstractPermitteeId.ExactUser]
*/
@get:JvmSynthetic
public val User.permitteeId: ExactUser
get() = ExactUser(id)
/**
* 创建 [AbstractPermitteeId.ExactMember]
*/
@get:JvmSynthetic
public val Member.permitteeId: ExactMember
get() = ExactMember(group.id, id)
/**
* 创建 [AbstractPermitteeId.ExactGroup]
*/
@get:JvmSynthetic
public val Group.permitteeId: ExactGroup
get() = ExactGroup(id)
/**
* 创建 [AbstractPermitteeId.ExactGroupTemp]
*/
@get:JvmSynthetic
public val Member.permitteeIdOnTemp: ExactGroupTemp
get() = ExactGroupTemp(group.id, id)
/**
* 创建 [AbstractPermitteeId.ExactStranger]
*/
@get:JvmSynthetic
public val Stranger.permitteeId: ExactStranger
get() = ExactStranger(id)
/**
* 创建 [AbstractPermitteeId.ExactStranger]
*/
@MiraiExperimentalApi
@get:JvmSynthetic
public inline val OtherClient.permitteeId: AnyOtherClient
get() = AnyOtherClient
}
}
/**
* 内建的 [PermitteeId].
*
* - 若指令 A 的权限被授予给 [AnyMember], 那么一个 [ExactMember] 可以执行这个指令.
*
* #### 字符串表示
*
* 当使用 [PermitteeId.asString] 时, 不同的类型的返回值如下表所示. 这些格式也适用于 [BuiltInCommands.PermissionCommand].
*
* (不区分大小写. 不区分 Bot).
*
* [查看字符串表示列表](https://github.com/mamoe/mirai-console/docs/Permissions.md#字符串表示)
*
* #### 关系图
*
* ```
* Console AnyContact
* ↑
* |
* +---------------------------+------------------------+---------------------+
* | | |
* AnyUser AnyGroup AnyOtherClient
* ↑ ↑ ↑
* | | |
* +--------------+---------------------+ | |
* | | | | |
* AnyFriend | AnyMemberFromAnyGroup | |
* ↑ | ↑ | |
* | | | | |
* | | +--------+--------------+ | |
* | | | | | |
* | | | AnyTempFromAnyGroup | |
* | | | ↑ | |
* | | AnyMember | | |
* | | ↑ | | |
* | ExactUser | | ExactGroup ExactOtherClient
* | ↑ ↑ | |
* | | | | |
* +------------+ +----------+ |
* | | |
* ExactFriend ExactMember |
* ↑ |
* | |
* +-----------------------+
* |
* |
* ExactTemp
* ```
*/
@Serializable(with = AsStringSerializer::class)
public sealed class AbstractPermitteeId(
public final override vararg val directParents: PermitteeId,
) : PermitteeId {
public final override fun toString(): String = asString()
public companion object {
/**
* 由 [AbstractPermitteeId.asString] 解析 [AbstractPermitteeId]
*/
@JvmStatic
public fun parseFromString(string: String): AbstractPermitteeId = parseFromStringImpl(string)
}
/**
* 使用 [asString] 序列化 [AbstractPermitteeId]
*/
@ConsoleExperimentalApi
public object AsStringSerializer : KSerializer<AbstractPermitteeId> by String.serializer().map(
serializer = AbstractPermitteeId::asString,
deserializer = ::parseFromString
)
/**
* 表示任何群对象. (不是指群成员, 而是指这个 '群')
*
* - **直接父标识符**: [AnyContact]
* - **间接父标识符**: 无
* - 字符串表示: "g*"
*
* @see AnyMember
*/
public object AnyGroup : AbstractPermitteeId(AnyContact) {
override fun asString(): String = "g*"
}
/**
* 表示一个群
*
* - **直接父标识符**: [AnyGroup]
* - **间接父标识符**: [AnyContact]
* - 字符串表示: "g$groupId"
*/
public data class ExactGroup(public val groupId: Long) : AbstractPermitteeId(AnyGroup) {
override fun asString(): String = "g$groupId"
}
/**
* 表示来自一个群的任意一个成员
*
* - **直接父标识符**: [AnyMemberFromAnyGroup]
* - **间接父标识符**: [AnyUser], [AnyContact]
* - 字符串表示: "m$groupId.*"
*/
public data class AnyMember(public val groupId: Long) : AbstractPermitteeId(AnyMemberFromAnyGroup) {
override fun asString(): String = "m$groupId.*"
}
/**
* 表示来自任意群的任意一个成员
*
* - **直接父标识符**: [AnyUser]
* - **间接父标识符**: [AnyContact]
* - 字符串表示: "m*"
*/
public object AnyMemberFromAnyGroup : AbstractPermitteeId(AnyUser) {
override fun asString(): String = "m*"
}
/**
* 表示唯一的一个群成员
*
* - **直接父标识符**: [AnyMember], [ExactUser]
* - **间接父标识符**: [AnyMemberFromAnyGroup], [AnyUser], [AnyContact]
* - 字符串表示: "m$groupId.$memberId"
*/
public data class ExactMember(
public val groupId: Long,
public val memberId: Long,
) : AbstractPermitteeId(AnyMember(groupId), ExactUser(memberId)) {
override fun asString(): String = "m$groupId.$memberId"
}
/**
* 表示任何好友
*
* - **直接父标识符**: [AnyUser]
* - **间接父标识符**: [AnyContact]
* - 字符串表示: "f*"
*/
public object AnyFriend : AbstractPermitteeId(AnyUser) {
override fun asString(): String = "f*"
}
/**
* 表示唯一的一个好友
*
* - **直接父标识符**: [ExactUser], [AnyFriend]
* - **间接父标识符**: [AnyUser], [AnyContact]
* - 字符串表示: "f$id"
*/
public data class ExactFriend(
public val id: Long,
) : AbstractPermitteeId(ExactUser(id), AnyFriend) {
override fun asString(): String = "f$id"
}
@Deprecated(
"use AnyGroupTemp",
ReplaceWith("AnyGroupTemp", "net.mamoe.mirai.console.permission.AbstractPermitteeId.AnyGroupTemp"),
DeprecationLevel.ERROR
)
public abstract class AnyTemp(
groupId: Long,
) : AbstractPermitteeId(AnyMember(groupId), AnyTempFromAnyGroup)
/**
* 表示任何一个通过一个群 *在临时会话发送消息的* [群成员][Member]
*
* - **直接父标识符**: [AnyMember], [AnyTempFromAnyGroup]
* - **间接父标识符**: [AnyMemberFromAnyGroup], [AnyUser], [AnyContact]
* - 字符串表示: "t$groupId.*"
*/
public data class AnyGroupTemp(
public val groupId: Long,
) : @Suppress("DEPRECATION_ERROR") AnyTemp(groupId) {
override fun asString(): String = "t$groupId.*"
}
/**
* 表示任何一个 *在临时会话发送消息的* [群成员][Member]
*
* - **直接父标识符**: [AnyUser]
* - **间接父标识符**: [AnyContact]
* - 字符串表示: "t*"
*/
public object AnyTempFromAnyGroup : AbstractPermitteeId(AnyUser) {
override fun asString(): String = "t*"
}
@Deprecated(
"use ExactGroupTemp",
ReplaceWith("ExactGroupTemp", "net.mamoe.mirai.console.permission.AbstractPermitteeId.ExactGroupTemp"),
DeprecationLevel.ERROR
)
public abstract class ExactTemp internal constructor(
groupId: Long,
memberId: Long,
) : AbstractPermitteeId(ExactMember(groupId, memberId))
/**
* 表示唯一的一个 *在临时会话发送消息的* [群成员][Member]
*
* - **直接父标识符**: [ExactMember]
* - **间接父标识符**: [AnyUser], [AnyMember], [ExactUser], [AnyContact], [AnyMemberFromAnyGroup]
* - 字符串表示: "t$groupId.$memberId"
*/
public data class ExactGroupTemp(
public val groupId: Long,
public val memberId: Long,
) : @Suppress("DEPRECATION_ERROR") ExactTemp(groupId, memberId) {
override fun asString(): String = "t$groupId.$memberId"
}
/**
* 表示唯一的一个 [陌生人][Stranger]
*
* - **直接父标识符**: [ExactUser], [AnyStranger]
* - **间接父标识符**: [AnyUser], [AnyContact]
* - 字符串表示: "s$id"
*/
public data class ExactStranger(
public val id: Long,
) : AbstractPermitteeId(ExactUser(id), AnyStranger) {
override fun asString(): String = "s$id"
}
/**
* 表示任何 [用户][User]
*
* - **直接父标识符**: [AnyContact]
* - **间接父标识符**: 无
* - 字符串表示: "u*"
*/
public object AnyUser : AbstractPermitteeId(AnyContact) {
override fun asString(): String = "u*"
}
/**
* 表示任何 [陌生人][Stranger]
*
* - **直接父标识符**: [AnyUser]
* - **间接父标识符**: [AnyContact]
* - 字符串表示: "s*"
*/
public object AnyStranger : AbstractPermitteeId(AnyUser) {
override fun asString(): String = "s*"
}
/**
* 表示精确 [用户][User]
*
* - **直接父标识符**: [AnyUser]
* - **间接父标识符**: [AnyContact]
* - 字符串表示: "u$id"
*/
public data class ExactUser(
public val id: Long,
) : AbstractPermitteeId(AnyUser) {
override fun asString(): String = "u$id"
}
/**
* 表示任何 [联系对象][Contact]
*
* - **直接父标识符**: 无
* - **间接父标识符**: 无
* - 字符串表示: "*"
*/
public object AnyContact : AbstractPermitteeId() {
override fun asString(): String = "*"
}
/**
* 表示控制台
*
* - **直接父标识符**: 无
* - **间接父标识符**: 无
* - 字符串表示: "console"
*/
public object Console : AbstractPermitteeId() {
override fun asString(): String = "console"
}
/**
* 表示任何其他客户端
*
* - **直接父标识符**: [AnyContact]
* - **间接父标识符**: 无
* - 字符串表示: "client*"
*/
public object AnyOtherClient : AbstractPermitteeId(AnyContact) {
override fun asString(): String = "client*"
}
}