Files
pages/internal/store/feeds_catalog.go
T
Anya Tokugawa bf9a9c2847
Build and publish container images / images (push) Successful in 10m33s
feat(feeds): inherit FreshRSS folders in cached subscription tree
2026-11-08 13:34:22 +09:00

151 lines
4.1 KiB
Go

package store
import (
"context"
"encoding/json"
"strings"
"unicode/utf8"
)
// Remote IDs are opaque identities, not URLs to fetch or display labels.
type FeedCategory struct {
ID string `json:"id"`
Label string `json:"label"`
}
type FeedRemoteSubscription struct {
ID string `json:"id"`
Title string `json:"title"`
Categories []FeedCategory `json:"categories"`
}
type FeedCatalogSnapshot struct {
Subscriptions []FeedRemoteSubscription `json:"subscriptions"`
Folders []FeedCategory `json:"folders"`
}
// Merge membership-derived folders with explicit folders, including empty
// ones. IDs, not labels or slash-delimited paths, are the identity boundary.
func FeedFolders(catalog []FeedRemoteSubscription, folders []FeedCategory) ([]FeedCategory, error) {
out := []FeedCategory{}
seen := map[string]string{}
add := func(c FeedCategory) error {
if !ValidFeedRemoteID(c.ID) || len(c.Label) > 512 || !utf8.ValidString(c.Label) {
return ErrTaskInvalid
}
if label, ok := seen[c.ID]; ok {
if label != c.Label {
return ErrTaskInvalid
}
return nil
}
seen[c.ID] = c.Label
out = append(out, c)
if len(out) > 1000 {
return ErrTaskDAVLimit
}
return nil
}
if len(folders) > 1000 {
return nil, ErrTaskDAVLimit
}
for _, c := range folders {
if e := add(c); e != nil {
return nil, e
}
}
for _, sub := range catalog {
for _, c := range sub.Categories {
if e := add(c); e != nil {
return nil, e
}
}
}
return out, nil
}
func ValidFeedRemoteID(id string) bool {
return id != "" && len(id) <= 2048 && utf8.ValidString(id) && !strings.ContainsAny(id, "\x00\r\n")
}
// Bound both the number of entries and the encoded snapshot independently of
// the HTTP response limit. Invalid snapshots must not erase a good cache.
func FeedCatalogJSON(catalog []FeedRemoteSubscription) (string, error) {
if len(catalog) > 1000 {
return "", ErrTaskDAVLimit
}
seen := map[string]bool{}
labels := map[string]string{}
for _, sub := range catalog {
if !ValidFeedRemoteID(sub.ID) || seen[sub.ID] || len(sub.Title) > 512 || !utf8.ValidString(sub.Title) || len(sub.Categories) > 64 {
return "", ErrTaskInvalid
}
seen[sub.ID] = true
cats := map[string]bool{}
for _, c := range sub.Categories {
if !ValidFeedRemoteID(c.ID) || cats[c.ID] || len(c.Label) > 512 || !utf8.ValidString(c.Label) {
return "", ErrTaskInvalid
}
if label, ok := labels[c.ID]; ok && label != c.Label {
return "", ErrTaskInvalid
}
cats[c.ID], labels[c.ID] = true, c.Label
}
}
if catalog == nil {
catalog = []FeedRemoteSubscription{}
}
raw, e := json.Marshal(catalog)
if e != nil {
return "", e
}
if len(raw) > 1<<20 {
return "", ErrTaskDAVLimit
}
return string(raw), nil
}
// Validate the complete snapshot before persisting or reporting fetch success.
func (v FeedCatalogSnapshot) Encode() (string, string, error) {
catalog, e := FeedCatalogJSON(v.Subscriptions)
if e != nil {
return "", "", e
}
folders, e := FeedFolders(v.Subscriptions, v.Folders)
if e != nil {
return "", "", e
}
raw, e := json.Marshal(folders)
if e != nil {
return "", "", e
}
if len(raw)+len(catalog) > 1<<20 {
return "", "", ErrTaskDAVLimit
}
return catalog, string(raw), nil
}
func (s *Store) FeedCatalog(ctx context.Context, domain, owner, source string) ([]FeedRemoteSubscription, error) {
v, e := s.FeedCatalogSnapshot(ctx, domain, owner, source)
return v.Subscriptions, e
}
func (s *Store) FeedCatalogSnapshot(ctx context.Context, domain, owner, source string) (FeedCatalogSnapshot, error) {
v := FeedCatalogSnapshot{}
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
return v, e
}
var raw, folders string
if e := s.DB.QueryRowContext(ctx, `SELECT remote_catalog,remote_folders FROM feed_sources WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, source, domain, owner).Scan(&raw, &folders); e != nil {
return v, e
}
if e := json.Unmarshal([]byte(raw), &v.Subscriptions); e != nil {
return v, e
}
if e := json.Unmarshal([]byte(folders), &v.Folders); e != nil {
return v, e
}
var e error
v.Folders, e = FeedFolders(v.Subscriptions, v.Folders)
return v, e
}