package subsonic import ( "encoding/json" "git.noroutes.net/NoRoutes/vibetunes/internal/database" ) func init() { register("getPlayQueue", handleGetPlayQueue) register("savePlayQueue", handleSavePlayQueue) } func handleGetPlayQueue(rc *Context) *apiError { db := rc.Server.DB q, err := database.GetPlayQueue(db, rc.User.ID) if err == database.ErrNotFound { rc.Response = newResponse() return nil } if err != nil { return newError(ErrGeneric, err.Error()) } var ids []string if uerr := json.Unmarshal([]byte(q.SongIDsJSON), &ids); uerr != nil { return newError(ErrGeneric, "corrupt saved play queue") } out := &PlayQueue{ Position: int64(q.PositionMs), Username: rc.User.Username, Changed: isoTime(q.UpdatedAt), ChangedBy: q.Client, } for i, id := range ids { song, serr := database.GetSong(db, id) if serr != nil { continue } mf, merr := database.GetMediaFile(db, song.MediaFileID) if merr != nil { continue } out.Entry = append(out.Entry, childFromSong(db, song, mf, albumOfSong(db, song), rc.User.ID)) if i == q.CurrentIndex { out.Current = song.ID } } rc.Response = newResponse() rc.Response.PlayQueue = out return nil } func handleSavePlayQueue(rc *Context) *apiError { ids := rc.Params.GetAll("id") current := rc.Params.Get("current") position := rc.Params.Int("position", 0) client := rc.Params.Get("c") currentIndex := 0 for i, id := range ids { if id == current { currentIndex = i break } } encoded, err := json.Marshal(ids) if err != nil { return newError(ErrGeneric, err.Error()) } if err := database.SavePlayQueue(rc.Server.DB, rc.User.ID, string(encoded), currentIndex, position, client); err != nil { return newError(ErrGeneric, err.Error()) } rc.Response = newResponse() return nil }