@@ -6,7 +6,7 @@ import app.revanced.api.configuration.schema.ApiResponseAnnouncement
6
6
import app.revanced.api.configuration.schema.ApiResponseAnnouncementId
7
7
import kotlinx.coroutines.Dispatchers
8
8
import kotlinx.coroutines.runBlocking
9
- import kotlinx.datetime.*
9
+ import kotlinx.datetime.toKotlinLocalDateTime
10
10
import org.jetbrains.exposed.dao.IntEntity
11
11
import org.jetbrains.exposed.dao.IntEntityClass
12
12
import org.jetbrains.exposed.dao.id.EntityID
@@ -20,7 +20,7 @@ import java.time.LocalDateTime
20
20
internal class AnnouncementRepository (private val database : Database ) {
21
21
// This is better than doing a maxByOrNull { it.id } on every request.
22
22
private var latestAnnouncement: Announcement ? = null
23
- private val latestAnnouncementByTag = mutableMapOf<Int , Announcement >()
23
+ private val latestAnnouncementByTag = mutableMapOf<String , Announcement >()
24
24
25
25
init {
26
26
runBlocking {
@@ -40,22 +40,23 @@ internal class AnnouncementRepository(private val database: Database) {
40
40
private fun initializeLatestAnnouncements () {
41
41
latestAnnouncement = Announcement .all().orderBy(Announcements .id to SortOrder .DESC ).firstOrNull()
42
42
43
- Tag .all().map { it.id.value }.forEach(::updateLatestAnnouncementForTag)
43
+ Tag .all().map { it.name }.forEach(::updateLatestAnnouncementForTag)
44
44
}
45
45
46
46
private fun updateLatestAnnouncement (new : Announcement ) {
47
47
if (latestAnnouncement == null || latestAnnouncement!! .id.value <= new.id.value) {
48
48
latestAnnouncement = new
49
- new.tags.forEach { tag -> latestAnnouncementByTag[tag.id.value ] = new }
49
+ new.tags.forEach { tag -> latestAnnouncementByTag[tag.name ] = new }
50
50
}
51
51
}
52
52
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]) }
59
60
60
61
latestAnnouncementForTag?.let { latestAnnouncementByTag[tag] = it }
61
62
}
@@ -64,16 +65,16 @@ internal class AnnouncementRepository(private val database: Database) {
64
65
latestAnnouncement.toApiResponseAnnouncement()
65
66
}
66
67
67
- suspend fun latest (tags : Set <Int >) = transaction {
68
+ suspend fun latest (tags : Set <String >) = transaction {
68
69
tags.mapNotNull { tag -> latestAnnouncementByTag[tag] }.toApiAnnouncement()
69
70
}
70
71
71
72
fun latestId () = latestAnnouncement?.id?.value.toApiResponseAnnouncementId()
72
73
73
- fun latestId (tags : Set <Int >) =
74
+ fun latestId (tags : Set <String >) =
74
75
tags.map { tag -> latestAnnouncementByTag[tag]?.id?.value }.toApiResponseAnnouncementId()
75
76
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 {
77
78
Announcement .find {
78
79
fun idLessEq () = Announcements .id lessEq cursor
79
80
fun archivedAtIsNull () = Announcements .archivedAt.isNull()
@@ -92,12 +93,12 @@ internal class AnnouncementRepository(private val database: Database) {
92
93
archivedAtIsNull() or archivedAtGreaterNow()
93
94
}
94
95
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 )
97
98
.select(AnnouncementTags .announcement)
98
- .where { AnnouncementTags .tag inList tags }
99
+ .where { Tags .name inList tags }
99
100
.withDistinct()
100
- }
101
+ )
101
102
102
103
idLessEq() and archivedAtGreaterOrNullOrTrue() and hasTags()
103
104
}
@@ -165,7 +166,7 @@ internal class AnnouncementRepository(private val database: Database) {
165
166
// Delete the tag if no other announcements are referencing it.
166
167
// One count means that the announcement is the only one referencing the tag.
167
168
announcement.tags.filter { tag -> tag.announcements.count() == 1L }.forEach { tag ->
168
- latestAnnouncementByTag - = tag.id.value
169
+ latestAnnouncementByTag - = tag.name
169
170
tag.delete()
170
171
}
171
172
@@ -250,7 +251,7 @@ internal class AnnouncementRepository(private val database: Database) {
250
251
title,
251
252
content,
252
253
attachments.map { it.url },
253
- tags.map { it.id.value },
254
+ tags.map { it.name },
254
255
createdAt,
255
256
archivedAt,
256
257
level,
@@ -259,7 +260,7 @@ internal class AnnouncementRepository(private val database: Database) {
259
260
260
261
private fun Iterable<Announcement>.toApiAnnouncement () = map { it.toApiResponseAnnouncement()!! }
261
262
262
- private fun Iterable<Tag>.toApiTag () = map { ApiAnnouncementTag (it.id.value, it. name) }
263
+ private fun Iterable<Tag>.toApiTag () = map { ApiAnnouncementTag (it.name) }
263
264
264
265
private fun Int?.toApiResponseAnnouncementId () = this ?.let { ApiResponseAnnouncementId (this ) }
265
266
0 commit comments