Skip to content

Commit

Permalink
sort threads as documented by discord (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
HopeBaron authored Oct 4, 2021
1 parent 1a28529 commit f6545a9
Showing 1 changed file with 61 additions and 39 deletions.
100 changes: 61 additions & 39 deletions core/src/main/kotlin/supplier/CacheEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -288,62 +288,84 @@ public class CacheEntitySupplier(private val kord: Kord) : EntitySupplier {
}.asFlow().map { ThreadMember(it, kord) }
}

override fun getActiveThreads(guildId: Snowflake): Flow<ThreadChannel> {
return cache.query<ChannelData> {
override fun getActiveThreads(guildId: Snowflake): Flow<ThreadChannel> = flow {
val result = cache.query<ChannelData> {
idEq(ChannelData::guildId, guildId)
}.asFlow().filter {
}.toCollection()
.sortedByDescending { it.id }
.asFlow()
.filter {
it.threadMetadata.value?.archived != true
}.mapNotNull {
Channel.from(it, kord) as? ThreadChannel
}
}

override fun getPublicArchivedThreads(channelId: Snowflake, before: Instant, limit: Int): Flow<ThreadChannel> {
return cache.query<ChannelData> {
idEq(ChannelData::parentId, channelId)
}.asFlow().filter {
val time = it.threadMetadata.value?.archiveTimestamp?.toInstant()
it.threadMetadata.value?.archived == true
&& time != null
&& time < before
&& (it.type == ChannelType.PublicGuildThread || it.type == ChannelType.PublicNewsThread)
}.take(limit).mapNotNull { Channel.from(it, kord) as? ThreadChannel }
emitAll(result)
}

override fun getPrivateArchivedThreads(channelId: Snowflake, before: Instant, limit: Int): Flow<ThreadChannel> {
return cache.query<ChannelData> {
idEq(ChannelData::parentId, channelId)
}.asFlow().filter {
val time = it.threadMetadata.value?.archiveTimestamp?.toInstant()
it.threadMetadata.value?.archived == true
&& time != null
&& time < before
&& it.type == ChannelType.PrivateThread
}.take(limit).mapNotNull { Channel.from(it, kord) as? ThreadChannel }
}
override fun getPublicArchivedThreads(channelId: Snowflake, before: Instant, limit: Int): Flow<ThreadChannel> =
flow {
val result = cache.query<ChannelData> {
idEq(ChannelData::parentId, channelId)
}.toCollection()
.sortedByDescending { it.threadMetadata.value?.archiveTimestamp?.toInstant() }
.asFlow()
.filter {
val time = it.threadMetadata.value?.archiveTimestamp?.toInstant()
it.threadMetadata.value?.archived == true
&& time != null
&& time < before
&& (it.type == ChannelType.PublicGuildThread || it.type == ChannelType.PublicNewsThread)
}.take(limit).mapNotNull { Channel.from(it, kord) as? ThreadChannel }

emitAll(result)
}

override fun getPrivateArchivedThreads(channelId: Snowflake, before: Instant, limit: Int): Flow<ThreadChannel> =
flow {
val result = cache.query<ChannelData> {
idEq(ChannelData::parentId, channelId)
}.toCollection()
.sortedByDescending { it.threadMetadata.value?.archiveTimestamp?.toInstant() }
.asFlow()
.filter {
val time = it.threadMetadata.value?.archiveTimestamp?.toInstant()
it.threadMetadata.value?.archived == true
&& time != null
&& time < before
&& it.type == ChannelType.PrivateThread
}.take(limit).mapNotNull { Channel.from(it, kord) as? ThreadChannel }

emitAll(result)
}

override fun getJoinedPrivateArchivedThreads(
channelId: Snowflake,
before: Snowflake,
limit: Int
): Flow<ThreadChannel> {
return cache.query<ChannelData> {
): Flow<ThreadChannel> = flow {
val result = cache.query<ChannelData> {
idEq(ChannelData::parentId, channelId)
}.asFlow().filter {
it.threadMetadata.value?.archived == true
&& it.id < before
&& it.type == ChannelType.PrivateThread
&& it.member !is Optional.Missing
}.take(limit).mapNotNull { Channel.from(it, kord) as? ThreadChannel }
}.toCollection()
.sortedByDescending { it.id }
.asFlow()
.filter {
it.threadMetadata.value?.archived == true
&& it.id < before
&& it.type == ChannelType.PrivateThread
&& it.member !is Optional.Missing
}.take(limit).mapNotNull { Channel.from(it, kord) as? ThreadChannel }

emitAll(result)
}

override fun getGuildApplicationCommands(
applicationId: Snowflake,
guildId: Snowflake
): Flow<GuildApplicationCommand> = cache.query<ApplicationCommandData> {
idEq(ApplicationCommandData::guildId, guildId)
idEq(ApplicationCommandData::applicationId, applicationId)
}.asFlow().map { GuildApplicationCommand(it, kord.rest.interaction) }
idEq(ApplicationCommandData::guildId, guildId)
idEq(ApplicationCommandData::applicationId, applicationId)
}.asFlow().map { GuildApplicationCommand(it, kord.rest.interaction) }


override suspend fun getGuildApplicationCommandOrNull(
Expand Down Expand Up @@ -383,9 +405,9 @@ public class CacheEntitySupplier(private val kord: Kord) : EntitySupplier {
applicationId: Snowflake,
guildId: Snowflake
): Flow<ApplicationCommandPermissions> = cache.query<GuildApplicationCommandPermissionsData> {
idEq(GuildApplicationCommandPermissionsData::guildId, guildId)
idEq(GuildApplicationCommandPermissionsData::applicationId, applicationId)
}.asFlow().map { ApplicationCommandPermissions(it) }
idEq(GuildApplicationCommandPermissionsData::guildId, guildId)
idEq(GuildApplicationCommandPermissionsData::applicationId, applicationId)
}.asFlow().map { ApplicationCommandPermissions(it) }


override suspend fun getApplicationCommandPermissionsOrNull(
Expand Down

0 comments on commit f6545a9

Please sign in to comment.