Implement annotations per user
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
@@ -20,14 +21,9 @@ type album struct {
|
||||
AlbumArtist string ``
|
||||
Year int `orm:"index"`
|
||||
Compilation bool ``
|
||||
Starred bool `orm:"index"`
|
||||
PlayCount int `orm:"index"`
|
||||
PlayDate time.Time `orm:"null;index"`
|
||||
SongCount int ``
|
||||
Duration int ``
|
||||
Rating int `orm:"index"`
|
||||
Genre string `orm:"index"`
|
||||
StarredAt time.Time `orm:"index;null"`
|
||||
CreatedAt time.Time `orm:"null"`
|
||||
UpdatedAt time.Time `orm:"null"`
|
||||
}
|
||||
@@ -115,9 +111,9 @@ func (r *albumRepository) Refresh(ids ...string) error {
|
||||
o := r.ormer
|
||||
sql := fmt.Sprintf(`
|
||||
select album_id as id, album as name, f.artist, f.album_artist, f.artist_id, f.compilation, f.genre,
|
||||
max(f.year) as year, sum(f.play_count) as play_count, max(f.play_date) as play_date, sum(f.duration) as duration,
|
||||
max(f.updated_at) as updated_at, min(f.created_at) as created_at, count(*) as song_count,
|
||||
a.id as current_id, f.id as cover_art_id, f.path as cover_art_path, f.has_cover_art
|
||||
max(f.year) as year, sum(f.duration) as duration, max(f.updated_at) as updated_at,
|
||||
min(f.created_at) as created_at, count(*) as song_count, a.id as current_id, f.id as cover_art_id,
|
||||
f.path as cover_art_path, f.has_cover_art
|
||||
from media_file f left outer join album a on f.album_id = a.id
|
||||
where f.album_id in ('%s')
|
||||
group by album_id order by f.id`, strings.Join(ids, "','"))
|
||||
@@ -157,9 +153,8 @@ group by album_id order by f.id`, strings.Join(ids, "','"))
|
||||
}
|
||||
if len(toUpdate) > 0 {
|
||||
for _, al := range toUpdate {
|
||||
// Don't update Starred/Rating
|
||||
_, err := o.Update(&al, "name", "artist_id", "cover_art_path", "cover_art_id", "artist", "album_artist",
|
||||
"year", "compilation", "play_count", "play_date", "song_count", "duration", "updated_at", "created_at")
|
||||
"year", "compilation", "song_count", "duration", "updated_at", "created_at")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -174,45 +169,28 @@ func (r *albumRepository) PurgeEmpty() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetStarred(options ...model.QueryOptions) (model.Albums, error) {
|
||||
func (r *albumRepository) GetStarred(userId string, options ...model.QueryOptions) (model.Albums, error) {
|
||||
var starred []album
|
||||
_, err := r.newQuery(options...).Filter("starred", true).All(&starred)
|
||||
sq := r.newRawQuery(options...).Join("annotation").Where("annotation.item_id = " + r.tableName + ".id")
|
||||
sq = sq.Where(squirrel.Eq{"annotation.user_id": userId})
|
||||
sql, args, err := sq.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&starred)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(starred), nil
|
||||
}
|
||||
|
||||
func (r *albumRepository) SetStar(starred bool, ids ...string) error {
|
||||
if len(ids) == 0 {
|
||||
return model.ErrNotFound
|
||||
}
|
||||
var starredAt time.Time
|
||||
if starred {
|
||||
starredAt = time.Now()
|
||||
}
|
||||
_, err := r.newQuery().Filter("id__in", ids).Update(orm.Params{
|
||||
"starred": starred,
|
||||
"starred_at": starredAt,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *albumRepository) MarkAsPlayed(id string, playDate time.Time) error {
|
||||
_, err := r.newQuery().Filter("id", id).Update(orm.Params{
|
||||
"play_count": orm.ColValue(orm.ColAdd, 1),
|
||||
"play_date": playDate,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *albumRepository) Search(q string, offset int, size int) (model.Albums, error) {
|
||||
if len(q) <= 2 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var results []album
|
||||
err := r.doSearch(r.tableName, q, offset, size, &results, "rating desc", "starred desc", "play_count desc", "name")
|
||||
err := r.doSearch(r.tableName, q, offset, size, &results, "name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ var _ = Describe("AlbumRepository", func() {
|
||||
|
||||
Describe("GetStarred", func() {
|
||||
It("returns all starred records", func() {
|
||||
Expect(repo.GetStarred(model.QueryOptions{})).To(Equal(model.Albums{
|
||||
Expect(repo.GetStarred("userid", model.QueryOptions{})).To(Equal(model.Albums{
|
||||
albumRadioactivity,
|
||||
}))
|
||||
})
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type annotation struct {
|
||||
AnnotationID string `orm:"pk;column(ann_id)"`
|
||||
UserID string `orm:"column(user_id)"`
|
||||
ItemID string `orm:"column(item_id)"`
|
||||
ItemType string `orm:"column(item_type)"`
|
||||
PlayCount int `orm:"index;null"`
|
||||
PlayDate time.Time `orm:"index;null"`
|
||||
Rating int `orm:"index;null"`
|
||||
Starred bool `orm:"index"`
|
||||
StarredAt time.Time `orm:"null"`
|
||||
}
|
||||
|
||||
func (u *annotation) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
[]string{"UserID", "ItemID", "ItemType"},
|
||||
}
|
||||
}
|
||||
|
||||
type annotationRepository struct {
|
||||
sqlRepository
|
||||
}
|
||||
|
||||
func NewAnnotationRepository(o orm.Ormer) model.AnnotationRepository {
|
||||
r := &annotationRepository{}
|
||||
r.ormer = o
|
||||
r.tableName = "annotation"
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *annotationRepository) Get(userID, itemType string, itemID string) (*model.Annotation, error) {
|
||||
if userID == "" {
|
||||
return nil, model.ErrInvalidAuth
|
||||
}
|
||||
q := r.newQuery().Filter("user_id", userID).Filter("item_type", itemType).Filter("item_id", itemID)
|
||||
var ann annotation
|
||||
err := q.One(&ann)
|
||||
if err == orm.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := model.Annotation(ann)
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (r *annotationRepository) GetMap(userID, itemType string, itemID []string) (model.AnnotationMap, error) {
|
||||
if userID == "" {
|
||||
return nil, model.ErrInvalidAuth
|
||||
}
|
||||
if len(itemID) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
q := r.newQuery().Filter("user_id", userID).Filter("item_type", itemType).Filter("item_id__in", itemID)
|
||||
var res []annotation
|
||||
_, err := q.All(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := make(model.AnnotationMap)
|
||||
for _, a := range res {
|
||||
m[a.ItemID] = model.Annotation(a)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (r *annotationRepository) new(userID, itemType string, itemID string) *annotation {
|
||||
id, _ := uuid.NewRandom()
|
||||
return &annotation{
|
||||
AnnotationID: id.String(),
|
||||
UserID: userID,
|
||||
ItemID: itemID,
|
||||
ItemType: itemType,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *annotationRepository) IncPlayCount(userID, itemType string, itemID string, ts time.Time) error {
|
||||
if userID == "" {
|
||||
return model.ErrInvalidAuth
|
||||
}
|
||||
q := r.newQuery().Filter("user_id", userID).Filter("item_type", itemType).Filter("item_id", itemID)
|
||||
c, err := q.Update(orm.Params{
|
||||
"play_count": orm.ColValue(orm.ColAdd, 1),
|
||||
"play_date": ts,
|
||||
})
|
||||
if c == 0 || err == orm.ErrNoRows {
|
||||
ann := r.new(userID, itemType, itemID)
|
||||
ann.PlayCount = 1
|
||||
ann.PlayDate = ts
|
||||
_, err = r.ormer.Insert(ann)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *annotationRepository) SetStar(starred bool, userID, itemType string, ids ...string) error {
|
||||
if userID == "" {
|
||||
return model.ErrInvalidAuth
|
||||
}
|
||||
q := r.newQuery().Filter("user_id", userID).Filter("item_type", itemType).Filter("item_id__in", ids)
|
||||
var starredAt time.Time
|
||||
if starred {
|
||||
starredAt = time.Now()
|
||||
}
|
||||
c, err := q.Update(orm.Params{
|
||||
"starred": starred,
|
||||
"starred_at": starredAt,
|
||||
})
|
||||
if c == 0 || err == orm.ErrNoRows {
|
||||
for _, id := range ids {
|
||||
ann := r.new(userID, itemType, id)
|
||||
ann.Starred = starred
|
||||
ann.StarredAt = starredAt
|
||||
_, err = r.ormer.Insert(ann)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *annotationRepository) SetRating(rating int, userID, itemType string, itemID string) error {
|
||||
if userID == "" {
|
||||
return model.ErrInvalidAuth
|
||||
}
|
||||
q := r.newQuery().Filter("user_id", userID).Filter("item_type", itemType).Filter("item_id", itemID)
|
||||
c, err := q.Update(orm.Params{
|
||||
"rating": rating,
|
||||
})
|
||||
if c == 0 || err == orm.ErrNoRows {
|
||||
ann := r.new(userID, itemType, itemID)
|
||||
ann.Rating = rating
|
||||
_, err = r.ormer.Insert(ann)
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (r *annotationRepository) Delete(userID, itemType string, itemID ...string) error {
|
||||
q := r.newQuery().Filter("user_id", userID).Filter("item_type", itemType).Filter("item_id__in", itemID)
|
||||
_, err := q.Delete()
|
||||
return err
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
@@ -14,11 +15,9 @@ import (
|
||||
)
|
||||
|
||||
type artist struct {
|
||||
ID string `orm:"pk;column(id)"`
|
||||
Name string `orm:"index"`
|
||||
AlbumCount int `orm:"column(album_count)"`
|
||||
Starred bool `orm:"index"`
|
||||
StarredAt time.Time `orm:"index;null"`
|
||||
ID string `orm:"pk;column(id)"`
|
||||
Name string `orm:"index"`
|
||||
AlbumCount int `orm:"column(album_count)"`
|
||||
}
|
||||
|
||||
type artistRepository struct {
|
||||
@@ -155,9 +154,15 @@ where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(id
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *artistRepository) GetStarred(options ...model.QueryOptions) (model.Artists, error) {
|
||||
func (r *artistRepository) GetStarred(userId string, options ...model.QueryOptions) (model.Artists, error) {
|
||||
var starred []artist
|
||||
_, err := r.newQuery(options...).Filter("starred", true).All(&starred)
|
||||
sq := r.newRawQuery(options...).Join("annotation").Where("annotation.item_id = " + r.tableName + ".id")
|
||||
sq = sq.Where(squirrel.Eq{"annotation.user_id": userId})
|
||||
sql, args, err := sq.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&starred)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
)
|
||||
@@ -28,11 +29,6 @@ type mediaFile struct {
|
||||
BitRate int ``
|
||||
Genre string `orm:"index"`
|
||||
Compilation bool ``
|
||||
PlayCount int `orm:"index"`
|
||||
PlayDate time.Time `orm:"null"`
|
||||
Rating int `orm:"index"`
|
||||
Starred bool `orm:"index"`
|
||||
StarredAt time.Time `orm:"index;null"`
|
||||
CreatedAt time.Time `orm:"null"`
|
||||
UpdatedAt time.Time `orm:"null"`
|
||||
}
|
||||
@@ -51,6 +47,7 @@ func NewMediaFileRepository(o orm.Ormer) model.MediaFileRepository {
|
||||
func (r *mediaFileRepository) Put(m *model.MediaFile) error {
|
||||
tm := mediaFile(*m)
|
||||
// Don't update media annotation fields (playcount, starred, etc..)
|
||||
// TODO Validate if this is still necessary, now that we don't have annotations in the mediafile model
|
||||
return r.put(m.ID, m.Title, &tm, "path", "title", "album", "artist", "artist_id", "album_artist",
|
||||
"album_id", "has_cover_art", "track_number", "disc_number", "year", "size", "suffix", "duration",
|
||||
"bit_rate", "genre", "compilation", "updated_at")
|
||||
@@ -144,53 +141,28 @@ func (r *mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.Me
|
||||
return r.toMediaFiles(results), err
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
func (r *mediaFileRepository) GetStarred(userId string, options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
var starred []mediaFile
|
||||
_, err := r.newQuery(options...).Filter("starred", true).All(&starred)
|
||||
sq := r.newRawQuery(options...).Join("annotation").Where("annotation.item_id = " + r.tableName + ".id")
|
||||
sq = sq.Where(squirrel.Eq{"annotation.user_id": userId})
|
||||
sql, args, err := sq.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(&starred)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toMediaFiles(starred), nil
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) SetStar(starred bool, ids ...string) error {
|
||||
if len(ids) == 0 {
|
||||
return model.ErrNotFound
|
||||
}
|
||||
var starredAt time.Time
|
||||
if starred {
|
||||
starredAt = time.Now()
|
||||
}
|
||||
_, err := r.newQuery().Filter("id__in", ids).Update(orm.Params{
|
||||
"starred": starred,
|
||||
"starred_at": starredAt,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) SetRating(rating int, ids ...string) error {
|
||||
if len(ids) == 0 {
|
||||
return model.ErrNotFound
|
||||
}
|
||||
_, err := r.newQuery().Filter("id__in", ids).Update(orm.Params{"rating": rating})
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) MarkAsPlayed(id string, playDate time.Time) error {
|
||||
_, err := r.newQuery().Filter("id", id).Update(orm.Params{
|
||||
"play_count": orm.ColValue(orm.ColAdd, 1),
|
||||
"play_date": playDate,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *mediaFileRepository) Search(q string, offset int, size int) (model.MediaFiles, error) {
|
||||
if len(q) <= 2 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var results []mediaFile
|
||||
err := r.doSearch(r.tableName, q, offset, size, &results, "rating desc", "starred desc", "play_count desc", "title")
|
||||
err := r.doSearch(r.tableName, q, offset, size, &results, "title")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -57,6 +57,10 @@ func (db *MockDataStore) User() model.UserRepository {
|
||||
return db.MockedUser
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Annotation() model.AnnotationRepository {
|
||||
return struct{ model.AnnotationRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) WithTx(block func(db model.DataStore) error) error {
|
||||
return block(db)
|
||||
}
|
||||
|
||||
@@ -73,6 +73,10 @@ func (db *SQLStore) User() model.UserRepository {
|
||||
return NewUserRepository(db.getOrmer())
|
||||
}
|
||||
|
||||
func (db *SQLStore) Annotation() model.AnnotationRepository {
|
||||
return NewAnnotationRepository(db.getOrmer())
|
||||
}
|
||||
|
||||
func (db *SQLStore) Resource(model interface{}) model.ResourceRepository {
|
||||
return NewResource(db.getOrmer(), model, getMappedModel(model))
|
||||
}
|
||||
@@ -159,6 +163,7 @@ func init() {
|
||||
registerModel(model.Property{}, new(property))
|
||||
registerModel(model.Playlist{}, new(playlist))
|
||||
registerModel(model.User{}, new(user))
|
||||
registerModel(model.Annotation{}, new(annotation))
|
||||
|
||||
orm.RegisterModel(new(checksum))
|
||||
orm.RegisterModel(new(search))
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
@@ -29,13 +30,18 @@ var testArtists = model.Artists{
|
||||
|
||||
var albumSgtPeppers = model.Album{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1", Genre: "Rock"}
|
||||
var albumAbbeyRoad = model.Album{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1", Genre: "Rock"}
|
||||
var albumRadioactivity = model.Album{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true, Genre: "Electronic"}
|
||||
var albumRadioactivity = model.Album{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Genre: "Electronic"}
|
||||
var testAlbums = model.Albums{
|
||||
albumSgtPeppers,
|
||||
albumAbbeyRoad,
|
||||
albumRadioactivity,
|
||||
}
|
||||
|
||||
var annRadioactivity = model.Annotation{AnnotationID: "1", UserID: "userid", ItemType: model.AlbumItemType, ItemID: "3", Starred: true}
|
||||
var testAnnotations = []model.Annotation{
|
||||
annRadioactivity,
|
||||
}
|
||||
|
||||
var songDayInALife = model.MediaFile{ID: "1", Title: "A Day In A Life", ArtistID: "3", AlbumID: "1", Genre: "Rock", Path: P("/beatles/1/sgt/a day.mp3")}
|
||||
var songComeTogether = model.MediaFile{ID: "2", Title: "Come Together", ArtistID: "3", AlbumID: "2", Genre: "Rock", Path: P("/beatles/1/come together.mp3")}
|
||||
var songRadioactivity = model.MediaFile{ID: "3", Title: "Radioactivity", ArtistID: "2", AlbumID: "3", Genre: "Electronic", Path: P("/kraft/radio/radio.mp3")}
|
||||
@@ -76,5 +82,14 @@ var _ = Describe("Initialize test DB", func() {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
o := orm.NewOrm()
|
||||
for _, a := range testAnnotations {
|
||||
ann := annotation(a)
|
||||
_, err := o.Insert(&ann)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user