mirror of
https://github.com/revanced/revanced-api.git
synced 2025-04-30 06:34:36 +02:00
feat: Make some announcements schema fields nullable
This commit is contained in:
parent
5d5533a920
commit
db22874f06
@ -63,9 +63,9 @@ class ApiAnnouncement(
|
|||||||
val title: String,
|
val title: String,
|
||||||
val content: String? = null,
|
val content: String? = null,
|
||||||
// Using a list instead of a set because set semantics are unnecessary here.
|
// 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.
|
// 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 createdAt: LocalDateTime = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()),
|
||||||
val archivedAt: LocalDateTime? = null,
|
val archivedAt: LocalDateTime? = null,
|
||||||
val level: Int = 0,
|
val level: Int = 0,
|
||||||
@ -78,9 +78,9 @@ class ApiResponseAnnouncement(
|
|||||||
val title: String,
|
val title: String,
|
||||||
val content: String? = null,
|
val content: String? = null,
|
||||||
// Using a list instead of a set because set semantics are unnecessary here.
|
// 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.
|
// 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 createdAt: LocalDateTime,
|
||||||
val archivedAt: LocalDateTime? = null,
|
val archivedAt: LocalDateTime? = null,
|
||||||
val level: Int = 0,
|
val level: Int = 0,
|
||||||
|
@ -69,8 +69,7 @@ internal class AnnouncementRepository(private val database: Database) {
|
|||||||
|
|
||||||
fun latestId() = latestAnnouncement?.id?.value.toApiResponseAnnouncementId()
|
fun latestId() = latestAnnouncement?.id?.value.toApiResponseAnnouncementId()
|
||||||
|
|
||||||
fun latestId(tags: Set<String>) =
|
fun latestId(tags: Set<String>) = tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
|
||||||
tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
|
|
||||||
|
|
||||||
suspend fun paged(cursor: Int, count: Int, tags: Set<String>?) = transaction {
|
suspend fun paged(cursor: Int, count: Int, tags: Set<String>?) = transaction {
|
||||||
Announcement.find {
|
Announcement.find {
|
||||||
@ -103,11 +102,13 @@ internal class AnnouncementRepository(private val database: Database) {
|
|||||||
createdAt = new.createdAt
|
createdAt = new.createdAt
|
||||||
archivedAt = new.archivedAt
|
archivedAt = new.archivedAt
|
||||||
level = new.level
|
level = new.level
|
||||||
|
if (new.tags != null) {
|
||||||
tags = SizedCollection(
|
tags = SizedCollection(
|
||||||
new.tags.map { tag -> Tag.find { Tags.name eq tag }.firstOrNull() ?: Tag.new { name = tag } },
|
new.tags.map { tag -> Tag.find { Tags.name eq tag }.firstOrNull() ?: Tag.new { name = tag } },
|
||||||
)
|
)
|
||||||
|
}
|
||||||
}.apply {
|
}.apply {
|
||||||
new.attachments.map { attachmentUrl ->
|
new.attachments?.map { attachmentUrl ->
|
||||||
Attachment.new {
|
Attachment.new {
|
||||||
url = attachmentUrl
|
url = attachmentUrl
|
||||||
announcement = this@apply
|
announcement = this@apply
|
||||||
@ -125,6 +126,7 @@ internal class AnnouncementRepository(private val database: Database) {
|
|||||||
it.archivedAt = new.archivedAt
|
it.archivedAt = new.archivedAt
|
||||||
it.level = new.level
|
it.level = new.level
|
||||||
|
|
||||||
|
if (new.tags != null) {
|
||||||
// Get the old tags, create new tags if they don't exist,
|
// 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.
|
// and delete tags that are not in the new tags, after updating the announcement.
|
||||||
val oldTags = it.tags.toList()
|
val oldTags = it.tags.toList()
|
||||||
@ -136,8 +138,10 @@ internal class AnnouncementRepository(private val database: Database) {
|
|||||||
if (tag in updatedTags || !tag.announcements.empty()) return@forEach
|
if (tag in updatedTags || !tag.announcements.empty()) return@forEach
|
||||||
tag.delete()
|
tag.delete()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Delete old attachments and create new attachments.
|
// Delete old attachments and create new attachments.
|
||||||
|
if (new.attachments != null) {
|
||||||
it.attachments.forEach { attachment -> attachment.delete() }
|
it.attachments.forEach { attachment -> attachment.delete() }
|
||||||
new.attachments.map { attachment ->
|
new.attachments.map { attachment ->
|
||||||
Attachment.new {
|
Attachment.new {
|
||||||
@ -145,6 +149,7 @@ internal class AnnouncementRepository(private val database: Database) {
|
|||||||
announcement = it
|
announcement = it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}?.let(::updateLatestAnnouncement) ?: Unit
|
}?.let(::updateLatestAnnouncement) ?: Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,8 +180,7 @@ internal class AnnouncementRepository(private val database: Database) {
|
|||||||
Tag.all().toList().toApiTag()
|
Tag.all().toList().toApiTag()
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun <T> transaction(statement: suspend Transaction.() -> T) =
|
private suspend fun <T> transaction(statement: suspend Transaction.() -> T) = newSuspendedTransaction(Dispatchers.IO, database, statement = statement)
|
||||||
newSuspendedTransaction(Dispatchers.IO, database, statement = statement)
|
|
||||||
|
|
||||||
private object Announcements : IntIdTable() {
|
private object Announcements : IntIdTable() {
|
||||||
val author = varchar("author", 32).nullable()
|
val author = varchar("author", 32).nullable()
|
||||||
|
@ -135,7 +135,7 @@ private object AnnouncementServiceTest {
|
|||||||
val latestAnnouncement = announcementService.latest()!!
|
val latestAnnouncement = announcementService.latest()!!
|
||||||
val latestId = latestAnnouncement.id
|
val latestId = latestAnnouncement.id
|
||||||
|
|
||||||
val attachments = latestAnnouncement.attachments
|
val attachments = latestAnnouncement.attachments!!
|
||||||
assertEquals(2, attachments.size)
|
assertEquals(2, attachments.size)
|
||||||
assert(attachments.any { it == "attachment1" })
|
assert(attachments.any { it == "attachment1" })
|
||||||
assert(attachments.any { it == "attachment2" })
|
assert(attachments.any { it == "attachment2" })
|
||||||
@ -144,7 +144,7 @@ private object AnnouncementServiceTest {
|
|||||||
latestId,
|
latestId,
|
||||||
ApiAnnouncement(title = "title", attachments = listOf("attachment1", "attachment3")),
|
ApiAnnouncement(title = "title", attachments = listOf("attachment1", "attachment3")),
|
||||||
)
|
)
|
||||||
assert(announcementService.get(latestId)!!.attachments.any { it == "attachment3" })
|
assert(announcementService.get(latestId)!!.attachments!!.any { it == "attachment3" })
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user