149 lines
5.0 KiB
Go
149 lines
5.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"sort"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type FeedSubscription struct {
|
|
Title string `json:"title"`
|
|
Folder string `json:"folder"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// Only normalize syntactic equivalence; never reorder query parameters, change
|
|
// path case or follow redirects to decide identity.
|
|
func FeedURLKey(raw string) string {
|
|
u, err := url.Parse(strings.TrimSpace(raw))
|
|
if err != nil {
|
|
return raw
|
|
}
|
|
u.Scheme = strings.ToLower(u.Scheme)
|
|
u.Host = strings.ToLower(u.Host)
|
|
if u.Scheme == "https" && u.Port() == "443" {
|
|
u.Host = strings.TrimSuffix(u.Host, ":443")
|
|
}
|
|
if u.Path == "" {
|
|
u.Path = "/"
|
|
}
|
|
u.Fragment = ""
|
|
return u.String()
|
|
}
|
|
|
|
func FeedSourcesSnapshot(sources []FeedSource) string {
|
|
versions := make([]string, 0, len(sources))
|
|
for _, s := range sources {
|
|
versions = append(versions, fmt.Sprintf("%s:%d", s.UUID, s.Version))
|
|
}
|
|
sort.Strings(versions)
|
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(versions, "\n"))))
|
|
}
|
|
|
|
type FeedImport struct {
|
|
RequestID string `json:"request_id"`
|
|
Expected string `json:"expected"`
|
|
Entries []FeedSubscription `json:"entries"`
|
|
Enabled bool `json:"enabled"`
|
|
Interval int `json:"interval_seconds"`
|
|
}
|
|
|
|
// Append-only import: source inserts and the durable retry receipt commit as a
|
|
// unit. A retry after later source deletion returns its receipt, never resurrects
|
|
// the deleted subscription. Authorization is checked even on receipt replay.
|
|
func (s *Store) ImportFeedSubscriptions(ctx context.Context, domain, owner string, input FeedImport) ([]string, error) {
|
|
if _, err := uuid.Parse(input.RequestID); err != nil {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
if len(input.Expected) != 64 || len(input.Entries) == 0 || len(input.Entries) > 32 || input.Interval < 300 || input.Interval > 86400 {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
keys := map[string]bool{}
|
|
for _, entry := range input.Entries {
|
|
u, err := url.Parse(entry.URL)
|
|
if err != nil || u.Scheme != "https" || u.Hostname() == "" || u.User != nil || u.Fragment != "" || (u.Port() != "" && u.Port() != "443") || len(entry.URL) > 4096 || strings.TrimSpace(entry.Title) == "" || len(entry.Title) > 200 || len(entry.Folder) > 200 || !utf8.ValidString(entry.Title+entry.Folder+entry.URL) {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
key := FeedURLKey(entry.URL)
|
|
if keys[key] {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
keys[key] = true
|
|
}
|
|
raw, _ := json.Marshal(input)
|
|
hash := fmt.Sprintf("%x", sha256.Sum256(raw))
|
|
ids := []string{}
|
|
err := s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
|
|
var oldHash, result string
|
|
err := tx.QueryRowContext(ctx, `SELECT input_hash,result FROM feed_import_receipts WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND request_id=`+s.sqlParam(3), domain, owner, input.RequestID).Scan(&oldHash, &result)
|
|
if err == nil {
|
|
if oldHash != hash {
|
|
return ErrTaskConflict
|
|
}
|
|
return json.Unmarshal([]byte(result), &ids)
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return err
|
|
}
|
|
rows, err := tx.QueryContext(ctx, `SELECT uuid,version,provider,url FROM feed_sources WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2), domain, owner)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
existing := []FeedSource{}
|
|
for rows.Next() {
|
|
var source FeedSource
|
|
if err = rows.Scan(&source.UUID, &source.Version, &source.Provider, &source.URL); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
existing = append(existing, source)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if FeedSourcesSnapshot(existing) != input.Expected {
|
|
return ErrTaskConflict
|
|
}
|
|
if len(existing)+len(input.Entries) > 32 {
|
|
return ErrTaskDAVLimit
|
|
}
|
|
for _, source := range existing {
|
|
if source.Provider == "local" && keys[FeedURLKey(source.URL)] {
|
|
return ErrTaskConflict
|
|
}
|
|
}
|
|
var receipts int
|
|
if err = tx.QueryRowContext(ctx, `SELECT count(*) FROM feed_import_receipts WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2), domain, owner).Scan(&receipts); err != nil {
|
|
return err
|
|
}
|
|
if receipts >= 1000 {
|
|
return ErrTaskDAVLimit
|
|
}
|
|
for _, entry := range input.Entries {
|
|
id := uuid.NewString()
|
|
if _, err = tx.ExecContext(ctx, `INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,version,enabled,interval_seconds) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,'local',`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,1,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, id, domain, owner, entry.Title, entry.Folder, entry.URL, boolToInt(input.Enabled), input.Interval); err != nil {
|
|
return err
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
encoded, _ := json.Marshal(ids)
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO feed_import_receipts VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`)`, domain, owner, input.RequestID, hash, string(encoded))
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ids, nil
|
|
}
|