Skip to content

Commit

Permalink
Unify permission overwrite API (Fix kordlib#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Oct 13, 2021
1 parent 6bb7881 commit e71399b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 94 deletions.
38 changes: 6 additions & 32 deletions rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package dev.kord.rest.builder.channel

import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Overwrite
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.OverwriteType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.GuildChannelCreateRequest
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

@KordDsl
class CategoryCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannelCreateRequest> {
class CategoryCreateBuilder(var name: String) : PermissionOverritesBuilder, AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _position: OptionalInt = OptionalInt.Missing
Expand All @@ -25,35 +20,14 @@ class CategoryCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannel
private var _nsfw: OptionalBoolean = OptionalBoolean.Missing
var nsfw: Boolean? by ::_nsfw.delegate()

val permissionOverwrites: MutableList<Overwrite> = mutableListOf()

/**
* adds a [Overwrite] for the [memberId].
*/
@OptIn(ExperimentalContracts::class)
inline fun addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}
permissionOverwrites += PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite()
}

/**
* adds a [Overwrite] for the [roleId].
*/
@OptIn(ExperimentalContracts::class)
inline fun addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}
permissionOverwrites += PermissionOverwriteBuilder(OverwriteType.Role, roleId).apply(builder).toOverwrite()
}
private var _permissionOverwrites: Optional<MutableSet<Overwrite>> = Optional.Missing()
override var permissionOverwrites by ::_permissionOverwrites.delegate()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name = name,
position = _position,
nsfw = _nsfw,
permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites),
permissionOverwrite = _permissionOverwrites,
type = ChannelType.GuildCategory
)
}
41 changes: 5 additions & 36 deletions rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package dev.kord.rest.builder.channel

import dev.kord.common.entity.Overwrite
import dev.kord.common.entity.Snowflake
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.OverwriteType
import dev.kord.common.entity.Overwrite
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.ChannelModifyPatchRequest
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

@KordDsl
class CategoryModifyBuilder : AuditRequestBuilder<ChannelModifyPatchRequest> {
class CategoryModifyBuilder : PermissionOverritesBuilder, AuditRequestBuilder<ChannelModifyPatchRequest> {

override var reason: String? = null

Expand All @@ -37,37 +32,11 @@ class CategoryModifyBuilder : AuditRequestBuilder<ChannelModifyPatchRequest> {
/**
* The permission overwrites for this category.
*/
var permissionOverwrites: MutableSet<Overwrite>? by ::_permissionOverwrites.delegate()

/**
* adds a [Overwrite] for the [memberId].
*/
@OptIn(ExperimentalContracts::class)
inline fun addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}
val overwrite = permissionOverwrites ?: mutableSetOf()
overwrite.add(PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite())
permissionOverwrites = overwrite
}

/**
* adds a [Overwrite] for the [roleId].
*/
@OptIn(ExperimentalContracts::class)
inline fun addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}
val overwrite = permissionOverwrites ?: mutableSetOf()
overwrite.add(PermissionOverwriteBuilder(OverwriteType.Role, roleId).apply(builder).toOverwrite())
permissionOverwrites = overwrite
}
override var permissionOverwrites by ::_permissionOverwrites.delegate()

override fun toRequest(): ChannelModifyPatchRequest = ChannelModifyPatchRequest(
name = _name,
position = _position,
permissionOverwrites = _permissionOverwrites
)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package dev.kord.rest.builder.channel

import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.Overwrite
import dev.kord.common.entity.Snowflake
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.ChannelModifyPatchRequest

@KordDsl
class TextChannelModifyBuilder : AuditRequestBuilder<ChannelModifyPatchRequest> {
class TextChannelModifyBuilder : PermissionOverritesBuilder,
AuditRequestBuilder<ChannelModifyPatchRequest> {
override var reason: String? = null

private var _name: Optional<String> = Optional.Missing()
Expand All @@ -34,7 +35,7 @@ class TextChannelModifyBuilder : AuditRequestBuilder<ChannelModifyPatchRequest>
var rateLimitPerUser: Int? by ::_rateLimitPerUser.delegate()

private var _permissionOverwrites: Optional<MutableSet<Overwrite>?> = Optional.Missing()
var permissionOverwrites: MutableSet<Overwrite>? by ::_permissionOverwrites.delegate()
override var permissionOverwrites: MutableSet<Overwrite>? by ::_permissionOverwrites.delegate()

override fun toRequest(): ChannelModifyPatchRequest = ChannelModifyPatchRequest(
name = _name,
Expand Down
16 changes: 9 additions & 7 deletions rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package dev.kord.rest.builder.channel

import dev.kord.common.entity.Overwrite
import dev.kord.common.entity.Snowflake
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Overwrite
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.GuildChannelCreateRequest

@KordDsl
class NewsChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannelCreateRequest> {
class NewsChannelCreateBuilder(var name: String) : PermissionOverritesBuilder,
AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _topic: Optional<String> = Optional.Missing()
Expand All @@ -28,15 +29,16 @@ class NewsChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChan
private var _position: OptionalInt = OptionalInt.Missing
var position: Int? by ::_position.delegate()

val permissionOverwrites: MutableList<Overwrite> = mutableListOf()
private var _permissionOverwrites: Optional<MutableSet<Overwrite>> = Optional.Missing()
override var permissionOverwrites by ::_permissionOverwrites.delegate()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name = name,
topic = _topic,
nsfw = _nsfw,
parentId = _parentId,
position = _position,
permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites),
permissionOverwrite = _permissionOverwrites,
type = ChannelType.GuildNews
)
}
}
44 changes: 44 additions & 0 deletions rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dev.kord.rest.builder.channel

