feat: mark search results that are already in the target mood
This commit is contained in:
1 parent
0613b8aedc
commit
0caeecb325
3 files changed
+13
-2
No files matched your search
@@ -181,6 +181,7 @@ data class SearchResultDto(
|
|||||||
val thumbnail: String? = null,
|
val thumbnail: String? = null,
|
||||||
val songId: String? = null,
|
val songId: String? = null,
|
||||||
val downloaded: Boolean = false,
|
val downloaded: Boolean = false,
|
||||||
|
val inMood: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import win.taptap.musicmanager.config.UsersConfig
|
import win.taptap.musicmanager.config.UsersConfig
|
||||||
|
import win.taptap.musicmanager.db.MoodRepository
|
||||||
import win.taptap.musicmanager.db.SongRecord
|
import win.taptap.musicmanager.db.SongRecord
|
||||||
import win.taptap.musicmanager.db.SongRepository
|
import win.taptap.musicmanager.db.SongRepository
|
||||||
import win.taptap.musicmanager.discovery.YtSearch
|
import win.taptap.musicmanager.discovery.YtSearch
|
||||||
@@ -38,13 +39,18 @@ fun Route.songRoutes(
|
|||||||
transcoder: FfmpegTranscoder,
|
transcoder: FfmpegTranscoder,
|
||||||
) {
|
) {
|
||||||
get("/search") {
|
get("/search") {
|
||||||
call.requireApiUser(users)
|
val user = call.requireApiUser(users)
|
||||||
val query = call.request.queryParameters["q"]?.trim().orEmpty()
|
val query = call.request.queryParameters["q"]?.trim().orEmpty()
|
||||||
if (query.isEmpty()) {
|
if (query.isEmpty()) {
|
||||||
call.respond(SearchResponse())
|
call.respond(SearchResponse())
|
||||||
return@get
|
return@get
|
||||||
}
|
}
|
||||||
val limit = (call.request.queryParameters["limit"]?.toIntOrNull() ?: 20).coerceIn(1, 25)
|
val limit = (call.request.queryParameters["limit"]?.toIntOrNull() ?: 20).coerceIn(1, 25)
|
||||||
|
val moodId = call.request.queryParameters["moodId"]?.takeIf { it.isNotBlank() }
|
||||||
|
val moodVideos = moodId
|
||||||
|
?.takeIf { MoodRepository.get(it, user.name) != null }
|
||||||
|
?.let(MoodRepository::videoIdsInMood)
|
||||||
|
.orEmpty()
|
||||||
|
|
||||||
val results = withContext(Dispatchers.IO) {
|
val results = withContext(Dispatchers.IO) {
|
||||||
runCatching { search.search(query, limit) }
|
runCatching { search.search(query, limit) }
|
||||||
@@ -73,6 +79,7 @@ fun Route.songRoutes(
|
|||||||
thumbnail = candidate.thumbnail,
|
thumbnail = candidate.thumbnail,
|
||||||
songId = song?.takeIf { downloaded }?.id,
|
songId = song?.takeIf { downloaded }?.id,
|
||||||
downloaded = downloaded,
|
downloaded = downloaded,
|
||||||
|
inMood = candidate.videoId in moodVideos,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -292,9 +292,11 @@ class ApiRoutesTest {
|
|||||||
testApplication {
|
testApplication {
|
||||||
setupApp(seed, search = FakeYt(results))
|
setupApp(seed, search = FakeYt(results))
|
||||||
val token = client.token("alice", "alice-pw")
|
val token = client.token("alice", "alice-pw")
|
||||||
|
val moodId = client.createMood(token, """{"name":"Search Mood","recommendationCount":0}""")
|
||||||
|
client.addSong(token, moodId, seed.ambient1)
|
||||||
|
|
||||||
val body = parse(
|
val body = parse(
|
||||||
client.get("/api/v1/search?q=ambient") { bearer(token) }.bodyAsText(),
|
client.get("/api/v1/search?q=ambient&moodId=$moodId") { bearer(token) }.bodyAsText(),
|
||||||
)
|
)
|
||||||
val found = body["results"]!!.jsonArray.map { it.jsonObject }
|
val found = body["results"]!!.jsonArray.map { it.jsonObject }
|
||||||
assertEquals(2, found.size)
|
assertEquals(2, found.size)
|
||||||
@@ -303,6 +305,7 @@ class ApiRoutesTest {
|
|||||||
|
|
||||||
val downloaded = found.first { it.string("videoId") == "vid-ambient-1" }
|
val downloaded = found.first { it.string("videoId") == "vid-ambient-1" }
|
||||||
assertTrue(downloaded["downloaded"]!!.jsonPrimitive.content.toBoolean())
|
assertTrue(downloaded["downloaded"]!!.jsonPrimitive.content.toBoolean())
|
||||||
|
assertTrue(downloaded["inMood"]!!.jsonPrimitive.content.toBoolean())
|
||||||
assertEquals(seed.ambient1, downloaded.string("songId"))
|
assertEquals(seed.ambient1, downloaded.string("songId"))
|
||||||
assertFalse(
|
assertFalse(
|
||||||
found.first { it.string("videoId") == "vid-new" }["downloaded"]!!
|
found.first { it.string("videoId") == "vid-new" }["downloaded"]!!
|
||||||
|
|||||||
Reference in new issue
Block a user