Skip to content

Commit db22874

Browse files
committedDec 20, 2024
feat: Make some announcements schema fields nullable
1 parent 5d5533a commit db22874

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed
 

Diff for: ‎src/main/kotlin/app/revanced/api/configuration/APISchema.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ class ApiAnnouncement(
6363
val title: String,
6464
val content: String? = null,
6565
// Using a list instead of a set because set semantics are unnecessary here.
66-
val attachments: List<String> = emptyList(),
66+
val attachments: List<String>? = null,
6767
// Using a list instead of a set because set semantics are unnecessary here.
68-
val tags: List<String> = emptyList(),
68+
val tags: List<String>? = null,
6969
val createdAt: LocalDateTime = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()),
7070
val archivedAt: LocalDateTime? = null,
7171
val level: Int = 0,
@@ -78,9 +78,9 @@ class ApiResponseAnnouncement(
7878
val title: String,
7979
val content: String? = null,
8080
// Using a list instead of a set because set semantics are unnecessary here.
81-
val attachments: List<String> = emptyList(),
81+
val attachments: List<String>? = null,
8282
// Using a list instead of a set because set semantics are unnecessary here.
83-
val tags: List<String> = emptyList(),
83+
val tags: List<String>? = null,
8484
val createdAt: LocalDateTime,
8585
val archivedAt: LocalDateTime? = null,
8686
val level: Int = 0,

Diff for: ‎src/main/kotlin/app/revanced/api/configuration/repository/AnnouncementRepository.kt

+27-23
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ internal class AnnouncementRepository(private val database: Database) {
6969

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

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

7574
suspend fun paged(cursor: Int, count: Int, tags: Set<String>?) = transaction {
7675
Announcement.find {
@@ -103,11 +102,13 @@ internal class AnnouncementRepository(private val database: Database) {
103102
createdAt = new.createdAt
104103
archivedAt = new.archivedAt
105104
level = new.level
106-
tags = SizedCollection(
107-
new.tags.map { tag -> Tag.find { Tags.name eq tag }.firstOrNull() ?: Tag.new { name = tag } },
108-
)
105+
if (new.tags != null) {
106+
tags = SizedCollection(
107+
new.tags.map { tag -> Tag.find { Tags.name eq tag }.firstOrNull() ?: Tag.new { name = tag } },
108+
)
109+
}
109110
}.apply {
110-
new.attachments.map { attachmentUrl ->
111+
new.attachments?.map { attachmentUrl ->
111112
Attachment.new {
112113
url = attachmentUrl
113114
announcement = this@apply
@@ -125,24 +126,28 @@ internal class AnnouncementRepository(private val database: Database) {
125126
it.archivedAt = new.archivedAt
126127
it.level = new.level
127128

128-
// Get the old tags, create new tags if they don't exist,
129-
// and delete tags that are not in the new tags, after updating the announcement.
130-
val oldTags = it.tags.toList()
131-
val updatedTags = new.tags.map { name ->
132-
Tag.find { Tags.name eq name }.firstOrNull() ?: Tag.new { this.name = name }
133-
}
134-
it.tags = SizedCollection(updatedTags)
135-
oldTags.forEach { tag ->
136-
if (tag in updatedTags || !tag.announcements.empty()) return@forEach
137-
tag.delete()
129+
if (new.tags != null) {
130+
// Get the old tags, create new tags if they don't exist,
131+
// and delete tags that are not in the new tags, after updating the announcement.
132+
val oldTags = it.tags.toList()
133+
val updatedTags = new.tags.map { name ->
134+
Tag.find { Tags.name eq name }.firstOrNull() ?: Tag.new { this.name = name }
135+
}
136+
it.tags = SizedCollection(updatedTags)
137+
oldTags.forEach { tag ->
138+
if (tag in updatedTags || !tag.announcements.empty()) return@forEach
139+
tag.delete()
140+
}
138141
}
139142

140143
// Delete old attachments and create new attachments.
141-
it.attachments.forEach { attachment -> attachment.delete() }
142-
new.attachments.map { attachment ->
143-
Attachment.new {
144-
url = attachment
145-
announcement = it
144+
if (new.attachments != null) {
145+
it.attachments.forEach { attachment -> attachment.delete() }
146+
new.attachments.map { attachment ->
147+
Attachment.new {
148+
url = attachment
149+
announcement = it
150+
}
146151
}
147152
}
148153
}?.let(::updateLatestAnnouncement) ?: Unit
@@ -175,8 +180,7 @@ internal class AnnouncementRepository(private val database: Database) {
175180
Tag.all().toList().toApiTag()
176181
}
177182

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

181185
private object Announcements : IntIdTable() {
182186
val author = varchar("author", 32).nullable()

Diff for: ‎src/test/kotlin/app/revanced/api/configuration/services/AnnouncementServiceTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private object AnnouncementServiceTest {
135135
val latestAnnouncement = announcementService.latest()!!
136136
val latestId = latestAnnouncement.id
137137

138-
val attachments = latestAnnouncement.attachments
138+
val attachments = latestAnnouncement.attachments!!
139139
assertEquals(2, attachments.size)
140140
assert(attachments.any { it == "attachment1" })
141141
assert(attachments.any { it == "attachment2" })
@@ -144,7 +144,7 @@ private object AnnouncementServiceTest {
144144
latestId,
145145
ApiAnnouncement(title = "title", attachments = listOf("attachment1", "attachment3")),
146146
)
147-
assert(announcementService.get(latestId)!!.attachments.any { it == "attachment3" })
147+
assert(announcementService.get(latestId)!!.attachments!!.any { it == "attachment3" })
148148
}
149149

150150
@Test

0 commit comments

Comments
 (0)