import dev.kord.common.entity.Overwrite
import dev.kord.common.entity.OverwriteType
import dev.kord.common.entity.Snowflake
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* Interface containing shared behavior on all builders modifying permission overwrites.
*/
interface PermissionOverritesBuilder {
/**
* The permission overwrites for this category.
*/
var permissionOverwrites: MutableSet<Overwrite>?
}

/**
* adds a [Overwrite] for the [memberId].
*/
@OptIn(ExperimentalContracts::class)
inline fun PermissionOverritesBuilder.addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}
val overwrite = permissionOverwrites ?: mutableSetOf()
overwrite.add(PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite())
permissionOverwrites = overwrite
}

/**
* adds a [Overwrite] for the [roleId].
*/
@OptIn(ExperimentalContracts::class)
inline fun PermissionOverritesBuilder.addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}
val overwrite = permissionOverwrites ?: mutableSetOf()
overwrite.add(PermissionOverwriteBuilder(OverwriteType.Role, roleId).apply(builder).toOverwrite())
permissionOverwrites = overwrite
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.GuildChannelCreateRequest

@KordDsl
class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannelCreateRequest> {
class TextChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _topic: Optional<String> = Optional.Missing()
Expand All @@ -31,7 +31,8 @@ class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChan
private var _nsfw: OptionalBoolean = OptionalBoolean.Missing
var nsfw: Boolean? by ::_nsfw.delegate()

val permissionOverwrites: MutableList<Overwrite> = mutableListOf()
private var _permissionOverwrites: Optional<MutableSet<Overwrite>> = Optional.Missing()
override var permissionOverwrites by ::_permissionOverwrites.delegate()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name,
Expand All @@ -41,6 +42,6 @@ class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChan
_position,
parentId = _parentId,
nsfw = _nsfw,
permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites),
permissionOverwrite = _permissionOverwrites,
)
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package dev.kord.rest.builder.channel

import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Overwrite
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.GuildChannelCreateRequest

@KordDsl
class VoiceChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannelCreateRequest> {
class VoiceChannelCreateBuilder(var name: String) :
PermissionOverritesBuilder, AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _bitrate: OptionalInt = OptionalInt.Missing
Expand All @@ -27,15 +28,16 @@ class VoiceChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildCha
private var _position: OptionalInt = OptionalInt.Missing
var position: Int? by ::_position.delegate()

val permissionOverwrites: MutableList<Overwrite> = mutableListOf()
private var _permissionOverwrites: Optional<MutableSet<Overwrite>> = Optional.Missing()
override var permissionOverwrites by ::_permissionOverwrites.delegate()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name = name,
bitrate = _bitrate,
userLimit = _userLimit,
parentId = _parentId,
position = _position,
permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites),
permissionOverwrite = _permissionOverwrites,
type = ChannelType.GuildVoice
)
}
}
24 changes: 18 additions & 6 deletions rest/src/main/kotlin/json/request/GuildRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@ package dev.kord.rest.json.request
import dev.kord.common.Color
import dev.kord.common.annotation.DeprecatedSinceKord
import dev.kord.common.annotation.KordExperimental
import dev.kord.common.entity.*
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.DefaultMessageNotificationLevel
import dev.kord.common.entity.DiscordWelcomeScreenChannel
import dev.kord.common.entity.ExplicitContentFilter
import dev.kord.common.entity.IntegrationExpireBehavior
import dev.kord.common.entity.Overwrite
import dev.kord.common.entity.Permissions
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.VerificationLevel
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.OptionalSnowflake
import dev.kord.rest.builder.guild.WelcomeScreenChannelBuilder
import kotlinx.serialization.*
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.listSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

Expand Down Expand Up @@ -47,7 +59,7 @@ data class GuildChannelCreateRequest(
val rateLimitPerUser: Optional<Int> = Optional.Missing(),
val position: OptionalInt = OptionalInt.Missing,
@SerialName("permission_overwrites")
val permissionOverwrite: Optional<List<Overwrite>> = Optional.Missing(),
val permissionOverwrite: Optional<Set<Overwrite>> = Optional.Missing(),
@SerialName("parent_id")
val parentId: OptionalSnowflake = OptionalSnowflake.Missing,
val nsfw: OptionalBoolean = OptionalBoolean.Missing,
Expand Down Expand Up @@ -237,4 +249,4 @@ data class GuildWelcomeScreenModifyRequest(
val enabled: OptionalBoolean = OptionalBoolean.Missing,
val welcomeScreenChannels: Optional<List<DiscordWelcomeScreenChannel>> = Optional.Missing(),
val description: Optional<String> = Optional.Missing()
)
)

0 comments on commit e71399b

Please sign in to comment.