Skip to content

Commit 51286be

Browse files
committed
feat(core): 完善黑名单相关的上层 API
1 parent 8990514 commit 51286be

File tree

12 files changed

+700
-33
lines changed

12 files changed

+700
-33
lines changed

build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ apiValidation {
7979
"love.forte.simbot.annotations.InternalSimbotAPI",
8080
"love.forte.simbot.kook.ExperimentalKookApi",
8181
"love.forte.simbot.kook.InternalKookApi",
82-
"love.forte.simbot.kook.api.template.ExperimentalTemplateApi"
82+
"love.forte.simbot.kook.api.template.ExperimentalTemplateApi",
83+
// blacklist APIs
84+
"love.forte.simbot.component.kook.blacklist.ExperimentalBlacklistApi"
8385
),
8486
)
8587

simbot-component-kook-api/src/jvmMain/java/module-info.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
exports love.forte.simbot.kook;
1717
exports love.forte.simbot.kook.api;
1818
exports love.forte.simbot.kook.api.asset;
19+
exports love.forte.simbot.kook.api.blacklist;
20+
exports love.forte.simbot.kook.api.category;
1921
exports love.forte.simbot.kook.api.channel;
2022
exports love.forte.simbot.kook.api.guild;
2123
exports love.forte.simbot.kook.api.invite;
2224
exports love.forte.simbot.kook.api.member;
2325
exports love.forte.simbot.kook.api.message;
2426
exports love.forte.simbot.kook.api.role;
27+
exports love.forte.simbot.kook.api.template;
28+
exports love.forte.simbot.kook.api.thread;
2529
exports love.forte.simbot.kook.api.user;
2630
exports love.forte.simbot.kook.api.userchat;
31+
exports love.forte.simbot.kook.api.voice;
2732
exports love.forte.simbot.kook.event;
2833
exports love.forte.simbot.kook.messages;
2934
exports love.forte.simbot.kook.objects;

simbot-component-kook-core/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ kotlin {
4848
optIn.addAll(
4949
"love.forte.simbot.kook.ExperimentalKookApi",
5050
"love.forte.simbot.kook.InternalKookApi",
51+
"love.forte.simbot.component.kook.blacklist.ExperimentalBlacklistApi"
5152
)
5253
}
5354

@@ -80,6 +81,7 @@ kotlin {
8081
implementation(libs.simbot.api)
8182
implementation(libs.simbot.core)
8283
implementation(libs.simbot.common.core)
84+
implementation(libs.ktor.client.mock)
8385
}
8486

