Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,18 @@ class DownloadEnqueuer(

/**
* Single-episode download. Resolves the episode's catalog row for rich
* sidecar metadata (series title, S/E numbers, still image). The
* `seriesContentId` is what gets associated on the server side; the
* sidecar metadata (series title, S/E numbers, still image). The server
* registers the entry under `seriesContentId` with `episodeContentId` as
* its episode, and rejects an episode sent as the content id; the
* `fileId` chosen here is the user-preferred quality.
*/
suspend fun startEpisode(
seriesContentId: String,
episodeContentId: String,
fileId: Int,
seriesTitle: String,
seasonNumber: Int,
episodeNumber: Int,
seasonNumber: Int?,
episodeNumber: Int?,
episodeTitle: String?,
posterUrl: String? = null,
downloadQualityOverride: DownloadQuality? = null,
Expand All @@ -108,11 +109,15 @@ class DownloadEnqueuer(
Log.i(TAG, "startEpisode: fileId=$fileId already queued/downloading — skipping duplicate")
return alreadyActive()
}
val displayTitle = "$seriesTitle S${seasonNumber}E${episodeNumber}" +
(episodeTitle?.takeIf { it.isNotBlank() }?.let { " · $it" } ?: "")
// Unknown numbers leave the S/E label out rather than print "S0E0".
val episodeCode = if (seasonNumber != null && episodeNumber != null) "S${seasonNumber}E${episodeNumber}" else null
val shownEpisodeTitle = episodeTitle?.takeIf { it.isNotBlank() }
val displayTitle = listOfNotNull(seriesTitle, episodeCode).joinToString(" ") +
(shownEpisodeTitle?.let { " · $it" } ?: "")
val record = when (val r = repository.create(
downloadRequest(
contentId = episodeContentId,
contentId = seriesContentId,
episodeId = episodeContentId,
fileId = fileId,
downloadQualityOverride = downloadQualityOverride,
),
Expand All @@ -127,7 +132,7 @@ class DownloadEnqueuer(
val sidecar = DownloadSidecar(
record = record,
title = seriesTitle,
subtitle = "S${seasonNumber}E${episodeNumber}" + (episodeTitle?.takeIf { it.isNotBlank() }?.let { " · $it" } ?: ""),
subtitle = listOfNotNull(episodeCode, shownEpisodeTitle).joinToString(" · ").ifEmpty { null },
posterUrl = posterUrl,
seriesTitle = seriesTitle,
seriesContentId = seriesContentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import kotlinx.serialization.json.Json
/**
* Maps between the on-the-wire-ish [DownloadSidecar] (the download's full local
* picture) and the Room [DownloadEntity] projection. Chapters serialize to a JSON
* column (no TypeConverter). `episodeId`/`batchId`/`deliveryFormat`/
* `targetBitrateKbps` on [DownloadRecord] have no entity column and aren't read
* anywhere in the download/playback paths, so they round-trip as null.
* column (no TypeConverter). `batchId`/`deliveryFormat`/`targetBitrateKbps` on
* [DownloadRecord] have no entity column and aren't read anywhere in the
* download/playback paths, so they round-trip as null.
*
* The server keys an episode's entry by its series (`contentId`) plus
* `episodeId`, while every local reader (offline playback, the Downloads list,
* watched state) looks a download up by the item it plays. The entity's
* `contentId` is therefore the episode for episodes; the series stays in
* `seriesContentId`.
*/
private val mappingJson = Json { ignoreUnknownKeys = true }

Expand All @@ -22,7 +28,7 @@ fun DownloadSidecar.toEntity(serverId: String, profileId: String): DownloadEntit
profileId = profileId,
mediaFileId = record.mediaFileId,
recordId = record.id,
contentId = record.contentId,
contentId = record.episodeId?.takeIf { it.isNotBlank() } ?: record.contentId,
title = title,
subtitle = subtitle,
posterUrl = posterUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.siloserver.silo.common.downloads

import org.siloserver.silo.model.download.DownloadRecord
import org.siloserver.silo.model.download.DownloadSidecar
import kotlin.test.Test
import kotlin.test.assertEquals

class DownloadSidecarMappingTest {
private fun sidecar(contentId: String, episodeId: String?) = DownloadSidecar(
record = DownloadRecord(
id = "dl-1",
contentId = contentId,
episodeId = episodeId,
mediaFileId = 7,
kind = "queued",
status = "ready",
createdAt = "2026-09-28T10:00:00Z",
),
title = "Show",
seriesContentId = "series-1",
updatedAtMs = 0L,
)

@Test
fun `an episode is stored under the episode it plays, not its series`() {
// The server keys the entry by series + episode; offline playback and
// the Downloads list look it up by the episode.
val entity = sidecar(contentId = "series-1", episodeId = "episode-2").toEntity("server", "profile")

assertEquals("episode-2", entity.contentId)
assertEquals("series-1", entity.seriesContentId)
}

@Test
fun `a movie keeps its own content id`() {
val entity = sidecar(contentId = "movie-1", episodeId = null).toEntity("server", "profile")

assertEquals("movie-1", entity.contentId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -748,15 +748,15 @@ fun ItemDetailScreen(
viewModel.onDownloadTapped(
version, episode.title,
forceRedownloadMissingLocal = episodeDownloadState.needsLocalRecovery,
downloadContentId = episode.contentId,
episode = episode,
)
},
qualityAction = { quality ->
viewModel.onDownloadTapped(
version, episode.title,
forceRedownloadMissingLocal = episodeDownloadState.needsLocalRecovery,
downloadQuality = quality,
downloadContentId = episode.contentId,
episode = episode,
)
},
estimate = org.siloserver.silo.model.download.DownloadSizeEstimate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ data class ItemDetailUiState(
val isLoadingSelectedEpisodeDetail: Boolean = false,
/** Parent-series portrait art used when an episode's own artwork is a wide still. */
val episodeSeriesPosterUrl: String? = null,
/** Parent series title on a standalone episode page, for download grouping. */
val episodeSeriesTitle: String? = null,
val episodeSeriesPosterThumbhash: String? = null,
/**
* Route-scoped episode lists keyed by season. Unlike the repository's
Expand Down Expand Up @@ -235,7 +237,7 @@ class ItemDetailViewModel(
displayTitle: String,
forceRedownloadMissingLocal: Boolean = false,
downloadQuality: DownloadQuality? = null,
downloadContentId: String = contentId,
episode: ItemDetail? = null,
) {
val existing = downloadRecordFor(version)
when (
Expand All @@ -257,11 +259,11 @@ class ItemDetailViewModel(
DetailDownloadTapAction.ReplaceAndStart -> viewModelScope.launch {
val staleRecord = existing
if (staleRecord == null || downloadsRepository.delete(staleRecord.id) is ApiResult.Success) {
startDownload(version, displayTitle, downloadQuality, downloadContentId)
startDownload(version, displayTitle, downloadQuality, episode)
}
}
DetailDownloadTapAction.Start -> viewModelScope.launch {
startDownload(version, displayTitle, downloadQuality, downloadContentId)
startDownload(version, displayTitle, downloadQuality, episode)
}
}
}
Expand All @@ -275,16 +277,58 @@ class ItemDetailViewModel(
version: FileVersion,
displayTitle: String,
downloadQuality: DownloadQuality?,
downloadContentId: String,
episode: ItemDetail?,
) {
// The series page names its selected episode; an episode page is
// itself the episode. Episodes register under their series, which the
// server requires, so they go through startEpisode.
val pageDetail = _uiState.value.detail
val item = episode ?: pageDetail
val seriesPage = pageDetail?.takeIf { it.type == "series" }
// On the series page the page itself is the parent when the episode
// row omits its series id.
val seriesId = item?.seriesId?.takeIf { it.isNotBlank() }
?: seriesPage?.takeIf { episode != null }?.contentId
// wifiOnly read from per-profile PlayerSettingsStore inside
// DownloadEnqueuer.start; default true.
val result = downloadEnqueuer.start(
contentId = downloadContentId,
fileId = version.fileId,
displayTitle = displayTitle,
downloadQualityOverride = downloadQuality,
)
// DownloadEnqueuer; default true.
val result = when {
item?.type == "episode" && seriesId != null -> {
// Never the episode's own title or art: the Downloads tab
// groups episodes under the series' name and poster.
val knownTitle = item.seriesTitle?.takeIf { it.isNotBlank() }
?: seriesPage?.title
?: _uiState.value.episodeSeriesTitle?.takeIf { it.isNotBlank() }
val knownPoster = seriesPage?.posterUrl ?: _uiState.value.episodeSeriesPosterUrl
// On an episode page the parent load may not have finished (or
// may not run at all); the title and poster are stored with the
// download, so fetch the parent now when either is missing. A
// series page already is the parent, so it never refetches.
val parent = if (seriesPage == null && (knownTitle == null || knownPoster == null)) {
(catalogRepository.getItemDetailForPrefetch(seriesId, libraryId = libraryId) as? ApiResult.Success)?.data
} else {
null
}
downloadEnqueuer.startEpisode(
seriesContentId = seriesId,
episodeContentId = item.contentId,
fileId = version.fileId,
seriesTitle = knownTitle ?: parent?.title?.takeIf { it.isNotBlank() } ?: "Series",
seasonNumber = item.seasonNumber,
episodeNumber = item.episodeNumber,
episodeTitle = item.title,
posterUrl = knownPoster ?: parent?.posterUrl ?: pageDetail?.posterUrl,
downloadQualityOverride = downloadQuality,
)
}
// The server rejects an episode sent without its series.
item?.type == "episode" -> ApiResult.Error(0, "missing_series", "This episode isn't linked to a series.")
else -> downloadEnqueuer.start(
contentId = item?.contentId ?: contentId,
fileId = version.fileId,
displayTitle = displayTitle,
downloadQualityOverride = downloadQuality,
)
}
_downloadStartEvents.emit(result is ApiResult.Success)
}

Expand Down Expand Up @@ -625,6 +669,7 @@ class ItemDetailViewModel(
it.copy(
episodeSeriesPosterUrl = result.data.posterUrl,
episodeSeriesPosterThumbhash = result.data.posterThumbhash,
episodeSeriesTitle = result.data.title,
)
}
}
Expand Down
Loading