support subsonic bookmarks
This commit is contained in:
79
server/ctrlsubsonic/handlers_bookmark.go
Normal file
79
server/ctrlsubsonic/handlers_bookmark.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package ctrlsubsonic
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||
"go.senan.xyz/gonic/server/db"
|
||||
)
|
||||
|
||||
func (c *Controller) ServeGetBookmarks(r *http.Request) *spec.Response {
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
bookmarks := []*db.Bookmark{}
|
||||
err := c.DB.
|
||||
Where("user_id=?", user.ID).
|
||||
Find(&bookmarks).
|
||||
Error
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
return spec.NewResponse()
|
||||
}
|
||||
sub := spec.NewResponse()
|
||||
sub.Bookmarks = &spec.Bookmarks{
|
||||
List: []*spec.Bookmark{},
|
||||
}
|
||||
for _, bookmark := range bookmarks {
|
||||
specid := &specid.ID{
|
||||
Type: specid.IDT(bookmark.EntryIDType),
|
||||
Value: bookmark.EntryID,
|
||||
}
|
||||
entries := []*spec.BookmarkEntry{{
|
||||
ID: specid,
|
||||
Type: bookmark.EntryIDType,
|
||||
}}
|
||||
sub.Bookmarks.List = append(sub.Bookmarks.List, &spec.Bookmark{
|
||||
Username: user.Name,
|
||||
Position: bookmark.Position,
|
||||
Comment: bookmark.Comment,
|
||||
Created: bookmark.CreatedAt,
|
||||
Changed: bookmark.UpdatedAt,
|
||||
Entries: entries,
|
||||
})
|
||||
}
|
||||
return sub
|
||||
}
|
||||
|
||||
func (c *Controller) ServeCreateBookmark(r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
id, err := params.GetID("id")
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` parameter")
|
||||
}
|
||||
bookmark := &db.Bookmark{}
|
||||
c.DB.FirstOrCreate(bookmark, db.Bookmark{
|
||||
UserID: user.ID,
|
||||
EntryIDType: string(id.Type),
|
||||
EntryID: id.Value,
|
||||
})
|
||||
bookmark.Comment = params.GetOr("comment", "")
|
||||
bookmark.Position = params.GetOrInt("position", 0)
|
||||
c.DB.Save(bookmark)
|
||||
return spec.NewResponse()
|
||||
}
|
||||
|
||||
func (c *Controller) ServeDeleteBookmark(r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
id, err := params.GetID("id")
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` parameter")
|
||||
}
|
||||
c.DB.
|
||||
Where("user_id=? AND entry_id_type=? AND entry_id=?", user.ID, id.Type, id.Value).
|
||||
Delete(&db.Bookmark{})
|
||||
return spec.NewResponse()
|
||||
}
|
||||
@@ -153,9 +153,12 @@ func (c *Controller) ServeSavePlayQueue(r *http.Request) *spec.Response {
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide some `id` parameters")
|
||||
}
|
||||
// TODO: support other play queue entries other than tracks
|
||||
trackIDs := make([]int, 0, len(tracks))
|
||||
for _, id := range tracks {
|
||||
trackIDs = append(trackIDs, id.Value)
|
||||
if id.Type == specid.Track {
|
||||
trackIDs = append(trackIDs, id.Value)
|
||||
}
|
||||
}
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
queue := &db.PlayQueue{UserID: user.ID}
|
||||
|
||||
@@ -44,7 +44,8 @@ type Response struct {
|
||||
PlayQueue *PlayQueue `xml:"playQueue" json:"playQueue,omitempty"`
|
||||
JukeboxStatus *JukeboxStatus `xml:"jukeboxStatus" json:"jukeboxStatus,omitempty"`
|
||||
JukeboxPlaylist *JukeboxPlaylist `xml:"jukeboxPlaylist" json:"jukeboxPlaylist,omitempty"`
|
||||
Podcasts *Podcasts `xml:"podcasts" json:"podcasts,omitempty"`
|
||||
Podcasts *Podcasts `xml:"podcasts" json:"podcasts,omitempty"`
|
||||
Bookmarks *Bookmarks `xml:"bookmarks" json:"bookmarks,omitempty"`
|
||||
}
|
||||
|
||||
func NewResponse() *Response {
|
||||
@@ -120,6 +121,7 @@ type TracksByGenre struct {
|
||||
}
|
||||
|
||||
type TrackChild struct {
|
||||
ID *specid.ID `xml:"id,attr,omitempty" json:"id,omitempty"`
|
||||
Album string `xml:"album,attr,omitempty" json:"album,omitempty"`
|
||||
AlbumID *specid.ID `xml:"albumId,attr,omitempty" json:"albumId,omitempty"`
|
||||
Artist string `xml:"artist,attr,omitempty" json:"artist,omitempty"`
|
||||
@@ -130,7 +132,6 @@ type TrackChild struct {
|
||||
CreatedAt time.Time `xml:"created,attr,omitempty" json:"created,omitempty"`
|
||||
Duration int `xml:"duration,attr,omitempty" json:"duration,omitempty"`
|
||||
Genre string `xml:"genre,attr,omitempty" json:"genre,omitempty"`
|
||||
ID *specid.ID `xml:"id,attr,omitempty" json:"id,omitempty"`
|
||||
IsDir bool `xml:"isDir,attr" json:"isDir"`
|
||||
IsVideo bool `xml:"isVideo,attr" json:"isVideo"`
|
||||
ParentID *specid.ID `xml:"parent,attr,omitempty" json:"parent,omitempty"`
|
||||
@@ -292,33 +293,51 @@ type Podcasts struct {
|
||||
}
|
||||
|
||||
type PodcastChannel struct {
|
||||
ID *specid.ID `xml:"id,attr" json:"id"`
|
||||
URL string `xml:"url,attr" json:"url"`
|
||||
Title string `xml:"title,attr" json:"title"`
|
||||
Description string `xml:"description,attr" json:"description"`
|
||||
CoverArt *specid.ID `xml:"coverArt,attr" json:"coverArt,omitempty"`
|
||||
OriginalImageURL string `xml:"originalImageUrl,attr" json:"originalImageUrl,omitempty"`
|
||||
Status string `xml:"status,attr" json:"status"`
|
||||
Episode []*PodcastEpisode `xml:"episode" json:"episode,omitempty"`
|
||||
ID *specid.ID `xml:"id,attr" json:"id"`
|
||||
URL string `xml:"url,attr" json:"url"`
|
||||
Title string `xml:"title,attr" json:"title"`
|
||||
Description string `xml:"description,attr" json:"description"`
|
||||
CoverArt *specid.ID `xml:"coverArt,attr" json:"coverArt,omitempty"`
|
||||
OriginalImageURL string `xml:"originalImageUrl,attr" json:"originalImageUrl,omitempty"`
|
||||
Status string `xml:"status,attr" json:"status"`
|
||||
Episode []*PodcastEpisode `xml:"episode" json:"episode,omitempty"`
|
||||
}
|
||||
|
||||
type PodcastEpisode struct {
|
||||
ID *specid.ID `xml:"id,attr" json:"id"`
|
||||
StreamID *specid.ID `xml:"streamId,attr" json:"streamId"`
|
||||
ChannelID *specid.ID `xml:"channelId,attr" json:"channelId"`
|
||||
Title string `xml:"title,attr" json:"title"`
|
||||
Description string `xml:"description,attr" json:"description"`
|
||||
PublishDate time.Time `xml:"publishDate,attr" json:"publishDate"`
|
||||
Status string `xml:"status,attr" json:"status"`
|
||||
Parent string `xml:"parent,attr" json:"parent"`
|
||||
IsDir bool `xml:"isDir,attr" json:"isDir"`
|
||||
Year int `xml:"year,attr" json:"year"`
|
||||
Genre string `xml:"genre,attr" json:"genre"`
|
||||
CoverArt *specid.ID `xml:"coverArt,attr" json:"coverArt"`
|
||||
Size int `xml:"size,attr" json:"size"`
|
||||
ContentType string `xml:"contentType,attr" json:"contentType"`
|
||||
Suffix string `xml:"suffix,attr" json:"suffix"`
|
||||
Duration int `xml:"duration,attr" json:"duration"`
|
||||
BitRate int `xml:"bitRate,attr" json:"bitrate"`
|
||||
Path string `xml:"path,attr" json:"path"`
|
||||
ID *specid.ID `xml:"id,attr" json:"id"`
|
||||
StreamID *specid.ID `xml:"streamId,attr" json:"streamId"`
|
||||
ChannelID *specid.ID `xml:"channelId,attr" json:"channelId"`
|
||||
Title string `xml:"title,attr" json:"title"`
|
||||
Description string `xml:"description,attr" json:"description"`
|
||||
PublishDate time.Time `xml:"publishDate,attr" json:"publishDate"`
|
||||
Status string `xml:"status,attr" json:"status"`
|
||||
Parent string `xml:"parent,attr" json:"parent"`
|
||||
IsDir bool `xml:"isDir,attr" json:"isDir"`
|
||||
Year int `xml:"year,attr" json:"year"`
|
||||
Genre string `xml:"genre,attr" json:"genre"`
|
||||
CoverArt *specid.ID `xml:"coverArt,attr" json:"coverArt"`
|
||||
Size int `xml:"size,attr" json:"size"`
|
||||
ContentType string `xml:"contentType,attr" json:"contentType"`
|
||||
Suffix string `xml:"suffix,attr" json:"suffix"`
|
||||
Duration int `xml:"duration,attr" json:"duration"`
|
||||
BitRate int `xml:"bitRate,attr" json:"bitrate"`
|
||||
Path string `xml:"path,attr" json:"path"`
|
||||
}
|
||||
|
||||
type Bookmarks struct {
|
||||
List []*Bookmark `xml:"bookmark" json:"bookmark"`
|
||||
}
|
||||
|
||||
type Bookmark struct {
|
||||
Entries []*BookmarkEntry `xml:"entry,omitempty" json:"entry,omitempty"`
|
||||
Username string `xml:"username,attr" json:"username"`
|
||||
Position int `xml:"position,attr" json:"position"`
|
||||
Comment string `xml:"comment,attr" json:"comment"`
|
||||
Created time.Time `xml:"created,attr" json:"created"`
|
||||
Changed time.Time `xml:"changed,attr" json:"changed"`
|
||||
}
|
||||
|
||||
type BookmarkEntry struct {
|
||||
ID *specid.ID `xml:"id,attr" json:"id"`
|
||||
Type string `xml:"type,attr" json:"type"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user