From b17ce76a723b4db67329a1b22f3e53d49fb18a69 Mon Sep 17 00:00:00 2001 From: sentriz Date: Sun, 18 Apr 2021 12:31:22 +0100 Subject: [PATCH] Set getUser.view jukebox/podcast role closes #130 --- cmd/gonic/main.go | 1 + server/ctrlsubsonic/handlers_common.go | 4 +-- server/server.go | 44 ++++++++++++++------------ 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cmd/gonic/main.go b/cmd/gonic/main.go index cde2fb0..4df174c 100644 --- a/cmd/gonic/main.go +++ b/cmd/gonic/main.go @@ -101,6 +101,7 @@ func main() { GenreSplit: *confGenreSplit, PodcastPath: *confPodcastPath, HTTPLog: *confHTTPLog, + JukeboxEnabled: *confJukeboxEnabled, }) var g run.Group diff --git a/server/ctrlsubsonic/handlers_common.go b/server/ctrlsubsonic/handlers_common.go index 10aa24e..bd82dbd 100644 --- a/server/ctrlsubsonic/handlers_common.go +++ b/server/ctrlsubsonic/handlers_common.go @@ -105,8 +105,8 @@ func (c *Controller) ServeGetUser(r *http.Request) *spec.Response { sub.User = &spec.User{ Username: user.Name, AdminRole: user.IsAdmin, - JukeboxRole: true, - PodcastRole: true, + JukeboxRole: c.Jukebox != nil, + PodcastRole: c.Podcasts != nil, ScrobblingEnabled: hasLastFM || hasListenBrainz, Folder: []int{1}, } diff --git a/server/server.go b/server/server.go index c096048..419eb4c 100644 --- a/server/server.go +++ b/server/server.go @@ -33,6 +33,7 @@ type Options struct { ProxyPrefix string GenreSplit string HTTPLog bool + JukeboxEnabled bool } type Server struct { @@ -44,58 +45,61 @@ type Server struct { } func New(opts Options) *Server { - // ** begin sanitation opts.MusicPath = filepath.Clean(opts.MusicPath) opts.CachePath = filepath.Clean(opts.CachePath) opts.PodcastPath = filepath.Clean(opts.PodcastPath) - // ** begin controllers + scanner := scanner.New(opts.MusicPath, opts.DB, opts.GenreSplit) - jukebox := jukebox.New(opts.MusicPath) - // the base controller, it's fields/middlewares are embedded/used by the - // other two admin ui and subsonic controllers base := &ctrlbase.Controller{ DB: opts.DB, MusicPath: opts.MusicPath, ProxyPrefix: opts.ProxyPrefix, Scanner: scanner, } + // router with common wares for admin / subsonic r := mux.NewRouter() if opts.HTTPLog { r.Use(base.WithLogging) } r.Use(base.WithCORS) - // + sessKey := opts.DB.GetOrCreateKey("session_key") sessDB := gormstore.New(opts.DB.DB, []byte(sessKey)) sessDB.SessionOpts.HttpOnly = true sessDB.SessionOpts.SameSite = http.SameSiteLaxMode - // - pcInit := &podcasts.Podcasts{DB: opts.DB, PodcastBasePath: opts.PodcastPath} - ctrlAdmin := ctrladmin.New(base, sessDB, pcInit) - scrobblers := []scrobble.Scrobbler{ - &lastfm.Scrobbler{DB: opts.DB}, - &listenbrainz.Scrobbler{}, - } + + podcast := &podcasts.Podcasts{DB: opts.DB, PodcastBasePath: opts.PodcastPath} + ctrlAdmin := ctrladmin.New(base, sessDB, podcast) ctrlSubsonic := &ctrlsubsonic.Controller{ Controller: base, CachePath: opts.CachePath, CoverCachePath: opts.CoverCachePath, - Jukebox: jukebox, - Scrobblers: scrobblers, - Podcasts: pcInit, + Scrobblers: []scrobble.Scrobbler{ + &lastfm.Scrobbler{DB: opts.DB}, + &listenbrainz.Scrobbler{}, + }, + Podcasts: podcast, } + setupMisc(r, base) setupAdmin(r.PathPrefix("/admin").Subrouter(), ctrlAdmin) setupSubsonic(r.PathPrefix("/rest").Subrouter(), ctrlSubsonic) - // - return &Server{ + + server := &Server{ scanner: scanner, - jukebox: jukebox, router: r, sessDB: sessDB, - podcast: &podcasts.Podcasts{DB: opts.DB, PodcastBasePath: opts.PodcastPath}, + podcast: podcast, } + + if opts.JukeboxEnabled { + jukebox := jukebox.New(opts.MusicPath) + ctrlSubsonic.Jukebox = jukebox + server.jukebox = jukebox + } + + return server } func setupMisc(r *mux.Router, ctrl *ctrlbase.Controller) {