Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginate getting bans #585

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions core/src/main/kotlin/supplier/CacheEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,13 @@ public class CacheEntitySupplier(private val kord: Kord) : EntitySupplier {
return Ban(data, kord)
}

override fun getGuildBans(guildId: Snowflake): Flow<Ban> = cache.query<BanData> {
idEq(BanData::guildId, guildId)
}.asFlow().map { Ban(it, kord) }
override fun getGuildBans(guildId: Snowflake, limit: Int?): Flow<Ban> {
checkLimit(limit)
return cache.query<BanData> { idEq(BanData::guildId, guildId) }
.asFlow()
.map { Ban(it, kord) }
.limit(limit)
}

override fun getGuildMembers(guildId: Snowflake, limit: Int?): Flow<Member> {
checkLimit(limit)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/supplier/EntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public interface EntitySupplier {
* The returned flow is lazily executed, any [RequestException] will be thrown on
* [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead.
*/
public fun getGuildBans(guildId: Snowflake): Flow<Ban>
public fun getGuildBans(guildId: Snowflake, limit: Int? = null): Flow<Ban>

/**
* Requests the [members][Member] of the [Guild] with the given [guildId].
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/supplier/FallbackEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private class FallbackEntitySupplier(val first: EntitySupplier, val second: Enti
override suspend fun getGuildBanOrNull(guildId: Snowflake, userId: Snowflake): Ban? =
first.getGuildBanOrNull(guildId, userId) ?: second.getGuildBanOrNull(guildId, userId)

override fun getGuildBans(guildId: Snowflake): Flow<Ban> =
first.getGuildBans(guildId).switchIfEmpty(second.getGuildBans(guildId))
override fun getGuildBans(guildId: Snowflake, limit: Int?): Flow<Ban> =
first.getGuildBans(guildId, limit).switchIfEmpty(second.getGuildBans(guildId, limit))

override fun getGuildMembers(guildId: Snowflake, limit: Int?): Flow<Member> =
first.getGuildMembers(guildId, limit).switchIfEmpty(second.getGuildMembers(guildId, limit))
Expand Down
14 changes: 10 additions & 4 deletions core/src/main/kotlin/supplier/RestEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,16 @@ public class RestEntitySupplier(public val kord: Kord) : EntitySupplier {
emit(Role(RoleData.from(guildId, roleData), kord))
}

override fun getGuildBans(guildId: Snowflake): Flow<Ban> = flow {
for (banData in guild.getGuildBans(guildId))
emit(Ban(BanData.from(guildId, banData), kord))
}
// maxBatchSize: see https://discord.com/developers/docs/resources/guild#get-guild-bans
override fun getGuildBans(guildId: Snowflake, limit: Int?): Flow<Ban> =
limitedPagination(limit, maxBatchSize = 1000) { batchSize ->
paginateForwards(batchSize, idSelector = { it.user.id }) { after ->
guild.getGuildBans(guildId, position = after, limit = batchSize)
}
}.map {
val data = BanData.from(guildId, it)
Ban(data, kord)
}

// maxBatchSize: see https://discord.com/developers/docs/resources/guild#list-guild-members
override fun getGuildMembers(guildId: Snowflake, limit: Int?): Flow<Member> =
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/supplier/StoreEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public class StoreEntitySupplier(
return storeAndReturn(supplier.getGuildBanOrNull(guildId, userId)) { it.data }
}

override fun getGuildBans(guildId: Snowflake): Flow<Ban> {
return storeOnEach(supplier.getGuildBans(guildId)) { it.data }
override fun getGuildBans(guildId: Snowflake, limit: Int?): Flow<Ban> {
return storeOnEach(supplier.getGuildBans(guildId, limit)) { it.data }
}

override fun getGuildMembers(guildId: Snowflake, limit: Int?): Flow<Member> {
Expand Down
9 changes: 8 additions & 1 deletion rest/src/main/kotlin/service/GuildService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,15 @@ public class GuildService(requestHandler: RequestHandler) : RestService(requestH
auditLogReason(reason)
}

public suspend fun getGuildBans(guildId: Snowflake): List<BanResponse> = call(Route.GuildBansGet) {
public suspend fun getGuildBans(
guildId: Snowflake,
position: Position.BeforeOrAfter? = null,
limit: Int? = null,
): List<BanResponse> = call(Route.GuildBansGet) {
keys[Route.GuildId] = guildId

limit?.let { parameter("limit", it) }
position?.let { parameter(it.key, it.value) }
}

public suspend fun getGuildBan(guildId: Snowflake, userId: Snowflake): BanResponse = call(Route.GuildBanGet) {
Expand Down