feat: Remove "archived" query parameter

It doesn't seem to be necessary for the purpose of viewing announcements.
This commit is contained in:
oSumAtrIX 2024-11-02 00:57:31 +01:00
parent fc40427fba
commit 8ad614ef4f
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
4 changed files with 11 additions and 36 deletions

View File

@ -6,7 +6,6 @@ import app.revanced.api.configuration.schema.ApiResponseAnnouncement
import app.revanced.api.configuration.schema.ApiResponseAnnouncementId
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.toKotlinLocalDateTime
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
@ -15,7 +14,6 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.kotlin.datetime.CurrentDateTime
import org.jetbrains.exposed.sql.kotlin.datetime.datetime
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import java.time.LocalDateTime
internal class AnnouncementRepository(private val database: Database) {
// This is better than doing a maxByOrNull { it.id } on every request.
@ -74,33 +72,21 @@ internal class AnnouncementRepository(private val database: Database) {
fun latestId(tags: Set<String>) =
tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
suspend fun paged(cursor: Int, count: Int, tags: Set<String>?, archived: Boolean) = transaction {
suspend fun paged(cursor: Int, count: Int, tags: Set<String>?) = transaction {
Announcement.find {
fun idLessEq() = Announcements.id lessEq cursor
fun archivedAtIsNull() = Announcements.archivedAt.isNull()
fun archivedAtGreaterNow() = Announcements.archivedAt greater LocalDateTime.now().toKotlinLocalDateTime()
if (tags == null) {
if (archived) {
idLessEq()
} else {
idLessEq() and (archivedAtIsNull() or archivedAtGreaterNow())
}
} else {
fun archivedAtGreaterOrNullOrTrue() = if (archived) {
Op.TRUE
} else {
archivedAtIsNull() or archivedAtGreaterNow()
}
fun hasTags() = Announcements.id inSubQuery (
Tags.innerJoin(AnnouncementTags)
AnnouncementTags.innerJoin(Tags)
.select(AnnouncementTags.announcement)
.where { Tags.name inList tags }
.withDistinct()
.where { Tags.name inList tags }
)
idLessEq() and archivedAtGreaterOrNullOrTrue() and hasTags()
idLessEq() and hasTags()
}
}.orderBy(Announcements.id to SortOrder.DESC).limit(count).toApiAnnouncement()
}

View File

@ -36,9 +36,8 @@ internal fun Route.announcementsRoute() = route("announcements") {
val cursor = call.parameters["cursor"]?.toInt() ?: Int.MAX_VALUE
val count = call.parameters["count"]?.toInt() ?: 16
val tags = call.parameters.getAll("tag")
val archived = call.parameters["archived"]?.toBoolean() ?: true
call.respond(announcementService.paged(cursor, count, tags?.toSet(), archived))
call.respond(announcementService.paged(cursor, count, tags?.toSet()))
}
}
@ -153,13 +152,6 @@ private fun Route.installAnnouncementsRouteDocumentation() = installNotarizedRou
description = "The tags to filter the announcements by. Default is all tags",
required = false,
),
Parameter(
name = "archived",
`in` = Parameter.Location.query,
schema = TypeDefinition.BOOLEAN,
description = "Whether to include archived announcements. Default is true",
required = false,
),
)
response {
responseCode(HttpStatusCode.OK)

View File

@ -14,8 +14,8 @@ internal class AnnouncementService(
fun latestId() = announcementRepository.latestId()
suspend fun paged(cursor: Int, limit: Int, tags: Set<String>?, archived: Boolean) =
announcementRepository.paged(cursor, limit, tags, archived)
suspend fun paged(cursor: Int, limit: Int, tags: Set<String>?) =
announcementRepository.paged(cursor, limit, tags)
suspend fun get(id: Int) = announcementRepository.get(id)

View File

@ -153,11 +153,11 @@ private object AnnouncementServiceTest {
announcementService.new(ApiAnnouncement(title = "title$it"))
}
val announcements = announcementService.paged(Int.MAX_VALUE, 5, null, true)
val announcements = announcementService.paged(Int.MAX_VALUE, 5, null)
assertEquals(5, announcements.size, "Returns correct number of announcements")
assertEquals("title9", announcements.first().title, "Starts from the latest announcement")
val announcements2 = announcementService.paged(5, 5, null, true)
val announcements2 = announcementService.paged(5, 5, null)
assertEquals(5, announcements2.size, "Returns correct number of announcements when starting from the cursor")
assertEquals("title4", announcements2.first().title, "Starts from the cursor")
@ -180,10 +180,7 @@ private object AnnouncementServiceTest {
val tags = announcementService.tags()
assertEquals(5, tags.size, "Returns correct number of newly created tags")
val announcements3 = announcementService.paged(5, 5, setOf(tags[1].name), true)
val announcements3 = announcementService.paged(5, 5, setOf(tags[1].name))
assertEquals(4, announcements3.size, "Filters announcements by tag")
val announcements4 = announcementService.paged(Int.MAX_VALUE, 10, null, false)
assertEquals(8, announcements4.size, "Filters out archived announcements")
}
}