feat: rebuild AudioManager around downloadable moods
- V8 wipe migration and the shared song store keyed by YouTube video id - media store, indexer, downloader, recommendation top-up and refcounted GC - mood songs plus the recommendation lifecycle and mixed radio pools - new search, song and recommendation API; remove library/longform/seed routes - chart PVC/media env, docs, Nix, README and an end-to-end smoke test
This commit is contained in:
1 parent
50d8b6c580
commit
824eb36dd2
59 files changed
+3838
-5276
No files matched your search
@@ -7,40 +7,39 @@ import io.ktor.server.routing.delete
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.patch
|
||||
import io.ktor.server.routing.post
|
||||
import java.nio.file.Files
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.Serializable
|
||||
import win.taptap.musicmanager.api.AddSeedRequest
|
||||
import win.taptap.musicmanager.api.AddToMoodResponse
|
||||
import win.taptap.musicmanager.api.CreateMoodRequest
|
||||
import win.taptap.musicmanager.api.MoodDetailDto
|
||||
import win.taptap.musicmanager.api.MoodDto
|
||||
import win.taptap.musicmanager.api.DownloadDto
|
||||
import win.taptap.musicmanager.api.RecommendationDecisionDto
|
||||
import win.taptap.musicmanager.api.RecommendationDto
|
||||
import win.taptap.musicmanager.api.SearchResponse
|
||||
import win.taptap.musicmanager.api.UpdateMoodRequest
|
||||
import win.taptap.musicmanager.api.seedDto
|
||||
import win.taptap.musicmanager.api.toDto
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UserAccount
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.DownloadRepository
|
||||
import win.taptap.musicmanager.db.LibraryRepository
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.db.SongRepository
|
||||
import win.taptap.musicmanager.discovery.DiscoveryService
|
||||
import win.taptap.musicmanager.discovery.TopUpService
|
||||
import win.taptap.musicmanager.discovery.YtResolver
|
||||
import win.taptap.musicmanager.discovery.isMusicLength
|
||||
import win.taptap.musicmanager.media.MediaGcService
|
||||
import win.taptap.musicmanager.media.MediaStore
|
||||
import win.taptap.musicmanager.radio.RadioService
|
||||
|
||||
@Serializable
|
||||
data class MoodsResponse(val moods: List<MoodDto>)
|
||||
|
||||
@Serializable
|
||||
data class RecommendationsResponse(val recommendations: List<RecommendationDto>)
|
||||
|
||||
fun Route.moodRoutes(
|
||||
users: UsersConfig,
|
||||
library: LibraryConfig,
|
||||
radioService: RadioService,
|
||||
discoveryService: DiscoveryService,
|
||||
downloadsEnabled: Boolean,
|
||||
topUpService: TopUpService,
|
||||
resolver: YtResolver,
|
||||
mediaStore: MediaStore,
|
||||
gc: MediaGcService,
|
||||
) {
|
||||
get("/moods") {
|
||||
val user = call.requireApiUser(users)
|
||||
@@ -53,30 +52,24 @@ fun Route.moodRoutes(
|
||||
val name = request.name.trim()
|
||||
if (name.isEmpty()) badRequest("Mood name is required")
|
||||
|
||||
val folders = user.musicFolderIds(library)
|
||||
val seedIds = request.seedSongIds
|
||||
.distinct()
|
||||
.filter { LibraryRepository.song(it, folders, user.name) != null }
|
||||
|
||||
val mood = MoodRepository.create(
|
||||
owner = user.name,
|
||||
name = name,
|
||||
description = request.description?.trim()?.takeIf { it.isNotEmpty() },
|
||||
query = request.query?.trim()?.takeIf { it.isNotEmpty() },
|
||||
genres = request.genres,
|
||||
targetSize = request.targetSize,
|
||||
collectionPercent = request.collectionPercent,
|
||||
recommendationCount = request.recommendationCount,
|
||||
refreshHours = request.refreshHours,
|
||||
ytEnabled = request.ytEnabled,
|
||||
seedSongIds = seedIds,
|
||||
seedPercent = request.seedPercent,
|
||||
)
|
||||
call.respond(HttpStatusCode.Created, detail(mood, user, library))
|
||||
if (mood.recommendationCount > 0) topUpService.trigger(mood)
|
||||
call.respond(HttpStatusCode.Created, detail(mood))
|
||||
}
|
||||
|
||||
get("/moods/{id}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
call.respond(detail(mood, user, library))
|
||||
call.respond(detail(mood))
|
||||
}
|
||||
|
||||
patch("/moods/{id}") {
|
||||
@@ -102,45 +95,20 @@ fun Route.moodRoutes(
|
||||
description = description,
|
||||
query = query,
|
||||
genres = request.genres ?: mood.genres,
|
||||
targetSize = request.targetSize ?: mood.targetSize,
|
||||
collectionPercent = request.collectionPercent ?: mood.collectionPercent,
|
||||
recommendationCount = request.recommendationCount ?: mood.recommendationCount,
|
||||
refreshHours = request.refreshHours ?: mood.refreshHours,
|
||||
ytEnabled = request.ytEnabled ?: mood.ytEnabled,
|
||||
seedPercent = request.seedPercent ?: mood.seedPercent,
|
||||
) ?: notFound("Mood")
|
||||
|
||||
call.respond(detail(updated, user, library))
|
||||
topUpService.trigger(updated)
|
||||
call.respond(detail(updated))
|
||||
}
|
||||
|
||||
delete("/moods/{id}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
MoodRepository.delete(mood.id, user.name)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
post("/moods/{id}/seeds") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val request = call.receiveJson<AddSeedRequest>()
|
||||
val song = requireMusicSong(request.songId, user, library)
|
||||
val added = MoodRepository.addSeed(
|
||||
id = mood.id,
|
||||
owner = user.name,
|
||||
songId = song.id,
|
||||
origin = request.origin,
|
||||
)
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(
|
||||
HttpStatusCode.OK,
|
||||
AddToMoodResponse(mood = refreshed.toDto(), added = added),
|
||||
)
|
||||
}
|
||||
|
||||
delete("/moods/{id}/seeds/{songId}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val songId = call.parameters["songId"] ?: badRequest("Song id is required")
|
||||
MoodRepository.removeSeed(mood.id, user.name, songId)
|
||||
gc.collectGarbage()
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
@@ -149,71 +117,169 @@ fun Route.moodRoutes(
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val limit = (call.request.queryParameters["limit"]?.toIntOrNull() ?: 20).coerceIn(1, 50)
|
||||
call.respond(SearchResponse(radioService.preview(mood, user, library, limit).map { it.toDto() }))
|
||||
call.respond(TracksResponse(radioService.preview(mood, user, limit).map { it.toDto() }))
|
||||
}
|
||||
|
||||
get("/moods/{id}/recommendations") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
call.respond(recommendations(mood, user, library))
|
||||
call.respond(RecommendationsResponse(recommendations(mood)))
|
||||
}
|
||||
|
||||
/** Runs a discovery pass now, bypassing the mood's refresh interval. */
|
||||
post("/moods/{id}/recommendations/refresh") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
if (!mood.ytEnabled) conflict("YT discovery is disabled for this mood")
|
||||
if (mood.recommendationCount <= 0) conflict("Recommendations are disabled for this mood")
|
||||
|
||||
val outcome = withContext(Dispatchers.IO) {
|
||||
discoveryService.refresh(mood, user, library, force = true)
|
||||
discoveryService.refresh(mood, force = true)
|
||||
}
|
||||
outcome.error?.let { message ->
|
||||
throw ApiException(HttpStatusCode.BadGateway, "discovery_failed", message)
|
||||
}
|
||||
topUpService.topUp(mood, force = false)
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(detail(refreshed, user, library))
|
||||
call.respond(detail(refreshed))
|
||||
}
|
||||
|
||||
post("/moods/{id}/recommendations/{videoId}/accept") {
|
||||
post("/moods/{id}/recommendations/{videoId}/add") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val videoId = call.parameters["videoId"] ?: badRequest("Video id is required")
|
||||
val recommendation = MoodRepository.recommendation(mood.id, videoId)
|
||||
?: notFound("Recommendation")
|
||||
|
||||
val matched = recommendation.matchedSongId?.let { songId ->
|
||||
LibraryRepository.song(songId, user.musicFolderIds(library), user.name)
|
||||
} ?: DownloadRepository.completedSongForVideo(recommendation.videoId)?.let { songId ->
|
||||
// The same video may already have been downloaded for another mood.
|
||||
LibraryRepository.song(songId, user.musicFolderIds(library), user.name)
|
||||
}
|
||||
val seedAdded = matched != null &&
|
||||
MoodRepository.addSeed(mood.id, user.name, matched.id, "recommendation")
|
||||
val alreadyAccepted = recommendation.state == "accepted"
|
||||
MoodRepository.setRecommendationState(mood.id, videoId, "accepted")
|
||||
if (!alreadyAccepted) MoodRepository.incrementRecommendationAdds(mood.id, videoId)
|
||||
if (matched == null && downloadsEnabled) {
|
||||
DownloadRepository.enqueue(
|
||||
userName = user.name,
|
||||
moodId = mood.id,
|
||||
videoId = recommendation.videoId,
|
||||
title = recommendation.title,
|
||||
artist = recommendation.artist,
|
||||
)
|
||||
if (recommendation.state == "failed") {
|
||||
conflict("Retry the download before adding this track")
|
||||
}
|
||||
|
||||
call.respond(decision(mood.id, videoId, user, library, seedAdded))
|
||||
MoodRepository.markAdded(mood.id, videoId)
|
||||
topUpService.trigger(mood)
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(
|
||||
RecommendationDecisionDto(
|
||||
detail = detail(refreshed),
|
||||
recommendation = MoodRepository.recommendation(mood.id, videoId)
|
||||
?.toDto(song = recommendationSong(mood.id, videoId))
|
||||
?: recommendation.toDto(song = recommendationSong(mood.id, videoId)),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
post("/moods/{id}/recommendations/{videoId}/reject") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val videoId = call.parameters["videoId"] ?: badRequest("Video id is required")
|
||||
MoodRepository.recommendation(mood.id, videoId) ?: notFound("Recommendation")
|
||||
MoodRepository.setRecommendationState(mood.id, videoId, "rejected")
|
||||
DownloadRepository.cancel(mood.id, videoId, "Recommendation rejected")
|
||||
call.respond(decision(mood.id, videoId, user, library, seedAdded = false))
|
||||
val recommendation = MoodRepository.recommendation(mood.id, videoId)
|
||||
?: notFound("Recommendation")
|
||||
|
||||
MoodRepository.markRejected(mood.id, videoId)
|
||||
DownloadRepository.cancelForMoodVideo(mood.id, videoId, "Recommendation rejected")
|
||||
recommendation.songId?.let(gc::collect)
|
||||
topUpService.trigger(mood)
|
||||
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(
|
||||
RecommendationDecisionDto(
|
||||
detail = detail(refreshed),
|
||||
recommendation = recommendation.copy(state = "rejected").toDto(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
post("/moods/{id}/recommendations/{videoId}/retry") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val videoId = call.parameters["videoId"] ?: badRequest("Video id is required")
|
||||
val recommendation = MoodRepository.recommendation(mood.id, videoId)
|
||||
?: notFound("Recommendation")
|
||||
if (recommendation.state != "failed") conflict("Only failed downloads can be retried")
|
||||
|
||||
MoodRepository.markRecommendationDownloading(mood.id, videoId)
|
||||
DownloadRepository.enqueue(
|
||||
videoId = videoId,
|
||||
moodId = mood.id,
|
||||
kind = DownloadRepository.KIND_RECOMMENDATION,
|
||||
)
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(detail(refreshed))
|
||||
}
|
||||
|
||||
post("/moods/{id}/songs") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val request = call.receiveJson<AddSongRequest>()
|
||||
val normalized = normalizeAdd(request, resolver)
|
||||
|
||||
val bySongId = normalized.songId?.let(SongRepository::byId)
|
||||
if (normalized.songId != null && bySongId == null) notFound("Song")
|
||||
val existing = (bySongId ?: normalized.videoId?.let(SongRepository::byVideoId))
|
||||
?.takeIf { mediaStore.file(it)?.let(Files::isRegularFile) == true }
|
||||
if (existing != null) {
|
||||
val added = MoodRepository.addSong(mood.id, existing.id, normalized.origin)
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(
|
||||
AddSongResponse(
|
||||
detail = detail(refreshed),
|
||||
added = added,
|
||||
downloading = false,
|
||||
),
|
||||
)
|
||||
return@post
|
||||
}
|
||||
|
||||
val videoId = normalized.videoId ?: badRequest("No song or video id was supplied")
|
||||
DownloadRepository.enqueue(
|
||||
videoId = videoId,
|
||||
moodId = mood.id,
|
||||
kind = DownloadRepository.KIND_SONG,
|
||||
)
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(
|
||||
HttpStatusCode.Accepted,
|
||||
AddSongResponse(detail = detail(refreshed), added = false, downloading = true),
|
||||
)
|
||||
}
|
||||
|
||||
delete("/moods/{id}/songs/{songId}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val songId = call.parameters["songId"] ?: badRequest("Song id is required")
|
||||
MoodRepository.removeSong(mood.id, songId)
|
||||
gc.collect(songId)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
}
|
||||
|
||||
private data class NormalizedAdd(
|
||||
val songId: String?,
|
||||
val videoId: String?,
|
||||
val origin: String,
|
||||
)
|
||||
|
||||
private fun normalizeAdd(request: AddSongRequest, resolver: YtResolver): NormalizedAdd = when {
|
||||
!request.songId.isNullOrBlank() -> NormalizedAdd(request.songId, null, "search")
|
||||
!request.videoId.isNullOrBlank() -> NormalizedAdd(null, request.videoId.trim(), "search")
|
||||
!request.url.isNullOrBlank() -> {
|
||||
val candidate = runCatching { resolver.resolve(request.url) }
|
||||
.getOrElse { error ->
|
||||
throw ApiException(
|
||||
HttpStatusCode.BadGateway,
|
||||
"resolve_failed",
|
||||
error.message ?: "Could not resolve the URL",
|
||||
)
|
||||
}
|
||||
?: throw ApiException(
|
||||
HttpStatusCode.BadGateway,
|
||||
"resolve_failed",
|
||||
"The URL did not resolve to a video",
|
||||
)
|
||||
if (!isMusicLength(candidate.duration)) {
|
||||
badRequest("Only tracks up to 10 minutes can be added")
|
||||
}
|
||||
NormalizedAdd(null, candidate.videoId, "url")
|
||||
}
|
||||
else -> badRequest("Supply songId, videoId or url")
|
||||
}
|
||||
|
||||
private fun ownedMood(id: String?, user: UserAccount): MoodRecord {
|
||||
@@ -221,83 +287,39 @@ private fun ownedMood(id: String?, user: UserAccount): MoodRecord {
|
||||
return MoodRepository.get(id, user.name) ?: notFound("Mood")
|
||||
}
|
||||
|
||||
private fun requireMusicSong(songId: String, user: UserAccount, library: LibraryConfig) =
|
||||
LibraryRepository.song(songId, user.musicFolderIds(library), user.name)
|
||||
?: notFound("Song")
|
||||
|
||||
private fun detail(mood: MoodRecord, user: UserAccount, library: LibraryConfig): MoodDetailDto {
|
||||
val folders = user.musicFolderIds(library)
|
||||
val seedRecords = MoodRepository.seeds(mood.id)
|
||||
val seedSongs = LibraryRepository
|
||||
.songsByIds(seedRecords.map { it.songId }, folders, user.name)
|
||||
.associateBy { it.id }
|
||||
val seeds = seedRecords.mapNotNull { seed ->
|
||||
seedSongs[seed.songId]?.let { seedDto(seed, it) }
|
||||
private fun detail(mood: MoodRecord): MoodDetailDto {
|
||||
val songRecords = MoodRepository.songRecords(mood.id).associateBy { it.id }
|
||||
val songs = MoodRepository.songs(mood.id).mapNotNull { record ->
|
||||
songRecords[record.songId]?.let { record.toDto(it) }
|
||||
}
|
||||
|
||||
return MoodDetailDto(
|
||||
mood = mood.toDto(),
|
||||
seeds = seeds,
|
||||
recommendations = recommendations(mood, user, library).recommendations,
|
||||
songs = songs,
|
||||
recommendations = recommendations(mood),
|
||||
)
|
||||
}
|
||||
|
||||
private fun recommendations(
|
||||
mood: MoodRecord,
|
||||
user: UserAccount,
|
||||
library: LibraryConfig,
|
||||
): RecommendationsResponse {
|
||||
private fun recommendations(mood: MoodRecord): List<RecommendationDto> {
|
||||
val records = MoodRepository.recommendations(mood.id)
|
||||
val matchedIds = records.mapNotNull { it.matchedSongId }
|
||||
val matchedSongs = LibraryRepository
|
||||
.songsByIds(matchedIds, user.visibleFolderIds(library), user.name)
|
||||
.associateBy { it.id }
|
||||
val downloads = DownloadRepository.forMood(user.name, mood.id)
|
||||
val songs = SongRepository.byIds(records.mapNotNull { it.songId }).associateBy { it.id }
|
||||
val jobs = DownloadRepository.forMood(mood.id)
|
||||
.filter { it.kind == DownloadRepository.KIND_RECOMMENDATION }
|
||||
.associateBy { it.videoId }
|
||||
|
||||
return RecommendationsResponse(
|
||||
recommendations = records.map { record ->
|
||||
RecommendationDto(
|
||||
videoId = record.videoId,
|
||||
title = record.title,
|
||||
artist = record.artist,
|
||||
album = record.album,
|
||||
duration = record.duration,
|
||||
thumbnail = record.thumbnail,
|
||||
state = record.state,
|
||||
matchedSong = record.matchedSongId?.let { matchedSongs[it] }?.toDto(),
|
||||
timesAdded = record.timesAdded,
|
||||
firstSeen = record.firstSeen,
|
||||
lastSeen = record.lastSeen,
|
||||
download = downloads[record.videoId]?.let { job ->
|
||||
DownloadDto(
|
||||
state = job.state,
|
||||
attempts = job.attempts,
|
||||
error = job.error,
|
||||
updatedAt = job.updatedAt,
|
||||
)
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
return records.map { record ->
|
||||
record.toDto(
|
||||
song = record.songId?.let { songs[it] },
|
||||
download = jobs[record.videoId]?.let { job ->
|
||||
DownloadDto(
|
||||
state = job.state,
|
||||
attempts = job.attempts,
|
||||
error = job.error,
|
||||
updatedAt = job.updatedAt,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun decision(
|
||||
moodId: String,
|
||||
videoId: String,
|
||||
user: UserAccount,
|
||||
library: LibraryConfig,
|
||||
seedAdded: Boolean,
|
||||
): RecommendationDecisionDto {
|
||||
val mood = MoodRepository.get(moodId, user.name) ?: notFound("Mood")
|
||||
val detail = detail(mood, user, library)
|
||||
val recommendation = detail.recommendations.firstOrNull { it.videoId == videoId }
|
||||
?: notFound("Recommendation")
|
||||
return RecommendationDecisionDto(
|
||||
detail = detail,
|
||||
recommendation = recommendation,
|
||||
seedAdded = seedAdded,
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class RecommendationsResponse(val recommendations: List<RecommendationDto>)
|
||||
private fun recommendationSong(moodId: String, videoId: String): SongRecord? =
|
||||
MoodRepository.recommendation(moodId, videoId)?.songId?.let(SongRepository::byId)
|
||||
Reference in new issue
Block a user