feat(sources): add managed RSS feeds (#41)

This commit is contained in:
2026-11-25 01:42:27 +01:00
parent 12acb36612
commit 136d9b3b59
38 changed files with 2103 additions and 67 deletions

View File

@ -3,9 +3,12 @@ package handlers
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/tradarr/backend/internal/auth"
"github.com/tradarr/backend/internal/sourcefeeds"
"io"
"net/http"
"strconv"
@ -234,7 +237,7 @@ func (h *Handler) ListAIModels(c *gin.Context) {
func (h *Handler) ListSources(c *gin.Context) {
sources, err := h.repo.ListSources()
if err != nil {
httputil.InternalError(c, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "source_configuration_unavailable"})
return
}
type sourceResponse struct {
@ -244,23 +247,51 @@ func (h *Handler) ListSources(c *gin.Context) {
}
result := make([]sourceResponse, 0, len(sources))
for _, source := range sources {
if source.SourceKey != nil && sourcefeeds.Supported(*source.SourceKey) {
source.FeedURLs, err = h.repo.SourceFeedURLs(c.Request.Context(), *source.SourceKey)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "source_configuration_unavailable"})
return
}
}
result = append(result, sourceResponse{Source: source, Supported: source.Supported(), Operational: source.Operational()})
}
httputil.OK(c, result)
}
type updateSourceRequest struct {
Enabled bool `json:"enabled"`
ArticleLimit *int `json:"article_limit" binding:"omitempty,min=1,max=200"`
FeedURLs json.RawMessage `json:"feed_urls"`
Enabled bool `json:"enabled"`
ArticleLimit *int `json:"article_limit" binding:"omitempty,min=1,max=200"`
}
func (h *Handler) UpdateSource(c *gin.Context) {
var req updateSourceRequest
if err := c.ShouldBindJSON(&req); err != nil {
httputil.BadRequest(c, err)
var req *updateSourceRequest
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 8192)
decoder := json.NewDecoder(c.Request.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&req); err != nil || req == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "source_configuration_invalid"})
return
}
if err := h.repo.UpdateSource(c.Param("id"), req.Enabled, req.ArticleLimit); err != nil {
if err := decoder.Decode(&struct{}{}); err != io.EOF || (req.ArticleLimit != nil && (*req.ArticleLimit < 1 || *req.ArticleLimit > 200)) {
c.JSON(http.StatusBadRequest, gin.H{"error": "source_configuration_invalid"})
return
}
var feeds *[]string
if len(req.FeedURLs) > 0 {
var values []string
if err := json.Unmarshal(req.FeedURLs, &values); err != nil || values == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "source_feeds_invalid"})
return
}
feeds = &values
}
if err := h.repo.UpdateSourceConfig(c.Param("id"), req.Enabled, req.ArticleLimit, feeds); err != nil {
if errors.Is(err, sourcefeeds.ErrInvalid) || errors.Is(err, sourcefeeds.ErrUnsupported) {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if errors.Is(err, models.ErrSourceNotSupported) {
c.JSON(http.StatusConflict, gin.H{"error": "source_not_supported"})
return
@ -269,7 +300,7 @@ func (h *Handler) UpdateSource(c *gin.Context) {
httputil.NotFound(c)
return
}
httputil.InternalError(c, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "source_configuration_unavailable"})
return
}
httputil.OK(c, gin.H{"ok": true})