feat(feeds): expose retained operation history and read-only rechecks
Build and publish container images / images (push) Successful in 11m14s
Build and publish container images / images (push) Successful in 11m14s
This commit is contained in:
1 parent
e4c184d71c
commit
1057be6d2b
12 files changed
+547
-10
No files matched your search
@@ -132,6 +132,9 @@ func (s *Server) syncFeedActions(ctx context.Context, client *http.Client) error
|
||||
}
|
||||
|
||||
func (s *Server) handleFeedAction(w http.ResponseWriter, r *http.Request, domain, actor, key string) bool {
|
||||
if s.handleFeedHistory(w, r, domain, actor, key) {
|
||||
return true
|
||||
}
|
||||
switch {
|
||||
case key == "action" && r.Method == "GET":
|
||||
a, e := s.store.FeedAction(r.Context(), domain, actor, r.URL.Query().Get("source"), r.URL.Query().Get("id"))
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func writeFeedHistoryJSON(w http.ResponseWriter, value any) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func (s *Server) handleFeedHistory(w http.ResponseWriter, r *http.Request, domain, actor, key string) bool {
|
||||
if r.Method != "GET" || (key != "actions" && key != "action/export") {
|
||||
return false
|
||||
}
|
||||
if key == "action/export" {
|
||||
a, e := s.store.FeedActionRecord(r.Context(), domain, actor, r.URL.Query().Get("request_id"))
|
||||
if e != nil {
|
||||
taskError(w, e)
|
||||
return true
|
||||
}
|
||||
id, _ := uuid.Parse(a.RequestID)
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="pageswiki-feed-action-`+id.String()+`.json"`)
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
writeFeedHistoryJSON(w, map[string]any{"format": "pageswiki.feed-action", "version": 1, "exported_ms": time.Now().UnixMilli(), "domain_uuid": domain, "owner_uuid": actor, "action": a})
|
||||
return true
|
||||
}
|
||||
q := r.URL.Query()
|
||||
query := store.FeedActionQuery{Source: q.Get("source"), Status: q.Get("status"), Before: -1, Key: q.Get("key")}
|
||||
if q.Has("before") {
|
||||
var e error
|
||||
query.Before, e = strconv.ParseInt(q.Get("before"), 10, 64)
|
||||
if e != nil || query.Before < 0 {
|
||||
taskError(w, store.ErrTaskInvalid)
|
||||
return true
|
||||
}
|
||||
}
|
||||
items, e := s.store.FeedActionHistory(r.Context(), domain, actor, query)
|
||||
if e != nil {
|
||||
taskError(w, e)
|
||||
return true
|
||||
}
|
||||
more := len(items) > 50
|
||||
if more {
|
||||
items = items[:50]
|
||||
}
|
||||
writeFeedHistoryJSON(w, map[string]any{"actions": items, "more": more})
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func feedHistoryResponse(t *testing.T, s *Server, token, query string) ([]store.FeedActionRecord, bool) {
|
||||
t.Helper()
|
||||
w := feedRequestTest(s, token, "GET", "actions"+query, nil)
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if w.Header().Get("Cache-Control") != "private, no-store" {
|
||||
t.Fatal("private response cache policy")
|
||||
}
|
||||
var data struct {
|
||||
Actions []store.FeedActionRecord `json:"actions"`
|
||||
More bool `json:"more"`
|
||||
}
|
||||
if e := json.Unmarshal(w.Body.Bytes(), &data); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return data.Actions, data.More
|
||||
}
|
||||
|
||||
func TestFeedHistoryOrphansExportAndRecheck(t *testing.T) {
|
||||
s, token, domain, actor, source := feedActionFixture(t)
|
||||
ctx := context.Background()
|
||||
a, e := s.store.QueueFeedAction(ctx, domain, actor, newFeedAction(source))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
queued, _ := feedHistoryResponse(t, s, token, "?status=pending")
|
||||
if len(queued) != 1 || queued[0].Status != "queued" || queued[0].CanCheck {
|
||||
t.Fatal("queued history", queued)
|
||||
}
|
||||
fake := &feedRemoteFake{timeout: true}
|
||||
if e = s.syncFeedActions(ctx, fake.client(t)); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
list, _ := feedHistoryResponse(t, s, token, "")
|
||||
if len(list) != 1 || list[0].Status != "unknown" || list[0].CanCheck || list[0].CheckAfterMS <= time.Now().UnixMilli() || !list[0].ItemPresent || !list[0].SourcePresent {
|
||||
t.Fatal("unknown cooldown", list)
|
||||
}
|
||||
if _, e = s.store.DB.Exec(`DELETE FROM feed_items WHERE source_uuid=?`, source.UUID); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.DB.Exec(`UPDATE feed_actions SET until_ms=0 WHERE request_id=?`, a.RequestID); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
// Read-only connector opt-out does not disable read-only result checks.
|
||||
source.Credential, source.WriteEnabled = "", false
|
||||
if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
list, _ = feedHistoryResponse(t, s, token, "?status=unknown")
|
||||
if len(list) != 1 || list[0].ItemPresent || !list[0].CanCheck || list[0].ItemTitle != "" || list[0].SourceTitle != "Remote" {
|
||||
t.Fatal("evicted item lost", list)
|
||||
}
|
||||
w := feedRequestTest(s, token, "GET", "action/export?request_id="+a.RequestID, nil)
|
||||
if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Disposition"), a.RequestID+".json") || w.Header().Get("X-Content-Type-Options") != "nosniff" {
|
||||
t.Fatal("export", w.Code, w.Header())
|
||||
}
|
||||
var exported struct {
|
||||
Format string `json:"format"`
|
||||
Action store.FeedActionRecord `json:"action"`
|
||||
}
|
||||
if e = json.Unmarshal(w.Body.Bytes(), &exported); e != nil || exported.Format != "pageswiki.feed-action" || exported.Action.RequestID != a.RequestID || exported.Action.Status != "unknown" {
|
||||
t.Fatal("export data", exported, e)
|
||||
}
|
||||
for _, secret := range []string{"credential", "secret", source.URL, `"claim"`, `"until_ms"`} {
|
||||
if strings.Contains(w.Body.String(), secret) {
|
||||
t.Fatal("export leaked internal credentials or worker metadata", secret)
|
||||
}
|
||||
}
|
||||
if w = feedRequestTest(s, token, "POST", "action/check", map[string]string{"request_id": a.RequestID}); w.Code != 202 {
|
||||
t.Fatal(w.Code, w.Body)
|
||||
}
|
||||
if e = s.syncFeedActions(ctx, fake.client(t)); e != nil || fake.edits != 1 {
|
||||
t.Fatal("recheck sent a second mutation", fake.edits, e)
|
||||
}
|
||||
list, _ = feedHistoryResponse(t, s, token, "")
|
||||
if len(list) != 0 {
|
||||
t.Fatal("confirmed action unresolved", list)
|
||||
}
|
||||
list, _ = feedHistoryResponse(t, s, token, "?status=confirmed")
|
||||
if len(list) != 1 || list[0].Status != "confirmed" || list[0].ItemPresent {
|
||||
t.Fatal("evicted receipt missing", list)
|
||||
}
|
||||
if _, e = s.store.DB.Exec(`UPDATE feed_actions SET status='unknown' WHERE request_id=?`, a.RequestID); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.DB.Exec(`DELETE FROM feed_sources WHERE uuid=?`, source.UUID); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
list, _ = feedHistoryResponse(t, s, token, "?source="+source.UUID)
|
||||
if len(list) != 1 || list[0].SourcePresent || list[0].ItemPresent || list[0].CanCheck || list[0].Error != "source_deleted" {
|
||||
t.Fatal("deleted source receipt lost", list)
|
||||
}
|
||||
if w = feedRequestTest(s, token, "POST", "action/check", map[string]string{"request_id": a.RequestID}); w.Code != 412 {
|
||||
t.Fatal("deleted source recheck accepted", w.Code)
|
||||
}
|
||||
if w = feedRequestTest(s, token, "GET", "action/export?request_id="+a.RequestID, nil); w.Code != 200 {
|
||||
t.Fatal("deleted source export", w.Code)
|
||||
}
|
||||
source.Version, source.Credential = 0, ""
|
||||
source.URL = "https://different.example.org/api/greader.php"
|
||||
if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); !errors.Is(e, store.ErrTaskConflict) {
|
||||
t.Fatal("rebound orphan receipt", e)
|
||||
}
|
||||
other, e := s.store.CreatePasswordAccount(domain, "historyreuse", "long test password", "Other", "")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.SaveFeedSource(ctx, domain, other.UUID, source); !errors.Is(e, store.ErrTaskConflict) {
|
||||
t.Fatal("rebound unresolved receipt across owners", e)
|
||||
}
|
||||
if _, e = s.store.DB.Exec(`UPDATE feed_actions SET status='confirmed' WHERE request_id=?`, a.RequestID); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); !errors.Is(e, store.ErrTaskConflict) {
|
||||
t.Fatal("rebound terminal receipt", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFeedHistoryPaginationValidationAndIsolation(t *testing.T) {
|
||||
s, token, domain, actor, source := feedActionFixture(t)
|
||||
ctx := context.Background()
|
||||
// Same millisecond, stable unique tie-breaker; well above a page boundary.
|
||||
for i := 0; i < 107; i++ {
|
||||
if _, e := s.store.DB.Exec(`INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES(?,?,?,?,?,'read',1,1,'confirmed',1234)`, domain, actor, uuid.NewString(), source.UUID, fmt.Sprint(i)); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
query, seen := "?status=all", map[string]bool{}
|
||||
for {
|
||||
list, more := feedHistoryResponse(t, s, token, query)
|
||||
if len(list) > 50 {
|
||||
t.Fatal("unbounded list")
|
||||
}
|
||||
for _, a := range list {
|
||||
if seen[a.RequestID] {
|
||||
t.Fatal("duplicate cursor record")
|
||||
}
|
||||
seen[a.RequestID] = true
|
||||
}
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
last := list[len(list)-1]
|
||||
query = fmt.Sprintf("?status=all&before=%d&key=%s", last.CreatedMS, last.RequestID)
|
||||
}
|
||||
if len(seen) != 107 {
|
||||
t.Fatal("cursor omissions", len(seen))
|
||||
}
|
||||
for _, query := range []string{"?status=bogus", "?source=bad", "?before=bad", "?before=-1", "?before=1", "?key=" + uuid.NewString(), "?before=1&key=bad"} {
|
||||
if w := feedRequestTest(s, token, "GET", "actions"+query, nil); w.Code != 422 {
|
||||
t.Fatal("invalid query", query, w.Code)
|
||||
}
|
||||
}
|
||||
other, e := s.store.CreatePasswordAccount(domain, "historyother", "long test password", "Other", "")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
otherToken, e := s.store.CreateAuthSession(domain, other.UUID)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
otherList, _ := feedHistoryResponse(t, s, otherToken, "?status=all&source="+source.UUID)
|
||||
if len(otherList) != 0 {
|
||||
t.Fatal("other owner's HTTP history leaked")
|
||||
}
|
||||
list, e := s.store.FeedActionHistory(ctx, domain, other.UUID, store.FeedActionQuery{Source: source.UUID, Status: "all", Before: -1})
|
||||
if e != nil || len(list) != 0 {
|
||||
t.Fatal("other owner's actions leaked", list, e)
|
||||
}
|
||||
for request := range seen {
|
||||
if w := feedRequestTest(s, otherToken, "GET", "action/export?request_id="+request, nil); w.Code != 404 {
|
||||
t.Fatal("other owner's HTTP export", w.Code)
|
||||
}
|
||||
if _, e = s.store.FeedActionRecord(ctx, domain, other.UUID, request); !errors.Is(e, sql.ErrNoRows) {
|
||||
t.Fatal("other owner's receipt leaked", e)
|
||||
}
|
||||
break
|
||||
}
|
||||
if _, e = s.store.FeedActionHistory(ctx, uuid.NewString(), actor, store.FeedActionQuery{Status: "all", Before: -1}); !errors.Is(e, store.ErrAccessDenied) {
|
||||
t.Fatal("other domain", e)
|
||||
}
|
||||
for _, key := range []string{"actions", "action/export?request_id=" + uuid.NewString()} {
|
||||
if w := feedRequestTest(s, "", "GET", key, nil); w.Code != 401 {
|
||||
t.Fatal("anonymous access", w.Code)
|
||||
}
|
||||
}
|
||||
if w := feedRequestTest(s, token, "GET", "action/export?request_id=invalid", nil); w.Code != 422 {
|
||||
t.Fatal("invalid receipt identifier", w.Code)
|
||||
}
|
||||
if e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if w := feedRequestTest(s, token, "GET", "actions", nil); w.Code != 404 {
|
||||
t.Fatal("extension disabled", w.Code)
|
||||
}
|
||||
if _, e = s.store.FeedActionRecord(ctx, domain, actor, uuid.NewString()); !errors.Is(e, store.ErrAccessDenied) {
|
||||
t.Fatal("store disabled", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFeedHistoryReadonlyAndSourceDisabled(t *testing.T) {
|
||||
s, token, domain, actor, source := feedActionFixture(t)
|
||||
ctx := context.Background()
|
||||
a, e := s.store.QueueFeedAction(ctx, domain, actor, newFeedAction(source))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.DB.Exec(`UPDATE feed_actions SET status='unknown',until_ms=0 WHERE request_id=?`, a.RequestID); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = s.store.SetPrincipalState(domain, actor, "readonly"); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
token, e = s.store.CreateAuthSession(domain, actor)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
list, _ := feedHistoryResponse(t, s, token, "")
|
||||
if len(list) != 1 || list[0].CanCheck {
|
||||
t.Fatal("readonly check permission", list)
|
||||
}
|
||||
if w := feedRequestTest(s, token, "POST", "action/check", map[string]string{"request_id": a.RequestID}); w.Code != 403 {
|
||||
t.Fatal("readonly mutation", w.Code)
|
||||
}
|
||||
if w := feedRequestTest(s, token, "GET", "action/export?request_id="+a.RequestID, nil); w.Code != 200 {
|
||||
t.Fatal("readonly export", w.Code)
|
||||
}
|
||||
if e = s.store.SetPrincipalState(domain, actor, "active"); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
token, e = s.store.CreateAuthSession(domain, actor)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
source.Credential, source.Enabled = "", false
|
||||
if _, e = s.store.SaveFeedSource(ctx, domain, actor, source); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
list, _ = feedHistoryResponse(t, s, token, "")
|
||||
if len(list) != 1 || list[0].CanCheck || !list[0].SourcePresent {
|
||||
t.Fatal("disabled source recheck", list)
|
||||
}
|
||||
}
|
||||
@@ -591,6 +591,28 @@ func TestFeedsChromium(t *testing.T) {
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/fixture/feed-history" && r.Method == "POST" {
|
||||
missing, orphan := uuid.NewString(), uuid.NewString()
|
||||
for _, entry := range []struct{ request, source, item string }{{missing, fresh.UUID, "history-evicted"}, {orphan, uuid.NewString(), `<img src=x onerror=alert(1)>`}} {
|
||||
if _, e := s.store.DB.Exec(`INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES(?,?,?,?,?,'read',1,1,'unknown',?)`, domain, actor, entry.request, entry.source, entry.item, time.Now().UnixMilli()); e != nil {
|
||||
t.Error(e)
|
||||
http.Error(w, "fixture insert failed", 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
writeEventJSON(w, 200, map[string]string{"missing": missing, "orphan": orphan})
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/fixture/check-feed-history" && r.Method == "POST" {
|
||||
fake := &feedRemoteFake{id: "history-evicted", read: true}
|
||||
if e := s.syncFeedActions(r.Context(), fake.client(t)); e != nil || fake.edits != 0 {
|
||||
t.Errorf("history read-only check: %v edits=%d", e, fake.edits)
|
||||
http.Error(w, "test recheck failed", 500)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/fixture" {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprint(w, "<!doctype html><body style='margin:0'><script>"+string(script)+"</script></body>")
|
||||
@@ -600,7 +622,7 @@ func TestFeedsChromium(t *testing.T) {
|
||||
}))
|
||||
defer web.Close()
|
||||
for _, width := range []string{"1440", "390"} {
|
||||
if _, e = s.store.DB.Exec(`DELETE FROM feed_actions WHERE source_uuid=?`, fresh.UUID); e != nil {
|
||||
if _, e = s.store.DB.Exec(`DELETE FROM feed_actions`); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.store.DB.Exec(`UPDATE feed_items SET is_read=0 WHERE source_uuid=?`, fresh.UUID); e != nil {
|
||||
|
||||
Vendored
+12
-2
@@ -41,7 +41,17 @@
|
||||
// Restore the synthetic source to read-only for the second viewport run.
|
||||
const api='/_/api/v1/extension/organizer.feeds/',source=(await (await w.fetch(api+'sources')).json()).sources.find(s=>s.title==='FreshRSS fixture');
|
||||
await w.fetch(api+'sources',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({uuid:source.uuid,version:source.version,provider:source.provider,title:source.title,folder:source.folder,url:source.url,interval_seconds:source.interval_seconds,enabled:true,write_enabled:false})});
|
||||
if(w.innerWidth<=600)d.querySelector('#feed-toolbar button').click();
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'Catalog overflow');document.body.dataset.vimReady='1';
|
||||
const seed=await (await w.fetch('/fixture/feed-history',{method:'POST'})).json();
|
||||
d.getElementById('feed-history').click();await wait(()=>d.querySelectorAll('#feed-history-panel tbody tr').length===2);
|
||||
const receipt=id=>d.querySelector('#feed-history-panel tr[data-request-id="'+id+'"]');
|
||||
assert(d.querySelector('.feed-layout').classList.contains('action-history'),'History full panel');assert(!receipt(seed.missing).querySelector('.feed-history-check').disabled,'Evicted item check unavailable');assert(receipt(seed.orphan).querySelector('.feed-history-check').disabled,'Deleted source check available');assert(!receipt(seed.orphan).querySelector('img'),'Untrusted ID rendered as HTML');
|
||||
const exported=await (await w.fetch(receipt(seed.orphan).querySelector('.feed-history-export').href)).json();assert(exported.format==='pageswiki.feed-action'&&exported.action.request_id===seed.orphan&&!exported.action.source_present,'Orphan receipt export');
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'History overflow');
|
||||
receipt(seed.missing).querySelector('.feed-history-check').click();await wait(()=>receipt(seed.missing)?.querySelector('[data-state=check_queued]'));
|
||||
assert((await w.fetch('/fixture/check-feed-history',{method:'POST'})).status===204,'Recheck worker failed');d.getElementById('feed-history-reload').click();await wait(()=>!receipt(seed.missing)&&receipt(seed.orphan));
|
||||
const hs=d.getElementById('feed-history-state');hs.value='all';hs.dispatchEvent(new w.Event('change'));await wait(()=>receipt(seed.missing)?.querySelector('[data-state=confirmed]'));
|
||||
d.dispatchEvent(new w.KeyboardEvent('keydown',{key:'Escape',bubbles:true}));assert(!d.querySelector('.feed-layout').classList.contains('action-history'),'History escape');
|
||||
d.getElementById('feed-history').click();await wait(()=>receipt(seed.orphan));d.getElementById('feed-history-state').value='all';d.getElementById('feed-history-state').dispatchEvent(new w.Event('change'));await wait(()=>receipt(seed.missing)?.querySelector('[data-state=confirmed]'));
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'History final overflow');document.body.dataset.vimReady='1';
|
||||
}catch(e){const out=document.createElement('pre');out.id='error';out.textContent=e.stack;document.body.append(out);}
|
||||
})();
|
||||
@@ -263,6 +263,17 @@ func (s *Store) SaveFeedSource(ctx context.Context, domain, owner string, v Feed
|
||||
if !errors.Is(lookup, sql.ErrNoRows) {
|
||||
return lookup
|
||||
}
|
||||
// Do not rebind retained receipts to a different remote identity.
|
||||
// Both predicates use existing indexes: owner history and the global
|
||||
// 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 {
|
||||
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 {
|
||||
return e
|
||||
|
||||
@@ -92,7 +92,7 @@ func (s *Store) QueueFeedAction(ctx context.Context, domain, owner string, a Fee
|
||||
// Recheck is deliberately read-only at the remote side, even with writeback OFF.
|
||||
func (s *Store) RecheckFeedAction(ctx context.Context, domain, owner, request string) error {
|
||||
return s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
|
||||
r, e := tx.ExecContext(ctx, `UPDATE feed_actions SET status='check_queued',error='' WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND status='unknown' AND until_ms<? AND EXISTS(SELECT 1 FROM feed_sources s WHERE s.uuid=feed_actions.source_uuid AND s.enabled=1)`, domain, owner, request, time.Now().UnixMilli())
|
||||
r, e := tx.ExecContext(ctx, `UPDATE feed_actions SET status='check_queued',error='' WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND status='unknown' AND until_ms<? AND EXISTS(SELECT 1 FROM feed_sources s WHERE s.uuid=feed_actions.source_uuid AND s.domain_uuid=feed_actions.domain_uuid AND s.owner_uuid=feed_actions.owner_uuid AND s.provider='freshrss' AND s.enabled=1)`, domain, owner, request, time.Now().UnixMilli())
|
||||
return feedActionAffected(r, e)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// FeedActionRecord is a receipt plus currently available local metadata. A
|
||||
// deleted source or evicted article must not hide an unresolved remote write.
|
||||
type FeedActionRecord struct {
|
||||
FeedAction
|
||||
SourceTitle string `json:"source_title"`
|
||||
ItemTitle string `json:"item_title"`
|
||||
SourcePresent bool `json:"source_present"`
|
||||
ItemPresent bool `json:"item_present"`
|
||||
CanCheck bool `json:"can_check"`
|
||||
CheckAfterMS int64 `json:"check_after_ms"`
|
||||
}
|
||||
|
||||
type FeedActionQuery struct {
|
||||
Source string
|
||||
Status string
|
||||
Before int64
|
||||
Key string
|
||||
}
|
||||
|
||||
const feedActionMetadata = `,COALESCE(s.title,''),COALESCE(i.title,''),s.uuid IS NOT NULL,i.id IS NOT NULL,COALESCE(s.enabled=1 AND s.provider='freshrss',0)`
|
||||
const feedActionJoins = ` FROM feed_actions a LEFT JOIN feed_sources s ON s.uuid=a.source_uuid AND s.domain_uuid=a.domain_uuid AND s.owner_uuid=a.owner_uuid LEFT JOIN feed_items i ON i.source_uuid=s.uuid AND i.id=a.item_id WHERE a.domain_uuid=? AND a.owner_uuid=?`
|
||||
|
||||
func feedActionRecordColumns() string {
|
||||
return "a." + strings.ReplaceAll(feedActionColumns, ",", ",a.") + feedActionMetadata
|
||||
}
|
||||
|
||||
func scanFeedActionRecord(row interface{ Scan(...any) error }, writable bool) (a FeedActionRecord, e error) {
|
||||
var enabled bool
|
||||
e = row.Scan(&a.Domain, &a.Owner, &a.RequestID, &a.Source, &a.ID, &a.Field, &a.Desired, &a.SourceVersion, &a.Status, &a.Claim, &a.UntilMS, &a.CreatedMS, &a.Error, &a.SourceTitle, &a.ItemTitle, &a.SourcePresent, &a.ItemPresent, &enabled)
|
||||
if a.Status == "unknown" {
|
||||
a.CheckAfterMS = a.UntilMS
|
||||
a.CanCheck = writable && enabled && a.UntilMS < time.Now().UnixMilli()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// FeedActionHistory returns at most 51 local receipts (50 plus a next-page
|
||||
// sentinel). The owner cap of 10,000 receipts bounds the sorting work even
|
||||
// without a new schema/index. No fetch, retry, expiry or deletion runs here.
|
||||
func (s *Store) FeedActionHistory(ctx context.Context, domain, owner string, q FeedActionQuery) ([]FeedActionRecord, error) {
|
||||
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
where := ""
|
||||
switch q.Status {
|
||||
case "", "unresolved":
|
||||
where = ` AND a.status NOT IN ('confirmed','cancelled')`
|
||||
case "all":
|
||||
case "pending":
|
||||
where = ` AND a.status IN ('queued','preparing','sending','check_queued','checking')`
|
||||
case "unknown", "confirmed", "failed", "conflict", "cancelled":
|
||||
where = ` AND a.status='` + q.Status + `'`
|
||||
default:
|
||||
return nil, ErrTaskInvalid
|
||||
}
|
||||
args := []any{domain, owner}
|
||||
if q.Source != "" {
|
||||
if _, e := uuid.Parse(q.Source); e != nil {
|
||||
return nil, ErrTaskInvalid
|
||||
}
|
||||
where += ` AND a.source_uuid=?`
|
||||
args = append(args, q.Source)
|
||||
}
|
||||
if q.Before >= 0 {
|
||||
if _, e := uuid.Parse(q.Key); e != nil {
|
||||
return nil, ErrTaskInvalid
|
||||
}
|
||||
where += ` AND (a.created_ms<? OR (a.created_ms=? AND a.request_id<?))`
|
||||
args = append(args, q.Before, q.Before, q.Key)
|
||||
} else if q.Before != -1 || q.Key != "" {
|
||||
return nil, ErrTaskInvalid
|
||||
}
|
||||
writable := !s.ReadOnly && feedActor(ctx, s.DB, domain, owner, true) == nil
|
||||
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedActionRecordColumns()+feedActionJoins+where+` ORDER BY a.created_ms DESC,a.request_id DESC LIMIT 51`, args...)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []FeedActionRecord{}
|
||||
for rows.Next() {
|
||||
a, e := scanFeedActionRecord(rows, writable)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) FeedActionRecord(ctx context.Context, domain, owner, request string) (FeedActionRecord, error) {
|
||||
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
|
||||
return FeedActionRecord{}, e
|
||||
}
|
||||
if _, e := uuid.Parse(request); e != nil {
|
||||
return FeedActionRecord{}, ErrTaskInvalid
|
||||
}
|
||||
writable := !s.ReadOnly && feedActor(ctx, s.DB, domain, owner, true) == nil
|
||||
return scanFeedActionRecord(s.DB.QueryRowContext(ctx, `SELECT `+feedActionRecordColumns()+feedActionJoins+` AND a.request_id=?`, domain, owner, request), writable)
|
||||
}
|
||||
Reference in new issue
Block a user