feat: add per-user moods as generated playlists
Add private server-managed playlist moods with curated seeds and related library fill. Moods are created or promoted from existing playlists in the web UI, refresh lazily after their TTL, and are marked stale after a scan. Symfonium stars pin the active mood: opening a mood restores its context, and a starred song becomes a permanent seed. Without an active session the song is pinned to every mood that already contains it. Includes the V4 migration, custom mood endpoints, management UI, tests and smoke coverage. Bump the chart to 0.1.4.
This commit is contained in:
1 parent
e08d993e35
commit
28bfdd9d00
25 files changed
+2039
-14
No files matched your search
@@ -20,6 +20,8 @@ import io.ktor.server.testing.testApplication
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.time.Instant
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.int
|
||||
@@ -50,6 +52,7 @@ import win.taptap.musicmanager.db.MediaRepository
|
||||
import win.taptap.musicmanager.db.SongArtistsTable
|
||||
import win.taptap.musicmanager.db.SongGenresTable
|
||||
import win.taptap.musicmanager.db.SongsTable
|
||||
import win.taptap.musicmanager.mood.MoodService
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.subsonic.routes.subsonicRoutes
|
||||
@@ -448,6 +451,7 @@ class BrowsingRoutesTest {
|
||||
library = seed.library,
|
||||
coverArtService = seed.coverArt,
|
||||
transcoder = FfmpegTranscoder("ffmpeg"),
|
||||
moodService = MoodService(CoroutineScope(Dispatchers.Unconfined)),
|
||||
startScan = { false },
|
||||
scanStatus = { ScanStatus(false, 0) },
|
||||
)
|
||||
|
||||
@@ -0,0 +1,410 @@
|
||||
package win.taptap.musicmanager.subsonic
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import io.ktor.server.application.install
|
||||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.server.plugins.partialcontent.PartialContent
|
||||
import io.ktor.server.routing.routing
|
||||
import io.ktor.server.testing.ApplicationTestBuilder
|
||||
import io.ktor.server.testing.testApplication
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.time.Instant
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.int
|
||||
import kotlinx.serialization.json.jsonArray
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.insertIgnore
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import win.taptap.musicmanager.config.AppConfig
|
||||
import win.taptap.musicmanager.config.FolderType
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.MusicFolder
|
||||
import win.taptap.musicmanager.config.UserAccount
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.AlbumArtistsTable
|
||||
import win.taptap.musicmanager.db.AlbumsTable
|
||||
import win.taptap.musicmanager.db.ArtistsTable
|
||||
import win.taptap.musicmanager.db.ConfigRepository
|
||||
import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.DatabaseHandle
|
||||
import win.taptap.musicmanager.db.SongArtistsTable
|
||||
import win.taptap.musicmanager.db.SongGenresTable
|
||||
import win.taptap.musicmanager.db.SongsTable
|
||||
import win.taptap.musicmanager.mood.MoodService
|
||||
import win.taptap.musicmanager.mood.MoodSessionTracker
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.subsonic.routes.subsonicRoutes
|
||||
import win.taptap.musicmanager.transcoding.FfmpegTranscoder
|
||||
import win.taptap.musicmanager.util.IdGenerator
|
||||
|
||||
class MoodRoutesTest {
|
||||
|
||||
@TempDir
|
||||
lateinit var tempDir: Path
|
||||
|
||||
@Test
|
||||
fun `creating a mood generates a private music-only playlist`() {
|
||||
clearSessions()
|
||||
seededContext().use { seed ->
|
||||
testApplication {
|
||||
setupApp(seed)
|
||||
val auth = credentials("alice", "alice-pw")
|
||||
|
||||
val mood = client.createMood(auth, "Focus", listOf(seed.aliceAmbient1), target = 10)
|
||||
val id = mood.string("id")
|
||||
assertEquals(1, mood.int("seedCount"))
|
||||
assertTrue(mood.string("generatedAt").isNotBlank())
|
||||
|
||||
val entries = mood.ids("entry")
|
||||
assertTrue(seed.aliceAmbient1 in entries)
|
||||
assertTrue(seed.aliceAmbient2 in entries)
|
||||
assertFalse(seed.bobSong in entries)
|
||||
assertFalse(seed.longformSong in entries)
|
||||
|
||||
// A long-form song is not a valid mood seed and must not enter the mix.
|
||||
val bookish = client.createMood(auth, "Bookish", listOf(seed.longformSong), target = 5)
|
||||
assertEquals(0, bookish.int("seedCount"))
|
||||
assertEquals(0, bookish.ids("entry").size)
|
||||
|
||||
val playlist = parseBody(
|
||||
client.get("/rest/getPlaylist?$auth&id=$id").bodyAsText(),
|
||||
)["playlist"]!!.jsonObject
|
||||
assertEquals("Focus", playlist.string("name"))
|
||||
assertEquals(entries.size, playlist["entry"]!!.jsonArray.size)
|
||||
|
||||
val bobAuth = credentials("bob", "bob-pw")
|
||||
val bobMoods = parseBody(client.get("/rest/getMoods?$bobAuth").bodyAsText())["moods"]!!.jsonObject
|
||||
assertEquals(0, bobMoods["mood"]?.jsonArray?.size ?: 0)
|
||||
|
||||
val denied = parseBody(client.get("/rest/getPlaylist?$bobAuth&id=$id").bodyAsText())
|
||||
assertEquals(70, denied["error"]!!.jsonObject["code"]!!.jsonPrimitive.int)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `promoting a playlist keeps its id and rejects song edits`() {
|
||||
clearSessions()
|
||||
seededContext().use { seed ->
|
||||
testApplication {
|
||||
setupApp(seed)
|
||||
val auth = credentials("alice", "alice-pw")
|
||||
|
||||
val playlist = parseBody(
|
||||
client.get(
|
||||
"/rest/createPlaylist?$auth&name=Road&songId=${seed.aliceAmbient1}&songId=${seed.aliceAmbient2}",
|
||||
).bodyAsText(),
|
||||
)["playlist"]!!.jsonObject
|
||||
val id = playlist.string("id")
|
||||
|
||||
val promoted = parseBody(
|
||||
client.get("/rest/promotePlaylistToMood?$auth&playlistId=$id&targetSize=10").bodyAsText(),
|
||||
)["mood"]!!.jsonObject
|
||||
assertEquals(id, promoted.string("id"))
|
||||
assertEquals(2, promoted.int("seedCount"))
|
||||
assertEquals(2, promoted.ids("seed").size)
|
||||
assertTrue(seed.aliceAmbient1 in promoted.ids("entry"))
|
||||
|
||||
val edit = parseBody(
|
||||
client.get(
|
||||
"/rest/updatePlaylist?$auth&playlistId=$id&songIdToAdd=${seed.aliceRock}",
|
||||
).bodyAsText(),
|
||||
)
|
||||
assertEquals(50, edit["error"]!!.jsonObject["code"]!!.jsonPrimitive.int)
|
||||
|
||||
val bobAuth = credentials("bob", "bob-pw")
|
||||
val denied = parseBody(client.get("/rest/getPlaylist?$bobAuth&id=$id").bodyAsText())
|
||||
assertEquals(70, denied["error"]!!.jsonObject["code"]!!.jsonPrimitive.int)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `star pins to the active mood and unstar removes only star seeds`() {
|
||||
clearSessions()
|
||||
seededContext().use { seed ->
|
||||
testApplication {
|
||||
setupApp(seed)
|
||||
val auth = credentials("alice", "alice-pw")
|
||||
|
||||
val mood = client.createMood(
|
||||
auth,
|
||||
"Pinned",
|
||||
listOf(seed.aliceAmbient1),
|
||||
target = 10,
|
||||
expandRelated = false,
|
||||
)
|
||||
val id = mood.string("id")
|
||||
|
||||
// Opening the mood makes it active.
|
||||
client.get("/rest/getPlaylist?$auth&id=$id").bodyAsText()
|
||||
|
||||
val star = parseBody(client.get("/rest/star?$auth&id=${seed.aliceAmbient2}").bodyAsText())
|
||||
assertEquals("ok", star.string("status"))
|
||||
|
||||
val afterStar = parseBody(client.get("/rest/getMood?$auth&id=$id").bodyAsText())["mood"]!!.jsonObject
|
||||
assertEquals(2, afterStar.int("seedCount"))
|
||||
assertTrue(seed.aliceAmbient2 in afterStar.ids("seed"))
|
||||
assertTrue(seed.aliceAmbient1 in afterStar.ids("seed"))
|
||||
|
||||
val refreshed = parseBody(client.get("/rest/refreshMood?$auth&id=$id").bodyAsText())["mood"]!!.jsonObject
|
||||
assertTrue(seed.aliceAmbient2 in refreshed.ids("entry"))
|
||||
|
||||
parseBody(client.get("/rest/unstar?$auth&id=${seed.aliceAmbient2}").bodyAsText())
|
||||
val afterUnstar = parseBody(client.get("/rest/getMood?$auth&id=$id").bodyAsText())["mood"]!!.jsonObject
|
||||
assertEquals(1, afterUnstar.int("seedCount"))
|
||||
assertTrue(seed.aliceAmbient1 in afterUnstar.ids("seed"))
|
||||
assertFalse(seed.aliceAmbient2 in afterUnstar.ids("seed"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `star without a session pins moods that already contain the song`() {
|
||||
clearSessions()
|
||||
seededContext().use { seed ->
|
||||
testApplication {
|
||||
setupApp(seed)
|
||||
val auth = credentials("alice", "alice-pw")
|
||||
|
||||
val mood = client.createMood(auth, "Related", listOf(seed.aliceAmbient1), target = 10)
|
||||
val id = mood.string("id")
|
||||
assertTrue(seed.aliceAmbient2 in mood.ids("entry"))
|
||||
|
||||
// No getPlaylist call: there is no active mood session.
|
||||
parseBody(client.get("/rest/star?$auth&id=${seed.aliceAmbient2}").bodyAsText())
|
||||
|
||||
val afterStar = parseBody(client.get("/rest/getMood?$auth&id=$id").bodyAsText())["mood"]!!.jsonObject
|
||||
assertEquals(2, afterStar.int("seedCount"))
|
||||
assertTrue(seed.aliceAmbient2 in afterStar.ids("seed"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun ApplicationTestBuilder.setupApp(seed: Seed) {
|
||||
application {
|
||||
install(ContentNegotiation) {
|
||||
json(Json { ignoreUnknownKeys = true; encodeDefaults = true })
|
||||
}
|
||||
install(PartialContent)
|
||||
routing {
|
||||
subsonicRoutes(
|
||||
users = seed.users,
|
||||
library = seed.library,
|
||||
coverArtService = seed.coverArt,
|
||||
transcoder = FfmpegTranscoder("ffmpeg"),
|
||||
moodService = MoodService(CoroutineScope(Dispatchers.Unconfined)),
|
||||
startScan = { false },
|
||||
scanStatus = { ScanStatus(false, 0) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun seededContext(): Seed {
|
||||
val musicA = Files.createDirectories(tempDir.resolve("music-a"))
|
||||
val musicB = Files.createDirectories(tempDir.resolve("music-b"))
|
||||
val longform = Files.createDirectories(tempDir.resolve("longform"))
|
||||
|
||||
val library = LibraryConfig(
|
||||
listOf(
|
||||
MusicFolder(1, "Alice Music", FolderType.MUSIC, musicA),
|
||||
MusicFolder(2, "Bob Music", FolderType.MUSIC, musicB),
|
||||
MusicFolder(10, "Long-form", FolderType.LONGFORM, longform),
|
||||
),
|
||||
)
|
||||
val users = UsersConfig(
|
||||
listOf(
|
||||
UserAccount("alice", "alice-pw", admin = true, folders = setOf(1)),
|
||||
UserAccount("bob", "bob-pw", admin = false, folders = setOf(2)),
|
||||
),
|
||||
)
|
||||
|
||||
val db = DatabaseFactory.connect(
|
||||
AppConfig(
|
||||
port = 8080,
|
||||
dataDir = tempDir.resolve("data"),
|
||||
libraryConfigPath = tempDir.resolve("library.yaml"),
|
||||
usersFile = tempDir.resolve("users.yaml"),
|
||||
ffmpegPath = "ffmpeg",
|
||||
),
|
||||
)
|
||||
ConfigRepository.sync(library, users)
|
||||
|
||||
val aliceAmbient1 = seedSong(
|
||||
db, 1, "Alpha", "Focus One", "Deep Work",
|
||||
"Alpha/Focus One/01.mp3", "Ambient", musicA,
|
||||
)
|
||||
val aliceAmbient2 = seedSong(
|
||||
db, 1, "Beta", "Focus Two", "Soft Light",
|
||||
"Beta/Focus Two/01.mp3", "Ambient", musicA,
|
||||
)
|
||||
val aliceRock = seedSong(
|
||||
db, 1, "Gamma", "Loud", "Noise",
|
||||
"Gamma/Loud/01.mp3", "Rock", musicA,
|
||||
)
|
||||
val aliceAmbient3 = seedSong(
|
||||
db, 1, "Alpha", "Focus One", "Second Wind",
|
||||
"Alpha/Focus One/02.mp3", "Ambient", musicA,
|
||||
)
|
||||
val bobSong = seedSong(
|
||||
db, 2, "Bob Artist", "Bob Album", "Bob Song",
|
||||
"Bob Artist/Bob Album/01.mp3", "Ambient", musicB,
|
||||
)
|
||||
val longformSong = seedSong(
|
||||
db, 10, "Long Narrator", "Long Book", "Chapter One",
|
||||
"Long Narrator/Long Book/01.mp3", "Ambient", longform,
|
||||
)
|
||||
|
||||
return Seed(
|
||||
db = db,
|
||||
library = library,
|
||||
users = users,
|
||||
coverArt = CoverArtService(tempDir.resolve("data")),
|
||||
aliceAmbient1 = aliceAmbient1,
|
||||
aliceAmbient2 = aliceAmbient2,
|
||||
aliceRock = aliceRock,
|
||||
aliceAmbient3 = aliceAmbient3,
|
||||
bobSong = bobSong,
|
||||
longformSong = longformSong,
|
||||
)
|
||||
}
|
||||
|
||||
private fun seedSong(
|
||||
db: DatabaseHandle,
|
||||
folderId: Int,
|
||||
artistName: String,
|
||||
albumName: String,
|
||||
title: String,
|
||||
relativePath: String,
|
||||
genre: String,
|
||||
root: Path,
|
||||
): String {
|
||||
val file = root.resolve(relativePath)
|
||||
Files.createDirectories(file.parent)
|
||||
Files.write(file, "ID3-fake-audio".toByteArray())
|
||||
|
||||
val artistId = IdGenerator.artistId(artistName)
|
||||
val albumId = IdGenerator.albumId(artistName, albumName)
|
||||
val songId = IdGenerator.songId(folderId, relativePath)
|
||||
val now = Instant.now().toString()
|
||||
|
||||
transaction(db.database) {
|
||||
ArtistsTable.insertIgnore {
|
||||
it[id] = artistId
|
||||
it[name] = artistName
|
||||
it[sortName] = artistName
|
||||
it[searchText] = IdGenerator.normalize(artistName)
|
||||
}
|
||||
AlbumsTable.insertIgnore {
|
||||
it[id] = albumId
|
||||
it[name] = albumName
|
||||
it[year] = 2026
|
||||
it[createdAt] = now
|
||||
it[searchText] = IdGenerator.normalize("$albumName $artistName")
|
||||
}
|
||||
AlbumArtistsTable.insertIgnore {
|
||||
it[this.albumId] = albumId
|
||||
it[this.artistId] = artistId
|
||||
it[position] = 0
|
||||
}
|
||||
SongsTable.insert {
|
||||
it[id] = songId
|
||||
it[this.albumId] = albumId
|
||||
it[this.folderId] = folderId
|
||||
it[this.title] = title
|
||||
it[displayArtist] = artistName
|
||||
it[this.albumName] = albumName
|
||||
it[trackNumber] = 1
|
||||
it[discNumber] = 1
|
||||
it[year] = 2026
|
||||
it[duration] = 120
|
||||
it[bitRate] = 128
|
||||
it[size] = Files.size(file)
|
||||
it[suffix] = "mp3"
|
||||
it[contentType] = "audio/mpeg"
|
||||
it[this.relativePath] = relativePath
|
||||
it[fileMtime] = 0
|
||||
it[createdAt] = now
|
||||
it[searchText] = IdGenerator.normalize("$title $artistName $albumName $artistName")
|
||||
}
|
||||
SongArtistsTable.insert {
|
||||
it[this.songId] = songId
|
||||
it[this.artistId] = artistId
|
||||
it[position] = 0
|
||||
}
|
||||
SongGenresTable.insert {
|
||||
it[this.songId] = songId
|
||||
it[this.genre] = genre
|
||||
}
|
||||
}
|
||||
|
||||
return songId
|
||||
}
|
||||
|
||||
private suspend fun HttpClient.createMood(
|
||||
auth: String,
|
||||
name: String,
|
||||
seeds: List<String>,
|
||||
target: Int,
|
||||
expandRelated: Boolean = true,
|
||||
): JsonObject {
|
||||
val seedParams = seeds.joinToString("") { "&songId=$it" }
|
||||
val body = get(
|
||||
"/rest/createMood?$auth&name=$name$seedParams&targetSize=$target&expandRelated=$expandRelated",
|
||||
).bodyAsText()
|
||||
return parseBody(body)["mood"]!!.jsonObject
|
||||
}
|
||||
|
||||
private fun clearSessions() {
|
||||
MoodSessionTracker.clear("alice")
|
||||
MoodSessionTracker.clear("bob")
|
||||
}
|
||||
|
||||
private fun credentials(user: String, password: String): String {
|
||||
val salt = "test-salt"
|
||||
return "u=$user&t=${SubsonicAuthenticator.md5Hex(password + salt)}&s=$salt"
|
||||
}
|
||||
|
||||
private fun parseBody(body: String): JsonObject =
|
||||
Json.parseToJsonElement(body).jsonObject["subsonic-response"]!!.jsonObject
|
||||
|
||||
private fun JsonObject.string(name: String): String =
|
||||
this[name]!!.jsonPrimitive.content
|
||||
|
||||
private fun JsonObject.int(name: String): Int =
|
||||
this[name]!!.jsonPrimitive.int
|
||||
|
||||
private fun JsonObject.ids(name: String): List<String> =
|
||||
this[name]!!.jsonArray.map { it.jsonObject["id"]!!.jsonPrimitive.content }
|
||||
|
||||
private data class Seed(
|
||||
val db: DatabaseHandle,
|
||||
val library: LibraryConfig,
|
||||
val users: UsersConfig,
|
||||
val coverArt: CoverArtService,
|
||||
val aliceAmbient1: String,
|
||||
val aliceAmbient2: String,
|
||||
val aliceRock: String,
|
||||
val aliceAmbient3: String,
|
||||
val bobSong: String,
|
||||
val longformSong: String,
|
||||
) : AutoCloseable {
|
||||
override fun close() = db.close()
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.server.routing.routing
|
||||
import io.ktor.server.testing.ApplicationTestBuilder
|
||||
import io.ktor.server.testing.testApplication
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.int
|
||||
@@ -25,6 +27,7 @@ import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.MusicFolder
|
||||
import win.taptap.musicmanager.config.UserAccount
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.mood.MoodService
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.subsonic.routes.subsonicRoutes
|
||||
@@ -180,6 +183,7 @@ class SystemRoutesTest {
|
||||
),
|
||||
coverArtService = CoverArtService(tempDir.resolve("data")),
|
||||
transcoder = FfmpegTranscoder("ffmpeg"),
|
||||
moodService = MoodService(CoroutineScope(Dispatchers.Unconfined)),
|
||||
startScan = startScan,
|
||||
scanStatus = scanStatus,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user