feat(feeds): queue explicit FreshRSS state changes with uncertain-outcome recovery
Build and publish container images / images (push) Failing after 12m40s

This commit is contained in:
Anya Tokugawa committed 2026-11-08 05:51:47 +09:00
1 parent 16e572397b
commit 131a7ca01f
12 files changed
+973 -58

No files matched your search

+5 -2
View File
@@ -42,6 +42,9 @@ func (s *Server) handleFeeds(w http.ResponseWriter, r *http.Request) {
return
}
key := strings.TrimPrefix(r.URL.Path, feedsAPI)
if s.handleFeedAction(w, r, domain, actor, key) {
return
}
if strings.HasPrefix(key, "opml/") {
s.handleFeedOPML(w, r, domain, actor, key)
return
@@ -211,6 +214,6 @@ func (s *Server) handleFeeds(w http.ResponseWriter, r *http.Request) {
func init() {
translations["en"]["feeds.title"] = "RSS"
translations["ja"]["feeds.title"] = "RSS"
translations["en"]["feeds.help"] = "Private local feeds and read-only FreshRSS integration"
translations["ja"]["feeds.help"] = "個人用の簡易RSSとFreshRSS読取連携"
translations["en"]["feeds.help"] = "Private local feeds and FreshRSS integration with optional read/star updates"
translations["ja"]["feeds.help"] = "個人用の簡易RSSとFreshRSS連携(既読・star変更は任意)"
}
+171
View File
@@ -0,0 +1,171 @@
package server
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"time"
"pageswiki/internal/store"
)
func freshRSSItem(ctx context.Context, client *http.Client, base, auth, id string) (*store.FeedItem, error) {
// FreshRSS uses POST for this read-only item lookup (not GET ?i=...).
raw, _, _, e := feedRequest(ctx, client, "POST", base+"/reader/api/0/stream/items/contents", auth, url.Values{"i": {id}}, nil)
if e != nil {
return nil, e
}
items, e := parseFreshRSSItems(raw, base)
if e != nil {
return nil, e
}
if len(items) != 1 || items[0].ID != id {
return nil, errFeedInvalid
}
return &items[0], nil
}
func feedActionMatches(a store.FeedAction, i *store.FeedItem) bool {
if a.Field == "read" {
return i.Read == a.Desired
}
return i.Starred == a.Desired
}
func (s *Server) runFeedAction(ctx context.Context, client *http.Client, a store.FeedAction, source store.FeedSource) (status, code string, observed *store.FeedItem) {
status = "failed"
if a.Status == "checking" {
status = "unknown"
}
fail := func(e error) (string, string, *store.FeedItem) {
c := e.Error()
switch c {
case "network", "credentials", "remote_error", "limit", "invalid_feed":
default:
c = "invalid_feed"
}
return status, c, nil
}
if !validCalendarSourceURL(source.URL) {
return fail(errFeedInvalid)
}
user, password, e := s.store.OpenFeedCredential(source)
if e != nil {
return fail(errors.New("credentials"))
}
auth, e := freshRSSLogin(ctx, client, source, user, password)
if e != nil {
return fail(e)
}
base := strings.TrimRight(source.URL, "/")
item, e := freshRSSItem(ctx, client, base, auth, a.ID)
if e != nil {
return fail(e)
}
if feedActionMatches(a, item) {
return "confirmed", "", item
}
if a.Status == "checking" {
return "conflict", "conflict", item
}
raw, _, _, e := feedRequest(ctx, client, "GET", base+"/reader/api/0/token", auth, nil, nil)
if e != nil {
return fail(e)
}
token := strings.TrimSpace(string(raw))
if token == "" || len(token) > 4096 || strings.ContainsAny(token, "\r\n") {
return fail(errFeedInvalid)
}
if e = s.store.MarkFeedActionSending(ctx, a, source); e != nil {
return fail(e)
}
// From this point every uncertain result is terminal until an explicit
// read-only recheck. Neither the worker nor a repeated HTTP request resends.
status = "unknown"
field := "r"
if a.Desired {
field = "a"
}
form := url.Values{"i": {a.ID}, "T": {token}, field: {"user/-/state/com.google/" + a.Field}}
raw, _, _, e = feedRequest(ctx, client, "POST", base+"/reader/api/0/edit-tag", auth, form, nil)
if e != nil {
return fail(e)
}
if strings.TrimSpace(string(raw)) != "OK" {
return fail(errFeedInvalid)
}
item, e = freshRSSItem(ctx, client, base, auth, a.ID)
if e != nil {
return fail(e)
}
if !feedActionMatches(a, item) {
return "conflict", "conflict", item
}
return "confirmed", "", item
}
func (s *Server) syncFeedActions(ctx context.Context, client *http.Client) error {
actions, e := s.store.DueFeedActions(ctx)
if e != nil {
return e
}
for _, a := range actions {
if ctx.Err() != nil {
break
}
if deadline, ok := ctx.Deadline(); ok && time.Until(deadline) < 9*time.Second {
break
}
a, source, e := s.store.ClaimFeedAction(ctx, a)
if e != nil {
continue
}
child, cancel := context.WithTimeout(ctx, 8*time.Second)
status, code, item := s.runFeedAction(child, client, a, source)
cancel()
e = s.store.CompleteFeedAction(ctx, a, source, status, code, item)
if e != nil && !errors.Is(e, store.ErrAccessDenied) && !errors.Is(e, store.ErrTaskConflict) {
return e
}
}
return nil
}
func (s *Server) handleFeedAction(w http.ResponseWriter, r *http.Request, domain, actor, key string) bool {
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"))
if e != nil {
taskError(w, e)
return true
}
writeEventJSON(w, 200, map[string]any{"action": a})
return true
case key == "remote-state" && r.Method == "POST":
var input store.FeedAction
if !taskJSON(w, r, &input) {
return true
}
a, e := s.store.QueueFeedAction(r.Context(), domain, actor, input)
if e != nil {
taskError(w, e)
return true
}
writeEventJSON(w, 202, map[string]any{"action": a})
return true
case key == "action/check" && r.Method == "POST":
var input struct {
RequestID string `json:"request_id"`
}
if !taskJSON(w, r, &input) {
return true
}
if e := s.store.RecheckFeedAction(r.Context(), domain, actor, input.RequestID); e != nil {
taskError(w, e)
return true
}
writeEventJSON(w, 202, map[string]bool{"ok": true})
return true
}
return false
}
+404
View File
@@ -0,0 +1,404 @@
package server
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func feedActionFixture(t *testing.T) (*Server, string, string, string, store.FeedSource) {
t.Helper()
s, token, domain, actor, _ := feedFixture(t)
ctx := context.Background()
v := store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "Remote", URL: "https://example.org/api/greader.php", Enabled: true, WriteEnabled: true, Interval: 300}
v.Credential, _ = s.store.SealFeedCredential(domain, actor, v.UUID, "reader", "secret")
if _, e := s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
all, e := s.store.FeedSources(ctx, domain, actor)
if e != nil {
t.Fatal(e)
}
for _, x := range all {
if x.UUID == v.UUID {
v = x
}
}
c, e := s.store.ClaimFeed(ctx, v)
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(ctx, c, []store.FeedItem{{ID: "one", Title: "Article", Starred: true}}, "", "", "", false); e != nil {
t.Fatal(e)
}
return s, token, domain, actor, v
}
func newFeedAction(v store.FeedSource) store.FeedAction {
return store.FeedAction{RequestID: uuid.NewString(), Source: v.UUID, ID: "one", Field: "read", Desired: true, SourceVersion: v.Version}
}
type feedRemoteFake struct {
id string
read, star, timeout bool
edits, lookups, requests int
beforeEdit func()
afterEdit func()
}
func (f *feedRemoteFake) client(t *testing.T) *http.Client {
return &http.Client{Transport: feedTransportFunc(func(r *http.Request) (*http.Response, error) {
id := f.id
if id == "" {
id = "one"
}
f.requests++
body := ""
switch {
case strings.HasSuffix(r.URL.Path, "/ClientLogin"):
body = "Auth=test-auth"
case strings.HasSuffix(r.URL.Path, "/stream/items/contents"):
if r.Method != "POST" {
t.Fatal("read lookup requires POST")
}
r.ParseForm()
if r.Form.Get("i") != id {
t.Fatal("wrong item")
}
f.lookups++
cats := []string{}
if f.read {
cats = append(cats, "user/reader/state/com.google/read")
}
if f.star {
cats = append(cats, "user/reader/state/com.google/starred")
}
raw, _ := json.Marshal(map[string]any{"items": []map[string]any{{"id": id, "title": "Article", "categories": cats}}})
body = string(raw)
case strings.HasSuffix(r.URL.Path, "/token"):
body = "write-token"
if f.beforeEdit != nil {
f.beforeEdit()
}
case strings.HasSuffix(r.URL.Path, "/edit-tag"):
if r.Method != "POST" {
t.Fatal("mutation method")
}
r.ParseForm()
if len(r.Form) != 3 || r.Form.Get("T") != "write-token" || r.Form.Get("i") != id {
t.Fatal("unexpected mutation fields", r.Form)
}
if tag := r.Form.Get("a"); tag != "" {
switch tag {
case "user/-/state/com.google/read":
f.read = true
case "user/-/state/com.google/starred":
f.star = true
default:
t.Fatal("wrong tag", tag)
}
} else {
switch r.Form.Get("r") {
case "user/-/state/com.google/read":
f.read = false
case "user/-/state/com.google/starred":
f.star = false
default:
t.Fatal("wrong tag")
}
}
f.edits++
if f.afterEdit != nil {
f.afterEdit()
}
if f.timeout {
return nil, errors.New("synthetic response lost after apply")
}
body = "OK"
default:
t.Fatal("unexpected path", r.URL.Path)
}
return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))}, nil
})}
}
func TestFeedActionWritebackReceiptAndIsolation(t *testing.T) {
s, token, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
w := feedRequestTest(s, token, "POST", "remote-state", a)
if w.Code != 202 {
t.Fatal(w.Code, w.Body)
}
if w = feedRequestTest(s, token, "POST", "remote-state", a); w.Code != 202 {
t.Fatal("replay", w.Code, w.Body)
}
changed := a
changed.Desired = false
if w = feedRequestTest(s, token, "POST", "remote-state", changed); w.Code != 412 {
t.Fatal("changed replay", w.Code)
}
changed.RequestID = uuid.NewString()
if w = feedRequestTest(s, token, "POST", "remote-state", changed); w.Code != 412 {
t.Fatal("parallel intent", w.Code)
}
item, _ := s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if item.Read {
t.Fatal("optimistic cache mutation")
}
f := &feedRemoteFake{star: true}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
got, e := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if e != nil || got.Status != "confirmed" || f.edits != 1 {
t.Fatal(got, e, f.edits)
}
item, _ = s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if !item.Read || !item.Starred {
t.Fatal("unrelated star overwritten", item)
}
if w = feedRequestTest(s, token, "POST", "remote-state", a); w.Code != 202 {
t.Fatal("receipt replay", w.Code)
}
if e = s.syncFeedActions(ctx, f.client(t)); e != nil || f.edits != 1 {
t.Fatal("replayed write", e, f.edits)
}
// A second explicit intent already satisfied remotely causes no edit-tag.
a.RequestID = uuid.NewString()
if _, e = s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
if e = s.syncFeedActions(ctx, f.client(t)); e != nil || f.edits != 1 {
t.Fatal("redundant write", e)
}
other, e := s.store.CreatePasswordAccount(domain, "actionother", "long test password", "Other", "")
if e != nil {
t.Fatal(e)
}
if got, e = s.store.FeedAction(ctx, domain, other.UUID, v.UUID, "one"); e != nil || got != nil {
t.Fatal("other owner receipt", got, e)
}
a.RequestID = uuid.NewString()
if _, e = s.store.QueueFeedAction(ctx, domain, other.UUID, a); e == nil {
t.Fatal("other owner mutation")
}
v.Credential = ""
v.WriteEnabled = false
if _, e = s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
a.SourceVersion++
if _, e = s.store.QueueFeedAction(ctx, domain, actor, a); e == nil {
t.Fatal("opt-out ignored")
}
}
func TestFeedActionUnknownNeverResendsAndReadOnlyRecheck(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
if _, e := s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
f := &feedRemoteFake{star: true, timeout: true}
client := f.client(t)
if e := s.syncFeedActions(ctx, client); e != nil {
t.Fatal(e)
}
got, e := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if e != nil || got.Status != "unknown" {
t.Fatal(got, e)
}
item, _ := s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if item.Read {
t.Fatal("unconfirmed cache")
}
requests := f.requests
if e = s.syncFeedActions(ctx, client); e != nil || f.requests != requests {
t.Fatal("automatic retry", e)
}
if e = s.store.RecheckFeedAction(ctx, domain, actor, a.RequestID); e == nil {
t.Fatal("must wait for claim expiry")
}
// Simulate the uncertainty cooldown without sleeping.
v.Credential = ""
v.WriteEnabled = false
if _, e = s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
s.store.DB.Exec(`UPDATE feed_actions SET until_ms=0 WHERE request_id=?`, a.RequestID)
if e = s.store.RecheckFeedAction(ctx, domain, actor, a.RequestID); e != nil {
t.Fatal(e)
}
if e = s.syncFeedActions(ctx, client); e != nil {
t.Fatal(e)
}
got, e = s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if e != nil || got.Status != "confirmed" || f.edits != 1 {
t.Fatal("recheck sent a write", got, e, f.edits)
}
}
func TestFeedActionConflictAndPostSendFence(t *testing.T) {
for _, changed := range []bool{false, true} {
t.Run(map[bool]string{false: "remote_conflict", true: "source_changed"}[changed], func(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
if _, e := s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
f := &feedRemoteFake{star: true}
f.afterEdit = func() {
if changed {
v.Credential = ""
v.WriteEnabled = false
if _, e := s.store.SaveFeedSource(ctx, domain, actor, v); e != nil {
t.Fatal(e)
}
} else {
f.read = false
}
}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
got, e := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
want := "conflict"
if changed {
want = "unknown"
}
if e != nil || got.Status != want || f.edits != 1 {
t.Fatal(got, e)
}
item, e := s.store.FeedItem(ctx, domain, actor, v.UUID, "one")
if e != nil || item.Read {
t.Fatal("unconfirmed/fenced value stored", item, e)
}
})
}
}
func TestFeedActionFencesAndRecovery(t *testing.T) {
for _, scenario := range []string{"config", "delete", "readonly", "extension", "crash"} {
t.Run(scenario, func(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
if _, e := s.store.QueueFeedAction(ctx, domain, actor, a); e != nil {
t.Fatal(e)
}
if scenario == "crash" {
due, e := s.store.DueFeedActions(ctx)
if e != nil || len(due) != 1 {
t.Fatal(e)
}
a, source, e := s.store.ClaimFeedAction(ctx, due[0])
if e != nil {
t.Fatal(e)
}
if _, e = s.store.ClaimFeed(ctx, v); !errors.Is(e, store.ErrTaskConflict) {
t.Fatal("fetch overlapped mutation", e)
}
if e = s.store.MarkFeedActionSending(ctx, a, source); e != nil {
t.Fatal(e)
}
s.store.DB.Exec(`UPDATE feed_actions SET until_ms=0 WHERE request_id=?`, a.RequestID)
due, e = s.store.DueFeedActions(ctx)
if e != nil || len(due) != 0 {
t.Fatal("crash requeued", due, e)
}
got, _ := s.store.FeedAction(ctx, domain, actor, v.UUID, "one")
if got.Status != "unknown" {
t.Fatal(got)
}
return
}
f := &feedRemoteFake{star: true}
f.beforeEdit = func() {
var e error
switch scenario {
case "config":
v.Credential = ""
v.WriteEnabled = false
_, e = s.store.SaveFeedSource(ctx, domain, actor, v)
case "delete":
e = s.store.FeedSourceAction(ctx, domain, actor, v.UUID, "delete", v.Version)
case "readonly":
e = s.store.SetPrincipalState(domain, actor, "readonly")
case "extension":
e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false)
}
if e != nil {
t.Fatal(e)
}
}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
if f.edits != 0 {
t.Fatal("sent after revocation")
}
})
}
}
func TestFeedActionSingleFieldAndConcurrentIntent(t *testing.T) {
for _, field := range []string{"read", "starred"} {
for _, desired := range []bool{false, true} {
t.Run(field+map[bool]string{false: "_remove", true: "_add"}[desired], func(t *testing.T) {
s, _, domain, actor, v := feedActionFixture(t)
ctx := context.Background()
a := newFeedAction(v)
a.Field = field
a.Desired = desired
results := make(chan error, 2)
for range 2 {
go func() {
copy := a
copy.RequestID = uuid.NewString()
_, e := s.store.QueueFeedAction(ctx, domain, actor, copy)
results <- e
}()
}
success := 0
for range 2 {
e := <-results
if e == nil {
success++
} else if !errors.Is(e, store.ErrTaskConflict) {
t.Fatal(e)
}
}
if success != 1 {
t.Fatal("concurrent intents accepted", success)
}
f := &feedRemoteFake{read: true, star: true}
if field == "read" {
f.read = !desired
} else {
f.star = !desired
}
if e := s.syncFeedActions(ctx, f.client(t)); e != nil {
t.Fatal(e)
}
if f.edits != 1 {
t.Fatal("wrong edit count", f.edits)
}
if field == "read" {
if f.read != desired || !f.star {
t.Fatal("wrong field changed")
}
} else {
if f.star != desired || !f.read {
t.Fatal("wrong field changed")
}
}
})
}
}
}
+36 -15
View File
@@ -328,11 +328,11 @@ func feedRequest(ctx context.Context, client *http.Client, method, target, auth
}
return raw, res.Header, res.StatusCode, nil
}
func fetchFreshRSS(ctx context.Context, client *http.Client, v store.FeedSource, user, password string, catalogs ...*[]store.FeedRemoteSubscription) ([]store.FeedItem, error) {
func freshRSSLogin(ctx context.Context, client *http.Client, v store.FeedSource, user, password string) (string, error) {
base := strings.TrimRight(v.URL, "/")
raw, _, _, e := feedRequest(ctx, client, "POST", base+"/accounts/ClientLogin", "", url.Values{"Email": {user}, "Passwd": {password}}, nil)
if e != nil {
return nil, e
return "", e
}
auth := ""
for _, line := range strings.Split(string(raw), "\n") {
@@ -341,12 +341,38 @@ func fetchFreshRSS(ctx context.Context, client *http.Client, v store.FeedSource,
}
}
if auth == "" || len(auth) > 4096 || strings.ContainsAny(auth, "\r\n") {
return nil, errors.New("credentials")
return "", errors.New("credentials")
}
raw, _, _, e = feedRequest(ctx, client, "GET", base+"/reader/api/0/stream/contents/reading-list?output=json&n=200", auth, nil, nil)
return auth, nil
}
func fetchFreshRSS(ctx context.Context, client *http.Client, v store.FeedSource, user, password string, catalogs ...*[]store.FeedRemoteSubscription) ([]store.FeedItem, error) {
base := strings.TrimRight(v.URL, "/")
auth, e := freshRSSLogin(ctx, client, v, user, password)
if e != nil {
return nil, e
}
raw, _, _, e := feedRequest(ctx, client, "GET", base+"/reader/api/0/stream/contents/reading-list?output=json&n=200", auth, nil, nil)
if e != nil {
return nil, e
}
items, e := parseFreshRSSItems(raw, base)
if e != nil {
return nil, e
}
if len(catalogs) > 0 {
raw, _, _, e = feedRequest(ctx, client, "GET", base+"/reader/api/0/subscription/list?output=json", auth, nil, nil)
if e != nil {
return nil, e
}
catalog, e := parseFreshRSSCatalog(raw)
if e != nil {
return nil, e
}
*catalogs[0] = catalog
}
return items, nil
}
func parseFreshRSSItems(raw []byte, base string) ([]store.FeedItem, error) {
var response struct {
Items []struct {
ID string `json:"id"`
@@ -411,17 +437,6 @@ func fetchFreshRSS(ctx context.Context, client *http.Client, v store.FeedSource,
}
out = append(out, item)
}
if len(catalogs) > 0 {
raw, _, _, e = feedRequest(ctx, client, "GET", base+"/reader/api/0/subscription/list?output=json", auth, nil, nil)
if e != nil {
return nil, e
}
catalog, e := parseFreshRSSCatalog(raw)
if e != nil {
return nil, e
}
*catalogs[0] = catalog
}
return out, nil
}
@@ -465,6 +480,9 @@ func (s *Server) syncFeedSources(ctx context.Context, job *store.EventJob) error
func (s *Server) syncFeedSourcesWithClient(ctx context.Context, job *store.EventJob, client *http.Client) error {
ctx, cancel := context.WithTimeout(ctx, 35*time.Second)
defer cancel()
if e := s.syncFeedActions(ctx, client); e != nil {
return e
}
sources, e := s.store.DueFeedSources(ctx)
if e != nil {
return e
@@ -474,6 +492,9 @@ func (s *Server) syncFeedSourcesWithClient(ctx context.Context, job *store.Event
if ctx.Err() != nil {
break
}
if deadline, ok := ctx.Deadline(); ok && time.Until(deadline) < 9*time.Second {
break
}
if !s.systemExtensionEnabled(v.Domain, "organizer.feeds") {
continue
}
+21 -1
View File
@@ -543,7 +543,12 @@ func TestFeedsChromium(t *testing.T) {
if e = s.store.CompleteFeed(context.Background(), claim, items, "", "", "", false); e != nil {
t.Fatal(e)
}
fresh, e := s.store.SaveFeedSource(context.Background(), domain, actor, store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "FreshRSS fixture", URL: "https://example.org/api/greader.php", Enabled: true, Interval: 300})
fresh := store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "FreshRSS fixture", URL: "https://example.org/api/greader.php", Enabled: true, Interval: 300}
fresh.Credential, e = s.store.SealFeedCredential(domain, actor, fresh.UUID, "fixture", "synthetic")
if e != nil {
t.Fatal(e)
}
fresh, e = s.store.SaveFeedSource(context.Background(), domain, actor, fresh)
if e != nil {
t.Fatal(e)
}
@@ -577,6 +582,15 @@ func TestFeedsChromium(t *testing.T) {
web := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
if r.URL.Path == "/fixture/run-feed-actions" && r.Method == "POST" {
fake := &feedRemoteFake{id: "catalog-b"}
if e := s.syncFeedActions(r.Context(), fake.client(t)); e != nil {
http.Error(w, "test worker 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>")
@@ -586,6 +600,12 @@ 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 {
t.Fatal(e)
}
if _, e = s.store.DB.Exec(`UPDATE feed_items SET is_read=0 WHERE source_uuid=?`, fresh.UUID); e != nil {
t.Fatal(e)
}
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", web.URL + "/fixture?width=" + url.QueryEscape(width), t.TempDir(), "/tmp/pageswiki-feeds-" + width + ".png", width, "850"}
out, e := exec.CommandContext(ctx, "node", args...).CombinedOutput()
+8
View File
@@ -33,6 +33,14 @@
d.getElementById('feed-category').value='';d.getElementById('feed-category').dispatchEvent(new w.Event('change'));await wait(()=>d.querySelectorAll('.feed-row').length===2);
d.querySelector('.feed-subscriptions button[data-feed="feed/2"]').click();await wait(()=>d.querySelectorAll('.feed-row').length===1&&d.querySelector('.feed-row').textContent.includes('Catalog second'));
d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body'));assert(d.querySelectorAll('#feed-reader .feed-actions button:disabled').length===2,'FreshRSS not read-only');
[...d.querySelectorAll('.feed-source')].find(n=>n.textContent.includes('FreshRSS fixture')).querySelector('button:last-child').click();await wait(()=>d.querySelector('#feed-reader form'));
const setting=d.querySelector('#feed-reader form');setting.querySelectorAll('input[type=checkbox]')[1].checked=true;setting.requestSubmit();await wait(()=>!d.querySelector('#feed-reader form')&&d.querySelectorAll('.feed-row').length===1);
d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body')&&!d.querySelectorAll('#feed-reader .feed-actions button')[1].disabled);
d.querySelectorAll('#feed-reader .feed-actions button')[1].click();await wait(()=>d.querySelector('.feed-action-status')?.textContent==='◷');assert(d.querySelector('.feed-row').classList.contains('unread'),'Queued state shown as confirmed');
const run=await w.fetch('/fixture/run-feed-actions',{method:'POST'});assert(run.status===204,'Background worker failed');await wait(()=>d.querySelector('.feed-action-status')?.textContent==='✓');assert(!d.querySelector('.feed-row').classList.contains('unread'),'Confirmed state not reflected');
// 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';
}catch(e){const out=document.createElement('pre');out.id='error';out.textContent=e.stack;document.body.append(out);}
+37 -23
View File
@@ -44,6 +44,16 @@ var feedMigrations = []db.SQLMigration{{Version: 1, Name: "private_feed_reader",
`ALTER TABLE feed_items ADD COLUMN remote_categories TEXT NOT NULL DEFAULT '[]'`,
`UPDATE feed_sources SET next_ms=0,lease='',lease_until=0,version=version+1 WHERE provider='freshrss'`,
}, Assertions: []string{schemaColumns("feed_sources", "remote_catalog"), schemaColumns("feed_items", "remote_feed,remote_categories")},
}, {
Version: 5, Name: "feed_explicit_remote_actions", Statements: []string{
`ALTER TABLE feed_sources ADD COLUMN write_enabled INTEGER NOT NULL DEFAULT 0`,
`CREATE TABLE feed_actions(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,request_id TEXT NOT NULL,source_uuid TEXT NOT NULL,item_id TEXT NOT NULL,field TEXT NOT NULL,desired INTEGER NOT NULL,source_version INTEGER NOT NULL,status TEXT NOT NULL,claim TEXT NOT NULL DEFAULT '',until_ms INTEGER NOT NULL DEFAULT 0,created_ms INTEGER NOT NULL,error TEXT NOT NULL DEFAULT '',PRIMARY KEY(domain_uuid,owner_uuid,request_id))`,
`CREATE INDEX feed_actions_due ON feed_actions(status,created_ms)`,
`CREATE INDEX feed_actions_item ON feed_actions(domain_uuid,owner_uuid,source_uuid,item_id,created_ms DESC)`,
`CREATE UNIQUE INDEX feed_actions_active ON feed_actions(source_uuid,item_id) WHERE status IN ('queued','preparing','sending','unknown','check_queued','checking')`,
`CREATE TRIGGER feed_actions_source_change AFTER UPDATE OF version ON feed_sources BEGIN UPDATE feed_actions SET status=CASE WHEN status IN ('sending','unknown','check_queued','checking') THEN 'unknown' ELSE 'cancelled' END,error='source_changed' WHERE source_uuid=old.uuid AND status IN ('queued','preparing','sending','unknown','check_queued','checking'); END`,
`CREATE TRIGGER feed_actions_source_delete AFTER DELETE ON feed_sources BEGIN UPDATE feed_actions SET status=CASE WHEN status IN ('sending','unknown','check_queued','checking') THEN 'unknown' ELSE 'cancelled' END,error='source_deleted' WHERE source_uuid=old.uuid AND status IN ('queued','preparing','sending','unknown','check_queued','checking'); END`,
}, Assertions: []string{schemaColumns("feed_sources", "write_enabled"), schemaUnique("feed_actions", "domain_uuid,owner_uuid,request_id")},
}}
// CaptureFeedNote creates an inert Wiki note and receipt atomically. It does not
@@ -111,24 +121,25 @@ func (s *Store) CaptureFeedNote(ctx context.Context, domain, owner, request, sou
}
type FeedSource struct {
UUID string `json:"uuid"`
Domain string `json:"-"`
Owner string `json:"-"`
Provider string `json:"provider"`
Title string `json:"title"`
Folder string `json:"folder"`
URL string `json:"url"`
Credential string `json:"-"`
Version int64 `json:"version"`
Enabled bool `json:"enabled"`
Interval int `json:"interval_seconds"`
NextMS int64 `json:"next_ms"`
Lease string `json:"-"`
LeaseUntil int64 `json:"-"`
LastMS int64 `json:"last_ms"`
Error string `json:"error"`
ETag string `json:"-"`
Modified string `json:"-"`
UUID string `json:"uuid"`
Domain string `json:"-"`
Owner string `json:"-"`
Provider string `json:"provider"`
Title string `json:"title"`
Folder string `json:"folder"`
URL string `json:"url"`
Credential string `json:"-"`
Version int64 `json:"version"`
Enabled bool `json:"enabled"`
Interval int `json:"interval_seconds"`
NextMS int64 `json:"next_ms"`
Lease string `json:"-"`
LeaseUntil int64 `json:"-"`
LastMS int64 `json:"last_ms"`
Error string `json:"error"`
ETag string `json:"-"`
Modified string `json:"-"`
WriteEnabled bool `json:"write_enabled"`
}
type FeedItem struct {
Source string `json:"source"`
@@ -148,10 +159,10 @@ type FeedItem struct {
RemoteCategories []string `json:"remote_categories,omitempty"`
}
const feedSourceColumns = `uuid,domain_uuid,owner_uuid,provider,title,folder,url,credential,version,enabled,interval_seconds,next_ms,lease,lease_until,last_ms,error,etag,modified`
const feedSourceColumns = `uuid,domain_uuid,owner_uuid,provider,title,folder,url,credential,version,enabled,interval_seconds,next_ms,lease,lease_until,last_ms,error,etag,modified,write_enabled`
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)
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 {
@@ -216,6 +227,9 @@ func (s *Store) FeedSources(ctx context.Context, domain, owner string) ([]FeedSo
return out, rows.Err()
}
func (s *Store) SaveFeedSource(ctx context.Context, domain, owner string, v FeedSource) (FeedSource, error) {
if v.WriteEnabled && v.Provider != "freshrss" {
return v, ErrTaskInvalid
}
if _, e := uuid.Parse(v.UUID); e != nil {
return v, ErrTaskInvalid
}
@@ -228,7 +242,7 @@ func (s *Store) SaveFeedSource(ctx context.Context, domain, owner string, v Feed
if v.Version == 0 {
old, lookup := scanFeedSource(tx.QueryRowContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE uuid=?`, 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 {
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
}
if old.Credential != v.Credential {
@@ -256,7 +270,7 @@ func (s *Store) SaveFeedSource(ctx context.Context, domain, owner string, v Feed
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) VALUES(?,?,?,?,?,?,?,?,1,?,?)`, v.UUID, domain, owner, v.Provider, v.Title, v.Folder, v.URL, v.Credential, v.Enabled, v.Interval)
_, 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)
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))
@@ -267,7 +281,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=?,version=version+1,next_ms=0,lease='',lease_until=0 WHERE uuid=? AND version=?`, v.Title, v.Folder, v.Enabled, v.Interval, v.UUID, v.Version)
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)
if e != nil {
return e
}
+203
View File
@@ -0,0 +1,203 @@
package store
import (
"context"
"database/sql"
"errors"
"time"
"github.com/google/uuid"
)
type FeedAction struct {
Domain string `json:"-"`
Owner string `json:"-"`
RequestID string `json:"request_id"`
Source string `json:"source"`
ID string `json:"id"`
Field string `json:"field"`
Desired bool `json:"desired"`
SourceVersion int64 `json:"source_version"`
Status string `json:"status"`
Claim string `json:"-"`
UntilMS int64 `json:"-"`
CreatedMS int64 `json:"created_ms"`
Error string `json:"error"`
}
const feedActionColumns = `domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,claim,until_ms,created_ms,error`
func scanFeedAction(row interface{ Scan(...any) error }) (a FeedAction, e error) {
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)
return
}
func (s *Store) FeedAction(ctx context.Context, domain, owner, source, id string) (*FeedAction, error) {
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
return nil, e
}
a, e := scanFeedAction(s.DB.QueryRowContext(ctx, `SELECT `+feedActionColumns+` FROM feed_actions WHERE domain_uuid=? AND owner_uuid=? AND source_uuid=? AND item_id=? ORDER BY created_ms DESC,rowid DESC LIMIT 1`, domain, owner, source, id))
if errors.Is(e, sql.ErrNoRows) {
return nil, nil
}
return &a, e
}
func (s *Store) QueueFeedAction(ctx context.Context, domain, owner string, a FeedAction) (FeedAction, error) {
if _, e := uuid.Parse(a.RequestID); e != nil {
return a, ErrTaskInvalid
}
if (a.Field != "read" && a.Field != "starred") || a.ID == "" || len(a.ID) > 1024 {
return a, ErrTaskInvalid
}
a.Domain, a.Owner = domain, owner
a.Error, a.Claim, a.UntilMS = "", "", 0
e := s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
old, e := scanFeedAction(tx.QueryRowContext(ctx, `SELECT `+feedActionColumns+` FROM feed_actions WHERE domain_uuid=? AND owner_uuid=? AND request_id=?`, domain, owner, a.RequestID))
if e == nil {
if old.Source != a.Source || old.ID != a.ID || old.Field != a.Field || old.Desired != a.Desired || old.SourceVersion != a.SourceVersion {
return ErrTaskConflict
}
a = old
return nil
}
if !errors.Is(e, sql.ErrNoRows) {
return e
}
var allowed bool
if e = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_sources s JOIN feed_items i ON i.source_uuid=s.uuid WHERE s.domain_uuid=? AND s.owner_uuid=? AND s.uuid=? AND i.id=? AND s.provider='freshrss' AND s.enabled=1 AND s.write_enabled=1 AND s.version=?)`, domain, owner, a.Source, a.ID, a.SourceVersion).Scan(&allowed); e != nil {
return e
}
if !allowed {
return ErrTaskConflict
}
var total, pending int
if e = tx.QueryRowContext(ctx, `SELECT count(*),COALESCE(sum(status IN ('queued','preparing','sending','unknown','check_queued','checking')),0) FROM feed_actions WHERE domain_uuid=? AND owner_uuid=?`, domain, owner).Scan(&total, &pending); e != nil {
return e
}
if total >= 10000 || pending >= 20 {
return ErrTaskDAVLimit
}
if e = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM feed_actions WHERE source_uuid=? AND item_id=? AND status IN ('queued','preparing','sending','unknown','check_queued','checking'))`, a.Source, a.ID).Scan(&allowed); e != nil {
return e
}
if allowed {
return ErrTaskConflict
}
a.Status, a.CreatedMS = "queued", time.Now().UnixMilli()
_, e = tx.ExecContext(ctx, `INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES(?,?,?,?,?,?,?,?,?,?)`, domain, owner, a.RequestID, a.Source, a.ID, a.Field, a.Desired, a.SourceVersion, a.Status, a.CreatedMS)
return e
})
return a, e
}
// 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())
return feedActionAffected(r, e)
})
}
func feedActionAffected(r sql.Result, e error) error {
if e != nil {
return e
}
n, e := r.RowsAffected()
if e != nil {
return e
}
if n != 1 {
return ErrTaskConflict
}
return nil
}
func (s *Store) DueFeedActions(ctx context.Context) ([]FeedAction, error) {
if s.ReadOnly {
return nil, ErrReadOnly
}
now := time.Now().UnixMilli()
// Recovery changes only our receipts, never retries an uncertain remote write.
if _, e := s.DB.ExecContext(ctx, `UPDATE feed_actions SET status=CASE WHEN status IN ('sending','checking') THEN 'unknown' ELSE 'failed' END,error='expired' WHERE (status IN ('preparing','sending','checking') AND until_ms<?) OR (status='queued' AND created_ms<?)`, now, now-300000); e != nil {
return nil, e
}
rows, e := s.DB.QueryContext(ctx, `SELECT `+feedActionColumns+` FROM feed_actions WHERE status IN ('queued','check_queued') AND EXISTS(SELECT 1 FROM feed_sources s JOIN principals p ON p.uuid=s.owner_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=s.domain_uuid JOIN domains d ON d.uuid=s.domain_uuid WHERE s.uuid=feed_actions.source_uuid AND s.enabled=1 AND s.lease_until<? 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=s.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 created_ms LIMIT 2`, now)
if e != nil {
return nil, e
}
defer rows.Close()
out := []FeedAction{}
for rows.Next() {
a, e := scanFeedAction(rows)
if e != nil {
return nil, e
}
out = append(out, a)
}
return out, rows.Err()
}
func (s *Store) ClaimFeedAction(ctx context.Context, a FeedAction) (FeedAction, FeedSource, error) {
var source FeedSource
e := s.feedWrite(ctx, a.Domain, a.Owner, func(tx *sql.Tx) error {
var e error
source, e = scanFeedSource(tx.QueryRowContext(ctx, `SELECT `+feedSourceColumns+` FROM feed_sources WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, a.Source, a.Domain, a.Owner))
if e != nil {
return e
}
check := a.Status == "check_queued"
if !source.Enabled || source.Provider != "freshrss" || (!check && (!source.WriteEnabled || source.Version != a.SourceVersion)) {
return ErrTaskConflict
}
now := time.Now().UnixMilli()
a.Claim = uuid.NewString()
a.UntilMS = now + 60000
state := "preparing"
if check {
state = "checking"
}
r, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease=?,lease_until=? WHERE uuid=? AND lease_until<?`, a.Claim, a.UntilMS, a.Source, now)
if e = feedActionAffected(r, e); e != nil {
return e
}
r, e = tx.ExecContext(ctx, `UPDATE feed_actions SET status=?,claim=?,until_ms=? WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND status=? AND (status='check_queued' OR created_ms>?)`, state, a.Claim, a.UntilMS, a.Domain, a.Owner, a.RequestID, a.Status, now-300000)
if e = feedActionAffected(r, e); e != nil {
return e
}
a.Status = state
source.Lease = a.Claim
source.LeaseUntil = a.UntilMS
return nil
})
return a, source, e
}
func (s *Store) MarkFeedActionSending(ctx context.Context, a FeedAction, source FeedSource) error {
return s.feedWrite(ctx, a.Domain, a.Owner, func(tx *sql.Tx) error {
r, e := tx.ExecContext(ctx, `UPDATE feed_actions SET status='sending' WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND claim=? AND status='preparing' AND until_ms>? AND EXISTS(SELECT 1 FROM feed_sources WHERE uuid=? AND enabled=1 AND write_enabled=1 AND version=? AND lease=?)`, a.Domain, a.Owner, a.RequestID, a.Claim, time.Now().UnixMilli(), a.Source, source.Version, a.Claim)
return feedActionAffected(r, e)
})
}
func (s *Store) CompleteFeedAction(ctx context.Context, a FeedAction, source FeedSource, status, code string, observed *FeedItem) error {
if (observed != nil && observed.ID != a.ID) || ((status == "confirmed" || status == "conflict") && observed == nil) {
return ErrTaskInvalid
}
if status != "confirmed" && status != "unknown" && status != "failed" && status != "conflict" {
return ErrTaskInvalid
}
switch code {
case "", "network", "credentials", "invalid_feed", "remote_error", "conflict", "limit":
default:
return ErrTaskInvalid
}
return s.feedWrite(ctx, a.Domain, a.Owner, func(tx *sql.Tx) error {
r, e := tx.ExecContext(ctx, `UPDATE feed_sources SET lease='',lease_until=0 WHERE uuid=? AND version=? AND lease=? AND lease_until>? AND enabled=1`, a.Source, source.Version, a.Claim, time.Now().UnixMilli())
if e = feedActionAffected(r, e); e != nil {
return e
}
r, e = tx.ExecContext(ctx, `UPDATE feed_actions SET status=?,error=?,until_ms=CASE WHEN ?='unknown' THEN until_ms ELSE 0 END WHERE domain_uuid=? AND owner_uuid=? AND request_id=? AND claim=? AND status IN ('preparing','sending','checking')`, status, code, status, a.Domain, a.Owner, a.RequestID, a.Claim)
if e = feedActionAffected(r, e); e != nil {
return e
}
if observed != nil {
_, e = tx.ExecContext(ctx, `UPDATE feed_items SET is_read=?,starred=? WHERE source_uuid=? AND id=?`, observed.Read, observed.Starred, a.Source, a.ID)
}
return e
})
}
+38
View File
@@ -150,3 +150,41 @@ func TestFeedCatalogMigrationRollbackAndRepeat(t *testing.T) {
t.Fatal(integrity, e)
}
}
func TestFeedActionsMigrationRollbackAndRepeat(t *testing.T) {
st := newSchemaTestStore(t)
ctx := context.Background()
if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:4]); e != nil {
t.Fatal(e)
}
if _, e := st.DB.Exec(`INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,version,enabled,interval_seconds) VALUES('s','d','u','freshrss','Title','','https://example.org',7,1,300)`); e != nil {
t.Fatal(e)
}
broken := append([]db.SQLMigration(nil), feedMigrations...)
broken[4].Statements = append(append([]string(nil), feedMigrations[4].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`)
if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil {
t.Fatal("expected failure")
}
var count int
if e := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('feed_sources') WHERE name='write_enabled'`).Scan(&count); e != nil || count != 0 {
t.Fatal("partial schema", count, e)
}
for range 2 {
if e := st.ensureSchemaComponent("feeds"); e != nil {
t.Fatal(e)
}
}
if e := st.DB.QueryRow(`SELECT write_enabled FROM feed_sources WHERE uuid='s'`).Scan(&count); e != nil || count != 0 {
t.Fatal("write enabled during upgrade", count, e)
}
if _, e := st.DB.Exec(`INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES('d','u','r','s','i','read',1,7,'sending',1); DELETE FROM feed_sources WHERE uuid='s'`); e != nil {
t.Fatal(e)
}
var status string
if e := st.DB.QueryRow(`SELECT status FROM feed_actions WHERE request_id='r'`).Scan(&status); e != nil || status != "unknown" {
t.Fatal("lost uncertain receipt", status, e)
}
if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&status); e != nil || status != "ok" {
t.Fatal(status, e)
}
}