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 { 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")) 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 }