-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Common functionality between ForumChannel and MediaChannel was extracted to a new ThreadOnlyChannel supertype. See discord/discord-api-docs#6232
- Loading branch information
1 parent
cbba5f3
commit 308f5e8
Showing
26 changed files
with
849 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
core/src/commonMain/kotlin/behavior/channel/MediaChannelBehavior.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package dev.kord.core.behavior.channel | ||
|
||
import dev.kord.common.exception.RequestException | ||
import dev.kord.core.cache.data.ChannelData | ||
import dev.kord.core.entity.channel.Channel | ||
import dev.kord.core.entity.channel.MediaChannel | ||
import dev.kord.core.exception.EntityNotFoundException | ||
import dev.kord.core.supplier.EntitySupplier | ||
import dev.kord.core.supplier.EntitySupplyStrategy | ||
import dev.kord.rest.builder.channel.MediaChannelModifyBuilder | ||
import dev.kord.rest.service.patchMediaChannel | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
public interface MediaChannelBehavior : ThreadOnlyChannelBehavior { | ||
|
||
/** | ||
* Requests to get this behavior as a [MediaChannel]. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
* @throws EntityNotFoundException if the channel wasn't present. | ||
* @throws ClassCastException if the channel isn't a [MediaChannel]. | ||
*/ | ||
override suspend fun asChannel(): MediaChannel = super.asChannel() as MediaChannel | ||
|
||
/** | ||
* Requests to get this behavior as a [MediaChannel], | ||
* returns null if the channel isn't present or if the channel isn't a [MediaChannel]. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
*/ | ||
override suspend fun asChannelOrNull(): MediaChannel? = super.asChannelOrNull() as? MediaChannel | ||
|
||
/** | ||
* Retrieve the [MediaChannel] associated with this behavior from the provided [EntitySupplier]. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
* @throws EntityNotFoundException if the user wasn't present. | ||
*/ | ||
override suspend fun fetchChannel(): MediaChannel = super.fetchChannel() as MediaChannel | ||
|
||
/** | ||
* Retrieve the [MediaChannel] associated with this behavior from the provided [EntitySupplier] | ||
* returns null if the [MediaChannel] isn't present. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
*/ | ||
override suspend fun fetchChannelOrNull(): MediaChannel? = super.fetchChannelOrNull() as? MediaChannel | ||
|
||
override fun withStrategy(strategy: EntitySupplyStrategy<*>): MediaChannelBehavior | ||
} | ||
|
||
public suspend inline fun MediaChannelBehavior.edit(builder: MediaChannelModifyBuilder.() -> Unit): MediaChannel { | ||
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } | ||
|
||
val response = kord.rest.channel.patchMediaChannel(id, builder) | ||
val data = ChannelData.from(response) | ||
|
||
return Channel.from(data, kord) as MediaChannel | ||
} |
75 changes: 75 additions & 0 deletions
75
core/src/commonMain/kotlin/behavior/channel/ThreadOnlyChannelBehavior.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package dev.kord.core.behavior.channel | ||
|
||
import dev.kord.common.exception.RequestException | ||
import dev.kord.core.behavior.channel.threads.ThreadParentChannelBehavior | ||
import dev.kord.core.cache.data.ChannelData | ||
import dev.kord.core.entity.channel.Channel | ||
import dev.kord.core.entity.channel.ThreadOnlyChannel | ||
import dev.kord.core.entity.channel.thread.TextChannelThread | ||
import dev.kord.core.exception.EntityNotFoundException | ||
import dev.kord.core.supplier.EntitySupplier | ||
import dev.kord.core.supplier.EntitySupplyStrategy | ||
import dev.kord.rest.builder.channel.thread.StartForumThreadBuilder | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.datetime.Instant | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
public interface ThreadOnlyChannelBehavior : ThreadParentChannelBehavior { | ||
|
||
override val activeThreads: Flow<TextChannelThread> get() = super.activeThreads.filterIsInstance() | ||
|
||
override fun getPublicArchivedThreads(before: Instant?, limit: Int?): Flow<TextChannelThread> = | ||
super.getPublicArchivedThreads(before, limit).filterIsInstance() | ||
|
||
public suspend fun startPublicThread(name: String, builder: StartForumThreadBuilder.() -> Unit): TextChannelThread = | ||
unsafeStartThreadInThreadOnlyChannel(name, builder) | ||
|
||
/** | ||
* Requests to get this behavior as a [ThreadOnlyChannel]. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
* @throws EntityNotFoundException if the channel wasn't present. | ||
* @throws ClassCastException if the channel isn't a [ThreadOnlyChannel]. | ||
*/ | ||
override suspend fun asChannel(): ThreadOnlyChannel = super.asChannel() as ThreadOnlyChannel | ||
|
||
/** | ||
* Requests to get this behavior as a [ThreadOnlyChannel], | ||
* returns null if the channel isn't present or if the channel isn't a [ThreadOnlyChannel]. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
*/ | ||
override suspend fun asChannelOrNull(): ThreadOnlyChannel? = super.asChannelOrNull() as? ThreadOnlyChannel | ||
|
||
/** | ||
* Retrieve the [ThreadOnlyChannel] associated with this behavior from the provided [EntitySupplier]. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
* @throws EntityNotFoundException if the user wasn't present. | ||
*/ | ||
override suspend fun fetchChannel(): ThreadOnlyChannel = super.fetchChannel() as ThreadOnlyChannel | ||
|
||
/** | ||
* Retrieve the [ThreadOnlyChannel] associated with this behavior from the provided [EntitySupplier] | ||
* returns null if the [ThreadOnlyChannel] isn't present. | ||
* | ||
* @throws RequestException if anything went wrong during the request. | ||
*/ | ||
override suspend fun fetchChannelOrNull(): ThreadOnlyChannel? = super.fetchChannelOrNull() as? ThreadOnlyChannel | ||
|
||
override fun withStrategy(strategy: EntitySupplyStrategy<*>): ThreadOnlyChannelBehavior | ||
} | ||
|
||
internal suspend fun ThreadParentChannelBehavior.unsafeStartThreadInThreadOnlyChannel( | ||
name: String, | ||
builder: StartForumThreadBuilder.() -> Unit, | ||
): TextChannelThread { | ||
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } | ||
|
||
val response = kord.rest.channel.startForumThread(id, name, builder) | ||
val data = ChannelData.from(response) | ||
|
||
return Channel.from(data, kord) as TextChannelThread | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.