navidrome/api/media_annotation.go

140 lines
3.9 KiB
Go
Raw Normal View History

package api
import (
2016-03-15 22:06:23 +01:00
"fmt"
"net/http"
"time"
"github.com/astaxie/beego"
2017-04-01 15:47:14 +02:00
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
)
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
beego.Debug("Setting rating", rating, "for id", id)
err = c.ratings.SetRating(id, rating)
2016-03-24 00:37:48 +01:00
switch {
case err == domain.ErrNotFound:
beego.Error(err)
return nil, NewError(responses.ErrorDataNotFound, "Id not found")
2016-03-24 00:37:48 +01:00
case err != nil:
beego.Error(err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
2016-03-24 00:37:48 +01:00
}
return NewEmpty(), nil
2016-03-24 00:37:48 +01:00
}
func (c *MediaAnnotationController) getIds(r *http.Request) ([]string, error) {
ids := ParamStrings(r, "id")
albumIds := ParamStrings(r,"albumId")
2016-03-23 22:46:54 +01:00
if len(ids) == 0 && len(albumIds) == 0 {
return nil, NewError(responses.ErrorMissingParameter, "Required id parameter is missing")
}
return append(ids, albumIds...), 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
}
2016-03-23 22:46:54 +01:00
beego.Debug("Starring ids:", ids)
err = c.ratings.SetStar(true, ids...)
2016-03-23 22:46:54 +01:00
switch {
case err == domain.ErrNotFound:
2016-03-23 22:46:54 +01:00
beego.Error(err)
return nil, NewError(responses.ErrorDataNotFound, "Id not found")
2016-03-23 22:46:54 +01:00
case err != nil:
beego.Error(err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
2016-03-23 22:46:54 +01:00
}
return NewEmpty(), 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
}
2016-03-23 22:46:54 +01:00
beego.Debug("Unstarring ids:", ids)
err = c.ratings.SetStar(false, ids...)
2016-03-23 22:46:54 +01:00
switch {
case err == domain.ErrNotFound:
2016-03-23 22:46:54 +01:00
beego.Error(err)
return nil, NewError(responses.ErrorDataNotFound, "Directory not found")
2016-03-23 22:46:54 +01:00
case err != nil:
beego.Error(err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
2016-03-23 22:46:54 +01:00
}
return NewEmpty(), 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")
2016-03-22 00:51:22 +01:00
beego.Debug("Scrobbling 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 {
2016-03-22 15:28:13 +01:00
mf, err := c.scrobbler.Register(playerId, id, t)
2016-03-22 00:35:40 +01:00
if err != nil {
2016-03-22 15:28:13 +01:00
beego.Error("Error scrobbling", id, "-", err)
continue
2016-03-22 00:35:40 +01:00
}
2016-03-22 15:28:13 +01:00
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, id, mf.Title, t))
2016-03-22 00:35:40 +01:00
} else {
mf, err := c.scrobbler.NowPlaying(playerId, playerName, id, username)
2016-03-22 00:35:40 +01:00
if err != nil {
2016-03-22 15:28:13 +01:00
beego.Error("Error setting", id, "as current song:", err)
continue
2016-03-22 00:35:40 +01:00
}
2016-03-22 15:28:13 +01:00
beego.Info(fmt.Sprintf(`Now Playing (%s) "%s" at %v`, id, mf.Title, t))
2016-03-17 02:04:41 +01:00
}
}
return NewEmpty(), nil
}