@@ -6,7 +6,7 @@ import app.revanced.api.configuration.schema.ApiResponseAnnouncement
66import app.revanced.api.configuration.schema.ApiResponseAnnouncementId
77import kotlinx.coroutines.Dispatchers
88import kotlinx.coroutines.runBlocking
9- import kotlinx.datetime.*
9+ import kotlinx.datetime.toKotlinLocalDateTime
1010import org.jetbrains.exposed.dao.IntEntity
1111import org.jetbrains.exposed.dao.IntEntityClass
1212import org.jetbrains.exposed.dao.id.EntityID
@@ -20,7 +20,7 @@ import java.time.LocalDateTime
2020internal class AnnouncementRepository (private val database : Database ) {
2121 // This is better than doing a maxByOrNull { it.id } on every request.
2222 private var latestAnnouncement: Announcement ? = null
23- private val latestAnnouncementByTag = mutableMapOf<Int , Announcement >()
23+ private val latestAnnouncementByTag = mutableMapOf<String , Announcement >()
2424
2525 init {
2626 runBlocking {
@@ -40,22 +40,23 @@ internal class AnnouncementRepository(private val database: Database) {
4040 private fun initializeLatestAnnouncements () {
4141 latestAnnouncement = Announcement .all().orderBy(Announcements .id to SortOrder .DESC ).firstOrNull()
4242
43- Tag .all().map { it.id.value }.forEach(::updateLatestAnnouncementForTag)
43+ Tag .all().map { it.name }.forEach(::updateLatestAnnouncementForTag)
4444 }
4545
4646 private fun updateLatestAnnouncement (new : Announcement ) {
4747 if (latestAnnouncement == null || latestAnnouncement!! .id.value <= new.id.value) {
4848 latestAnnouncement = new
49- new.tags.forEach { tag -> latestAnnouncementByTag[tag.id.value ] = new }
49+ new.tags.forEach { tag -> latestAnnouncementByTag[tag.name ] = new }
5050 }
5151 }
5252
53- private fun updateLatestAnnouncementForTag (tag : Int ) {
54- val latestAnnouncementForTag = AnnouncementTags .select(AnnouncementTags .announcement)
55- .where { AnnouncementTags .tag eq tag }
56- .map { it[AnnouncementTags .announcement] }
57- .mapNotNull { Announcement .findById(it) }
58- .maxByOrNull { it.id }
53+ private fun updateLatestAnnouncementForTag (tag : String ) {
54+ val latestAnnouncementForTag = Tags .innerJoin(AnnouncementTags )
55+ .select(AnnouncementTags .announcement)
56+ .where { Tags .name eq tag }
57+ .orderBy(AnnouncementTags .announcement to SortOrder .DESC )
58+ .limit(1 )
59+ .firstNotNullOfOrNull { Announcement .findById(it[AnnouncementTags .announcement]) }
5960
6061 latestAnnouncementForTag?.let { latestAnnouncementByTag[tag] = it }
6162 }
@@ -64,16 +65,16 @@ internal class AnnouncementRepository(private val database: Database) {
6465 latestAnnouncement.toApiResponseAnnouncement()
6566 }
6667
67- suspend fun latest (tags : Set <Int >) = transaction {
68+ suspend fun latest (tags : Set <String >) = transaction {
6869 tags.mapNotNull { tag -> latestAnnouncementByTag[tag] }.toApiAnnouncement()
6970 }
7071
7172 fun latestId () = latestAnnouncement?.id?.value.toApiResponseAnnouncementId()
7273
73- fun latestId (tags : Set <Int >) =
74+ fun latestId (tags : Set <String >) =
7475 tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
7576
76- suspend fun paged (cursor : Int , count : Int , tags : Set <Int >? , archived : Boolean ) = transaction {
77+ suspend fun paged (cursor : Int , count : Int , tags : Set <String >? , archived : Boolean ) = transaction {
7778 Announcement .find {
7879 fun idLessEq () = Announcements .id lessEq cursor
7980 fun archivedAtIsNull () = Announcements .archivedAt.isNull()
@@ -92,12 +93,12 @@ internal class AnnouncementRepository(private val database: Database) {
9293 archivedAtIsNull() or archivedAtGreaterNow()
9394 }
9495
95- fun hasTags () = tags.mapNotNull { Tag .findById(it)?. id }. let { tags ->
96- Announcements .id inSubQuery Announcements .leftJoin (AnnouncementTags )
96+ fun hasTags () = Announcements . id inSubQuery (
97+ Tags .innerJoin (AnnouncementTags )
9798 .select(AnnouncementTags .announcement)
98- .where { AnnouncementTags .tag inList tags }
99+ .where { Tags .name inList tags }
99100 .withDistinct()
100- }
101+ )
101102
102103 idLessEq() and archivedAtGreaterOrNullOrTrue() and hasTags()
103104 }
@@ -165,7 +166,7 @@ internal class AnnouncementRepository(private val database: Database) {
165166 // Delete the tag if no other announcements are referencing it.
166167 // One count means that the announcement is the only one referencing the tag.
167168 announcement.tags.filter { tag -> tag.announcements.count() == 1L }.forEach { tag ->
168- latestAnnouncementByTag - = tag.id.value
169+ latestAnnouncementByTag - = tag.name
169170 tag.delete()
170171 }
171172
@@ -250,7 +251,7 @@ internal class AnnouncementRepository(private val database: Database) {
250251 title,
251252 content,
252253 attachments.map { it.url },
253- tags.map { it.id.value },
254+ tags.map { it.name },
254255 createdAt,
255256 archivedAt,
256257 level,
@@ -259,7 +260,7 @@ internal class AnnouncementRepository(private val database: Database) {
259260
260261 private fun Iterable<Announcement>.toApiAnnouncement () = map { it.toApiResponseAnnouncement()!! }
261262
262- private fun Iterable<Tag>.toApiTag () = map { ApiAnnouncementTag (it.id.value, it. name) }
263+ private fun Iterable<Tag>.toApiTag () = map { ApiAnnouncementTag (it.name) }
263264
264265 private fun Int?.toApiResponseAnnouncementId () = this ?.let { ApiResponseAnnouncementId (this ) }
265266
0 commit comments