navidrome/server/subsonic/media_annotation.go

142 lines
4.1 KiB
Go
Raw Normal View History

package subsonic
import (
"net/http"
"time"
2017-04-01 15:47:14 +02:00
"github.com/cloudsonic/sonic-server/engine"
2020-01-09 02:45:07 +01:00
"github.com/cloudsonic/sonic-server/log"
2020-01-15 04:22:34 +01:00
"github.com/cloudsonic/sonic-server/model"
"github.com/cloudsonic/sonic-server/server/subsonic/responses"
)
type MediaAnnotationController struct {
scrobbler engine.Scrobbler
2016-03-23 22:46:54 +01:00
ratings engine.Ratings
}
func NewMediaAnnotationController(scrobbler engine.Scrobbler, ratings engine.Ratings) *MediaAnnotationController {
return &MediaAnnotationController{
scrobbler: scrobbler,
ratings: ratings,
}
2016-03-23 22:46:54 +01:00
}
func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id, err := RequiredParamString(r, "id", "Required id parameter is missing")
if err != nil {
return nil, err
}
rating, err := RequiredParamInt(r, "rating", "Required rating parameter is missing")
if err != nil {
return nil, err
}
2016-03-24 00:37:48 +01:00
2020-01-09 02:45:07 +01:00
log.Debug(r, "Setting rating", "rating", rating, "id", id)
err = c.ratings.SetRating(r.Context(), id, rating)
2016-03-24 00:37:48 +01:00
switch {
2020-01-15 04:22:34 +01:00
case err == model.ErrNotFound:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, NewError(responses.ErrorDataNotFound, "ID not found")
2016-03-24 00:37:48 +01:00
case err != nil:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
2016-03-24 00:37:48 +01:00
}
2020-01-11 23:18:20 +01:00
return NewResponse(), nil
2016-03-24 00:37:48 +01:00
}
func (c *MediaAnnotationController) getIds(r *http.Request) ([]string, error) {
ids := ParamStrings(r, "id")
2020-01-09 02:45:07 +01:00
albumIds := ParamStrings(r, "albumId")
artistIds := ParamStrings(r, "artistId")
2016-03-23 22:46:54 +01:00
if len(ids)+len(albumIds)+len(artistIds) == 0 {
return nil, NewError(responses.ErrorMissingParameter, "Required id parameter is missing")
}
ids = append(ids, albumIds...)
ids = append(ids, artistIds...)
return ids, nil
}
func (c *MediaAnnotationController) Star(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids, err := c.getIds(r)
if err != nil {
return nil, err
}
2020-01-09 02:45:07 +01:00
log.Debug(r, "Starring items", "ids", ids)
err = c.ratings.SetStar(r.Context(), true, ids...)
2016-03-23 22:46:54 +01:00
switch {
2020-01-15 04:22:34 +01:00
case err == model.ErrNotFound:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, NewError(responses.ErrorDataNotFound, "ID not found")
2016-03-23 22:46:54 +01:00
case err != nil:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
2016-03-23 22:46:54 +01:00
}
2020-01-11 23:18:20 +01:00
return NewResponse(), nil
2016-03-23 22:46:54 +01:00
}
func (c *MediaAnnotationController) Unstar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids, err := c.getIds(r)
if err != nil {
return nil, err
}
2020-01-09 02:45:07 +01:00
log.Debug(r, "Unstarring items", "ids", ids)
err = c.ratings.SetStar(r.Context(), false, ids...)
2016-03-23 22:46:54 +01:00
switch {
2020-01-15 04:22:34 +01:00
case err == model.ErrNotFound:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, NewError(responses.ErrorDataNotFound, "Directory not found")
2016-03-23 22:46:54 +01:00
case err != nil:
2020-01-09 02:45:07 +01:00
log.Error(r, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
2016-03-23 22:46:54 +01:00
}
2020-01-11 23:18:20 +01:00
return NewResponse(), nil
}
func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
ids, err := RequiredParamStrings(r, "id", "Required id parameter is missing")
if err != nil {
return nil, err
}
times := ParamTimes(r, "time")
2016-03-22 00:35:40 +01:00
if len(times) > 0 && len(times) != len(ids) {
return nil, NewError(responses.ErrorGeneric, "Wrong number of timestamps: %d, should be %d", len(times), len(ids))
2016-03-22 00:35:40 +01:00
}
submission := ParamBool(r, "submission", true)
2016-03-17 23:58:09 +01:00
playerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)
playerName := ParamString(r, "c")
username := ParamString(r, "u")
2020-01-09 02:45:07 +01:00
log.Debug(r, "Scrobbling tracks", "ids", ids, "times", times, "submission", submission)
2016-03-22 15:28:13 +01:00
for i, id := range ids {
2016-03-22 00:35:40 +01:00
var t time.Time
if len(times) > 0 {
t = times[i]
} else {
t = time.Now()
}
2016-03-22 00:35:40 +01:00
if submission {
2020-01-09 02:45:07 +01:00
mf, err := c.scrobbler.Register(r.Context(), playerId, id, t)
2016-03-22 00:35:40 +01:00
if err != nil {
2020-01-09 02:45:07 +01:00
log.Error(r, "Error scrobbling track", "id", id, err)
continue
2016-03-22 00:35:40 +01:00
}
2020-01-09 02:45:07 +01:00
log.Info(r, "Scrobbled", "id", id, "title", mf.Title, "timestamp", t)
2016-03-22 00:35:40 +01:00
} else {
2020-01-09 02:45:07 +01:00
mf, err := c.scrobbler.NowPlaying(r.Context(), playerId, playerName, id, username)
2016-03-22 00:35:40 +01:00
if err != nil {
2020-01-09 02:45:07 +01:00
log.Error(r, "Error setting current song", "id", id, err)
continue
2016-03-22 00:35:40 +01:00
}
2020-01-09 02:45:07 +01:00
log.Info(r, "Now Playing", "id", id, "title", mf.Title, "timestamp", t)
2016-03-17 02:04:41 +01:00
}
}
2020-01-11 23:18:20 +01:00
return NewResponse(), nil
}