45 lines
1.6 KiB
Go
45 lines
1.6 KiB
Go
package api
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/ponzischeme89/memby/server/internal/store"
|
||
|
|
)
|
||
|
|
|
||
|
|
type arrIntegrationStatus struct {
|
||
|
|
SonarrConfigured bool `json:"sonarrConfigured"`
|
||
|
|
RadarrConfigured bool `json:"radarrConfigured"`
|
||
|
|
SonarrEnabled bool `json:"sonarrEnabled"`
|
||
|
|
RadarrEnabled bool `json:"radarrEnabled"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) arrIntegrationStatus(r *http.Request) arrIntegrationStatus {
|
||
|
|
policy, err := s.store.ArrIntegrationPolicy(r.Context())
|
||
|
|
if err != nil {
|
||
|
|
policy = store.DefaultArrIntegrationPolicy()
|
||
|
|
}
|
||
|
|
return arrIntegrationStatus{SonarrConfigured: s.sonarr != nil, RadarrConfigured: s.radarr != nil, SonarrEnabled: s.sonarr != nil && policy.SonarrEnabled, RadarrEnabled: s.radarr != nil && policy.RadarrEnabled}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) handleAdminArrIntegrations(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if r.Method == http.MethodGet {
|
||
|
|
writeJSON(w, http.StatusOK, s.arrIntegrationStatus(r))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var req struct {
|
||
|
|
SonarrEnabled bool `json:"sonarrEnabled"`
|
||
|
|
RadarrEnabled bool `json:"radarrEnabled"`
|
||
|
|
}
|
||
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&req); err != nil {
|
||
|
|
writeError(w, http.StatusBadRequest, "malformed request body")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := s.store.SetArrIntegrationPolicy(r.Context(), store.ArrIntegrationPolicy{SonarrEnabled: req.SonarrEnabled, RadarrEnabled: req.RadarrEnabled}); err != nil {
|
||
|
|
writeError(w, http.StatusInternalServerError, "could not save integration settings")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.loggerFor(r.Context()).Info("arr integrations changed", "sonarr_enabled", req.SonarrEnabled, "radarr_enabled", req.RadarrEnabled)
|
||
|
|
writeJSON(w, http.StatusOK, s.arrIntegrationStatus(r))
|
||
|
|
}
|