add init search2

This commit is contained in:
sentriz
2019-05-28 16:12:55 +01:00
parent e7ed19abd9
commit 8429c980f9
5 changed files with 114 additions and 48 deletions

View File

@@ -6,13 +6,16 @@ import (
) )
func makeChildFromFolder(f *model.Folder, parent *model.Folder) *subsonic.Child { func makeChildFromFolder(f *model.Folder, parent *model.Folder) *subsonic.Child {
return &subsonic.Child{ child := &subsonic.Child{
ID: f.ID, ID: f.ID,
Title: f.Name, Title: f.Name,
CoverID: f.CoverID, CoverID: f.CoverID,
ParentID: parent.ID,
IsDir: true, IsDir: true,
} }
if parent != nil {
child.ParentID = parent.ID
}
return child
} }
func makeChildFromTrack(t *model.Track, parent *model.Folder) *subsonic.Child { func makeChildFromTrack(t *model.Track, parent *model.Folder) *subsonic.Child {

View File

@@ -1,6 +1,7 @@
package handler package handler
import ( import (
"fmt"
"net/http" "net/http"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@@ -9,11 +10,15 @@ import (
"github.com/sentriz/gonic/server/subsonic" "github.com/sentriz/gonic/server/subsonic"
) )
// the subsonic spec metions "artist" a lot when talking about the
// browse by folder endpoints. but since we're not browsing by tag
// we can't access artists. so instead we'll consider the artist of
// an track to be the it's respective folder that comes directly
// under the root directory
func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) { func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
// we are browsing by folder, but the subsonic docs show sub <artist> elements
// for this, so we're going to return root directories as "artists"
var folders []model.Folder var folders []model.Folder
c.DB.Where("parent_id = ?", 1).Find(&folders) c.DB.Where("parent_id = 1").Find(&folders)
var indexMap = make(map[rune]*subsonic.Index) var indexMap = make(map[rune]*subsonic.Index)
var indexes []*subsonic.Index var indexes []*subsonic.Index
for _, folder := range folders { for _, folder := range folders {
@@ -97,14 +102,11 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
q := c.DB q := c.DB
switch listType { switch listType {
case "alphabeticalByArtist": case "alphabeticalByArtist":
// not sure what it meant by "artist" since we're browsing by folder
// - so we'll consider the parent folder's name to be the "artist"
q = q.Joins(` q = q.Joins(`
JOIN folders AS parent_folders JOIN folders AS parent_folders
ON folders.parent_id = parent_folders.id`) ON folders.parent_id = parent_folders.id`)
q = q.Order("parent_folders.name") q = q.Order("parent_folders.name")
case "alphabeticalByName": case "alphabeticalByName":
// not sure about "name" either, so lets use the folder's name
q = q.Order("name") q = q.Order("name")
case "frequent": case "frequent":
user := r.Context().Value(contextUserKey).(*model.User) user := r.Context().Value(contextUserKey).(*model.User)
@@ -144,3 +146,55 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
} }
respond(w, r, sub) respond(w, r, sub)
} }
func (c *Controller) SearchTwo(w http.ResponseWriter, r *http.Request) {
query := getStrParam(r, "query")
if query == "" {
respondError(w, r, 10, "please provide a `query` parameter")
return
}
query = fmt.Sprintf("%%%s%%", query)
results := &subsonic.SearchResultTwo{}
//
// search "artists"
var artists []model.Folder
c.DB.
Where("parent_id = 1 AND name LIKE ?", query).
Offset(getIntParamOr(r, "artistOffset", 0)).
Limit(getIntParamOr(r, "artistCount", 20)).
Find(&artists)
for _, a := range artists {
results.Artists = append(results.Artists,
makeChildFromFolder(&a, nil))
}
//
// search "albums"
var albums []model.Folder
c.DB.
Preload("Parent").
Where("has_tracks = 1 AND name LIKE ?", query).
Offset(getIntParamOr(r, "albumOffset", 0)).
Limit(getIntParamOr(r, "albumCount", 20)).
Find(&albums)
for _, a := range albums {
results.Albums = append(results.Albums,
makeChildFromFolder(&a, a.Parent))
}
//
// search "artists"
var tracks []model.Track
c.DB.
Preload("Folder").
Where("title LIKE ?", query).
Offset(getIntParamOr(r, "songOffset", 0)).
Limit(getIntParamOr(r, "songCount", 20)).
Find(&tracks)
for _, t := range tracks {
results.Tracks = append(results.Tracks,
makeChildFromTrack(&t, &t.Folder))
}
//
sub := subsonic.NewResponse()
sub.SearchResultTwo = results
respond(w, r, sub)
}

View File

@@ -41,4 +41,6 @@ func (s *Server) setupSubsonic() {
s.mux.HandleFunc("/rest/getMusicDirectory.view", withWare(s.GetMusicDirectory)) s.mux.HandleFunc("/rest/getMusicDirectory.view", withWare(s.GetMusicDirectory))
s.mux.HandleFunc("/rest/getAlbumList", withWare(s.GetAlbumList)) s.mux.HandleFunc("/rest/getAlbumList", withWare(s.GetAlbumList))
s.mux.HandleFunc("/rest/getAlbumList.view", withWare(s.GetAlbumList)) s.mux.HandleFunc("/rest/getAlbumList.view", withWare(s.GetAlbumList))
s.mux.HandleFunc("/rest/search2", withWare(s.SearchTwo))
s.mux.HandleFunc("/rest/search2.view", withWare(s.SearchTwo))
} }

View File

@@ -123,3 +123,9 @@ type ScanStatus struct {
Scanning bool `xml:"scanning,attr" json:"scanning"` Scanning bool `xml:"scanning,attr" json:"scanning"`
Count int `xml:"count,attr,omitempty" json:"count,omitempty"` Count int `xml:"count,attr,omitempty" json:"count,omitempty"`
} }
type SearchResultTwo struct {
Artists []*Child `xml:"artist" json:"artist"`
Albums []*Child `xml:"album" json:"album"`
Tracks []*Child `xml:"song" json:"song"`
}

View File

@@ -31,6 +31,7 @@ type Response struct {
MusicFolders *MusicFolders `xml:"musicFolders" json:"musicFolders,omitempty"` MusicFolders *MusicFolders `xml:"musicFolders" json:"musicFolders,omitempty"`
ScanStatus *ScanStatus `xml:"scanStatus" json:"scanStatus,omitempty"` ScanStatus *ScanStatus `xml:"scanStatus" json:"scanStatus,omitempty"`
Licence *Licence `xml:"license" json:"license,omitempty"` Licence *Licence `xml:"license" json:"license,omitempty"`
SearchResultTwo *SearchResultTwo `xml:"searchResult2" json:"searchResult2,omitempty"`
} }
type Error struct { type Error struct {