8587
jvmTest.dependencies {

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/KookGuild.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import love.forte.simbot.annotations.ExperimentalSimbotAPI
2525
import love.forte.simbot.common.collectable.Collectable
2626
import love.forte.simbot.common.id.ID
2727
import love.forte.simbot.common.id.StringID.Companion.ID
28+
import love.forte.simbot.component.kook.blacklist.ExperimentalBlacklistApi
29+
import love.forte.simbot.component.kook.blacklist.KookGuildBlacklistOperator
2830
import love.forte.simbot.component.kook.role.KookGuildRole
2931
import love.forte.simbot.component.kook.role.KookGuildRoleCreator
3032
import love.forte.simbot.component.kook.role.KookRole
@@ -207,4 +209,15 @@ public interface KookGuild : Guild, CoroutineScope, KookRoleOperator {
207209
@ExperimentalSimbotAPI
208210
override fun roleCreator(): KookGuildRoleCreator
209211
//endregion
212+
213+
// Blacklist
214+
215+
/**
216+
* 获取针对当前频道服务器的黑名单操作器。
217+
*
218+
* @since 4.4.0
219+
* @see KookGuildBlacklistOperator
220+
*/
221+
@ExperimentalBlacklistApi
222+
public val blacklist: KookGuildBlacklistOperator
210223
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025. ForteScarlet.
3+
*
4+
* This file is part of simbot-component-kook.
5+
*
6+
* simbot-component-kook is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* simbot-component-kook is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with simbot-component-kook,
18+
* If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
package love.forte.simbot.component.kook.blacklist
22+
23+
/**
24+
* 与频道服务器黑名单列表相关的试验性 API。
25+
* 这些 API 仍处于试验性阶段,可能会随时被更改、删除,不保证稳定性。
26+
*
27+
* @since 4.4.0
28+
* @author ForteScarlet
29+
*/
30+
@Retention(AnnotationRetention.BINARY)
31+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
32+
@MustBeDocumented
33+
@RequiresOptIn(
34+
message = "与频道服务器黑名单列表相关的试验性 API。" +
35+
"这些 API 仍处于试验性阶段,可能会随时被更改、删除,不保证稳定性。",
36+
level = RequiresOptIn.Level.WARNING
37+
)
38+
public annotation class ExperimentalBlacklistApi

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/blacklist/KookBlacklistItem.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import love.forte.simbot.suspendrunner.ST
3535
*
3636
* @author ForteScarlet
3737
*/
38+
@ExperimentalBlacklistApi
3839
public interface KookBlacklistItem : DeleteSupport {
3940
/**
4041
* 用户 ID

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/blacklist/KookBlacklistOperator.kt renamed to simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/blacklist/KookGuildBlacklistOperator.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ import love.forte.simbot.kook.api.blacklist.CreateBlacklistApi
3030
import love.forte.simbot.suspendrunner.ST
3131

3232
/**
33-
* KOOK 黑名单相关内容的操作器
33+
* KOOK 服务器黑名单相关内容的操作器
3434
*
3535
* @since 4.4.0
3636
*
3737
* @author ForteScarlet
3838
*/
39-
public interface KookBlacklistOperator {
39+
@ExperimentalBlacklistApi
40+
public interface KookGuildBlacklistOperator {
41+
/**
42+
* 服务器ID
43+
*/
44+
public val guildId: ID
45+
4046
/**
4147
* 获取黑名单的分页列表。
4248
*
@@ -45,21 +51,20 @@ public interface KookBlacklistOperator {
4551
* @return 分页列表
4652
*/
4753
@ST
48-
public suspend fun list(guildId: ID, page: Int?, size: Int?): ListData<KookBlacklistItem>
54+
public suspend fun list(page: Int?, size: Int?): ListData<KookBlacklistItem>
4955

5056
/**
5157
* 获取全量列表数据。
5258
*/
5359
@ST
54-
public suspend fun all(guildId: ID): List<KookBlacklistItem> =
55-
flow(guildId).toList()
60+
public suspend fun all(): List<KookBlacklistItem> = flow().toList()
5661

5762
/**
5863
* 获取黑名单列表元素的 Flow。
5964
*
6065
* @param batchSize 每批次大小
6166
*/
62-
public fun flow(guildId: ID, batchSize: Int? = null): Flow<KookBlacklistItem>
67+
public fun flow(batchSize: Int? = null): Flow<KookBlacklistItem>
6368

6469
/**
6570
* 添加一个目标到黑名单。
@@ -71,7 +76,7 @@ public interface KookBlacklistOperator {
7176
* @see CreateBlacklistApi
7277
*/
7378
@ST
74-
public suspend fun add(guildId: ID, targetId: ID, remark: String?, delMsgDays: Int?)
79+
public suspend fun add(targetId: ID, remark: String?, delMsgDays: Int?)
7580

7681
/**
7782
* 添加一个目标到黑名单。
@@ -81,8 +86,8 @@ public interface KookBlacklistOperator {
8186
* @see CreateBlacklistApi
8287
*/
8388
@ST
84-
public suspend fun add(guildId: ID, targetId: ID) {
85-
add(guildId, targetId, null, null)
89+
public suspend fun add(targetId: ID) {
90+
add(targetId, null, null)
8691
}
8792

8893
/**
@@ -92,5 +97,5 @@ public interface KookBlacklistOperator {
9297
* 并且没有提供 [StandardDeleteOption.IGNORE_ON_FAILURE]
9398
*/
9499
@ST
95-
public suspend fun delete(guildId: ID, targetId: ID, vararg options: DeleteOption)
100+
public suspend fun delete(targetId: ID, vararg options: DeleteOption)
96101
}

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/blacklist/internal/KookBlacklistItemImpl.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import love.forte.simbot.common.id.ID
2525
import love.forte.simbot.common.id.StringID.Companion.ID
2626
import love.forte.simbot.common.time.Timestamp
2727
import love.forte.simbot.component.kook.blacklist.KookBlacklistItem
28+
import love.forte.simbot.component.kook.blacklist.KookGuildBlacklistOperator
2829
import love.forte.simbot.kook.api.blacklist.BlacklistItem
2930
import love.forte.simbot.kook.objects.User
3031

@@ -34,7 +35,8 @@ import love.forte.simbot.kook.objects.User
3435
*/
3536
internal class KookBlacklistItemImpl(
3637
private val source: BlacklistItem,
37-
override val guildId: ID
38+
override val guildId: ID,
39+
private val operator: KookGuildBlacklistOperator
3840
) : KookBlacklistItem {
3941
override val userId: ID
4042
get() = source.userId.ID
@@ -46,6 +48,13 @@ internal class KookBlacklistItemImpl(
4648
override val createdTime: Timestamp = Timestamp.ofMilliseconds(source.createdTime)
4749

4850
override suspend fun delete(vararg options: DeleteOption) {
49-
TODO("Not yet implemented")
51+
operator.delete(targetId = userId, options = options)
5052
}
51-
}
53+
54+
override fun toString(): String {
55+
return "KookBlacklistItemImpl(source=$source, guildId=$guildId, userId=$userId)"
56+
}
57+
}
58+
59+
internal fun BlacklistItem.toKookBlacklistItem(guildId: ID, operator: KookGuildBlacklistOperator): KookBlacklistItem =
60+
KookBlacklistItemImpl(this, guildId, operator)

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/blacklist/internal/KookBlacklistOperatorImpl.kt renamed to simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/blacklist/internal/KookGuildBlacklistOperatorImpl.kt

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,63 @@ import kotlinx.coroutines.flow.asFlow
2626
import kotlinx.coroutines.flow.flatMapConcat
2727
import kotlinx.coroutines.flow.map
2828
import love.forte.simbot.ability.DeleteOption
29+
import love.forte.simbot.ability.StandardDeleteOption
2930
import love.forte.simbot.common.id.ID
3031
import love.forte.simbot.common.id.literal
3132
import love.forte.simbot.component.kook.blacklist.KookBlacklistItem
32-
import love.forte.simbot.component.kook.blacklist.KookBlacklistOperator
33+
import love.forte.simbot.component.kook.blacklist.KookGuildBlacklistOperator
3334
import love.forte.simbot.component.kook.bot.KookBot
3435
import love.forte.simbot.component.kook.util.requestData
3536
import love.forte.simbot.kook.api.ListData
37+
import love.forte.simbot.kook.api.blacklist.CreateBlacklistApi
38+
import love.forte.simbot.kook.api.blacklist.DeleteBlacklistApi
3639
import love.forte.simbot.kook.api.blacklist.GetBlacklistListApi
3740
import love.forte.simbot.kook.api.blacklist.createFlow
3841

3942
/**
4043
*
4144
* @author ForteScarlet
4245
*/
43-
internal class KookBlacklistOperatorImpl(private val bot: KookBot) : KookBlacklistOperator {
46+
internal class KookGuildBlacklistOperatorImpl(
47+
private val bot: KookBot,
48+
override val guildId: ID
49+
) : KookGuildBlacklistOperator {
4450
override suspend fun list(
45-
guildId: ID,
4651
page: Int?,
4752
size: Int?
4853
): ListData<KookBlacklistItem> {
49-
TODO("Not yet implemented")
54+
val api = GetBlacklistListApi.create(guildId = guildId.literal, page = page, pageSize = size)
55+
val raw = bot.requestData(api)
56+
return ListData(
57+
raw.items.map { it.toKookBlacklistItem(guildId, this) },
58+
raw.meta,
59+
raw.sort
60+
)
5061
}
5162

5263
@OptIn(ExperimentalCoroutinesApi::class)
53-
override fun flow(guildId: ID, batchSize: Int?): Flow<KookBlacklistItem> {
64+
override fun flow(batchSize: Int?): Flow<KookBlacklistItem> {
5465
return GetBlacklistListApi.createFlow { page ->
5566
val api = GetBlacklistListApi.create(guildId = guildId.literal, page = page, pageSize = batchSize)
5667
bot.requestData(api)
5768
}.flatMapConcat { it.items.asFlow() }
58-
.map { TODO("Not yet implemented") }
69+
.map { it.toKookBlacklistItem(guildId, this) }
5970
}
6071

61-
override suspend fun add(
62-
guildId: ID,
63-
targetId: ID,
64-
remark: String?,
65-
delMsgDays: Int?
66-
) {
67-
TODO("Not yet implemented")
72+
override suspend fun add(targetId: ID, remark: String?, delMsgDays: Int?) {
73+
val api = CreateBlacklistApi.create(guildId.literal, targetId.literal, remark, delMsgDays)
74+
bot.requestData(api)
6875
}
6976

70-
override suspend fun delete(
71-
guildId: ID,
72-
targetId: ID,
73-
vararg options: DeleteOption
74-
) {
75-
TODO("Not yet implemented")
77+
override suspend fun delete(targetId: ID, vararg options: DeleteOption) {
78+
val api = DeleteBlacklistApi.create(guildId.literal, targetId.literal)
79+
80+
if (options.contains(StandardDeleteOption.IGNORE_ON_FAILURE)) {
81+
runCatching { bot.requestData(api) }
82+
// Log?
83+
return
84+
}
85+
86+
bot.requestData(api)
7687
}
7788
}

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/internal/KookGuildImpl.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import love.forte.simbot.common.id.ID
2929
import love.forte.simbot.common.id.StringID.Companion.ID
3030
import love.forte.simbot.common.id.literal
3131
import love.forte.simbot.component.kook.*
32+
import love.forte.simbot.component.kook.blacklist.ExperimentalBlacklistApi
33+
import love.forte.simbot.component.kook.blacklist.KookGuildBlacklistOperator
34+
import love.forte.simbot.component.kook.blacklist.internal.KookGuildBlacklistOperatorImpl
3235
import love.forte.simbot.component.kook.bot.internal.KookBotImpl
3336
import love.forte.simbot.component.kook.role.KookGuildRole
3437
import love.forte.simbot.component.kook.role.KookGuildRoleCreator
@@ -143,6 +146,10 @@ internal class KookGuildImpl(
143146
@ExperimentalSimbotAPI
144147
override fun roleCreator(): KookGuildRoleCreator = KookGuildRoleCreatorImpl(bot, this)
145148

149+
@ExperimentalBlacklistApi
150+
override val blacklist: KookGuildBlacklistOperator
151+
get() = KookGuildBlacklistOperatorImpl(bot, id)
152+
146153
override fun toString(): String {
147154
return "KookGuild(id=${source.id}, name=${source.name})"
148155
}

0 commit comments

Comments
 (0)