Add Internet Radio support (#2063)
* add internet radio support * Add dynamic sidebar icon to Radios * Fix typos * Make URL suffix consistent * Fix typo * address feedback * Don't need to preload when playing Internet Radios * Reorder migration, or else it won't be applied * Make Radio list view responsive Also added filter by name, removed RadioActions and RadioContextMenu, and added a default radio icon, in case of favicon is not available. * Simplify StreamField usage * fix button, hide progress on mobile * use js styles over index.css Co-authored-by: Deluan <deluan@navidrome.org>
This commit is contained in:
@@ -52,6 +52,10 @@ func (s *SQLStore) Property(ctx context.Context) model.PropertyRepository {
|
||||
return NewPropertyRepository(ctx, s.getOrmer())
|
||||
}
|
||||
|
||||
func (s *SQLStore) Radio(ctx context.Context) model.RadioRepository {
|
||||
return NewRadioRepository(ctx, s.getOrmer())
|
||||
}
|
||||
|
||||
func (s *SQLStore) UserProps(ctx context.Context) model.UserPropsRepository {
|
||||
return NewUserPropsRepository(ctx, s.getOrmer())
|
||||
}
|
||||
@@ -94,6 +98,8 @@ func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRe
|
||||
return s.Genre(ctx).(model.ResourceRepository)
|
||||
case model.Playlist:
|
||||
return s.Playlist(ctx).(model.ResourceRepository)
|
||||
case model.Radio:
|
||||
return s.Radio(ctx).(model.ResourceRepository)
|
||||
case model.Share:
|
||||
return s.Share(ctx).(model.ResourceRepository)
|
||||
}
|
||||
|
||||
@@ -69,6 +69,12 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
radioWithoutHomePage = model.Radio{ID: "1235", StreamUrl: "https://example.com:8000/1/stream.mp3", HomePageUrl: "", Name: "No Homepage"}
|
||||
radioWithHomePage = model.Radio{ID: "5010", StreamUrl: "https://example.com/stream.mp3", Name: "Example Radio", HomePageUrl: "https://example.com"}
|
||||
testRadios = model.Radios{radioWithoutHomePage, radioWithHomePage}
|
||||
)
|
||||
|
||||
var (
|
||||
plsBest model.Playlist
|
||||
plsCool model.Playlist
|
||||
@@ -84,7 +90,7 @@ func P(path string) string {
|
||||
var _ = BeforeSuite(func() {
|
||||
o := orm.NewOrm()
|
||||
ctx := log.NewContext(context.TODO())
|
||||
user := model.User{ID: "userid", UserName: "userid"}
|
||||
user := model.User{ID: "userid", UserName: "userid", IsAdmin: true}
|
||||
ctx = request.WithUser(ctx, user)
|
||||
|
||||
ur := NewUserRepository(ctx, o)
|
||||
@@ -129,6 +135,15 @@ var _ = BeforeSuite(func() {
|
||||
}
|
||||
}
|
||||
|
||||
rar := NewRadioRepository(ctx, o)
|
||||
for i := range testRadios {
|
||||
r := testRadios[i]
|
||||
err := rar.Put(&r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
plsBest = model.Playlist{
|
||||
Name: "Best",
|
||||
Comment: "No Comments",
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "github.com/Masterminds/squirrel"
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
"github.com/deluan/rest"
|
||||
"github.com/google/uuid"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
)
|
||||
|
||||
type radioRepository struct {
|
||||
sqlRepository
|
||||
sqlRestful
|
||||
}
|
||||
|
||||
func NewRadioRepository(ctx context.Context, o orm.QueryExecutor) model.RadioRepository {
|
||||
r := &radioRepository{}
|
||||
r.ctx = ctx
|
||||
r.ormer = o
|
||||
r.tableName = "radio"
|
||||
r.filterMappings = map[string]filterFunc{
|
||||
"name": containsFilter,
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *radioRepository) isPermitted() bool {
|
||||
user := loggedUser(r.ctx)
|
||||
return user.IsAdmin
|
||||
}
|
||||
|
||||
func (r *radioRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
||||
sql := r.newSelect(options...)
|
||||
return r.count(sql, options...)
|
||||
}
|
||||
|
||||
func (r *radioRepository) Delete(id string) error {
|
||||
if !r.isPermitted() {
|
||||
return rest.ErrPermissionDenied
|
||||
}
|
||||
|
||||
return r.delete(Eq{"id": id})
|
||||
}
|
||||
|
||||
func (r *radioRepository) Get(id string) (*model.Radio, error) {
|
||||
sel := r.newSelect().Where(Eq{"id": id}).Columns("*")
|
||||
res := model.Radio{}
|
||||
err := r.queryOne(sel, &res)
|
||||
return &res, err
|
||||
}
|
||||
|
||||
func (r *radioRepository) GetAll(options ...model.QueryOptions) (model.Radios, error) {
|
||||
sel := r.newSelect(options...).Columns("*")
|
||||
res := model.Radios{}
|
||||
err := r.queryAll(sel, &res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (r *radioRepository) Put(radio *model.Radio) error {
|
||||
if !r.isPermitted() {
|
||||
return rest.ErrPermissionDenied
|
||||
}
|
||||
|
||||
var values map[string]interface{}
|
||||
|
||||
radio.UpdatedAt = time.Now()
|
||||
|
||||
if radio.ID == "" {
|
||||
radio.CreatedAt = time.Now()
|
||||
radio.ID = strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
values, _ = toSqlArgs(*radio)
|
||||
} else {
|
||||
values, _ = toSqlArgs(*radio)
|
||||
update := Update(r.tableName).Where(Eq{"id": radio.ID}).SetMap(values)
|
||||
count, err := r.executeSQL(update)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if count > 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
values["created_at"] = time.Now()
|
||||
insert := Insert(r.tableName).SetMap(values)
|
||||
_, err := r.executeSQL(insert)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *radioRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
||||
return r.CountAll(r.parseRestOptions(options...))
|
||||
}
|
||||
|
||||
func (r *radioRepository) EntityName() string {
|
||||
return "radio"
|
||||
}
|
||||
|
||||
func (r *radioRepository) NewInstance() interface{} {
|
||||
return &model.Radio{}
|
||||
}
|
||||
|
||||
func (r *radioRepository) Read(id string) (interface{}, error) {
|
||||
return r.Get(id)
|
||||
}
|
||||
|
||||
func (r *radioRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
|
||||
return r.GetAll(r.parseRestOptions(options...))
|
||||
}
|
||||
|
||||
func (r *radioRepository) Save(entity interface{}) (string, error) {
|
||||
t := entity.(*model.Radio)
|
||||
if !r.isPermitted() {
|
||||
return "", rest.ErrPermissionDenied
|
||||
}
|
||||
err := r.Put(t)
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return "", rest.ErrNotFound
|
||||
}
|
||||
return t.ID, err
|
||||
}
|
||||
|
||||
func (r *radioRepository) Update(id string, entity interface{}, cols ...string) error {
|
||||
t := entity.(*model.Radio)
|
||||
t.ID = id
|
||||
if !r.isPermitted() {
|
||||
return rest.ErrPermissionDenied
|
||||
}
|
||||
err := r.Put(t)
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return rest.ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ model.RadioRepository = (*radioRepository)(nil)
|
||||
var _ rest.Repository = (*radioRepository)(nil)
|
||||
var _ rest.Persistable = (*radioRepository)(nil)
|
||||
@@ -0,0 +1,176 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
"github.com/deluan/rest"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/model/request"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var (
|
||||
NewId string = "123-456-789"
|
||||
)
|
||||
|
||||
var _ = Describe("RadioRepository", func() {
|
||||
var repo model.RadioRepository
|
||||
|
||||
Describe("Admin User", func() {
|
||||
BeforeEach(func() {
|
||||
ctx := log.NewContext(context.TODO())
|
||||
ctx = request.WithUser(ctx, model.User{ID: "userid", UserName: "userid", IsAdmin: true})
|
||||
repo = NewRadioRepository(ctx, orm.NewOrm())
|
||||
_ = repo.Put(&radioWithHomePage)
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
all, _ := repo.GetAll()
|
||||
|
||||
for _, radio := range all {
|
||||
_ = repo.Delete(radio.ID)
|
||||
}
|
||||
|
||||
for i := range testRadios {
|
||||
r := testRadios[i]
|
||||
err := repo.Put(&r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Describe("Count", func() {
|
||||
It("returns the number of radios in the DB", func() {
|
||||
Expect(repo.CountAll()).To(Equal(int64(2)))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Delete", func() {
|
||||
It("deletes existing item", func() {
|
||||
err := repo.Delete(radioWithHomePage.ID)
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
_, err = repo.Get(radioWithHomePage.ID)
|
||||
Expect(err).To(MatchError(model.ErrNotFound))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Get", func() {
|
||||
It("returns an existing item", func() {
|
||||
res, err := repo.Get(radioWithHomePage.ID)
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
Expect(res.ID).To(Equal(radioWithHomePage.ID))
|
||||
})
|
||||
|
||||
It("errors when missing", func() {
|
||||
_, err := repo.Get("notanid")
|
||||
|
||||
Expect(err).To(MatchError(model.ErrNotFound))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetAll", func() {
|
||||
It("returns all items from the DB", func() {
|
||||
all, err := repo.GetAll()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(all[0].ID).To(Equal(radioWithoutHomePage.ID))
|
||||
Expect(all[1].ID).To(Equal(radioWithHomePage.ID))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Put", func() {
|
||||
It("successfully updates item", func() {
|
||||
err := repo.Put(&model.Radio{
|
||||
ID: radioWithHomePage.ID,
|
||||
Name: "New Name",
|
||||
StreamUrl: "https://example.com:4533/app",
|
||||
})
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
item, err := repo.Get(radioWithHomePage.ID)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(item.HomePageUrl).To(Equal(""))
|
||||
})
|
||||
|
||||
It("successfully creates item", func() {
|
||||
err := repo.Put(&model.Radio{
|
||||
Name: "New radio",
|
||||
StreamUrl: "https://example.com:4533/app",
|
||||
})
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
Expect(repo.CountAll()).To(Equal(int64(3)))
|
||||
|
||||
all, err := repo.GetAll()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(all[2].StreamUrl).To(Equal("https://example.com:4533/app"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Regular User", func() {
|
||||
BeforeEach(func() {
|
||||
ctx := log.NewContext(context.TODO())
|
||||
ctx = request.WithUser(ctx, model.User{ID: "userid", UserName: "userid", IsAdmin: false})
|
||||
repo = NewRadioRepository(ctx, orm.NewOrm())
|
||||
})
|
||||
|
||||
Describe("Count", func() {
|
||||
It("returns the number of radios in the DB", func() {
|
||||
Expect(repo.CountAll()).To(Equal(int64(2)))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Delete", func() {
|
||||
It("fails to delete items", func() {
|
||||
err := repo.Delete(radioWithHomePage.ID)
|
||||
|
||||
Expect(err).To(Equal(rest.ErrPermissionDenied))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Get", func() {
|
||||
It("returns an existing item", func() {
|
||||
res, err := repo.Get(radioWithHomePage.ID)
|
||||
|
||||
Expect(err).To((BeNil()))
|
||||
Expect(res.ID).To(Equal(radioWithHomePage.ID))
|
||||
})
|
||||
|
||||
It("errors when missing", func() {
|
||||
_, err := repo.Get("notanid")
|
||||
|
||||
Expect(err).To(MatchError(model.ErrNotFound))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetAll", func() {
|
||||
It("returns all items from the DB", func() {
|
||||
all, err := repo.GetAll()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(all[0].ID).To(Equal(radioWithoutHomePage.ID))
|
||||
Expect(all[1].ID).To(Equal(radioWithHomePage.ID))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Put", func() {
|
||||
It("fails to update item", func() {
|
||||
err := repo.Put(&model.Radio{
|
||||
ID: radioWithHomePage.ID,
|
||||
Name: "New Name",
|
||||
StreamUrl: "https://example.com:4533/app",
|
||||
})
|
||||
|
||||
Expect(err).To(Equal(rest.ErrPermissionDenied))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user