Skip to content

Commit

Permalink
feat: Use tag name directly instead of ID
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Nov 1, 2024
1 parent e8dfefe commit fc40427
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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.*
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 @@ -20,7 +20,7 @@ import java.time.LocalDateTime
internal class AnnouncementRepository(private val database: Database) {
// This is better than doing a maxByOrNull { it.id } on every request.
private var latestAnnouncement: Announcement? = null
private val latestAnnouncementByTag = mutableMapOf<Int, Announcement>()
private val latestAnnouncementByTag = mutableMapOf<String, Announcement>()

init {
runBlocking {
Expand All @@ -40,22 +40,23 @@ internal class AnnouncementRepository(private val database: Database) {
private fun initializeLatestAnnouncements() {
latestAnnouncement = Announcement.all().orderBy(Announcements.id to SortOrder.DESC).firstOrNull()

Tag.all().map { it.id.value }.forEach(::updateLatestAnnouncementForTag)
Tag.all().map { it.name }.forEach(::updateLatestAnnouncementForTag)
}

private fun updateLatestAnnouncement(new: Announcement) {
if (latestAnnouncement == null || latestAnnouncement!!.id.value <= new.id.value) {
latestAnnouncement = new
new.tags.forEach { tag -> latestAnnouncementByTag[tag.id.value] = new }
new.tags.forEach { tag -> latestAnnouncementByTag[tag.name] = new }
}
}

private fun updateLatestAnnouncementForTag(tag: Int) {
val latestAnnouncementForTag = AnnouncementTags.select(AnnouncementTags.announcement)
.where { AnnouncementTags.tag eq tag }
.map { it[AnnouncementTags.announcement] }
.mapNotNull { Announcement.findById(it) }
.maxByOrNull { it.id }
private fun updateLatestAnnouncementForTag(tag: String) {
val latestAnnouncementForTag = Tags.innerJoin(AnnouncementTags)
.select(AnnouncementTags.announcement)
.where { Tags.name eq tag }
.orderBy(AnnouncementTags.announcement to SortOrder.DESC)
.limit(1)
.firstNotNullOfOrNull { Announcement.findById(it[AnnouncementTags.announcement]) }

latestAnnouncementForTag?.let { latestAnnouncementByTag[tag] = it }
}
Expand All @@ -64,16 +65,16 @@ internal class AnnouncementRepository(private val database: Database) {
latestAnnouncement.toApiResponseAnnouncement()
}

suspend fun latest(tags: Set<Int>) = transaction {
suspend fun latest(tags: Set<String>) = transaction {
tags.mapNotNull { tag -> latestAnnouncementByTag[tag] }.toApiAnnouncement()
}

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

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

suspend fun paged(cursor: Int, count: Int, tags: Set<Int>?, archived: Boolean) = transaction {
suspend fun paged(cursor: Int, count: Int, tags: Set<String>?, archived: Boolean) = transaction {
Announcement.find {
fun idLessEq() = Announcements.id lessEq cursor
fun archivedAtIsNull() = Announcements.archivedAt.isNull()
Expand All @@ -92,12 +93,12 @@ internal class AnnouncementRepository(private val database: Database) {
archivedAtIsNull() or archivedAtGreaterNow()
}

fun hasTags() = tags.mapNotNull { Tag.findById(it)?.id }.let { tags ->
Announcements.id inSubQuery Announcements.leftJoin(AnnouncementTags)
fun hasTags() = Announcements.id inSubQuery (
Tags.innerJoin(AnnouncementTags)
.select(AnnouncementTags.announcement)
.where { AnnouncementTags.tag inList tags }
.where { Tags.name inList tags }
.withDistinct()
}
)

idLessEq() and archivedAtGreaterOrNullOrTrue() and hasTags()
}
Expand Down Expand Up @@ -165,7 +166,7 @@ internal class AnnouncementRepository(private val database: Database) {
// Delete the tag if no other announcements are referencing it.
// One count means that the announcement is the only one referencing the tag.
announcement.tags.filter { tag -> tag.announcements.count() == 1L }.forEach { tag ->
latestAnnouncementByTag -= tag.id.value
latestAnnouncementByTag -= tag.name
tag.delete()
}

Expand Down Expand Up @@ -250,7 +251,7 @@ internal class AnnouncementRepository(private val database: Database) {
title,
content,
attachments.map { it.url },
tags.map { it.id.value },
tags.map { it.name },
createdAt,
archivedAt,
level,
Expand All @@ -259,7 +260,7 @@ internal class AnnouncementRepository(private val database: Database) {

private fun Iterable<Announcement>.toApiAnnouncement() = map { it.toApiResponseAnnouncement()!! }

private fun Iterable<Tag>.toApiTag() = map { ApiAnnouncementTag(it.id.value, it.name) }
private fun Iterable<Tag>.toApiTag() = map { ApiAnnouncementTag(it.name) }

private fun Int?.toApiResponseAnnouncementId() = this?.let { ApiResponseAnnouncementId(this) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import app.revanced.api.configuration.schema.ApiAnnouncement
import app.revanced.api.configuration.schema.ApiResponseAnnouncement
import app.revanced.api.configuration.schema.ApiResponseAnnouncementId
import app.revanced.api.configuration.services.AnnouncementService
import io.bkbn.kompendium.core.metadata.*
import io.bkbn.kompendium.core.metadata.DeleteInfo
import io.bkbn.kompendium.core.metadata.GetInfo
import io.bkbn.kompendium.core.metadata.PatchInfo
import io.bkbn.kompendium.core.metadata.PostInfo
import io.bkbn.kompendium.json.schema.definition.TypeDefinition
import io.bkbn.kompendium.oas.payload.Parameter
import io.ktor.http.*
Expand All @@ -35,7 +38,7 @@ internal fun Route.announcementsRoute() = route("announcements") {
val tags = call.parameters.getAll("tag")
val archived = call.parameters["archived"]?.toBoolean() ?: true

call.respond(announcementService.paged(cursor, count, tags?.map { it.toInt() }?.toSet(), archived))
call.respond(announcementService.paged(cursor, count, tags?.toSet(), archived))
}
}

Expand All @@ -55,7 +58,7 @@ internal fun Route.announcementsRoute() = route("announcements") {
val tags = call.parameters.getAll("tag")

if (tags?.isNotEmpty() == true) {
call.respond(announcementService.latest(tags.map { it.toInt() }.toSet()))
call.respond(announcementService.latest(tags.toSet()))
} else {
call.respondOrNotFound(announcementService.latest())
}
Expand All @@ -68,7 +71,7 @@ internal fun Route.announcementsRoute() = route("announcements") {
val tags = call.parameters.getAll("tag")

if (tags?.isNotEmpty() == true) {
call.respond(announcementService.latestId(tags.map { it.toInt() }.toSet()))
call.respond(announcementService.latestId(tags.toSet()))
} else {
call.respondOrNotFound(announcementService.latestId())
}
Expand Down Expand Up @@ -146,8 +149,8 @@ private fun Route.installAnnouncementsRouteDocumentation() = installNotarizedRou
Parameter(
name = "tag",
`in` = Parameter.Location.query,
schema = TypeDefinition.INT,
description = "The tag IDs to filter the announcements by. Default is all tags",
schema = TypeDefinition.STRING,
description = "The tags to filter the announcements by. Default is all tags",
required = false,
),
Parameter(
Expand Down Expand Up @@ -193,8 +196,8 @@ private fun Route.installAnnouncementsLatestRouteDocumentation() = installNotari
Parameter(
name = "tag",
`in` = Parameter.Location.query,
schema = TypeDefinition.INT,
description = "The tag IDs to filter the latest announcements by",
schema = TypeDefinition.STRING,
description = "The tags to filter the latest announcements by",
required = false,
),
)
Expand Down Expand Up @@ -228,8 +231,8 @@ private fun Route.installAnnouncementsLatestIdRouteDocumentation() = installNota
Parameter(
name = "tag",
`in` = Parameter.Location.query,
schema = TypeDefinition.INT,
description = "The tag IDs to filter the latest announcements by",
schema = TypeDefinition.STRING,
description = "The tags to filter the latest announcements by",
required = false,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ApiResponseAnnouncement(
// Using a list instead of a set because set semantics are unnecessary here.
val attachments: List<String> = emptyList(),
// Using a list instead of a set because set semantics are unnecessary here.
val tags: List<Int> = emptyList(),
val tags: List<String> = emptyList(),
val createdAt: LocalDateTime,
val archivedAt: LocalDateTime? = null,
val level: Int = 0,
Expand All @@ -94,7 +94,6 @@ class ApiAnnouncementArchivedAt(

@Serializable
class ApiAnnouncementTag(
val id: Int,
val name: String,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import app.revanced.api.configuration.schema.ApiAnnouncement
internal class AnnouncementService(
private val announcementRepository: AnnouncementRepository,
) {
suspend fun latest(tags: Set<Int>) = announcementRepository.latest(tags)
suspend fun latest(tags: Set<String>) = announcementRepository.latest(tags)

suspend fun latest() = announcementRepository.latest()

fun latestId(tags: Set<Int>) = announcementRepository.latestId(tags)
fun latestId(tags: Set<String>) = announcementRepository.latestId(tags)

fun latestId() = announcementRepository.latestId()

suspend fun paged(cursor: Int, limit: Int, tags: Set<Int>?, archived: Boolean) =
suspend fun paged(cursor: Int, limit: Int, tags: Set<String>?, archived: Boolean) =
announcementRepository.paged(cursor, limit, tags, archived)

suspend fun get(id: Int) = announcementRepository.get(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import app.revanced.api.configuration.schema.ApiAnnouncement
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.toKotlinLocalDateTime
import org.jetbrains.exposed.sql.Database
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.time.LocalDateTime
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
Expand Down Expand Up @@ -84,27 +86,22 @@ private object AnnouncementServiceTest {
announcementService.new(ApiAnnouncement(title = "2", tags = listOf("tag1", "tag3")))
announcementService.new(ApiAnnouncement(title = "3", tags = listOf("tag1", "tag4")))

val tag2 = announcementService.tags().find { it.name == "tag2" }!!.id
assert(announcementService.latest(setOf(tag2)).first().title == "1")
assert(announcementService.latest(setOf("tag2")).first().title == "1")
assert(announcementService.latest(setOf("tag3")).last().title == "2")

val tag3 = announcementService.tags().find { it.name == "tag3" }!!.id
assert(announcementService.latest(setOf(tag3)).last().title == "2")

val tag1and3 =
announcementService.tags().filter { it.name == "tag1" || it.name == "tag3" }.map { it.id }.toSet()
val announcement2and3 = announcementService.latest(tag1and3)
val announcement2and3 = announcementService.latest(setOf("tag1", "tag3"))
assert(announcement2and3.size == 2)
assert(announcement2and3.any { it.title == "2" })
assert(announcement2and3.any { it.title == "3" })

announcementService.delete(announcementService.latestId()!!.id)
assert(announcementService.latest(tag1and3).first().title == "2")
assert(announcementService.latest(setOf("tag1", "tag3")).first().title == "2")

announcementService.delete(announcementService.latestId()!!.id)
assert(announcementService.latest(tag1and3).first().title == "1")
assert(announcementService.latest(setOf("tag1", "tag3")).first().title == "1")

announcementService.delete(announcementService.latestId()!!.id)
assert(announcementService.latest(tag1and3).isEmpty())
assert(announcementService.latest(setOf("tag1", "tag3")).isEmpty())
assert(announcementService.tags().isEmpty())
}

Expand Down Expand Up @@ -183,7 +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].id), true)
val announcements3 = announcementService.paged(5, 5, setOf(tags[1].name), true)
assertEquals(4, announcements3.size, "Filters announcements by tag")

val announcements4 = announcementService.paged(Int.MAX_VALUE, 10, null, false)
Expand Down

0 comments on commit fc40427

Please sign in to comment.