Subsonic: album/song lists (getAlbumList/2, getRandomSongs, getSongsByGenre, getNowPlaying, getStarred/2) and search (search, search2, search3)

This commit is contained in:
2026-08-23 00:08:06 -05:00
parent f40a011c46
commit a297454065
6 changed files with 742 additions and 0 deletions
+49
View File
@@ -89,3 +89,52 @@ func SimilarSongsByArtist(db *sql.DB, artistID string, limit int) ([]*Song, erro
defer rows.Close()
return scanSongs(rows)
}
// AlbumsByHighestRating orders albums by their average user rating
// (unrated albums last).
func AlbumsByHighestRating(db *sql.DB, limit, offset int) ([]*Album, error) {
rows, err := db.Query(`SELECT al.id, al.title, al.sort_title, al.year, al.original_year, al.record_label, al.musicbrainz_id, al.primary_album_artist_id, al.cover_art_id, al.created_at
FROM albums al
LEFT JOIN (SELECT item_id, AVG(rating) AS avg_rating FROM ratings WHERE item_type='album' GROUP BY item_id) r ON r.item_id = al.id
ORDER BY COALESCE(r.avg_rating, 0) DESC, al.sort_title COLLATE NOCASE
LIMIT ? OFFSET ?`, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
return scanAlbums(rows)
}
// AlbumsByPlayCount orders albums by total play count across all users
// (an album with all its songs never played sorts last).
func AlbumsByPlayCount(db *sql.DB, limit, offset int) ([]*Album, error) {
rows, err := db.Query(`SELECT al.id, al.title, al.sort_title, al.year, al.original_year, al.record_label, al.musicbrainz_id, al.primary_album_artist_id, al.cover_art_id, al.created_at
FROM albums al
LEFT JOIN songs s ON s.album_id = al.id
LEFT JOIN playback_history ph ON ph.song_id = s.id
GROUP BY al.id
ORDER BY COUNT(ph.id) DESC, al.sort_title COLLATE NOCASE
LIMIT ? OFFSET ?`, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
return scanAlbums(rows)
}
// AlbumsByRecentlyPlayed orders albums by the most recent play of any of
// their songs by the given user, most recent first.
func AlbumsByRecentlyPlayed(db *sql.DB, userID string, limit, offset int) ([]*Album, error) {
rows, err := db.Query(`SELECT al.id, al.title, al.sort_title, al.year, al.original_year, al.record_label, al.musicbrainz_id, al.primary_album_artist_id, al.cover_art_id, al.created_at
FROM albums al
JOIN songs s ON s.album_id = al.id
JOIN playback_history ph ON ph.song_id = s.id AND ph.user_id = ?
GROUP BY al.id
ORDER BY MAX(ph.played_at) DESC
LIMIT ? OFFSET ?`, userID, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
return scanAlbums(rows)
}