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

Thread updates #358

Merged
merged 4 commits into from
Aug 3, 2021
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
39 changes: 39 additions & 0 deletions common/src/main/kotlin/entity/AuditLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ data class DiscordAuditLog(
@SerialName("audit_log_entries")
val auditLogEntries: List<DiscordAuditLogEntry>,
val integrations: List<DiscordPartialIntegration>,
val threads: List<DiscordChannel>
)

@Serializable
Expand Down Expand Up @@ -301,6 +302,20 @@ sealed class AuditLogChangeKey<T>(val name: String, val serializer: KSerializer<
@SerialName("expire_grace_period")
object ExpireGracePeriod : AuditLogChangeKey<Int>("expire_grace_period", serializer())

@SerialName("user_limit")
object UserLimit : AuditLogChangeKey<Int>("user_limit", serializer())

@SerialName("archived")
object Archived : AuditLogChangeKey<Boolean>("archived", serializer())

@SerialName("locked")
object Locked : AuditLogChangeKey<Boolean>("locked", serializer())

@SerialName("auto_archive_duration")
object AutoArchiveDuration : AuditLogChangeKey<ArchiveDuration>("auto_archive_duration", serializer())

@SerialName("default_auto_archive_duration")
object DefaultAutoArchiveDuration : AuditLogChangeKey<ArchiveDuration>("default_auto_archive_duration", serializer())

internal class Serializer<T>(val type: KSerializer<T>) : KSerializer<AuditLogChangeKey<T>> {
override val descriptor: SerialDescriptor
Expand Down Expand Up @@ -361,6 +376,11 @@ sealed class AuditLogChangeKey<T>(val name: String, val serializer: KSerializer<
"enable_emoticons" -> EnableEmoticons
"expire_behavior" -> ExpireBehavior
"expire_grace_period" -> ExpireGracePeriod
"user_limit" -> UserLimit
"locked" -> Locked
"archived" -> Archived
"auto_archive_duration" -> AutoArchiveDuration
"default_auto_archive_duration" -> DefaultAutoArchiveDuration
else -> Unknown(name)
} as AuditLogChangeKey<T>
}
Expand Down Expand Up @@ -405,6 +425,16 @@ sealed class AuditLogEvent(val value: Int) {
object IntegrationCreate : AuditLogEvent(80)
object IntegrationUpdate : AuditLogEvent(81)
object IntegrationDelete : AuditLogEvent(82)
object StageInstanceCreate : AuditLogEvent(83)
object StageInstanceUpdate : AuditLogEvent(84)
object StageInstanceDelete : AuditLogEvent(85)
object StickerCreate : AuditLogEvent(90)
object StickerUpdate : AuditLogEvent(91)
object StickerDelete : AuditLogEvent(92)
object ThreadCreate : AuditLogEvent(110)
object ThreadUpdate : AuditLogEvent(111)
object ThreadDelete : AuditLogEvent(112)


internal object Serializer : KSerializer<AuditLogEvent> {
override val descriptor: SerialDescriptor
Expand Down Expand Up @@ -450,6 +480,15 @@ sealed class AuditLogEvent(val value: Int) {
80 -> IntegrationCreate
81 -> IntegrationUpdate
82 -> IntegrationDelete
83 -> StageInstanceCreate
84 -> StageInstanceUpdate
85 -> StageInstanceDelete
90 -> StickerCreate
91 -> StickerUpdate
92 -> StickerDelete
110 -> ThreadCreate
111 -> ThreadUpdate
112 -> ThreadDelete
else -> Unknown(value)
}
}
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dev.kord.core.cache.idEq
import dev.kord.core.catchDiscordError
import dev.kord.core.entity.*
import dev.kord.core.entity.channel.*
import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.entity.interaction.GuildApplicationCommand
import dev.kord.core.event.guild.MembersChunkEvent
import dev.kord.core.exception.EntityNotFoundException
Expand Down Expand Up @@ -65,6 +66,17 @@ interface GuildBehavior : KordEntity, Strategizable {
val bans: Flow<Ban>
get() = supplier.getGuildBans(id)

/**
* Returns all active public and private threads in this guild
* Threads are ordered by their id, in descending order.
*
* 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.
*/
val activeThreads: Flow<ThreadChannel>
get() = supplier.getActiveThreads(id)

/**
* Requests to get all present webhooks for this guild.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.json.request.StartThreadRequest
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import java.util.*
Expand All @@ -33,7 +34,7 @@ interface ThreadParentChannelBehavior : TopGuildMessageChannelBehavior {
* [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead.
*/
val activeThreads: Flow<ThreadChannel> get() = supplier.getActiveThreads(id)
val activeThreads: Flow<ThreadChannel> get() = supplier.getActiveThreads(guildId).filter { it.parentId == id }

/**
* Returns archived threads in the channel that are public.
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/kotlin/entity/AuditLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import dev.kord.common.entity.*
import dev.kord.common.entity.optional.orEmpty
import dev.kord.core.Kord
import dev.kord.core.KordObject
import dev.kord.core.cache.data.ChannelData
import dev.kord.core.cache.data.IntegrationData
import dev.kord.core.cache.data.UserData
import dev.kord.core.cache.data.WebhookData
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.thread.ThreadChannel

class AuditLog(val data: DiscordAuditLog, val guildId: Snowflake, override val kord: Kord) : KordObject {

Expand All @@ -16,6 +19,11 @@ class AuditLog(val data: DiscordAuditLog, val guildId: Snowflake, override val k

val integrations: List<Snowflake> get() = data.integrations.map { it.id }

val threads: List<ThreadChannel> get() = data.threads.map {
val data = ChannelData.from(it)
Channel.from(data, kord)
}.filterIsInstance<ThreadChannel>()

val entries: List<AuditLogEntry> get() = data.auditLogEntries.map { AuditLogEntry(it, kord) }

}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/supplier/CacheAwareRestSupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ class StoreEntitySupplier(
return storeOnEach(supplier.getThreadMembers(channelId)) { it.data }
}

override fun getActiveThreads(channelId: Snowflake): Flow<ThreadChannel> {
return storeOnEach(supplier.getActiveThreads(channelId)) { it.data }
override fun getActiveThreads(guildId: Snowflake): Flow<ThreadChannel> {
return storeOnEach(supplier.getActiveThreads(guildId)) { it.data }
}

override fun getPublicArchivedThreads(channelId: Snowflake, before: Instant, limit: Int): Flow<ThreadChannel> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/supplier/CacheEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ class CacheEntitySupplier(private val kord: Kord) : EntitySupplier {
}.asFlow().map { ThreadMember(it, kord) }
}

override fun getActiveThreads(channelId: Snowflake): Flow<ThreadChannel> {
override fun getActiveThreads(guildId: Snowflake): Flow<ThreadChannel> {
return cache.query<ChannelData> {
idEq(ChannelData::parentId, channelId)
idEq(ChannelData::guildId, guildId)
}.asFlow().filter {
it.threadMetadata.value?.archived != true
}.mapNotNull {
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 @@ -400,7 +400,7 @@ interface EntitySupplier {

fun getThreadMembers(channelId: Snowflake): Flow<ThreadMember>

fun getActiveThreads(channelId: Snowflake): Flow<ThreadChannel>
fun getActiveThreads(guildId: Snowflake): Flow<ThreadChannel>

fun getPublicArchivedThreads(channelId: Snowflake, before: Instant, limit: Int): Flow<ThreadChannel>

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 @@ -124,8 +124,8 @@ private class FallbackEntitySupplier(val first: EntitySupplier, val second: Enti
return first.getThreadMembers(channelId).switchIfEmpty(second.getThreadMembers(channelId))
}

override fun getActiveThreads(channelId: Snowflake): Flow<ThreadChannel> {
return first.getActiveThreads(channelId).switchIfEmpty(second.getActiveThreads(channelId))
override fun getActiveThreads(guildId: Snowflake): Flow<ThreadChannel> {
return first.getActiveThreads(guildId).switchIfEmpty(second.getActiveThreads(guildId))
}

override fun getPublicArchivedThreads(channelId: Snowflake, before: Instant, limit: Int): Flow<ThreadChannel> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/supplier/RestEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ class RestEntitySupplier(val kord: Kord) : EntitySupplier {
}
}

override fun getActiveThreads(channelId: Snowflake): Flow<ThreadChannel> = flow {
kord.rest.channel.listActiveThreads(channelId).threads.onEach {
override fun getActiveThreads(guildId: Snowflake): Flow<ThreadChannel> = flow {
kord.rest.guild.listActiveThreads(guildId).threads.onEach {
val data = ChannelData.from(it)
val channel = Channel.from(data, kord)
if (channel is ThreadChannel) emit(channel)
Expand Down
12 changes: 5 additions & 7 deletions rest/src/main/kotlin/route/Route.kt
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,6 @@ sealed class Route<T>(
ListSerializer(DiscordThreadMember.serializer())
)

object ActiveThreadsGet :
Route<ListThreadsResponse>(
HttpMethod.Get,
"/channels/${ChannelId}/threads/active",
ListThreadsResponse.serializer()
)


object PrivateThreadsGet :
Route<ListThreadsResponse>(
Expand Down Expand Up @@ -750,6 +743,11 @@ sealed class Route<T>(
"/channels/$ChannelId/users/@me/threads/archived/private",
ListThreadsResponse.serializer()
)
object ActiveThreadsGet : Route<ListThreadsResponse>(
HttpMethod.Get,
" /guilds/${GuildId}/threads/active",
ListThreadsResponse.serializer()
)


companion object {
Expand Down
6 changes: 0 additions & 6 deletions rest/src/main/kotlin/service/ChannelService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,6 @@ class ChannelService(requestHandler: RequestHandler) : RestService(requestHandle
}
}

suspend fun listActiveThreads(channelId: Snowflake): ListThreadsResponse {
return call(Route.ActiveThreadsGet) {
keys[Route.ChannelId] = channelId

}
}

suspend fun listPublicArchivedThreads(channelId: Snowflake, request: ListThreadsByTimestampRequest): ListThreadsResponse {
return call(Route.PublicArchivedThreadsGet) {
Expand Down
6 changes: 6 additions & 0 deletions rest/src/main/kotlin/service/GuildService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import dev.kord.rest.builder.role.RoleCreateBuilder
import dev.kord.rest.builder.role.RoleModifyBuilder
import dev.kord.rest.builder.role.RolePositionsModifyBuilder
import dev.kord.rest.json.request.*
import dev.kord.rest.json.response.ListThreadsResponse
import dev.kord.rest.request.RequestHandler
import dev.kord.rest.request.auditLogReason
import dev.kord.rest.route.Position
Expand Down Expand Up @@ -408,6 +409,11 @@ class GuildService(requestHandler: RequestHandler) : RestService(requestHandler)
}


suspend fun listActiveThreads(guildId: Snowflake): ListThreadsResponse {
return call(Route.ActiveThreadsGet) {
keys[Route.GuildId] = guildId
}
}
}

@OptIn(ExperimentalContracts::class)
Expand Down
3 changes: 2 additions & 1 deletion rest/src/test/resources/json/auditlog.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@
"id": "1234567"
}
}
]
],
"threads" : []
}