Skip to content

Commit

Permalink
Forgot to fix Calendar object as well
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFox161 committed Oct 7, 2023
1 parent aa48f65 commit fa9f3ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.dreamexposure.discal.core.business
import discord4j.common.util.Snowflake
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import kotlinx.coroutines.reactor.mono
import org.dreamexposure.discal.CalendarCache
import org.dreamexposure.discal.core.crypto.AESEncryption
import org.dreamexposure.discal.core.database.CalendarRepository
Expand All @@ -19,7 +20,7 @@ class DefaultCalendarService(
if (calendars != null) return calendars

calendars = calendarRepository.findAllByGuildId(guildId.asLong())
.map(::Calendar)
.flatMap { mono { Calendar(it) } }
.collectList()
.awaitSingle()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.dreamexposure.discal.core.extensions.asInstantMilli
import org.dreamexposure.discal.core.extensions.asSnowflake
import java.time.Instant

data class Calendar(
data class Calendar private constructor(
val guildId: Snowflake,
val number: Int,
val host: CalendarHost,
Expand All @@ -18,34 +18,33 @@ data class Calendar(
val external: Boolean,
val secrets: Secrets,
) {
constructor(data: CalendarData) : this(
guildId = data.guildId.asSnowflake(),
number = data.calendarNumber,
host = CalendarHost.valueOf(data.host),
id = data.calendarId,
address = data.calendarAddress,
external = data.external,
secrets = Secrets(data)
)
companion object {
suspend operator fun invoke(data: CalendarData): Calendar {
val aes = AESEncryption(data.privateKey)

return Calendar(
guildId = data.guildId.asSnowflake(),
number = data.calendarNumber,
host = CalendarHost.valueOf(data.host),
id = data.calendarId,
address = data.calendarAddress,
external = data.external,
secrets = Secrets(
credentialId = data.credentialId,
privateKey = data.privateKey,
expiresAt = data.expiresAt.asInstantMilli(),
refreshToken = aes.decrypt(data.refreshToken).awaitSingle(),
accessToken = aes.decrypt(data.accessToken).awaitSingle(),
)
)
}
}

data class Secrets(
val credentialId: Int,
val privateKey: String,
var expiresAt: Instant,
) {
lateinit var refreshToken: String
lateinit var accessToken: String

constructor(data: CalendarData) : this(
credentialId = data.credentialId,
privateKey = data.privateKey,
expiresAt = data.expiresAt.asInstantMilli()
) {
suspend {
val aes = AESEncryption(privateKey)
refreshToken = aes.decrypt(data.refreshToken).awaitSingle()
accessToken = aes.decrypt(data.accessToken).awaitSingle()
}
}
}
var refreshToken: String,
var accessToken: String,
)
}

0 comments on commit fa9f3ad

Please sign in to comment.