Skip to content

Commit

Permalink
feat: Make some announcements schema fields nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Dec 20, 2024
1 parent 5d5533a commit db22874
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/app/revanced/api/configuration/APISchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class ApiAnnouncement(
val title: String,
val content: String? = null,
// Using a list instead of a set because set semantics are unnecessary here.
val attachments: List<String> = emptyList(),
val attachments: List<String>? = null,
// Using a list instead of a set because set semantics are unnecessary here.
val tags: List<String> = emptyList(),
val tags: List<String>? = null,
val createdAt: LocalDateTime = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()),
val archivedAt: LocalDateTime? = null,
val level: Int = 0,
Expand All @@ -78,9 +78,9 @@ class ApiResponseAnnouncement(
val title: String,
val content: String? = null,
// Using a list instead of a set because set semantics are unnecessary here.
val attachments: List<String> = emptyList(),
val attachments: List<String>? = null,
// Using a list instead of a set because set semantics are unnecessary here.
val tags: List<String> = emptyList(),
val tags: List<String>? = null,
val createdAt: LocalDateTime,
val archivedAt: LocalDateTime? = null,
val level: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ internal class AnnouncementRepository(private val database: Database) {

fun latestId() = latestAnnouncement?.id?.value.toApiResponseAnnouncementId()

fun latestId(tags: Set<String>) =
tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
fun latestId(tags: Set<String>) = tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()

suspend fun paged(cursor: Int, count: Int, tags: Set<String>?) = transaction {
Announcement.find {
Expand Down Expand Up @@ -103,11 +102,13 @@ internal class AnnouncementRepository(private val database: Database) {
createdAt = new.createdAt
archivedAt = new.archivedAt
level = new.level
tags = SizedCollection(
new.tags.map { tag -> Tag.find { Tags.name eq tag }.firstOrNull() ?: Tag.new { name = tag } },
)
if (new.tags != null) {
tags = SizedCollection(
new.tags.map { tag -> Tag.find { Tags.name eq tag }.firstOrNull() ?: Tag.new { name = tag } },
)
}
}.apply {
new.attachments.map { attachmentUrl ->
new.attachments?.map { attachmentUrl ->
Attachment.new {
url = attachmentUrl
announcement = this@apply
Expand All @@ -125,24 +126,28 @@ internal class AnnouncementRepository(private val database: Database) {
it.archivedAt = new.archivedAt
it.level = new.level

// Get the old tags, create new tags if they don't exist,
// and delete tags that are not in the new tags, after updating the announcement.
val oldTags = it.tags.toList()
val updatedTags = new.tags.map { name ->
Tag.find { Tags.name eq name }.firstOrNull() ?: Tag.new { this.name = name }
}
it.tags = SizedCollection(updatedTags)
oldTags.forEach { tag ->
if (tag in updatedTags || !tag.announcements.empty()) return@forEach
tag.delete()
if (new.tags != null) {
// Get the old tags, create new tags if they don't exist,
// and delete tags that are not in the new tags, after updating the announcement.
val oldTags = it.tags.toList()
val updatedTags = new.tags.map { name ->
Tag.find { Tags.name eq name }.firstOrNull() ?: Tag.new { this.name = name }
}
it.tags = SizedCollection(updatedTags)
oldTags.forEach { tag ->
if (tag in updatedTags || !tag.announcements.empty()) return@forEach
tag.delete()
}
}

// Delete old attachments and create new attachments.
it.attachments.forEach { attachment -> attachment.delete() }
new.attachments.map { attachment ->
Attachment.new {
url = attachment
announcement = it
if (new.attachments != null) {
it.attachments.forEach { attachment -> attachment.delete() }
new.attachments.map { attachment ->
Attachment.new {
url = attachment
announcement = it
}
}
}
}?.let(::updateLatestAnnouncement) ?: Unit
Expand Down Expand Up @@ -175,8 +180,7 @@ internal class AnnouncementRepository(private val database: Database) {
Tag.all().toList().toApiTag()
}

private suspend fun <T> transaction(statement: suspend Transaction.() -> T) =
newSuspendedTransaction(Dispatchers.IO, database, statement = statement)
private suspend fun <T> transaction(statement: suspend Transaction.() -> T) = newSuspendedTransaction(Dispatchers.IO, database, statement = statement)

private object Announcements : IntIdTable() {
val author = varchar("author", 32).nullable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private object AnnouncementServiceTest {
val latestAnnouncement = announcementService.latest()!!
val latestId = latestAnnouncement.id

val attachments = latestAnnouncement.attachments
val attachments = latestAnnouncement.attachments!!
assertEquals(2, attachments.size)
assert(attachments.any { it == "attachment1" })
assert(attachments.any { it == "attachment2" })
Expand All @@ -144,7 +144,7 @@ private object AnnouncementServiceTest {
latestId,
ApiAnnouncement(title = "title", attachments = listOf("attachment1", "attachment3")),
)
assert(announcementService.get(latestId)!!.attachments.any { it == "attachment3" })
assert(announcementService.get(latestId)!!.attachments!!.any { it == "attachment3" })
}

@Test
Expand Down

0 comments on commit db22874

Please sign in to comment.