Skip to content

Commit

Permalink
feat: show favored and last read time in web novel list
Browse files Browse the repository at this point in the history
  • Loading branch information
FishHawk committed Jun 23, 2024
1 parent b06d255 commit 2333c8c
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 121 deletions.
3 changes: 2 additions & 1 deletion server/src/main/kotlin/api/RouteWebNovel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class WebNovelApi(

return metadataRepo
.search(
userId = user?.id,
userQuery = queryString,
filterProvider = filterProviderParsed,
filterType = filterType,
Expand Down Expand Up @@ -385,7 +386,7 @@ class WebNovelApi(
)

private suspend fun buildNovelDto(
novel: WebNovelMetadata,
novel: WebNovel,
user: User?,
): NovelDto {
val dto = NovelDto(
Expand Down
10 changes: 8 additions & 2 deletions server/src/main/kotlin/api/model/WebNovel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api.model

import infra.web.WebNovelAttention
import infra.web.WebNovelMetadataListItem
import infra.web.WebNovelListItem
import infra.web.WebNovelType
import kotlinx.serialization.Serializable

Expand All @@ -15,6 +15,10 @@ data class WebNovelOutlineDto(
val attentions: List<WebNovelAttention>,
val keywords: List<String>,
val extra: String?,
//
val favored: String?,
val lastReadAt: Long?,
//
val total: Long,
val jp: Long,
val baidu: Long,
Expand All @@ -24,7 +28,7 @@ data class WebNovelOutlineDto(
val updateAt: Long?,
)

fun WebNovelMetadataListItem.asDto() =
fun WebNovelListItem.asDto() =
WebNovelOutlineDto(
providerId = providerId,
novelId = novelId,
Expand All @@ -34,6 +38,8 @@ fun WebNovelMetadataListItem.asDto() =
attentions = attentions,
keywords = keywords,
extra = extra,
favored = favored,
lastReadAt = lastReadAt?.epochSeconds,
total = total,
jp = jp,
baidu = baidu,
Expand Down
12 changes: 8 additions & 4 deletions server/src/main/kotlin/infra/web/WebNovel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum class WebNovelType {
}

@Serializable
data class WebNovelMetadataListItem(
data class WebNovelListItem(
val providerId: String,
@SerialName("bookId")
val novelId: String,
Expand All @@ -58,6 +58,10 @@ data class WebNovelMetadataListItem(
val type: WebNovelType?,
val attentions: List<WebNovelAttention>,
val keywords: List<String>,
//
val favored: String? = null,
@Contextual val lastReadAt: Instant? = null,
//
val total: Long = 0,
val jp: Long = 0,
val baidu: Long = 0,
Expand All @@ -69,7 +73,7 @@ data class WebNovelMetadataListItem(
)

@Serializable
class WebNovelMetadata(
class WebNovel(
@Contextual @SerialName("_id") val id: ObjectId,
val providerId: String,
@SerialName("bookId")
Expand Down Expand Up @@ -104,8 +108,8 @@ class WebNovelMetadata(
companion object {
fun byId(providerId: String, novelId: String): Bson =
and(
eq(WebNovelMetadata::providerId.field(), providerId),
eq(WebNovelMetadata::novelId.field(), novelId),
eq(WebNovel::providerId.field(), providerId),
eq(WebNovel::novelId.field(), novelId),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class WebNovelEsDataSource(
}

suspend fun syncNovel(
novel: WebNovelMetadata,
novel: WebNovel,
) {
es.indexDocument(
id = "${novel.providerId}.${novel.novelId}",
Expand All @@ -197,7 +197,7 @@ class WebNovelEsDataSource(
}

suspend fun syncVisited(
novel: WebNovelMetadata,
novel: WebNovel,
) {
es.updateDocument(
id = "${novel.providerId}.${novel.novelId}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import infra.common.Glossary
import infra.common.TranslatorId
import infra.web.WebNovelChapter
import infra.web.WebNovelChapterTranslationState
import infra.web.WebNovelMetadata
import infra.web.WebNovel
import infra.web.datasource.WebNovelHttpDataSource
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.toList
Expand All @@ -23,7 +23,7 @@ class WebNovelChapterRepository(
mongo: MongoClient,
) {
private val webNovelMetadataCollection =
mongo.database.getCollection<WebNovelMetadata>(
mongo.database.getCollection<WebNovel>(
MongoCollectionNames.WEB_NOVEL,
)
private val webNovelChapterCollection =
Expand Down Expand Up @@ -216,17 +216,17 @@ class WebNovelChapterRepository(
)
)
val zhProperty = when (translatorId) {
TranslatorId.Baidu -> WebNovelMetadata::baidu
TranslatorId.Youdao -> WebNovelMetadata::youdao
TranslatorId.Gpt -> WebNovelMetadata::gpt
TranslatorId.Sakura -> WebNovelMetadata::sakura
TranslatorId.Baidu -> WebNovel::baidu
TranslatorId.Youdao -> WebNovel::youdao
TranslatorId.Gpt -> WebNovel::gpt
TranslatorId.Sakura -> WebNovel::sakura
}
webNovelMetadataCollection
.updateOne(
WebNovelMetadata.byId(providerId, novelId),
WebNovel.byId(providerId, novelId),
combine(
set(zhProperty.field(), zh),
set(WebNovelMetadata::changeAt.field(), Clock.System.now()),
set(WebNovel::changeAt.field(), Clock.System.now()),
),
)
return zh
Expand All @@ -240,10 +240,10 @@ class WebNovelChapterRepository(
.countDocuments(WebNovelChapter.byNovelId(providerId, novelId))
webNovelMetadataCollection
.updateOne(
WebNovelMetadata.byId(providerId, novelId),
WebNovel.byId(providerId, novelId),
combine(
set(WebNovelMetadata::jp.field(), jp),
set(WebNovelMetadata::changeAt.field(), Clock.System.now()),
set(WebNovel::jp.field(), jp),
set(WebNovel::changeAt.field(), Clock.System.now()),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import infra.common.FavoredNovelListSort
import infra.common.Page
import infra.common.emptyPage
import infra.web.WebNovelFavoriteDbModel
import infra.web.WebNovelMetadata
import infra.web.WebNovelMetadataListItem
import infra.web.WebNovel
import infra.web.WebNovelListItem
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
Expand Down Expand Up @@ -47,11 +47,11 @@ class WebNovelFavoredRepository(
page: Int,
pageSize: Int,
sort: FavoredNovelListSort,
): Page<WebNovelMetadataListItem> {
): Page<WebNovelListItem> {
@Serializable
data class PageModel(
val total: Int = 0,
val items: List<WebNovelMetadata>,
val items: List<WebNovel>,
)

val sortProperty = when (sort) {
Expand Down Expand Up @@ -79,7 +79,7 @@ class WebNovelFavoredRepository(
lookup(
/* from = */ MongoCollectionNames.WEB_NOVEL,
/* localField = */ WebNovelFavoriteDbModel::novelId.field(),
/* foreignField = */ WebNovelMetadata::id.field(),
/* foreignField = */ WebNovel::id.field(),
/* as = */ "novel"
),
unwind("\$novel"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import infra.common.NovelFileTranslationsMode
import infra.common.NovelFileType
import infra.common.TranslatorId
import infra.web.WebNovelChapter
import infra.web.WebNovelMetadata
import infra.web.WebNovel
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.datetime.toKotlinInstant
import util.serialName
Expand All @@ -21,7 +21,7 @@ class WebNovelFileRepository(
mongo: MongoClient,
) {
private val webNovelMetadataCollection =
mongo.database.getCollection<WebNovelMetadata>(
mongo.database.getCollection<WebNovel>(
MongoCollectionNames.WEB_NOVEL,
)
private val webNovelChapterCollection =
Expand All @@ -40,7 +40,7 @@ class WebNovelFileRepository(
type: NovelFileType,
): String? {
val novel = webNovelMetadataCollection
.find(WebNovelMetadata.byId(providerId, novelId))
.find(WebNovel.byId(providerId, novelId))
.firstOrNull()
?: return null

Expand Down
Loading

0 comments on commit 2333c8c

Please sign in to comment.