Skip to content

Commit

Permalink
Fix some cache links (#934)
Browse files Browse the repository at this point in the history
Links are supposed to go from the primary data to the data that should
be removed on deletion of the primary data (e.g. guild -> member,
members should be removed when a guild is deleted).

The link from UserData to WebhookData was removed, the webhook shouldn't
be deleted if the user that created it is deleted.

More links were added:

 * from ChannelData to MessageData, ThreadMemberData, WebhookData and
   VoiceStateData
 
 * from GuildData to EmojiData
 
 * from UserData to ThreadMemberData

See https://github.com/kordlib/cache/blob/024dedd2b866d4098df5d3f885636c7eb6a22a8f/README.md#cascading
  • Loading branch information
lukellmann authored Apr 12, 2024
1 parent 681b22d commit 2a0d3f8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public data class ApplicationCommandData(
val version: Snowflake
) {
public companion object {
public val description: DataDescription<ApplicationCommandData, Snowflake> = description(ApplicationCommandData::id) {
link(ApplicationCommandData::guildId to GuildData::id)
}
public val description: DataDescription<ApplicationCommandData, Snowflake> =
description(ApplicationCommandData::id) {
link(ApplicationCommandData::id to GuildApplicationCommandPermissionsData::id)
}

public fun from(command: DiscordApplicationCommand): ApplicationCommandData {
return with(command) {
ApplicationCommandData(
Expand Down
7 changes: 6 additions & 1 deletion core/src/commonMain/kotlin/cache/data/ChannelData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public data class ChannelData(


public companion object {
public val description: DataDescription<ChannelData, Snowflake> = description(ChannelData::id)
public val description: DataDescription<ChannelData, Snowflake> = description(ChannelData::id) {
link(ChannelData::id to MessageData::channelId)
link(ChannelData::id to ThreadMemberData::id)
link(ChannelData::id to WebhookData::channelId)
link(ChannelData::id to VoiceStateData::channelId)
}

public fun from(entity: DiscordChannel): ChannelData = with(entity) {
ChannelData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ public data class GuildApplicationCommandPermissionsData(

public companion object {
public val description: DataDescription<GuildApplicationCommandPermissionsData, Snowflake> =
description(GuildApplicationCommandPermissionsData::id) {
link(GuildApplicationCommandPermissionsData::guildId to GuildData::id)
link(GuildApplicationCommandPermissionsData::id to ApplicationCommandData::id)
}
description(GuildApplicationCommandPermissionsData::id)

public fun from(permissions: DiscordGuildApplicationCommandPermissions): GuildApplicationCommandPermissionsData =
with(permissions) {
Expand Down
7 changes: 6 additions & 1 deletion core/src/commonMain/kotlin/cache/data/GuildData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import dev.kord.common.serialization.DurationInSeconds
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable

private val ApplicationCommandData.nullableGuildId get() = guildId.value
private val MessageData.nullableGuildId get() = guildId.value
private val ChannelData.nullableGuildId get() = guildId.value
private val WebhookData.nullableGuildId get() = guildId.value
private val StickerData.nullableGuildId get() = guildId.value

@Serializable
public data class GuildData(
Expand Down Expand Up @@ -71,14 +73,17 @@ public data class GuildData(
public companion object {

public val description: DataDescription<GuildData, Snowflake> = description(GuildData::id) {

link(GuildData::id to ApplicationCommandData::nullableGuildId)
link(GuildData::id to GuildApplicationCommandPermissionsData::guildId)
link(GuildData::id to RoleData::guildId)
link(GuildData::id to ChannelData::nullableGuildId)
link(GuildData::id to MemberData::guildId)
link(GuildData::id to MessageData::nullableGuildId)
link(GuildData::id to WebhookData::nullableGuildId)
link(GuildData::id to VoiceStateData::guildId)
link(GuildData::id to PresenceData::guildId)
link(GuildData::id to StickerData::nullableGuildId)
link(GuildData::id to EmojiData::guildId)
link(GuildData::id to AutoModerationRuleData::guildId)
}

Expand Down
15 changes: 7 additions & 8 deletions core/src/commonMain/kotlin/cache/data/StickerData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ public data class StickerData(
) {
public companion object {

public val description: DataDescription<StickerData, Snowflake> = description(StickerData::id) {
link(StickerData::guildId to GuildData::id)
link(StickerData::packId to StickerPackData::id)
}

public val description: DataDescription<StickerData, Snowflake> = description(StickerData::id)

public fun from(entity: DiscordMessageSticker): StickerData = with(entity) {
StickerData(id, packId, name, description, tags, formatType, available, guildId, user.map { it.toData() }, sortValue)
Expand All @@ -49,6 +45,7 @@ public data class StickerItemData(
}
}

private val StickerData.nullablePackId get() = packId.value

public data class StickerPackData(
val id: Snowflake,
Expand All @@ -58,13 +55,15 @@ public data class StickerPackData(
val coverStickerId: OptionalSnowflake = OptionalSnowflake.Missing,
val description: String,
val bannerAssetId: Snowflake
) {
) {
public companion object {

public val description: DataDescription<StickerPackData, Snowflake> = description(StickerPackData::id)
public val description: DataDescription<StickerPackData, Snowflake> = description(StickerPackData::id) {
link(StickerPackData::id to StickerData::nullablePackId)
}

public fun from(entity: DiscordStickerPack): StickerPackData = with(entity) {
StickerPackData(id, stickers.map { StickerData.from(it) }, name, skuId, coverStickerId, description, bannerAssetId)
}
}
}
}
4 changes: 2 additions & 2 deletions core/src/commonMain/kotlin/cache/data/UserData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import kotlinx.serialization.Serializable

private val WebhookData.nullableUserId get() = userId.value
private val ThreadMemberData.nullableUserId get() = userId.value

@Serializable
public data class UserData(
Expand All @@ -29,7 +29,7 @@ public data class UserData(

public val description: DataDescription<UserData, Snowflake> = description(UserData::id) {
link(UserData::id to MemberData::userId)
link(UserData::id to WebhookData::nullableUserId)
link(UserData::id to ThreadMemberData::nullableUserId)
link(UserData::id to VoiceStateData::userId)
link(UserData::id to PresenceData::userId)
}
Expand Down

0 comments on commit 2a0d3f8

Please sign in to comment.