Add optional MDBList movie ratings
This commit is contained in:
@@ -42,6 +42,7 @@ func (s *Server) adminRoutes() http.Handler {
|
||||
mux.Handle("POST /admin/api/update-policy", s.adminAuth(s.handleAdminUpdatePolicy))
|
||||
mux.Handle("POST /admin/api/request-policy", s.adminAuth(s.handleAdminRequestPolicy))
|
||||
mux.Handle("POST /admin/api/playback-policy", s.adminAuth(s.handleAdminPlaybackPolicy))
|
||||
mux.Handle("POST /admin/api/mdblist-settings", s.adminAuth(s.handleAdminMDBListSettings))
|
||||
mux.Handle("POST /admin/api/features", s.adminAuth(s.handleAdminFeaturePolicy))
|
||||
mux.Handle("POST /admin/api/release", s.releasePublishAuth(s.handleReleasePublish))
|
||||
|
||||
@@ -51,7 +52,7 @@ func (s *Server) adminRoutes() http.Handler {
|
||||
var adminPages = map[string]bool{
|
||||
"library": true, "recommendations": true, "requests": true,
|
||||
"features": true, "playback": true, "maintenance": true, "updates": true, "engagement": true,
|
||||
"imports": true, "logs": true,
|
||||
"ratings": true, "imports": true, "logs": true,
|
||||
}
|
||||
|
||||
func (s *Server) handleAdminRoot(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -171,6 +172,7 @@ type adminStatus struct {
|
||||
ForYouRunning bool `json:"forYouRunning"`
|
||||
RequestPolicy store.RequestPolicy `json:"requestPolicy"`
|
||||
PlaybackPolicy store.PlaybackPolicy `json:"playbackPolicy"`
|
||||
MDBList mdblistAdminSettings `json:"mdblist"`
|
||||
Features featureResponse `json:"features"`
|
||||
RequestUsers []store.KnownUser `json:"requestUsers"`
|
||||
Clients []store.KnownClient `json:"clients"`
|
||||
@@ -236,11 +238,98 @@ func (s *Server) handleAdminStatus(w http.ResponseWriter, r *http.Request) {
|
||||
Features: featurePayload(s.currentFeaturePolicy(ctx), membyProtocolVersion),
|
||||
RequestUsers: requestUsers,
|
||||
Clients: clients,
|
||||
SonarrReady: s.sonarr != nil,
|
||||
RadarrReady: s.radarr != nil,
|
||||
MDBList: func() mdblistAdminSettings {
|
||||
settings, settingsErr := s.store.MDBListSettings(ctx)
|
||||
if settingsErr != nil {
|
||||
s.log.Warn("MDBList settings read failed", "error", settingsErr)
|
||||
settings = store.DefaultMDBListSettings()
|
||||
}
|
||||
return publicMDBListSettings(settings)
|
||||
}(),
|
||||
SonarrReady: s.sonarr != nil,
|
||||
RadarrReady: s.radarr != nil,
|
||||
})
|
||||
}
|
||||
|
||||
type mdblistAdminSettings struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
APIKeyConfigured bool `json:"apiKeyConfigured"`
|
||||
Sources []string `json:"sources"`
|
||||
AvailableSources []string `json:"availableSources"`
|
||||
}
|
||||
|
||||
type mdblistSettingsRequest struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
APIKey string `json:"apiKey"`
|
||||
ClearAPIKey bool `json:"clearApiKey"`
|
||||
Sources []string `json:"sources"`
|
||||
}
|
||||
|
||||
func publicMDBListSettings(settings store.MDBListSettings) mdblistAdminSettings {
|
||||
return mdblistAdminSettings{
|
||||
Enabled: settings.Enabled, APIKeyConfigured: settings.APIKey != "",
|
||||
Sources: settings.Sources, AvailableSources: store.MDBListSources(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleAdminMDBListSettings(w http.ResponseWriter, r *http.Request) {
|
||||
var req mdblistSettingsRequest
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 16<<10)).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "malformed request body")
|
||||
return
|
||||
}
|
||||
current, err := s.store.MDBListSettings(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "could not read MDBList settings")
|
||||
return
|
||||
}
|
||||
apiKey := current.APIKey
|
||||
if req.ClearAPIKey {
|
||||
apiKey = ""
|
||||
} else if replacement := strings.TrimSpace(req.APIKey); replacement != "" {
|
||||
apiKey = replacement
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
sources := make([]string, 0, len(req.Sources))
|
||||
for _, source := range req.Sources {
|
||||
source = strings.ToLower(strings.TrimSpace(source))
|
||||
if source == "" || seen[source] {
|
||||
continue
|
||||
}
|
||||
if !store.ValidMDBListSource(source) {
|
||||
writeError(w, http.StatusBadRequest, "unknown MDBList rating source")
|
||||
return
|
||||
}
|
||||
seen[source] = true
|
||||
sources = append(sources, source)
|
||||
}
|
||||
if req.Enabled && apiKey == "" {
|
||||
writeError(w, http.StatusBadRequest, "set an MDBList API key before enabling ratings")
|
||||
return
|
||||
}
|
||||
if req.Enabled && len(sources) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "select at least one MDBList rating source")
|
||||
return
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
sources = current.Sources
|
||||
}
|
||||
next := store.MDBListSettings{Enabled: req.Enabled, APIKey: apiKey, Sources: sources}
|
||||
if err := s.store.SetMDBListSettings(r.Context(), next); err != nil {
|
||||
s.log.Error("MDBList settings write failed", "error", err)
|
||||
writeError(w, http.StatusInternalServerError, "could not save MDBList settings")
|
||||
return
|
||||
}
|
||||
stored, err := s.store.MDBListSettings(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "could not reload MDBList settings")
|
||||
return
|
||||
}
|
||||
s.log.Info("MDBList settings changed", "enabled", stored.Enabled, "sources", len(stored.Sources),
|
||||
"api_key_configured", stored.APIKey != "")
|
||||
writeJSON(w, http.StatusOK, publicMDBListSettings(stored))
|
||||
}
|
||||
|
||||
type playbackPolicyRequest struct {
|
||||
PrerollEnabled bool `json:"prerollEnabled"`
|
||||
PrerollDurationMs int64 `json:"prerollDurationMs"`
|
||||
|
||||
Reference in New Issue
Block a user