@@ -3,14 +3,17 @@ package dev.typetype.server.services
33import dev.typetype.server.db.DatabaseFactory
44import dev.typetype.server.db.tables.HistoryTable
55import dev.typetype.server.db.tables.ProgressTable
6+ import dev.typetype.server.db.tables.SubscriptionsTable
67import dev.typetype.server.models.HistoryItem
78import org.jetbrains.exposed.v1.core.LowerCase
89import org.jetbrains.exposed.v1.core.SortOrder
910import org.jetbrains.exposed.v1.core.and
1011import org.jetbrains.exposed.v1.core.eq
1112import org.jetbrains.exposed.v1.core.greaterEq
13+ import org.jetbrains.exposed.v1.core.inList
1214import org.jetbrains.exposed.v1.core.less
1315import org.jetbrains.exposed.v1.core.like
16+ import org.jetbrains.exposed.v1.core.neq
1417import org.jetbrains.exposed.v1.core.or
1518import org.jetbrains.exposed.v1.jdbc.andWhere
1619import org.jetbrains.exposed.v1.jdbc.batchInsert
@@ -26,20 +29,49 @@ class HistoryService {
2629 .orderBy(HistoryTable .watchedAt to SortOrder .DESC , HistoryTable .id to SortOrder .DESC )
2730 .toList()
2831 HistoryProgressMapper .toHistoryItemsForExport(userId, rows)
32+ }.withSubscriptionAvatars(userId)
33+
34+ suspend fun search (userId : String , q : String? , from : Long? , to : Long? , limit : Int , offset : Int ): Pair <List <HistoryItem >, Long> {
35+ val (items, total) = DatabaseFactory .query {
36+ val query = HistoryTable .selectAll().where { HistoryTable .userId eq userId }
37+ if (! q.isNullOrBlank()) {
38+ val pattern = " %${q.lowercase()} %"
39+ query.andWhere { (LowerCase (HistoryTable .title) like pattern) or (LowerCase (HistoryTable .channelName) like pattern) }
40+ }
41+ if (from != null ) query.andWhere { HistoryTable .watchedAt greaterEq from }
42+ if (to != null ) query.andWhere { HistoryTable .watchedAt less to }
43+ val total = query.count()
44+ val rows = query.orderBy(HistoryTable .watchedAt to SortOrder .DESC , HistoryTable .id to SortOrder .DESC ).limit(limit).offset(offset.toLong()).toList()
45+ HistoryProgressMapper .toHistoryItems(userId, rows) to total
46+ }
47+ return items.withSubscriptionAvatars(userId) to total
2948 }
3049
31- suspend fun search (userId : String , q : String? , from : Long? , to : Long? , limit : Int , offset : Int ): Pair <List <HistoryItem >, Long> = DatabaseFactory .query {
32- val query = HistoryTable .selectAll().where { HistoryTable .userId eq userId }
33- if (! q.isNullOrBlank()) {
34- val pattern = " %${q.lowercase()} %"
35- query.andWhere { (LowerCase (HistoryTable .title) like pattern) or (LowerCase (HistoryTable .channelName) like pattern) }
50+ private suspend fun List<HistoryItem>.withSubscriptionAvatars (userId : String ): List <HistoryItem > {
51+ val missingUrls = filter { it.channelAvatar.isBlank() && it.channelUrl.isNotBlank() }
52+ .map { ChannelUrlCanonicalizer .canonicalize(it.channelUrl) }
53+ .distinct()
54+ if (missingUrls.isEmpty()) return this
55+ val avatars = DatabaseFactory .query {
56+ SubscriptionsTable .selectAll()
57+ .where {
58+ (SubscriptionsTable .userId eq userId) and
59+ (SubscriptionsTable .channelUrl inList missingUrls) and
60+ (SubscriptionsTable .avatarUrl neq " " )
61+ }
62+ .map {
63+ ChannelUrlCanonicalizer .canonicalize(it[SubscriptionsTable .channelUrl]) to
64+ it[SubscriptionsTable .avatarUrl]
65+ }
66+ .toMap()
67+ }
68+ if (avatars.isEmpty()) return this
69+ return map { item ->
70+ if (item.channelAvatar.isNotBlank()) item
71+ else avatars[ChannelUrlCanonicalizer .canonicalize(item.channelUrl)]?.let {
72+ item.copy(channelAvatar = it)
73+ } ? : item
3674 }
37- if (from != null ) query.andWhere { HistoryTable .watchedAt greaterEq from }
38- if (to != null ) query.andWhere { HistoryTable .watchedAt less to }
39- val total = query.count()
40- val rows = query.orderBy(HistoryTable .watchedAt to SortOrder .DESC , HistoryTable .id to SortOrder .DESC ).limit(limit).offset(offset.toLong()).toList()
41- val items = HistoryProgressMapper .toHistoryItems(userId, rows)
42- items to total
4375 }
4476
4577 suspend fun add (userId : String , item : HistoryItem ): HistoryItem = insert(userId, item, System .currentTimeMillis())
0 commit comments