Skip to content

Commit

Permalink
feat: Remove "archived" query parameter
Browse files Browse the repository at this point in the history
It doesn't seem to be necessary for the purpose of viewing announcements.
  • Loading branch information
oSumAtrIX committed Nov 1, 2024
1 parent fc40427 commit 8ad614e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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())
}
idLessEq()
} 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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
}
}

0 comments on commit 8ad614e

Please sign in to comment.