feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
+71
-42
@@ -79,12 +79,12 @@ func (s *Store) CaptureFeedNote(ctx context.Context, domain, owner, request, sou
|
||||
hash := fmt.Sprintf("%x", sha256.Sum256(raw))
|
||||
page := ""
|
||||
e := s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
|
||||
caps, e := loadLocalityAccess(tx, domain, owner)
|
||||
caps, e := s.loadLocalityAccess(tx, domain, owner)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
for _, capability := range []string{"read", "create", "write"} {
|
||||
basis := authorizeGrantFrom(tx, AuthorizationRequest{DomainUUID: domain, PrincipalUUID: owner, Capability: capability, TargetType: "path", TargetID: target}, false)
|
||||
basis := s.authorizeGrantContext(ctx, tx, AuthorizationRequest{DomainUUID: domain, PrincipalUUID: owner, Capability: capability, TargetType: "path", TargetID: target}, false)
|
||||
if basis != nil && !errors.Is(basis, ErrAccessDenied) {
|
||||
return basis
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func (s *Store) CaptureFeedNote(ctx context.Context, domain, owner, request, sou
|
||||
}
|
||||
}
|
||||
var oldHash string
|
||||
e = tx.QueryRowContext(ctx, `SELECT input_hash,page_uuid FROM feed_note_receipts WHERE domain_uuid=? AND owner_uuid=? AND request_id=?`, domain, owner, request).Scan(&oldHash, &page)
|
||||
e = tx.QueryRowContext(ctx, `SELECT input_hash,page_uuid FROM feed_note_receipts WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND request_id=`+s.sqlParam(3), domain, owner, request).Scan(&oldHash, &page)
|
||||
if e == nil {
|
||||
if oldHash != hash {
|
||||
return ErrTaskConflict
|
||||
@@ -104,7 +104,7 @@ func (s *Store) CaptureFeedNote(ctx context.Context, domain, owner, request, sou
|
||||
return e
|
||||
}
|
||||
var exists bool
|
||||
if e = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_items i JOIN feed_sources s ON s.uuid=i.source_uuid WHERE s.domain_uuid=? AND s.owner_uuid=? AND s.uuid=? AND i.id=?)`, domain, owner, source, id).Scan(&exists); e != nil {
|
||||
if e = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_items i JOIN feed_sources s ON s.uuid=i.source_uuid WHERE s.domain_uuid=`+s.sqlParam(1)+` AND s.owner_uuid=`+s.sqlParam(2)+` AND s.uuid=`+s.sqlParam(3)+` AND i.id=`+s.sqlParam(4)+`)`, domain, owner, source, id).Scan(&exists); e != nil {
|
||||
return e
|
||||
}
|
||||
if !exists {
|
||||
@@ -115,7 +115,7 @@ func (s *Store) CaptureFeedNote(ctx context.Context, domain, owner, request, sou
|
||||
return e
|
||||
}
|
||||
page = item.PageUUID
|
||||
_, e = tx.ExecContext(ctx, `INSERT INTO feed_note_receipts VALUES(?,?,?,?,?)`, domain, owner, request, hash, page)
|
||||
_, e = tx.ExecContext(ctx, `INSERT INTO feed_note_receipts VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`)`, domain, owner, request, hash, page)
|
||||
return e
|
||||
})
|
||||
if e == nil {
|
||||
@@ -169,14 +169,14 @@ func scanFeedSource(row interface{ Scan(...any) error }) (v FeedSource, e error)
|
||||
e = row.Scan(&v.UUID, &v.Domain, &v.Owner, &v.Provider, &v.Title, &v.Folder, &v.URL, &v.Credential, &v.Version, &v.Enabled, &v.Interval, &v.NextMS, &v.Lease, &v.LeaseUntil, &v.LastMS, &v.Error, &v.ETag, &v.Modified, &v.WriteEnabled)
|
||||
return
|
||||
}
|
||||
func feedActor(ctx context.Context, q interface {
|
||||
func (s *Store) feedActor(ctx context.Context, q interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}, domain, owner string, write bool) error {
|
||||
if e := taskActor(ctx, q, domain, owner, write); e != nil {
|
||||
if e := s.taskActor(ctx, q, domain, owner, write); e != nil {
|
||||
return e
|
||||
}
|
||||
var raw string
|
||||
e := q.QueryRowContext(ctx, `SELECT value FROM config WHERE domain_uuid=? AND key='system_extension_states'`, domain).Scan(&raw)
|
||||
e := q.QueryRowContext(ctx, `SELECT value FROM config WHERE domain_uuid=`+s.sqlParam(1)+` AND key='system_extension_states'`, domain).Scan(&raw)
|
||||
if e != nil && !errors.Is(e, sql.ErrNoRows) {
|
||||
return e
|
||||
}
|
||||
@@ -189,7 +189,8 @@ func feedActor(ctx context.Context, q interface {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *Store) feedWrite(ctx context.Context, domain, owner string, fn func(*sql.Tx) error) error {
|
||||
func (s *Store) feedWrite(ctx context.Context, domain, owner string, fn func(*sql.Tx) error) (resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("feed write", resultErr) }()
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
@@ -200,10 +201,13 @@ func (s *Store) feedWrite(ctx context.Context, domain, owner string, fn func(*sq
|
||||
return e
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, e = tx.ExecContext(ctx, `UPDATE domain_memberships SET state=state WHERE domain_uuid=? AND principal_uuid=?`, domain, owner); e != nil {
|
||||
if e = s.lockTaskWriter(ctx, tx); e != nil {
|
||||
return e
|
||||
}
|
||||
if e = feedActor(ctx, tx, domain, owner, true); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `UPDATE domain_memberships SET state=state WHERE domain_uuid=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2), domain, owner); e != nil {
|
||||
return e
|
||||
}
|
||||
if e = s.feedActor(ctx, tx, domain, owner, true); e != nil {
|
||||
return e
|
||||
}
|
||||
if e = fn(tx); e != nil {
|
||||
@@ -211,11 +215,14 @@ func (s *Store) feedWrite(ctx context.Context, domain, owner string, fn func(*sq
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
func (s *Store) FeedSources(ctx context.Context, domain, owner string) ([]FeedSource, error) {
|
||||
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
func (s *Store) FeedSources(ctx context.Context, domain, owner string) (result []FeedSource, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("feeds repository", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
if e := s.feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE domain_uuid=? AND owner_uuid=? ORDER BY folder,title,uuid LIMIT 32`, domain, owner)
|
||||
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` ORDER BY folder,title,uuid LIMIT 32`, domain, owner)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
@@ -244,7 +251,7 @@ func (s *Store) SaveFeedSource(ctx context.Context, domain, owner string, v Feed
|
||||
}
|
||||
e := s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
|
||||
if v.Version == 0 {
|
||||
old, lookup := scanFeedSource(tx.QueryRowContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE uuid=?`, v.UUID))
|
||||
old, lookup := scanFeedSource(tx.QueryRowContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE uuid=`+s.sqlParam(1), v.UUID))
|
||||
if lookup == nil {
|
||||
if old.Domain != domain || old.Owner != owner || old.Version != 1 || old.Provider != v.Provider || old.Title != v.Title || old.Folder != v.Folder || old.URL != v.URL || old.Interval != v.Interval || old.Enabled != v.Enabled || old.WriteEnabled != v.WriteEnabled {
|
||||
return ErrTaskConflict
|
||||
@@ -272,23 +279,23 @@ func (s *Store) SaveFeedSource(ctx context.Context, domain, owner string, v Feed
|
||||
// partial index of unresolved operations. Other owners' terminal
|
||||
// receipts remain isolated by domain/owner in metadata lookups.
|
||||
var retired bool
|
||||
if e := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_actions WHERE domain_uuid=? AND owner_uuid=? AND source_uuid=?) OR EXISTS(SELECT 1 FROM feed_actions WHERE source_uuid=? AND status IN ('queued','preparing','sending','unknown','check_queued','checking'))`, domain, owner, v.UUID, v.UUID).Scan(&retired); e != nil {
|
||||
if e := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_actions WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND source_uuid=`+s.sqlParam(3)+`) OR EXISTS(SELECT 1 FROM feed_actions WHERE source_uuid=`+s.sqlParam(4)+` AND status IN ('queued','preparing','sending','unknown','check_queued','checking'))`, domain, owner, v.UUID, v.UUID).Scan(&retired); e != nil {
|
||||
return e
|
||||
}
|
||||
if retired {
|
||||
return ErrTaskConflict
|
||||
}
|
||||
var n int
|
||||
if e := tx.QueryRowContext(ctx, `SELECT count(*) FROM feed_sources WHERE domain_uuid=? AND owner_uuid=?`, domain, owner).Scan(&n); e != nil {
|
||||
if e := tx.QueryRowContext(ctx, `SELECT count(*) FROM feed_sources WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2), domain, owner).Scan(&n); e != nil {
|
||||
return e
|
||||
}
|
||||
if n >= 32 {
|
||||
return ErrTaskDAVLimit
|
||||
}
|
||||
_, e := tx.ExecContext(ctx, `INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,credential,version,enabled,interval_seconds,write_enabled) VALUES(?,?,?,?,?,?,?,?,1,?,?,?)`, v.UUID, domain, owner, v.Provider, v.Title, v.Folder, v.URL, v.Credential, v.Enabled, v.Interval, v.WriteEnabled)
|
||||
_, e := tx.ExecContext(ctx, `INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,credential,version,enabled,interval_seconds,write_enabled) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`,1,`+s.sqlParam(9)+`,`+s.sqlParam(10)+`,`+s.sqlParam(11)+`)`, v.UUID, domain, owner, v.Provider, v.Title, v.Folder, v.URL, v.Credential, boolToInt(v.Enabled), v.Interval, boolToInt(v.WriteEnabled))
|
||||
return e
|
||||
}
|
||||
old, e := scanFeedSource(tx.QueryRowContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, v.UUID, domain, owner))
|
||||
old, e := scanFeedSource(tx.QueryRowContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND owner_uuid=`+s.sqlParam(3), v.UUID, domain, owner))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -296,7 +303,7 @@ func (s *Store) SaveFeedSource(ctx context.Context, domain, owner string, v Feed
|
||||
if old.Provider != v.Provider || old.URL != v.URL || v.Credential != "" {
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
result, e := tx.ExecContext(ctx, `UPDATE feed_sources SET title=?,folder=?,enabled=?,interval_seconds=?,write_enabled=?,version=version+1,next_ms=0,lease='',lease_until=0 WHERE uuid=? AND version=?`, v.Title, v.Folder, v.Enabled, v.Interval, v.WriteEnabled, v.UUID, v.Version)
|
||||
result, e := tx.ExecContext(ctx, `UPDATE feed_sources SET title=`+s.sqlParam(1)+`,folder=`+s.sqlParam(2)+`,enabled=`+s.sqlParam(3)+`,interval_seconds=`+s.sqlParam(4)+`,write_enabled=`+s.sqlParam(5)+`,version=version+1,next_ms=0,lease='',lease_until=0 WHERE uuid=`+s.sqlParam(6)+` AND version=`+s.sqlParam(7), v.Title, v.Folder, boolToInt(v.Enabled), v.Interval, boolToInt(v.WriteEnabled), v.UUID, v.Version)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -316,9 +323,9 @@ func (s *Store) FeedSourceAction(ctx context.Context, domain, owner, id, action
|
||||
var e error
|
||||
switch action {
|
||||
case "refresh":
|
||||
result, e = tx.ExecContext(ctx, `UPDATE feed_sources SET next_ms=0 WHERE uuid=? AND domain_uuid=? AND owner_uuid=? AND version=? AND enabled=1 AND lease_until<? AND (last_ms=0 OR last_ms<?)`, id, domain, owner, version, time.Now().UnixMilli(), time.Now().Add(-time.Minute).UnixMilli())
|
||||
result, e = tx.ExecContext(ctx, `UPDATE feed_sources SET next_ms=0 WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND owner_uuid=`+s.sqlParam(3)+` AND version=`+s.sqlParam(4)+` AND enabled=1 AND lease_until<`+s.sqlParam(5)+` AND (last_ms=0 OR last_ms<`+s.sqlParam(6)+`)`, id, domain, owner, version, time.Now().UnixMilli(), time.Now().Add(-time.Minute).UnixMilli())
|
||||
case "delete":
|
||||
result, e = tx.ExecContext(ctx, `DELETE FROM feed_sources WHERE uuid=? AND domain_uuid=? AND owner_uuid=? AND version=?`, id, domain, owner, version)
|
||||
result, e = tx.ExecContext(ctx, `DELETE FROM feed_sources WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND owner_uuid=`+s.sqlParam(3)+` AND version=`+s.sqlParam(4), id, domain, owner, version)
|
||||
default:
|
||||
return ErrTaskInvalid
|
||||
}
|
||||
@@ -332,7 +339,10 @@ func (s *Store) FeedSourceAction(ctx context.Context, domain, owner, id, action
|
||||
return nil
|
||||
})
|
||||
}
|
||||
func (s *Store) FeedItems(ctx context.Context, domain, owner, source, filter, query string, before int64, beforeID string, scope ...string) ([]FeedItem, error) {
|
||||
func (s *Store) FeedItems(ctx context.Context, domain, owner, source, filter, query string, before int64, beforeID string, scope ...string) (result []FeedItem, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("feeds repository", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
remoteFeed, category := "", ""
|
||||
if len(scope) > 3 {
|
||||
return nil, ErrTaskInvalid
|
||||
@@ -356,17 +366,27 @@ func (s *Store) FeedItems(ctx context.Context, domain, owner, source, filter, qu
|
||||
if len(query) > 200 || len(beforeID) > 1200 {
|
||||
return nil, ErrTaskInvalid
|
||||
}
|
||||
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
if e := s.feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
args := []any{domain, owner, source, source, query, query, query, before, before, before, beforeID, filter, filter, remoteFeed, remoteFeed, category, category}
|
||||
position := "instr"
|
||||
categories := "json_each(i.remote_categories)"
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
position = "strpos"
|
||||
categories = "jsonb_array_elements_text(i.remote_categories::jsonb)"
|
||||
}
|
||||
extra := ""
|
||||
if mode == "uncategorized" {
|
||||
// The uncorrelated list is built once, not a catalog scan per article.
|
||||
extra = ` AND i.remote_feed IN (SELECT json_extract(c.value,'$.id') FROM feed_sources cs,json_each(cs.remote_catalog) c WHERE cs.uuid=? AND cs.domain_uuid=? AND cs.owner_uuid=? AND json_array_length(c.value,'$.categories')=0)`
|
||||
idExpr, catalogExpr, categoryCount := `json_extract(c.value,'$.id')`, `json_each(cs.remote_catalog)`, `json_array_length(c.value,'$.categories')`
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
idExpr, catalogExpr, categoryCount = `c.value->>'id'`, `jsonb_array_elements(cs.remote_catalog::jsonb)`, `jsonb_array_length(CASE WHEN jsonb_typeof(c.value->'categories')='array' THEN c.value->'categories' ELSE '[]'::jsonb END)`
|
||||
}
|
||||
extra = ` AND i.remote_feed IN (SELECT ` + idExpr + ` FROM feed_sources cs,` + catalogExpr + ` c WHERE cs.uuid=` + s.sqlParam(18) + ` AND cs.domain_uuid=` + s.sqlParam(19) + ` AND cs.owner_uuid=` + s.sqlParam(20) + ` AND ` + categoryCount + `=0)`
|
||||
args = append(args, source, domain, owner)
|
||||
}
|
||||
rows, e := s.DB.QueryContext(ctx, `SELECT i.source_uuid,i.id,i.title,i.url,i.published_ms,i.feed,CASE WHEN s.folder<>'' THEN s.folder ELSE i.folder END,CASE WHEN s.provider='local' THEN COALESCE(t.is_read,0) ELSE i.is_read END,CASE WHEN s.provider='local' THEN COALESCE(t.starred,0) ELSE i.starred END,COALESCE(t.version,0),s.provider FROM feed_items i JOIN feed_sources s ON s.uuid=i.source_uuid LEFT JOIN feed_states t ON t.source_uuid=i.source_uuid AND t.id=i.id WHERE s.domain_uuid=? AND s.owner_uuid=? AND (?='' OR s.uuid=?) AND (?='' OR instr(lower(i.title),lower(?))>0 OR instr(lower(i.feed),lower(?))>0) AND (?<0 OR i.published_ms<? OR (i.published_ms=? AND (i.source_uuid||':'||i.id)<?)) AND (?<>'unread' OR (CASE WHEN s.provider='local' THEN COALESCE(t.is_read,0) ELSE i.is_read END)=0) AND (?<>'starred' OR (CASE WHEN s.provider='local' THEN COALESCE(t.starred,0) ELSE i.starred END)=1) AND (?='' OR i.remote_feed=?) AND (?='' OR EXISTS(SELECT 1 FROM json_each(i.remote_categories) WHERE value=?))`+extra+` ORDER BY i.published_ms DESC,i.source_uuid||':'||i.id DESC LIMIT 101`, args...)
|
||||
rows, e := s.DB.QueryContext(ctx, `SELECT i.source_uuid,i.id,i.title,i.url,i.published_ms,i.feed,CASE WHEN s.folder<>'' THEN s.folder ELSE i.folder END,CASE WHEN s.provider='local' THEN COALESCE(t.is_read,0) ELSE i.is_read END,CASE WHEN s.provider='local' THEN COALESCE(t.starred,0) ELSE i.starred END,COALESCE(t.version,0),s.provider FROM feed_items i JOIN feed_sources s ON s.uuid=i.source_uuid LEFT JOIN feed_states t ON t.source_uuid=i.source_uuid AND t.id=i.id WHERE s.domain_uuid=`+s.sqlParam(1)+` AND s.owner_uuid=`+s.sqlParam(2)+` AND (`+s.sqlParam(3)+`='' OR s.uuid=`+s.sqlParam(4)+`) AND (`+s.sqlParam(5)+`='' OR `+position+`(lower(i.title),lower(`+s.sqlParam(6)+`))>0 OR `+position+`(lower(i.feed),lower(`+s.sqlParam(7)+`))>0) AND (CAST(`+s.sqlParam(8)+` AS BIGINT)<0 OR i.published_ms<`+s.sqlParam(9)+` OR (i.published_ms=`+s.sqlParam(10)+` AND (i.source_uuid||':'||i.id)<`+s.sqlParam(11)+`)) AND (`+s.sqlParam(12)+`<>'unread' OR (CASE WHEN s.provider='local' THEN COALESCE(t.is_read,0) ELSE i.is_read END)=0) AND (`+s.sqlParam(13)+`<>'starred' OR (CASE WHEN s.provider='local' THEN COALESCE(t.starred,0) ELSE i.starred END)=1) AND (`+s.sqlParam(14)+`='' OR i.remote_feed=`+s.sqlParam(15)+`) AND (`+s.sqlParam(16)+`='' OR EXISTS(SELECT 1 FROM `+categories+` WHERE value=`+s.sqlParam(17)+`))`+extra+` ORDER BY i.published_ms DESC,i.source_uuid||':'||i.id DESC LIMIT 101`, args...)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
@@ -381,18 +401,21 @@ func (s *Store) FeedItems(ctx context.Context, domain, owner, source, filter, qu
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (s *Store) FeedItem(ctx context.Context, domain, owner, source, id string) (FeedItem, error) {
|
||||
func (s *Store) FeedItem(ctx context.Context, domain, owner, source, id string) (result FeedItem, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("feeds repository", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
var v FeedItem
|
||||
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
if e := s.feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
return v, e
|
||||
}
|
||||
e := s.DB.QueryRowContext(ctx, `SELECT i.source_uuid,i.id,i.title,i.url,i.body,i.body_html,i.published_ms,i.feed,i.folder,CASE WHEN s.provider='local' THEN COALESCE(t.is_read,0) ELSE i.is_read END,CASE WHEN s.provider='local' THEN COALESCE(t.starred,0) ELSE i.starred END,COALESCE(t.version,0),s.provider FROM feed_items i JOIN feed_sources s ON s.uuid=i.source_uuid LEFT JOIN feed_states t ON t.source_uuid=i.source_uuid AND t.id=i.id WHERE s.domain_uuid=? AND s.owner_uuid=? AND s.uuid=? AND i.id=?`, domain, owner, source, id).Scan(&v.Source, &v.ID, &v.Title, &v.URL, &v.Body, &v.BodyHTML, &v.PublishedMS, &v.Feed, &v.Folder, &v.Read, &v.Starred, &v.Version, &v.Provider)
|
||||
e := s.DB.QueryRowContext(ctx, `SELECT i.source_uuid,i.id,i.title,i.url,i.body,i.body_html,i.published_ms,i.feed,i.folder,CASE WHEN s.provider='local' THEN COALESCE(t.is_read,0) ELSE i.is_read END,CASE WHEN s.provider='local' THEN COALESCE(t.starred,0) ELSE i.starred END,COALESCE(t.version,0),s.provider FROM feed_items i JOIN feed_sources s ON s.uuid=i.source_uuid LEFT JOIN feed_states t ON t.source_uuid=i.source_uuid AND t.id=i.id WHERE s.domain_uuid=`+s.sqlParam(1)+` AND s.owner_uuid=`+s.sqlParam(2)+` AND s.uuid=`+s.sqlParam(3)+` AND i.id=`+s.sqlParam(4), domain, owner, source, id).Scan(&v.Source, &v.ID, &v.Title, &v.URL, &v.Body, &v.BodyHTML, &v.PublishedMS, &v.Feed, &v.Folder, &v.Read, &v.Starred, &v.Version, &v.Provider)
|
||||
return v, e
|
||||
}
|
||||
func (s *Store) SetFeedState(ctx context.Context, domain, owner, source, id string, read, star bool, version int64) error {
|
||||
return s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
|
||||
var provider string
|
||||
if e := tx.QueryRowContext(ctx, `SELECT s.provider FROM feed_sources s JOIN feed_items i ON i.source_uuid=s.uuid WHERE s.uuid=? AND s.domain_uuid=? AND s.owner_uuid=? AND i.id=?`, source, domain, owner, id).Scan(&provider); e != nil {
|
||||
if e := tx.QueryRowContext(ctx, `SELECT s.provider FROM feed_sources s JOIN feed_items i ON i.source_uuid=s.uuid WHERE s.uuid=`+s.sqlParam(1)+` AND s.domain_uuid=`+s.sqlParam(2)+` AND s.owner_uuid=`+s.sqlParam(3)+` AND i.id=`+s.sqlParam(4), source, domain, owner, id).Scan(&provider); e != nil {
|
||||
return e
|
||||
}
|
||||
if provider != "local" {
|
||||
@@ -400,14 +423,14 @@ func (s *Store) SetFeedState(ctx context.Context, domain, owner, source, id stri
|
||||
}
|
||||
if version == 0 {
|
||||
var n int
|
||||
if e := tx.QueryRowContext(ctx, `SELECT count(*) FROM feed_states WHERE source_uuid=?`, source).Scan(&n); e != nil {
|
||||
if e := tx.QueryRowContext(ctx, `SELECT count(*) FROM feed_states WHERE source_uuid=`+s.sqlParam(1), source).Scan(&n); e != nil {
|
||||
return e
|
||||
}
|
||||
if n >= 10000 {
|
||||
return ErrTaskDAVLimit
|
||||
}
|
||||
}
|
||||
result, e := tx.ExecContext(ctx, `INSERT INTO feed_states(source_uuid,id,is_read,starred,version) SELECT ?,?,?,?,1 WHERE ?=0 ON CONFLICT(source_uuid,id) DO NOTHING`, source, id, read, star, version)
|
||||
result, e := tx.ExecContext(ctx, `INSERT INTO feed_states(source_uuid,id,is_read,starred,version) SELECT `+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,1 WHERE CAST(`+s.sqlParam(5)+` AS BIGINT)=0 ON CONFLICT(source_uuid,id) DO NOTHING`, source, id, boolToInt(read), boolToInt(star), version)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -415,7 +438,7 @@ func (s *Store) SetFeedState(ctx context.Context, domain, owner, source, id stri
|
||||
if n == 1 {
|
||||
return nil
|
||||
}
|
||||
result, e = tx.ExecContext(ctx, `UPDATE feed_states SET is_read=?,starred=?,version=version+1 WHERE source_uuid=? AND id=? AND version=?`, read, star, source, id, version)
|
||||
result, e = tx.ExecContext(ctx, `UPDATE feed_states SET is_read=`+s.sqlParam(1)+`,starred=`+s.sqlParam(2)+`,version=version+1 WHERE source_uuid=`+s.sqlParam(3)+` AND id=`+s.sqlParam(4)+` AND version=`+s.sqlParam(5), boolToInt(read), boolToInt(star), source, id, version)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -442,15 +465,21 @@ func (s *Store) OpenFeedCredential(v FeedSource) (string, string, error) {
|
||||
return f[4], f[5], nil
|
||||
}
|
||||
func (s *Store) EnsureFeedEvent() error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return s.ensureNativeBuiltinEvent("builtin.feed.sync", "RSS source synchronization", FeedSyncEventType, 60, -5, 1)
|
||||
}
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, e := s.DB.Exec(`INSERT OR IGNORE INTO event_definitions(uuid,name,event_type,event_key,payload,schedule_kind,interval_seconds,next_run_at_ms,enabled,priority,max_attempts,created_at_ms,updated_at_ms) VALUES('builtin.feed.sync','RSS source synchronization',?,'local','{}','interval',60,?,1,-5,1,?,?)`, FeedSyncEventType, now, now, now)
|
||||
_, e := s.DB.Exec(`INSERT OR IGNORE INTO event_definitions(uuid,name,event_type,event_key,payload,schedule_kind,interval_seconds,next_run_at_ms,enabled,priority,max_attempts,created_at_ms,updated_at_ms) VALUES('builtin.feed.sync','RSS source synchronization',`+s.sqlParam(1)+`,'local','{}','interval',60,`+s.sqlParam(2)+`,1,-5,1,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`)`, FeedSyncEventType, now, now, now)
|
||||
return e
|
||||
}
|
||||
func (s *Store) DueFeedSources(ctx context.Context) ([]FeedSource, error) {
|
||||
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE enabled=1 AND next_ms<=? AND lease_until<? AND EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE p.uuid=feed_sources.owner_uuid AND m.domain_uuid=feed_sources.domain_uuid AND p.state='active' AND m.state='active' AND d.enabled=1 AND COALESCE(d.placement,'local')='local') AND NOT EXISTS(SELECT 1 FROM config c WHERE c.domain_uuid=feed_sources.domain_uuid AND c.key='system_extension_states' AND COALESCE(json_extract(CASE WHEN json_valid(c.value) THEN c.value ELSE '{"organizer.feeds":false}' END,'$."organizer.feeds"'),1)=0) ORDER BY next_ms,uuid LIMIT 4`, time.Now().UnixMilli(), time.Now().UnixMilli())
|
||||
func (s *Store) DueFeedSources(ctx context.Context) (result []FeedSource, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("feeds repository", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE enabled=1 AND next_ms<=`+s.sqlParam(1)+` AND lease_until<`+s.sqlParam(2)+` AND EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE p.uuid=feed_sources.owner_uuid AND m.domain_uuid=feed_sources.domain_uuid AND p.state='active' AND m.state='active' AND d.enabled=1 AND COALESCE(d.placement,'local')='local'`+s.feedCampusSQL()+`) AND NOT EXISTS(SELECT 1 FROM config c WHERE c.domain_uuid=feed_sources.domain_uuid AND c.key='system_extension_states' AND `+s.feedDisabledConfigSQL()+`) ORDER BY next_ms,uuid LIMIT 4`, time.Now().UnixMilli(), time.Now().UnixMilli())
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
@@ -469,7 +498,7 @@ func (s *Store) ClaimFeed(ctx context.Context, v FeedSource) (FeedSource, error)
|
||||
v.Lease = uuid.NewString()
|
||||
e := s.feedWrite(ctx, v.Domain, v.Owner, func(tx *sql.Tx) error {
|
||||
now := time.Now().UnixMilli()
|
||||
result, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease=?,lease_until=?,next_ms=? WHERE uuid=? AND version=? AND enabled=1 AND lease_until<? AND next_ms<=?`, v.Lease, now+60000, now+int64(v.Interval)*1000, v.UUID, v.Version, now, now)
|
||||
result, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease=`+s.sqlParam(1)+`,lease_until=`+s.sqlParam(2)+`,next_ms=`+s.sqlParam(3)+` WHERE uuid=`+s.sqlParam(4)+` AND version=`+s.sqlParam(5)+` AND enabled=1 AND lease_until<`+s.sqlParam(6)+` AND next_ms<=`+s.sqlParam(7), v.Lease, now+60000, now+int64(v.Interval)*1000, v.UUID, v.Version, now, now)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -529,7 +558,7 @@ func (s *Store) CompleteFeed(ctx context.Context, v FeedSource, items []FeedItem
|
||||
}
|
||||
}
|
||||
return s.feedWrite(ctx, v.Domain, v.Owner, func(tx *sql.Tx) error {
|
||||
result, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease='',lease_until=0,last_ms=CASE WHEN ?='' THEN ? ELSE last_ms END,error=?,etag=CASE WHEN ?='' THEN ? ELSE etag END,modified=CASE WHEN ?='' THEN ? ELSE modified END WHERE uuid=? AND version=? AND lease=? AND enabled=1`, code, time.Now().UnixMilli(), code, code, etag, code, modified, v.UUID, v.Version, v.Lease)
|
||||
result, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease='',lease_until=0,last_ms=CASE WHEN `+s.sqlParam(1)+`='' THEN `+s.sqlParam(2)+` ELSE last_ms END,error=`+s.sqlParam(3)+`,etag=CASE WHEN `+s.sqlParam(4)+`='' THEN `+s.sqlParam(5)+` ELSE etag END,modified=CASE WHEN `+s.sqlParam(6)+`='' THEN `+s.sqlParam(7)+` ELSE modified END WHERE uuid=`+s.sqlParam(8)+` AND version=`+s.sqlParam(9)+` AND lease=`+s.sqlParam(10)+` AND enabled=1`, code, time.Now().UnixMilli(), code, code, etag, code, modified, v.UUID, v.Version, v.Lease)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -541,14 +570,14 @@ func (s *Store) CompleteFeed(ctx context.Context, v FeedSource, items []FeedItem
|
||||
return nil
|
||||
}
|
||||
if catalog != "" {
|
||||
if _, e = tx.ExecContext(ctx, `UPDATE feed_sources SET remote_catalog=?,remote_folders=? WHERE uuid=?`, catalog, folders, v.UUID); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `UPDATE feed_sources SET remote_catalog=`+s.sqlParam(1)+`,remote_folders=`+s.sqlParam(2)+` WHERE uuid=`+s.sqlParam(3), catalog, folders, v.UUID); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
// FreshRSS is authoritative: retain exactly this bounded recent window,
|
||||
// not stale rows from unsubscribed feeds or old external read/star state.
|
||||
if v.Provider == "freshrss" {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM feed_items WHERE source_uuid=?`, v.UUID); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM feed_items WHERE source_uuid=`+s.sqlParam(1), v.UUID); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
@@ -558,12 +587,12 @@ func (s *Store) CompleteFeed(ctx context.Context, v FeedSource, items []FeedItem
|
||||
categories = []string{}
|
||||
}
|
||||
raw, _ := json.Marshal(categories)
|
||||
_, e = tx.ExecContext(ctx, `INSERT INTO feed_items(source_uuid,id,title,url,body,body_html,published_ms,feed,folder,is_read,starred,remote_feed,remote_categories) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(source_uuid,id) DO UPDATE SET title=excluded.title,url=excluded.url,body=excluded.body,body_html=excluded.body_html,published_ms=excluded.published_ms,feed=excluded.feed,folder=excluded.folder,is_read=excluded.is_read,starred=excluded.starred,remote_feed=excluded.remote_feed,remote_categories=excluded.remote_categories`, v.UUID, i.ID, i.Title, i.URL, i.Body, i.BodyHTML, i.PublishedMS, i.Feed, i.Folder, i.Read, i.Starred, i.RemoteFeed, string(raw))
|
||||
_, e = tx.ExecContext(ctx, `INSERT INTO feed_items(source_uuid,id,title,url,body,body_html,published_ms,feed,folder,is_read,starred,remote_feed,remote_categories) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`,`+s.sqlParam(9)+`,`+s.sqlParam(10)+`,`+s.sqlParam(11)+`,`+s.sqlParam(12)+`,`+s.sqlParam(13)+`) ON CONFLICT(source_uuid,id) DO UPDATE SET title=excluded.title,url=excluded.url,body=excluded.body,body_html=excluded.body_html,published_ms=excluded.published_ms,feed=excluded.feed,folder=excluded.folder,is_read=excluded.is_read,starred=excluded.starred,remote_feed=excluded.remote_feed,remote_categories=excluded.remote_categories`, v.UUID, i.ID, i.Title, i.URL, i.Body, i.BodyHTML, i.PublishedMS, i.Feed, i.Folder, boolToInt(i.Read), boolToInt(i.Starred), i.RemoteFeed, string(raw))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
_, e = tx.ExecContext(ctx, `DELETE FROM feed_items WHERE source_uuid=? AND id NOT IN(SELECT id FROM feed_items WHERE source_uuid=? ORDER BY published_ms DESC,id DESC LIMIT 500)`, v.UUID, v.UUID)
|
||||
_, e = tx.ExecContext(ctx, `DELETE FROM feed_items WHERE source_uuid=`+s.sqlParam(1)+` AND id NOT IN(SELECT id FROM feed_items WHERE source_uuid=`+s.sqlParam(2)+` ORDER BY published_ms DESC,id DESC LIMIT 500)`, v.UUID, v.UUID)
|
||||
return e
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user