24 Commits
Author SHA1 Message Date
Anya Tokugawa 5321d613b4 feat(feeds): add opt-in Gecko relay and folder shortcuts
Build and publish container images / images (push) Successful in 11m45s
Release container images / release (push) Successful in 21s
Document engine redistribution requirements; exclude upstream engine assets and keep the viewer disabled by default.
2026-11-08 16:14:34 +09:00
Anya Tokugawa b23e63d4c6 docs: record v0.12.0 production release [skip ci] 2026-11-08 14:57:15 +09:00
Anya Tokugawa f4869c7c8b feat(feeds): add optional sandboxed in-tab article preview
Build and publish container images / images (push) Successful in 10m41s
Release container images / release (push) Successful in 24s
2026-11-08 13:55:22 +09:00
Anya Tokugawa ea1ca432c5 feat(feeds): share original articles through X extension
Build and publish container images / images (push) Successful in 11m18s
2026-11-08 13:50:24 +09:00
Anya Tokugawa c2bc637704 fix(feeds): use opaque themed capture dialog surface
Build and publish container images / images (push) Successful in 11m27s
2026-11-08 13:46:46 +09:00
Anya Tokugawa a5f90f933d feat(feeds): add templated capture paths and inline modal
Build and publish container images / images (push) Successful in 11m2s
2026-11-08 13:44:20 +09:00
Anya Tokugawa 78da33d549 feat(feeds): prefill Wiki capture path from personal defaults
Build and publish container images / images (push) Successful in 11m41s
2026-11-08 13:39:54 +09:00
Anya Tokugawa 50a90bcc14 fix(feeds): dismiss shortcut help without leaving article
Build and publish container images / images (push) Successful in 11m20s
2026-11-08 13:37:56 +09:00
Anya Tokugawa bf9a9c2847 feat(feeds): inherit FreshRSS folders in cached subscription tree
Build and publish container images / images (push) Successful in 10m33s
2026-11-08 13:34:22 +09:00
Anya Tokugawa 1057be6d2b feat(feeds): expose retained operation history and read-only rechecks
Build and publish container images / images (push) Successful in 11m14s
2026-11-08 06:35:53 +09:00
Anya Tokugawa e4c184d71c docs: record approved backup and image retention cleanup [skip ci] 2026-11-08 06:35:53 +09:00
Anya Tokugawa 4711a0efbf docs: record RSS release and verified production rollout [skip ci] 2026-11-08 06:23:36 +09:00
Anya Tokugawa 61ecd1ffbd ci: isolate temporary test databases from runner overlay I/O
Build and publish container images / images (push) Successful in 10m42s
2026-11-08 06:08:09 +09:00
Anya Tokugawa 131a7ca01f feat(feeds): queue explicit FreshRSS state changes with uncertain-outcome recovery
Build and publish container images / images (push) Failing after 12m40s
2026-11-08 05:51:47 +09:00
Anya Tokugawa 16e572397b ci: serialize test packages to reduce SQLite initialization contention
Build and publish container images / images (push) Successful in 13m10s
Release container images / release (push) Successful in 25s
2026-11-08 05:42:30 +09:00
Anya Tokugawa bfed3d20f5 feat(feeds): cache FreshRSS subscriptions and category navigation
Build and publish container images / images (push) Failing after 6m7s
2026-11-08 04:48:55 +09:00
Anya Tokugawa 7fe64113c7 feat: add previewed atomic OPML subscription import and export
Build and publish container images / images (push) Failing after 7m31s
2026-11-08 04:39:54 +09:00
Anya Tokugawa 38daea737d feat: render RSS HTML bodies and simplify reader navigation
Build and publish container images / images (push) Successful in 12m32s
2026-11-08 04:00:27 +09:00
Anya Tokugawa 0d8997972c feat: add private RSS reader and read-only FreshRSS integration
Build and publish container images / images (push) Failing after 6m19s
2026-11-08 03:35:02 +09:00
Anya Tokugawa 0c2cf263b6 docs: define embedded and FreshRSS source authority [skip ci] 2026-11-08 02:48:14 +09:00
Anya Tokugawa 269f835c3a docs: prioritize feature subprojects starting with RSS [skip ci] 2026-11-08 02:45:25 +09:00
Anya Tokugawa b9e775de13 docs: reconcile implementation backlog and priorities [skip ci] 2026-11-08 02:40:55 +09:00
Anya Tokugawa e65ca56f92 feat: add audited work records and controlled digest retries
Build and publish container images / images (push) Successful in 12m27s
2026-11-08 02:35:29 +09:00
Anya Tokugawa 6e0aecdfb3 docs: record wallpaper palette v0.10.0 deployment [skip ci] 2026-11-07 23:14:51 +09:00
105 changed files with 10153 additions and 191 deletions

No files matched your search

+7 -1
View File
@@ -11,7 +11,13 @@ RUN go mod download
COPY . . COPY . .
FROM source AS test FROM source AS test
RUN go test ./... # Test packages create independent SQLite stores. Serialize packages to avoid
# concurrent server/store initialization exhausting the shared runner's I/O.
# Keep production database deadlines and the complete test suite unchanged.
# Temporary test databases use bounded RAM-backed storage rather than the
# runner's overlay filesystem. Compiler temporary files remain on disk.
RUN --mount=type=tmpfs,target=/test-tmp,size=2147483648 \
TMPDIR=/test-tmp GOTMPDIR=/tmp go test -p 1 ./...
FROM source AS runtime FROM source AS runtime
RUN go build -o /pageswiki ./cmd/pageswiki RUN go build -o /pageswiki ./cmd/pageswiki
+18
View File
@@ -0,0 +1,18 @@
# The relay image contains no browser engine. Mount the independently verified
# fixed Gecko distribution read-only at /gecko (see docs/operations/gecko-relay.md).
FROM golang:1.25-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY internal/geckorelay ./internal/geckorelay
COPY cmd/gecko-relay ./cmd/gecko-relay
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /gecko-relay ./cmd/gecko-relay
FROM scratch
COPY --from=build /gecko-relay /usr/local/bin/gecko-relay
COPY --from=build /usr/local/go/LICENSE /licenses/Go.txt
COPY --from=build /go/pkg/mod/golang.org/x/net@v0.35.0/LICENSE /licenses/golang-x-net.txt
COPY internal/geckorelay/NOTICE.txt /licenses/NOTICE.txt
USER 10001:10001
EXPOSE 3012
ENTRYPOINT ["/usr/local/bin/gecko-relay"]
+76
View File
@@ -0,0 +1,76 @@
package main
import (
"crypto/rand"
"encoding/hex"
"flag"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"pageswiki/internal/geckorelay"
)
func main() {
status := flag.Bool("status", false, "print bounded relay resource counters")
flag.Parse()
// File mount avoids placing the shared secret in image layers or process args.
key, err := os.ReadFile(os.Getenv("PAGESWIKI_GECKO_KEY_FILE"))
if err != nil {
log.Fatal("Gecko relay key file unavailable")
}
secret := strings.TrimSpace(string(key))
addr := os.Getenv("PAGESWIKI_GECKO_LISTEN")
if addr == "" {
addr = ":3012"
}
if *status {
host, port, err := net.SplitHostPort(addr)
if err != nil {
log.Fatal("invalid listen address")
}
if host == "" || host == "0.0.0.0" {
host = "127.0.0.1"
}
if host == "::" {
host = "::1"
}
nonce := make([]byte, 24)
if _, err = rand.Read(nonce); err != nil {
log.Fatal("random source unavailable")
}
req, _ := http.NewRequest("GET", "http://"+net.JoinHostPort(host, port)+"/status", nil)
req.Header.Set(geckorelay.GrantHeader, geckorelay.Sign(secret, geckorelay.Grant{Subject: "operator:status", Nonce: hex.EncodeToString(nonce), Expires: time.Now().Add(30 * time.Second).Unix()}))
client := http.Client{Timeout: 5 * time.Second, Transport: &http.Transport{Proxy: nil}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
resp, err := client.Do(req)
if err != nil {
log.Fatal("relay status unavailable")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatal("relay status denied")
}
if _, err = io.Copy(os.Stdout, io.LimitReader(resp.Body, 4096)); err != nil {
log.Fatal("relay status read failed")
}
return
}
assets := os.Getenv("PAGESWIKI_GECKO_ASSETS")
if assets == "" {
assets = "/gecko"
}
if err := geckorelay.VerifyAssets(assets); err != nil {
log.Fatal(err)
}
relay, err := geckorelay.New(secret, assets)
if err != nil {
log.Fatal(err)
}
s := &http.Server{Addr: addr, Handler: relay, ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 30 * time.Second, MaxHeaderBytes: 16 << 10}
log.Printf("Gecko relay listening; authenticated TCP 80/443 only")
log.Fatal(s.ListenAndServe())
}
+2 -2
View File
@@ -1398,13 +1398,13 @@ class PagesWikiCLI:
if not args: if not args:
for extension_id, enabled in sorted(states.items()): for extension_id, enabled in sorted(states.items()):
print(f"{extension_id}\t{'enabled' if enabled else 'disabled'}") print(f"{extension_id}\t{'enabled' if enabled else 'disabled'}")
print("(extensions not listed here use the default: enabled)") print("(unlisted extensions use their manifest default; viewer.gecko: disabled, others: enabled)")
return return
extension_id = args[0].strip() extension_id = args[0].strip()
if not re.fullmatch(r"[a-z][a-z0-9._-]{1,63}", extension_id): if not re.fullmatch(r"[a-z][a-z0-9._-]{1,63}", extension_id):
raise CLIError("invalid extension ID") raise CLIError("invalid extension ID")
if len(args) == 1: if len(args) == 1:
print(f"{extension_id}\t{'enabled' if states.get(extension_id, True) else 'disabled'}") print(f"{extension_id}\t{'enabled' if states.get(extension_id, extension_id != 'viewer.gecko') else 'disabled'}")
return return
value = args[1].lower() value = args[1].lower()
if value not in {"on", "off"}: if value not in {"on", "off"}:
+22
View File
@@ -1,6 +1,10 @@
import importlib.util import importlib.util
from pathlib import Path from pathlib import Path
import unittest import unittest
import sqlite3
import io
from contextlib import redirect_stdout
from types import SimpleNamespace
SCRIPT = Path(__file__).with_name("pageswiki-cli") SCRIPT = Path(__file__).with_name("pageswiki-cli")
@@ -43,5 +47,23 @@ class ContentTypeDetectionTest(unittest.TestCase):
self.assertEqual(content_type, "text/plain; charset=shift_jis") self.assertEqual(content_type, "text/plain; charset=shift_jis")
class ExtensionDefaultTest(unittest.TestCase):
def test_gecko_is_opt_in(self) -> None:
db = sqlite3.connect(":memory:")
self.addCleanup(db.close)
db.execute("CREATE TABLE config(domain_uuid TEXT,key TEXT,value TEXT,updated_at INTEGER,UNIQUE(domain_uuid,key))")
# Exercise the CLI command without reading any deployment database.
owner = next(value for value in vars(pageswiki_cli).values() if isinstance(value, type) and hasattr(value, "command_extension"))
fixture = SimpleNamespace(db=db, domain_uuid="synthetic")
output = io.StringIO()
with redirect_stdout(output):
owner.command_extension(fixture, ["viewer.gecko"])
owner.command_extension(fixture, ["viewer.gecko", "on"])
owner.command_extension(fixture, ["viewer.gecko"])
owner.command_extension(fixture, ["viewer.gecko", "off"])
owner.command_extension(fixture, ["viewer.gecko"])
self.assertEqual(output.getvalue().splitlines(), ["viewer.gecko\tdisabled", "viewer.gecko\ton", "viewer.gecko\tenabled", "viewer.gecko\toff", "viewer.gecko\tdisabled"])
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
+47
View File
@@ -0,0 +1,47 @@
# Optional development/pilot overlay, never selected by the production recipe.
# Requires host egress rules; an isolated bridge alone is not an egress firewall.
services:
app:
environment:
PAGESWIKI_GECKO_RELAY: http://gecko-relay:3012
PAGESWIKI_GECKO_KEY_FILE: /run/secrets/gecko-key
volumes:
- ${PAGESWIKI_GECKO_KEY_PATH:?Set absolute relay key path}:/run/secrets/gecko-key:ro
networks:
default: {}
gecko-front: {}
gecko-relay:
build:
context: .
dockerfile: Dockerfile.gecko-relay
image: pageswiki-gecko-relay:dev
environment:
PAGESWIKI_GECKO_KEY_FILE: /run/secrets/gecko-key
volumes:
- ${PAGESWIKI_GECKO_KEY_PATH:?Set absolute relay key path}:/run/secrets/gecko-key:ro
- ${PAGESWIKI_GECKO_ASSETS_DIR:?Set verified Gecko dist directory}:/gecko:ro
networks:
gecko-front: {}
gecko-egress: {}
read_only: true
cap_drop: [ALL]
security_opt: [no-new-privileges:true]
mem_limit: 256m
cpus: 0.5
pids_limit: 64
restart: unless-stopped
healthcheck:
test: [CMD, /usr/local/bin/gecko-relay, --status]
interval: 30s
timeout: 3s
retries: 3
networks:
gecko-front:
internal: true
ipam:
config:
- subnet: ${PAGESWIKI_GECKO_FRONT_SUBNET:-172.24.0.0/27}
gecko-egress:
ipam:
config:
- subnet: ${PAGESWIKI_GECKO_EGRESS_SUBNET:-172.24.0.32/27}
+60 -20
View File
@@ -1,5 +1,35 @@
# Architecture Implementation Roadmap # Architecture Implementation Roadmap
## Current backlog reconciliation (2026-11-08)
Use the [Japanese implementation backlog](ja/implementation-backlog.md) for the current
execution order, stable task IDs, acceptance criteria and explicit holds. Baseline: `e65ca56`.
Broad historical checkboxes below may contain implemented slices; they are not a count of
unimplemented features. Source completion, physical-client acceptance and production rollout
remain separate gates. The 2026-10-31 UX priorities below are historical context.
## Daily email history and controlled retries (2026-11-08)
- [x] Private, paginated delivery history and explicit same-day retry from personal settings.
- [x] Durable request receipts, attempt-generation fencing, opt-out/version/revocation rechecks and duplicate-delivery acknowledgment.
- [x] Bounded 90-day detail cleanup; preserve daily reservations and retry receipts permanently.
- [x] Additive schema v4 with old-state preservation, interrupted-migration rollback and integrity tests.
- [ ] Production rollout; backup restoration must reconcile external SMTP effects before resuming delivery.
- [ ] Retention controls, safe receipt compaction, queued-request cancellation and Inbox outcomes.
See [delivery contracts and recovery](ja/task-digest-delivery.md).
## Work history and project reporting (2026-11-08)
- [x] Owner-scoped paginated work history with inline correction/cancellation and lazy revision display.
- [x] Atomic immutable audit snapshots, optimistic concurrency, current authorization and cross-Domain overlap checks.
- [x] Exclude cancelled time from reports; aggregate by current project without per-project queries.
- [x] Task/dashboard navigation, Japanese/English UI and desktop/mobile browser coverage.
- [ ] Production rollout with an aligned DB backup; old binaries must not read the upgraded DB.
- [ ] Revoked-Domain recording recovery, cancellation restoration and audit retention policy.
See [work history contracts and migration](ja/task-productivity-contacts.md).
## Organizer dashboard (2026-11-02) ## Organizer dashboard (2026-11-02)
- [x] Common navigation for tasks, calendars, contacts and dashboard; home/navbar contacts entry. - [x] Common navigation for tasks, calendars, contacts and dashboard; home/navbar contacts entry.
@@ -17,7 +47,7 @@ See [dashboard contracts](ja/organizer-dashboard.md). Production rollout is a se
- [x] Add home search commands with project/calendar suggestions and explicit creation confirmation. - [x] Add home search commands with project/calendar suggestions and explicit creation confirmation.
- [x] Add private contacts CRUD, trash/restore, vCard import/export and unknown-property preservation. - [x] Add private contacts CRUD, trash/restore, vCard import/export and unknown-property preservation.
- [x] Add private CardDAV serving with isolated credentials, conditional writes, bounded reports and sync tokens; revision preview/restore and whole-book export (2026-11-02). - [x] Add private CardDAV serving with isolated credentials, conditional writes, bounded reports and sync tokens; revision preview/restore and whole-book export (2026-11-02).
- [ ] Shared address books, external CardDAV connectors, physical-device compatibility, work-record corrections and manual digest retry. - [ ] Shared address books, external CardDAV connectors and physical-device compatibility.
See [CardDAV operation and limits](../operations/carddav-contacts.md). The private profile is not full CardDAV compliance; production rollout is tracked separately. See [CardDAV operation and limits](../operations/carddav-contacts.md). The private profile is not full CardDAV compliance; production rollout is tracked separately.
@@ -78,9 +108,11 @@ See [component names, boundaries and acceptance criteria](ja/component-names.md)
All eight initial component extractions are implemented; unchecked items describe remaining capabilities. All eight initial component extractions are implemented; unchecked items describe remaining capabilities.
Released and deployed as [v0.2.0](../operations/deployments/2026-11-01-v0.2.0.md) on 2026-11-01. Released and deployed as [v0.2.0](../operations/deployments/2026-11-01-v0.2.0.md) on 2026-11-01.
- [ ] Priority follow-up: eliminate AddEventLog deferred read-to-write transaction contention; - [x] Eliminate AddEventLog deferred read-to-write transaction contention with writer-first
preserve atomic raw-log/bucket writes and test independent concurrent writers. Production acquisition, atomic raw-log/bucket writes and independent concurrent-writer tests.
start-log failures are now observable; job execution succeeded, but log completeness is not guaranteed. Completion-log failure does not replay successful business work; see [Dregarnuhr](ja/dregarnuhr.md).
- [ ] Recover missing logs, improve event-only fallback/retention and define completion/log
atomicity. Writer-first acquisition does not guarantee all logs survive prolonged contention.
### Storage portability and operations (2026-09-27) ### Storage portability and operations (2026-09-27)
@@ -141,7 +173,10 @@ See [active notification contract](ja/active-notifications.md) and
- [x] Private local calendar time-block links, durable retry intents, current event time/ETag lookup, unlink tombstones; imported/shared/public calendars excluded. - [x] Private local calendar time-block links, durable retry intents, current event time/ETag lookup, unlink tombstones; imported/shared/public calendars excluded.
- [x] Persistent Pomodoro: start/pause/resume/finish/cancel, one active session per principal, conditional transitions, bounded scheduler completion and Inbox notification. - [x] Persistent Pomodoro: start/pause/resume/finish/cancel, one active session per principal, conditional transitions, bounded scheduler completion and Inbox notification.
- [x] Today bounded estimates/current local time blocks/recorded focus intervals, overlap and incomplete-total indicators. Calendar event summaries include an explicitly approved task-title snapshot, never notes. - [x] Today bounded estimates/current local time blocks/recorded focus intervals, overlap and incomplete-total indicators. Calendar event summaries include an explicitly approved task-title snapshot, never notes.
- [ ] Personal capacity, carry-over and period/project reports; historical focus-span backfill; calendar reminders, notification defaults/quiet hours/digests; Pomodoro personal defaults/cycles and routines. - [x] Daily carry-over, work-period/current-project reports, personal notification defaults and
opt-in daily task digest with delivery history and manual retry (source through 2026-11-08).
- [ ] Personal capacity, historical focus-span backfill, general calendar reminders, quiet hours,
Pomodoro personal defaults/cycles and routines; see TASK-01 / ORG-01 in the current backlog.
See [time-block and Pomodoro contracts](ja/task-planning-focus.md) for API, privacy, See [time-block and Pomodoro contracts](ja/task-planning-focus.md) for API, privacy,
two-store recovery, polling limits and remaining work. Canonical calendars and task two-store recovery, polling limits and remaining work. Canonical calendars and task
@@ -190,14 +225,16 @@ System-service plugins are distinct from document adapters. See
- [x] Separate system-extension catalog and settings area for trusted built-ins. - [x] Separate system-extension catalog and settings area for trusted built-ins.
- [x] Unified execution/resource monitor (running jobs only), paginated job history with lazy logs. - [x] Unified execution/resource monitor (running jobs only), paginated job history with lazy logs.
- [x] Protect group owners from ordinary member downgrades and atomically retain an owner during concurrent removal. - [x] Protect group owners from ordinary member downgrades and atomically retain an owner during concurrent removal.
- [ ] Transactional activity outbox and Domain/principal recent-edit projections. - [x] Transactional Domain-sequenced activity outbox and independent typed consumer checkpoints;
task/project source mutations append atomically. See [delivery contract](ja/outbox-delivery.md).
- [ ] General Domain/principal recent-edit projections, consumer operations and safe outbox retention.
- [x] Diary/blog initial built-ins: Page UUID metadata, calendar/recent entries, - [x] Diary/blog initial built-ins: Page UUID metadata, calendar/recent entries,
categories, read-filtered indices, per-Domain enable/disable and registration UI. categories, read-filtered indices, per-Domain enable/disable and registration UI.
See [initial scope](diary-blog-plugins.md); this is authenticated, existing-Page registration only. See [initial scope](diary-blog-plugins.md); this is authenticated, existing-Page registration only.
- [x] Diary/blog atomic Markdown new-post composition, transactional metadata/Revision, - [x] Diary/blog atomic Markdown new-post composition, transactional metadata/Revision,
embedded-asset freezing, idempotent retry and create/write enforcement. embedded-asset freezing, idempotent retry and create/write enforcement.
- [ ] Diary/blog: anonymous metadata approval/feeds, - [x] Diary/blog approved anonymous metadata and full-body RSS feeds; see [current scope](ja/blog-diary.md).
recent comments and safe resumable tDiary import; declarative time/OS/sunset theme rules. - [ ] Recent comments and safe resumable tDiary import; declarative time/OS/sunset theme rules.
- [x] Diary/blog per-Domain prefixes, optional keyword URL planner, multiple articles - [x] Diary/blog per-Domain prefixes, optional keyword URL planner, multiple articles
per day, and authenticated Domain-root diary/blog entry view (2026-09-17). per day, and authenticated Domain-root diary/blog entry view (2026-09-17).
Legacy Page registration remains available. Legacy Page registration remains available.
@@ -211,8 +248,8 @@ System-service plugins are distinct from document adapters. See
filter, legacy-query redirects, and canonical calendar/filter/pagination navigation. filter, legacy-query redirects, and canonical calendar/filter/pagination navigation.
- [x] Local same-Campus diary/blog index/calendar projection without copying articles; - [x] Local same-Campus diary/blog index/calendar projection without copying articles;
source and destination read-policy filtering, source body/edit links, target write guard. source and destination read-policy filtering, source body/edit links, target write guard.
- [ ] Project article bodies on destination host, approved anonymous projection, - [x] Same-instance destination-host article bodies, approved anonymous projection and RSS.
cross-instance source transport/revocation and source-setting federation. - [ ] Cross-instance source transport/revocation and source-setting federation.
- [ ] Rankings, reactions and moderated Page/inline comment services. - [ ] Rankings, reactions and moderated Page/inline comment services.
- [ ] Linkding-compatible bookmark-source injection with scoped credentials. - [ ] Linkding-compatible bookmark-source injection with scoped credentials.
- [ ] Explicit external-search fallback and PagesWiki/GROWI/PukiWiki connectors. - [ ] Explicit external-search fallback and PagesWiki/GROWI/PukiWiki connectors.
@@ -456,7 +493,8 @@ See [Home layouts and sidebar design](navigation-home-sidebar.md).
manifests, inert HTML, mutation invalidation and denial/revocation tests. manifests, inert HTML, mutation invalidation and denial/revocation tests.
- [ ] Extend those gates to anonymous discovery/dependencies and delegated - [ ] Extend those gates to anonymous discovery/dependencies and delegated
shares with full impact-aware mutation/alias handling. shares with full impact-aware mutation/alias handling.
- [ ] Add Domain groups, group Role bindings, and `/g/` management surfaces. - [x] Add Domain groups, group Role bindings, and `/g/` management surfaces.
Full authorization auditing and external group mapping remain separate gates.
- [ ] Add explicit OIDC/LDAP group mappings and policy-version invalidation; - [ ] Add explicit OIDC/LDAP group mappings and policy-version invalidation;
LDAP authentication must remain usable without enabling group synchronization. LDAP authentication must remain usable without enabling group synchronization.
- [ ] Add auditable policy decisions. - [ ] Add auditable policy decisions.
@@ -473,11 +511,12 @@ These items must land before the later feature that depends on them. They are
listed separately because they replace concrete single-feature assumptions listed separately because they replace concrete single-feature assumptions
without changing user-visible Page identity. without changing user-visible Page identity.
- [ ] Put filesystem `DocumentRoot` access behind the seekable, content-addressed - [x] Introduce the seekable, content-addressed Document Store interface and Geduldh
Document Store interface before adding S3 or multi-node payload placement. local payload/materialization adapter. External backends, legacy context propagation
- [ ] Generalize topology/event-job records into a versioned transactional and placement contracts remain open; see [Geduldh](ja/geduldh.md).
Domain event outbox before notifications, feeds, metrics, comments, or DR - [x] Introduce a transactional Domain event outbox with typed subscriptions and checkpoints.
consume mutation events. - [ ] Extend producer coverage and replay/retention contracts before new consumers depend on it;
see [remaining delivery work](ja/outbox-delivery.md).
- [ ] Add a typed extension manifest and provider registry before adding third- - [ ] Add a typed extension manifest and provider registry before adding third-
party transpilers, widgets, scanners, storage backends, or themes. party transpilers, widgets, scanners, storage backends, or themes.
- [ ] Inventory process-local sessions, caches, preview slots, scheduler state, - [ ] Inventory process-local sessions, caches, preview slots, scheduler state,
@@ -550,10 +589,11 @@ without changing user-visible Page identity.
- [ ] Formalize a Domain-sequenced event outbox for Page, Revision, Path, - [ ] Formalize a Domain-sequenced event outbox for Page, Revision, Path,
topology, policy, share, comment, reaction, quality, and federation events. topology, policy, share, comment, reaction, quality, and federation events.
- [ ] Add Page/Spot/Street/Quartier subscription and change-notification rules. - [ ] Add Page/Spot/Street/Quartier subscription and change-notification rules.
- [ ] Add a per-Principal notification inbox with unread state, grouping, - [x] Per-Principal Inbox, unread state, cursor pagination, topbar/modal/detail UI,
filtering, retention, cursor pagination, and authorization-safe deep links. task/broadcast producers and current-authorization checks for supported types.
- [ ] Add delivery channels for Web inbox, email digest, webhook, and future - [ ] General notification grouping/filtering/retention and new-producer authorization coverage.
push adapters, with retry/dead-letter state in the event scheduler. - [x] Web Inbox, opt-in Web Push and daily task email; their delivery contracts are distinct.
- [ ] General channel subscriptions, webhook delivery, quiet hours and retry/dead-letter operations.
- [ ] Add recently edited and recently viewed Page projections; keep private - [ ] Add recently edited and recently viewed Page projections; keep private
activity private and provide retention/opt-out controls. activity private and provide retention/opt-out controls.
- [ ] Add privacy-aware Page view counters with bot filtering, idempotent event - [ ] Add privacy-aware Page view counters with bot filtering, idempotent event
+26
View File
@@ -1,5 +1,9 @@
# Implementation status and hardening backlog # Implementation status and hardening backlog
2026-11-08 reconciliation: use the [current Japanese implementation backlog](ja/implementation-backlog.md)
for actionable IDs, priorities, dependencies and acceptance gates, based on `e65ca56`.
This is a source/document audit, not a production audit or a new deployment.
For the consolidated current reading order, use the [Japanese handbook](ja/index.md) For the consolidated current reading order, use the [Japanese handbook](ja/index.md)
and [decision register](ja/decisions.md). Dated entries below preserve history; and [decision register](ja/decisions.md). Dated entries below preserve history;
older statements about missing anonymous navigation, blog publication or RSS are older statements about missing anonymous navigation, blog publication or RSS are
@@ -13,6 +17,28 @@ security.
## Current state ## Current state
2026-11-08 digest follow-up: private delivery history and explicit same-day retry
are implemented in source. Manual requests are queued for the existing scheduler;
attempt IDs fence stale completions, and consent/version/authorization are checked
again before SMTP. Detailed terminal attempts expire after 90 days, while daily
reservations and request receipts remain. Additive schema v4 and recovery rules
preserve duplicate prevention; production rollout remains deferred. See
[delivery history and migration](ja/task-digest-delivery.md).
2026-11-08 work history: owner-only bounded history, inline correction/cancellation,
immutable audit snapshots and project-level reports are implemented in source.
Cancelled spans leave the report but not the audit. Task/dashboard navigation and
Japanese/English desktop/mobile UI are connected. Schema migration v3 requires an
aligned DB backup before production rollout; do not hot-swap older binaries onto
the upgraded DB. See [contracts and remaining scope](ja/task-productivity-contacts.md).
The dated task/inbox paragraphs below describe earlier slices. VTODO, time blocks,
Pomodoro, active notifications, daily task email, private CardDAV and the organizer
dashboard have since been implemented. Their remaining work is shared access,
external connectors, conflict resolution and retention, not reimplementation of
the existing private interfaces. The [Japanese roadmap](ja/roadmap.md) tracks the
current order.
2026-09-25 task reminders: explicit per-task due/planned-date switches and a local 2026-09-25 task reminders: explicit per-task due/planned-date switches and a local
notification time now schedule owner-only Inbox reminders. Reservations are updated notification time now schedule owner-only Inbox reminders. Reservations are updated
in the source transaction; the bounded scheduler rechecks current state/authority, in the source transaction; the bounded scheduler rechecks current state/authority,
+3 -3
View File
@@ -8,8 +8,8 @@
| --- | --- | --- | | --- | --- | --- |
| Wiki URL | 人が開く説明・知識への入口 | rename可能なPage Path | | Wiki URL | 人が開く説明・知識への入口 | rename可能なPage Path |
| iCalendar | 予定・タスク等の交換形式 | export/import、UIDと元データを保持 | | iCalendar | 予定・タスク等の交換形式 | export/import、UIDと元データを保持 |
| CalDAV | カレンダー資源を扱うWebDAV拡張 | remote connector、将来の提供面 | | CalDAV | カレンダー資源を扱うWebDAV拡張 | 読取remote connectorと限定VEVENT/VTODO提供面は実装済み |
| CardDAV / vCard | 連絡先の通信 / データ形式 | 個人・共有address book | | CardDAV / vCard | 連絡先の通信 / データ形式 | 個人address book提供は実装済み、共有・外部connectorは後続 |
iCalendarとCalDAVは同義ではなく、`.ics`を配信するだけでCalDAVサーバーにはなりません。vCardとCardDAVも区別します。[RFC 5545](https://www.rfc-editor.org/rfc/rfc5545)、[RFC 4791](https://www.rfc-editor.org/rfc/rfc4791)、[RFC 6350](https://www.rfc-editor.org/rfc/rfc6350)、[RFC 6352](https://www.rfc-editor.org/rfc/rfc6352)。 iCalendarとCalDAVは同義ではなく、`.ics`を配信するだけでCalDAVサーバーにはなりません。vCardとCardDAVも区別します。[RFC 5545](https://www.rfc-editor.org/rfc/rfc5545)、[RFC 4791](https://www.rfc-editor.org/rfc/rfc4791)、[RFC 6350](https://www.rfc-editor.org/rfc/rfc6350)、[RFC 6352](https://www.rfc-editor.org/rfc/rfc6352)。
@@ -29,7 +29,7 @@ local item UUID、calendar UID、remote href、Wiki Page UUIDを対応表に持
connectorはremote ETagとlocal versionを別々に持ち、更新前の値で条件付き操作します。ETagをDocument SHA-256と仮定しません。対応サーバーではcollection sync-tokenを利用する案ですが、無効なtokenからの再同期と未対応時のboundedな全量照合も必要です。[WebDAV同期仕様](https://www.rfc-editor.org/rfc/rfc6578)。 connectorはremote ETagとlocal versionを別々に持ち、更新前の値で条件付き操作します。ETagをDocument SHA-256と仮定しません。対応サーバーではcollection sync-tokenを利用する案ですが、無効なtokenからの再同期と未対応時のboundedな全量照合も必要です。[WebDAV同期仕様](https://www.rfc-editor.org/rfc/rfc6578)。
通信失敗では外部削除と見なさず、tombstoneは確認できた削除だけです。両側で変更された場合はbase/local/remoteを比較し、内容を保存した競合状態へ移します。勝手な最新版上書きはしません。 外部connectorの設計では、通信失敗を外部削除と見なさず、tombstoneは確認できた削除だけにします。双方向書込を追加する際はbase/local/remoteを保存して競合選択へ進めます。これを現在の全DAV入口の挙動とは扱いません。タスク提供面では利用者指定により無条件PUT/DELETEを上書き受理し履歴を保持します。競合候補・通知・採否選択は[所属統一の後続](task-dav-project-unification.md)で、読取外部カレンダーconnectorとは別契約です。
## 連絡先 ## 連絡先
+2 -2
View File
@@ -27,7 +27,7 @@
| ADR-O1 | organizerはtyped metadataと任意Page参照 | タスク初版は専用表+既存CAS Document Store(2026-09-24)。通常のObject閲覧・リンク・削除の権限境界を拡大せず、個人所有を確実に分離する。DBは捨てられる索引ではない。[実装契約](tasks-implementation.md) | | ADR-O1 | organizerはtyped metadataと任意Page参照 | タスク初版は専用表+既存CAS Document Store(2026-09-24)。通常のObject閲覧・リンク・削除の権限境界を拡大せず、個人所有を確実に分離する。DBは捨てられる索引ではない。[実装契約](tasks-implementation.md) |
| ADR-O2 | task共通部+ticket workflow | 提案。番号・外部依頼者・遷移権限の要件確認 | | ADR-O2 | task共通部+ticket workflow | 提案。番号・外部依頼者・遷移権限の要件確認 |
| ADR-O3 | ICS/vCard入出力→read-only DAV→双方向 | 推奨順。対象サーバー/clientを選んで互換表を作る | | ADR-O3 | ICS/vCard入出力→read-only DAV→双方向 | 推奨順。対象サーバー/clientを選んで互換表を作る |
| ADR-O4 | FreshRSS正本+PagesWiki connector+newsboat | 第一候補。利用環境・追加運用負荷を確認。内蔵readerは代案 | | ADR-O4 | 簡易利用は内蔵正本、本格運用はFreshRSS正本+PagesWiki connector+newsboat | 2026-11-08利用者指定で採用。内蔵・FreshRSS読取・共通UI・OPML入出力まで実装。source単位で正本を分離し、自動fallbackや二重管理はしない。外部書戻しは残件。実環境の接続先・版・資格情報は別途指定 |
| ADR-O5 | IMAP read-only、Wikiコピーは明示操作 | 初期設計方針。providerとsampleが実装前提 | | ADR-O5 | IMAP read-only、Wikiコピーは明示操作 | 初期設計方針。providerとsampleが実装前提 |
| ADR-O6 | HTMLパーツを先行し全面WYSIWYGは保留 | 提案。round-tripと操作改善の評価でgo/no-go | | ADR-O6 | HTMLパーツを先行し全面WYSIWYGは保留 | 提案。round-tripと操作改善の評価でgo/no-go |
| ADR-O7 | Wiki Pathと外部UID/hrefを分離 | 共通の設計制約。rename fixtureを全connectorで実施 | | ADR-O7 | Wiki Pathと外部UID/hrefを分離 | 共通の設計制約。rename fixtureを全connectorで実施 |
@@ -35,7 +35,7 @@
## 未決事項 ## 未決事項
利用するFreshRSSの有無と版、対象CalDAV/CardDAV client、IMAP provider、taskの共有単位、ticketの依頼者公開範囲、保持期限、外部接続を許可するhost、RPO/RTO、WYSIWYG採用基準です。これらが未決でもlocal fixtureと設計は進められますが、外部サービスを勝手に立ち上げたり資格情報を流用したりしません。 利用するFreshRSSの接続先・版・資格情報、対象CalDAV/CardDAV client、IMAP provider、taskの共有単位、ticketの依頼者公開範囲、保持期限、外部接続を許可するhost、RPO/RTO、WYSIWYG採用基準です。これらが未決でもlocal fixtureと設計は進められますが、外部サービスを勝手に立ち上げたり資格情報を流用したりしません。
## 文書運用 ## 文書運用
+402 -16
View File
@@ -1,41 +1,427 @@
# 15. RSSビューア・FreshRSS・newsboat # 15. RSSビューア・FreshRSS・newsboat
状態:設計案。既存のブログRSS「配信」と、本章の外部RSS「購読」は別機能。 状態:内蔵簡易reader、FreshRSS読取connector、共通viewer、Wiki保存・タスク化、ダッシュボードを実装。
OPMLの確認付き追加取り込み・書出し、FreshRSS購読・カテゴリのnavigation、opt-inの既読・star書戻しを実装済み。本番反映と実FreshRSSへの接続は未実施。
既存のブログRSS「配信」と、本章の外部RSS「購読」は別機能。
## 選択肢と推奨する試作順 2026-11-08:利用者指定のサブプロジェクト順で最初に実施する。内蔵readerは簡易利用の
正本、本格運用はFreshRSSを正本とする。実環境の接続先・版・資格情報は別途指定する。
| 案 | 正本 | 利点 | 追加する責任 | ## 正本と責務
| モード | 正本 | PagesWikiの役割 | 範囲 |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| PagesWiki内蔵reader | ローカルの購読と状態 | Wiki保存と一体のUI | 取得・既読・同期・client対応を全て持つ | | 内蔵簡易reader | PagesWikiの購読・フォルダ・既読・star | feed取得、保持上限内の記事保存、閲覧とWiki保存 | 個人の簡易利用。外部reader向け同期サーバーや高度なルール機能は初期範囲外 |
| FreshRSS connector | FreshRSSの購読・既読 | 既存reader資産を利用 | API互換性、権限、cacheと失効 | | FreshRSS連携 | FreshRSSの購読・フォルダ・記事・既読・star | 表示用cache、連携UI、Wiki保存・タスク化 | 本格運用。PagesWikiは独立した購読・既読の正本を重ねない |
| newsboat単独の橋渡し | newsboat側の購読・状態 | CLI利用に合わせやすい | ローカル連携経路・export/importの設計 | | newsboat等のclient | 接続先FreshRSS | PagesWikiと同じ正本を使う別clientとして扱う | PagesWiki経由の代理同期やnewsboatのDB共有は行わない |
FreshRSSにはGoogle Reader互換APIがあり、APIの有効化と専用passwordを使います。newsboatにはFreshRSS連携が用意され、カテゴリのtag化やstar操作を扱えます。[FreshRSS公式API](https://freshrss.github.io/FreshRSS/en/developers/06_GoogleReader_API.html)、[newsboat公式マニュアル](https://newsboat.org/releases/2.42/docs/newsboat.html#_freshrss)。 FreshRSSにはGoogle Reader互換APIがあり、APIの有効化と専用passwordを使います。newsboatにはFreshRSS連携が用意され、カテゴリのtag化やstar操作を扱えます。[FreshRSS公式API](https://freshrss.github.io/FreshRSS/en/developers/06_GoogleReader_API.html)、[newsboat公式マニュアル](https://newsboat.org/releases/2.42/docs/newsboat.html#_freshrss)。
このため最初の候補は **FreshRSSを正本、PagesWikiを読取connector、newsboatを同じFreshRSSのclient** とする構成です。これは設計上の提案で、ユーザー環境のFreshRSS導入・採用決定ではありません。PagesWikiがnewsboatをサーバー上で起動したり、そのSQLiteを共有したりする必要はありません。 本格運用は **FreshRSSを正本、PagesWikiとnewsboatをそのclient** とする構成を採用します。
内蔵簡易readerも別providerとして提供し、共通の一覧・保存UIを使用します。FreshRSSを
PagesWiki全体の必須依存にはせず、外部サービスの新設・接続は設定時の明示操作とします。
## 読取MVP sourceは一つのproviderに束縛し、記事キーはDomain/所有者/source UUID/remote IDで
区別します。同じURLの記事でも別sourceの既読を自動統合しません。FreshRSSのcacheは
再取得可能な派生データですが、内蔵の購読・状態は正本として保全対象にします。
購読一覧、フォルダ、記事一覧、外部原文、Wiki保存を提供します。初期段階では表示しただけでFreshRSSへ既読を送りません。UIには「読取専用・既読は同期していない」と明示します。ブラウザへFreshRSS password/tokenを渡さず、backend connectorがsecret参照を使います。 FreshRSS停止時は取得済みcacheと鮮度・接続状態を示し、内蔵正本へ自動切替しません。
外部操作の未送信状態は正本と区別し、成功していない既読・starを同期済みと表示しません。
内蔵からFreshRSSへの移行はOPML等による明示的なexport/importとし、初期版では既読・star・
記事IDの完全移行を保証しません。移行完了の確認前に内蔵データを削除しません。
最小cache案はsource UUID、remote feed/item ID、title、safe excerpt/body hash、canonical URL、published/updated/fetched時刻、remote state/versionです。IDなしRSSにはfeed単位の保守的な代替キーを使い、題名だけで重複排除しません。Atomはentry IDと更新情報を持つ形式です。[Atom仕様](https://www.rfc-editor.org/rfc/rfc4287)。 ## 実装済みの読取機能
## 既読・star同期を追加する条件 `/v/feeds` に購読一覧、フォルダ、記事一覧、外部原文へのリンク、Wiki保存を提供します。
FreshRSSの記事・既読・starを取得します。既定は読取専用で、source設定で明示的に許可した場合だけ状態の変更を送信します。
FreshRSS passwordを設定時以外にブラウザへ返さず、login tokenはbackend内でのみ利用します。
資格情報は既存の暗号化機構を使い、Domain・所有者・source UUID・用途に束縛します。
cacheはsource UUID、remote item ID、title、安全化したHTML本文とテキスト本文、記事URL、公開日時、配信元、
フォルダ、read/starを保持します。取得時刻・ETag・Last-Modified・失敗分類はsource側に持ちます。
IDなしRSSは記事URL、URLもなければ題名・日時・本文の組合せから代替キーを作り、題名だけで
重複排除しません。Atomはentry IDを使います。[Atom仕様](https://www.rfc-editor.org/rfc/rfc4287)。
### 高密度ビューア
newsboat/neomuttを参考に、デスクトップは左から購読元・一行記事一覧・本文の3ペインとします。
一覧には未読/star、日付、配信元、件名を並べます。狭幅は一覧と本文を切り替え、スマホでは
行とボタンのタッチ領域を広げます。モバイルの共通ナビはアイコンのみとし、名称はアクセシブル名と
tooltipに残します。タブ名は「RSS」、原文リンクはアイコンです。件数・保持上限・操作の説明は常設せず、
キー案内は`?`で表示します。設定と保存フォームは本文ペイン内に置き、モーダルを増やしません。
`j/k`・上下矢印で記事移動、`Shift+j/k`で次/前のFreshRSSフォルダ、`Enter/l`で本文、`Esc/h`で一覧、`/`で件名・配信元検索、`n`で次の未読、
`m`で既読切替、`*`でstar、`r`で背景更新予約、`?`でキー案内です。フォーム入力・IME変換中は
一覧ショートカットを抑止します。FreshRSSでは書戻し許可時だけ`m`と`*`で操作を予約します。
HTML本文は見出し・表・箇条書き・強調・リンクを保持して描画し、plain textは文字情報として表示します。
server側の許可リストでscript・イベント属性・任意CSS・form・iframe・画像等を除去し、表示時にも再検査します。
Shadow DOMでWiki UIと表示スタイルを分離し、本文リンクは安全なHTTP(S) URLに解決して別タブで開きます。
Wiki macroは展開せず、記事表示のための外部画像や原文サイトのfetchも行いません。
検索とページングは保存済みcacheだけを対象にし、画面表示で
feed取得の完了を待ちません。ダッシュボードには任意追加のRSS widgetを用意します。
### APIと保存層
API prefixは `/_/api/v1/extension/organizer.feeds/`。Domain内の本人専用で、匿名・他人のsourceには
アクセスできません。readonlyアカウントは読取のみです。すべての応答はprivate/no-storeとします。
| endpoint | 操作 |
| --- | --- |
| `GET sources` / `POST sources` | source一覧/追加・設定更新。新規はUUIDで再送を識別、更新はversion CAS |
| `POST sources/{uuid}/refresh` | 次回背景取得を予約。画面から外部通信しない |
| `POST sources/{uuid}/delete` | versionを照合してsource・cache・内蔵read/starを削除 |
| `GET items` | source/unread/starred/件名・配信元で絞り込み、100件単位のkeysetページング |
| `GET catalog?source=…` | 本人のFreshRSS購読・カテゴリの取得済み一覧。`subscriptions`と空フォルダも含む`folders`。外部通信なし |
| `GET item?source=…&id=…` | 本人の記事本文 |
| `POST state` | 内蔵read/starの条件付き変更。FreshRSSは拒否 |
| `POST remote-state` | FreshRSSの一記事・一項目の変更を予約。request_id、source、id、field、desired、source_versionを指定 |
| `GET action?source=…&id=…` | 本人の記事の最新操作記録。外部通信なし |
| `GET actions` | 本人の操作履歴。status/sourceで絞り込み、50件単位のkeysetページング |
| `GET action/export?request_id=…` | 本人の操作記録1件をJSON attachmentで保存。資格情報・本文・worker claimは含めない |
| `POST action/check` | request_idで結果不明操作の読取専用再確認を予約 |
| `POST note` | 公開範囲確認付きのWiki単独保存。request_idと入力hashで再送を識別 |
| `POST capture-preview` | Wiki親Pathのプレースホルダを展開。保存・外部通信なし |
| `GET opml/export` | 本人の内蔵購読をOPML 2.0としてダウンロード |
| `POST opml/preview` | ファイルを解析し、追加可能・登録済み・重複・未対応等を返す。保存・外部通信なし |
| `POST opml/import` | 選択行を再解析・検査し、確認時の購読設定と一致する場合だけ原子的に追加 |
managed schema `feeds` v1に `feed_sources`、`feed_items`、`feed_states`、`feed_note_receipts`を追加し、
v2で`feed_items.body_html`を追加します。既存のテキスト本文・既読・starは保持し、条件付きGETのvalidatorを
一度クリアして次回背景取得でHTMLを取り直します。migration前のworkerは設定versionで無効化します。
v3で`feed_import_receipts`を追加し、購読の一括追加と再送記録を同時保存します。
v4でsourceに購読一覧、記事にremote feed ID・カテゴリIDを追加します。FreshRSSの旧workerを
versionで無効化し、次の背景取得を予約します。内蔵sourceのversion・取得予約は変更しません。
v5でsourceの`write_enabled`(既定OFF)と`feed_actions`を追加します。操作記録はsourceを削除しても保持し、
送信済みの可能性があるものはunknown、未送信はcancelledにします。
v6でsourceに`remote_folders`を追加します。既存の購読・記事・設定version・取得予約・worker claim・
書戻しreceiptは変更しません。初回の背景取得までは既存の購読情報から分類を組み立て、
空フォルダは次の取得成功時に追加します。DB更新前にバックアップを取得し、
旧バイナリへの切戻しだけで追加schemaが巻き戻るとは扱いません。
既存テーブルを破壊・再構成しません。SQLiteのforeign_keys設定に依存せず削除を完遂するtriggerを
持ちます。内蔵read/starを本文cacheから分離し、記事を保持窓から落としても状態は黙って消しません。
DBは内蔵購読・状態・保存receiptの正本であり、全体を捨てられるcacheとは扱いません。
sourceのprovider・URL・資格情報の変更は別sourceを作る契約です。設定versionとworker claimの
両方を照合し、停止/削除/設定更新後に古い取得結果を書き戻しません。完了保存時にも本人の
所属・状態・extension有効状態をwriter transaction内で確認します。
### 背景取得の上限
| 項目 | 初期実装の値・挙動 |
| --- | --- |
| 個人のsource数 | Domain・本人ごとに32 |
| 定期event | `feed.source.sync`、既定60秒周期、低優先度、1回最大4 sourceを直列取得 |
| 書戻しworker | 同じevent内で最大2操作を先に処理。各8秒、取得と合計35秒の予算を共有し、残り9秒未満なら次の操作・取得を開始しない |
| source更新間隔 | 5〜1440分、既定30分。取得候補になる間隔であり、混雑時の完了時刻保証ではない |
| 通信予算 | 1 source 8秒、batch 35秒、応答/展開後本文4 MiB |
| 外部接続 | 公開HTTPS/443のみ。DNS検査・接続先固定、内部アドレス・redirect・proxyを拒否 |
| 内蔵記事cache | 1 source最大500件、text本文32 KiB・HTML本文128 KiB。HTML上限超過はtextへフォールバックし、text切詰めを明示 |
| 内蔵read/star | 1 source最大10,000件。上限では新規状態保存を拒否し、既存状態を捨てない |
| FreshRSS | reading-list最新200件の窓を取得。更新成功時はその窓で置換し、全履歴同期とはしない |
| FreshRSS購読・分類一覧 | 最大1,000購読・1,000フォルダ、1購読64カテゴリ、保存JSON合計1 MiB。記事と同じ取得予算で収集 |
| 再試行 | 次の設定間隔まで待機。手動予約にも最終成功から60秒の制限、取得中の重複claim拒否 |
内蔵は条件付きGET、304ではcacheを保持します。どちらのproviderも通信・認証・構文失敗で
前回cacheを消しません。sourceには安全な失敗分類と最終成功日時を表示し、イベントログには
成功/失敗件数だけを記録します。資格情報・生本文・秘密を含み得るURLをログへ出しません。
extension無効化は画面・API・取得・完了保存に適用します。購読を登録するまで外部通信は発生しません。
### FreshRSSの購読とカテゴリ
背景取得ではlogin後にreading-list・`subscription/list`・`tag/list`を読み、すべてに成功した場合だけ
記事・購読一覧・分類一覧を同じtransactionで置換します。一部の取得失敗、上限超過、不正なsnapshotでは
前回のcache全体を保持します。記事が取得窓内にない購読も一覧には残します。
`tag/list`は`type=folder`だけを分類として取り込み、記事タグとシステムの状態タグは除外します。
購読一覧のカテゴリとIDで統合し、購読元のない空フォルダも保持します。
左ペインでFreshRSS sourceを選ぶと、接続先→分類フォルダ→購読元のツリーを表示します。
フォルダ名は記事の絞り込み、隣の矢印は開閉と役割を分けます。開閉状態は接続先UUID単位で
ブラウザのlocalStorageに保存し、利用できない場合もその画面内では保持します。
閉じたフォルダは購読元のDOMを作らず、展開時は50件ずつ描画します。購読名・分類名で検索すると
該当購読を重複なしで一覧化します。名前に含まれる`/`は文字として扱い、独自の階層には分割しません。
カテゴリは表示名でなくremote category ID、購読は`origin.streamId`とsubscription IDを対応させます。
同名でも混同せず、複数カテゴリに属する購読は各フォルダに表示します。記事の分類絞り込みは
記事タグではなく同期した購読元の所属を使い、FreshRSSでの分類移動に追従します。
分類のない購読は「未分類」に表示し、購読一覧にない記事を未分類へ混在させません。
`GET items`の`feed`/`category`/`category_mode=uncategorized`は
source指定と組み合わせ、既存の未読・star・検索・keysetページングと併用できます。
`category`と`category_mode`の同時指定は拒否します。
絞り込み対象は最新200記事のcache内に限られ、購読を選んでも履歴全件を外部取得しません。
購読の追加・削除やカテゴリの編集はFreshRSS側で行います。
`Shift+j`/`Shift+k`は、選択中のFreshRSS接続内のフォルダを表示順に選び直します。
空フォルダと未分類も含め、先頭・末尾では停止します。「すべての購読」からは、下方向なら先頭、
上方向なら末尾を選びます。購読元を選択中なら、その所属フォルダを起点とします。
記事検索・未読/starフィルタ・フォルダ開閉状態は維持し、個別購読元の絞り込みは解除します。
購読・フォルダの検索でツリーが隠れている場合は、そのナビゲータ検索だけを解除します。
前の本文と本文リクエストは破棄して一覧へ戻し、連打では最後の選択だけを反映します。
フォーム・編集可能領域・モーダル・IME入力中、およびCtrl/Alt/Commandとの併用では実行しません。
source自体の切替、分類の移動・書換え、外部再取得、未読/既読変更は行いません。
ショートカットヘルプは`?`で開閉し、`Esc`または閉じるボタンでも閉じます。
ヘルプだけを閉じる操作では閲覧中の記事や入力フォームを変更せず、エラー等のstatus表示も保持します。
`sharing.x`が有効なDomainでは、本文の操作列にX共有アイコンを表示します。
共有するのは記事の件名と原文URLで、RSS画面や非公開WikiのURLではありません。
URLはintentの`url`にだけ渡し、`text`へ重複させません。原文URLのない記事では表示しません。
通常の`target=_blank`リンクを一つだけ使い、JavaScriptによる二重openや自動投稿は行いません。
個人設定の「RSSの記事リンクの開き方」は`rss_link_mode`として保存します。既定は`tab`、
`embedded`を選ぶと原文リンクとHTML本文中の外部HTTP(S)リンクを同一タブ内の大きなdialogで開きます。
外部ページはクリック時だけiframeへ読み込み、閉じる際に破棄します。元の記事・一覧の状態は維持します。
Ctrl/Command/Shift付きのクリック等はブラウザ標準の別タブ操作を維持し、X共有アイコンも対象外です。
設定はRSS画面の再読込後に反映します。個人設定はタスク拡張のpreferencesに保存し、無効時は別タブを使います。
iframeは`allow-scripts`だけのsandboxと`no-referrer`を使い、親画面への操作・同一origin権限・
フォーム送信・popup等は許可しません。PagesWiki自身のoriginはiframe化しません。
リンク先のCSP/X-Frame-Options、ブラウザのmixed-content制限を回避するproxyは用意しません。
表示できないページやログイン・フォーム操作が必要な場合は、dialog内の別タブアイコンを使います。
表示可否をcross-origin DOMの読取で判定したり、失敗を装って自動で別タブへ遷移したりはしません。
### Geckoと自前Wispによる原文閲覧
通信中継方式を採用する。実装は独立した実験的ビューアとして段階導入し、現行の`tab`/`embedded`を
自動置換しない。Geckoビューア・中継・個人設定の接続を実装した。`viewer.gecko`は既定無効で、
管理者による有効化と中継の構成が揃った場合だけ選択できる。本番への配備は別途行う。
端末上の`gecko.js`で1ページを描画し、外部HTTP(S)通信だけを管理者が配置したWispサービスへ中継する。
サーバー上でFirefoxを起動したり画面をストリーミングしたりはしない。Firefox全体の操作画面は含めず、
既存の記事上dialogにcanvas、戻る・進む・再読込・閉じる・別タブの操作を置く。
自前中継を必須とし、Puter等の公開中継への自動fallbackは設けない。
現行upstreamはGeckoのソケット通信をWispへ接続する。通常のWebページの`fetch`へ単純に置き換えて
任意のサイトを直接取得することは前提にしない。GeckoとWisp実装は検証したrevision・プロトコルで固定し、
更新時にサイト互換性とセキュリティ修正を再評価する。
[gecko.js v0.0.1](https://github.com/HeyPuter/firefox-wasm/releases/tag/v0.0.1)、
[Wisp仕様](https://github.com/MercuryWorkshop/wisp-protocol/blob/main/protocol.md)。
中継はPagesWiki本体とは別コンテナ・別ネットワーク境界とする。外部から無認証で利用できる公開proxyにしない。
既存の認証・Domain・本人に束縛した短命セッションを発行し、WebSocketのOriginを検証する。
失効・ログアウト・拡張無効化は既存の接続にも反映する。Cookie・WikiのBearer token・FreshRSS資格情報を
外部ページへ渡さず、中継用の秘密をURLやログに残さない。
接続先は公開HTTP(S)用のTCPポートに限定し、UDPや任意の管理用ポートは許可しない。
DNS解決後の実接続先でloopback・private・link-local・metadata等を拒否し、IPv4/IPv6・DNS rebindingを
含めて検査する。接続検査だけでなくコンテナの外向き通信制限を併用し、Wiki・DB・管理ネットワークへ到達させない。
同時セッション・ソケット・帯域・idle/接続timeoutの上限を設ける。ログは通信量・失敗分類・処理時間に限り、
記事本文や資格情報を記録しない。HTTPSの証明書検証を無効化せず、HTML改変やTLS終端による閲覧制限回避は行わない。
原文クリック時だけエンジンを遅延ロードする。静的WASM資産はversion付きでキャッシュするが、
記事本文・Cookieの永続化は初期対象外とし、上限付きのメモリプロファイルを渡す。
固定版の`destroy()`はWASMの完全終了を保証しないため、初期実装では短時間保持を行わず、
閉じるとiframeごと破棄する。タブ非表示が60秒続いた場合も閉じる。
戻る/進むは最大64件のビューア内URL履歴から再読み込みし、フォームやスクロール位置は復元しない。
上流の最小GREではSessionStoreFunctionsがなく、標準history.back()は失敗する。
中継の接続数・転送量・拒否数は認証付きstatus/CLIで取得できる。設定UIへの監視統合と端末側計測は残件とする。
pthread/SharedArrayBufferのcross-origin isolationはRSSの親documentまで含めて検証する。
COOP/COEPをWiki全体へ一律追加せず、対象ビューだけで既存の壁紙・外部リソース・共有操作への影響を確認する。
isolation不可・端末のメモリ不足・未対応サイトではエラーと別タブ操作を示し、黙って外部中継や別タブへ切り替えない。
受入条件は、合成ページでのnavigation・日本語入力・検索・コピー、私有アドレス拒否・認証失効・中継停止・
資源上限・無効化、デスクトップとiPhone SEのSafariでの初回/再表示時間・スクロール・メモリ確認とする。
実記事での測定は明示した対象で行い、実FreshRSSの全原文や外部カレンダー・メール内容を自動巡回しない。
現時点ではChromiumで合成Wisp記事の描画・クリック遷移・ビューア内の戻る・接続破棄、
接続先制限・資格情報分離・ログアウト/拡張無効化時のWebSocket終了を自動テストした。
導入方法、資産hash、資源上限、外向きfirewallと本番前の残件は[Gecko中継の試験導入](../../operations/gecko-relay.md)にまとめる。
## 既読とstarの書戻し
server-authoritative stateと未送信の操作を分けます。操作ごとのrequest_idで重複を防ぎ、成功応答前に確定済みと表示しません。newsboat等で同時変更された場合、古いローカルcacheをまとめて送り返しません。接続解除・認可失効時は未送信変更を勝手に再送しません。 server-authoritative stateと未送信の操作を分けます。操作ごとのrequest_idで重複を防ぎ、成功応答前に確定済みと表示しません。newsboat等で同時変更された場合、古いローカルcacheをまとめて送り返しません。接続解除・認可失効時は未送信変更を勝手に再送しません。
FreshRSS APIに条件付き更新がない操作もあり得るため、対応版の実機fixtureで競合動作を確認するまで完全な双方向同期を約束しません。 FreshRSSへの変更は一項目のtag追加・削除です。更新直前の状態と不可分に照合するCASではないため、
他clientとの同時操作を完全に直列化する双方向同期ではありません。
## 内蔵fetcherを選ぶ場合 ### 操作記録と送信状態
定期event、条件付きGET、host別同時数、timeout、応答サイズ、展開サイズ、backoffを設けます。XML外部entityを無効にし、feed HTMLを安全化します。redirect/DNSを含むSSRF対策、外部画像の同意、個人購読URLや認証情報の秘匿が必要です。 RSS-3は合成環境で実装・検証済みです。sourceごとに既定OFFの「FreshRSSの既読・star変更を許可」を設け、操作は`read`または`starred`の一項目と希望値に限定します。
cache内の他項目を同時に送らず、全件既読・購読編集も初期対象に含めません。
OPMLは購読の入出力として扱い、既読・star・全本文のバックアップと同一視しません。importはpreview・追加/置換選択・重複確認を行い、既存購読を黙って削除しません。 操作記録は記事cacheから分離し、Domain・本人・request_id、source・記事ID、対象項目、希望値、
source version、状態、claim期限を保持します。同一request_idの同じ入力は同じ記録を返し、
違う入力は拒否します。同じ記事に未解決の操作がある間は次の操作を受け付けません。
受付APIは外部通信せず、低優先度workerが送信します。Domain・本人ごとに未解決20件、全記録10,000件を上限とし、
上限で新規受付を拒否します。receiptを自動削除して古い要求を再送可能にすることはしません。
queuedは受付から5分で失効します。claimは60秒で、期限切れsending/checkingはunknownにし、変更を再送しません。
| 状態 | 次の処理 |
| --- | --- |
| queued | sourceの有効状態・書戻し設定・version・本人の権限を再検査してclaim。古すぎる未送信操作は中止 |
| preparing | FreshRSSの現在値を取得。すでに希望値なら書き込まず確認済み。異なる場合だけ変更tokenを取得 |
| sending | この状態をDBに確定してから変更を送る。再起動やtimeoutでも自動再送しない |
| confirmed | 送信後の読取で希望値を確認。確認できた値だけcacheへ反映 |
| unknown | 送信済みか適用済みか判断できない。成功扱いにせず、利用者の再確認操作を待つ |
| checking | unknownの再確認。読取のみで、希望値ならconfirmed、異なれば競合として提示 |
| failed / cancelled / conflict | 原因を示して終了。新たに送る場合は利用者の明示操作を必要とする |
通常取得と書戻しはsourceのleaseを共有し、古い取得結果が書戻し後の値を上書きしないようにします。
設定変更・無効化・削除は未送信操作を止めますが、すでに外へ送った要求は取り消せません。
その場合はunknownを保持し、設定変更後のcacheへ旧workerが反映することを拒否します。
外部clientとの同時変更を原子的に防げないAPIでは、確認直後に状態が再変更される可能性もあります。
readbackは観測結果であり、exactly-onceやリモートCASの保証とは区別します。
結果不明時は操作欄の`↻`からclaim期限後に再確認できます。書戻し設定をOFFにしても、sourceが有効なら
この読取専用再確認は可能です。画面は未解決操作の間だけ2秒間隔で自サーバーの記録を確認し、
背景タブでは停止します。記事の既読・star表示は遠隔の状態を確認するまで切り替えません。
再確認で希望値と異なる場合はconflictを表示し、新しい変更は利用者が明示的に操作します。
### 操作履歴の一覧と保全
ツールバーの時計アイコンから、リーダー内に操作履歴を開きます。モーダルは増やしません。
日時、記事・購読元、変更対象と希望値、状態をまとめ、記事がcache内なら本文へ戻れます。
スマホ幅では日時・件名・状態を縦にまとめ、再確認・保存のアイコンをタッチできる大きさにします。
既定の「未解決」はconfirmed/cancelled以外を表示し、処理中、結果不明、すべて、確認済み、
未送信、不一致、中止で絞り込めます。一覧はローカルDBだけを読み、外部通信や自動再試行を行いません。
履歴の更新は再読込アイコンと絞り込み操作で行い、記事画面の2秒pollは履歴画面では停止します。
`GET actions`のstatusは`unresolved`(省略時も同じ)、`pending`、`unknown`、`all`、
`confirmed`、`failed`、`conflict`、`cancelled`です。sourceは削除済みUUIDも指定できます。
続きは末尾のcreated_msとrequest_idを`before`/`key`として渡します。同時刻の操作も重複・欠落なく辿ります。
1回50件+続き判定の1件を読み、所有者の10,000件上限でソート量を制限します。
sourceや記事へのinner joinは使わず、削除済み購読元やcache窓から外れた記事の記録も残します。
記事名・購読元名は現在の取得済み情報であり、操作時点の監査snapshotではありません。
取得できない場合は元のsource UUID/記事IDと「削除済み」「キャッシュ外」を表示します。
結果不明の記録は、同じDomain・本人の有効なFreshRSS sourceが残り、claim期限を過ぎた場合だけ
読取専用再確認を予約できます。書戻しOFFでも再確認でき、readonlyアカウントは閲覧・保存のみです。
削除済みsourceは自動再接続しません。本人の記録に残るsource UUID、および全体の未解決操作に
残るsource UUIDの新規登録を拒否し、別接続先への誤った結び付けを防ぎます。
各行からJSON形式(`pageswiki.feed-action`、version 1)の記録を書き出せます。
Domain/本人/request ID、操作内容、状態、日時、現在の表示用メタデータを含み、
外部URL・資格情報・本文・内部claimは出力しません。ただし元の記事ID自体がURLの場合はそのIDを保持します。
一覧・保存ともDomain・本人で制限し、private/no-storeで返します。schemaはv5のままです。
保存操作はDBからの削除を意味しません。receiptを削除すると同じrequest_idの再送を防げなくなるため、
自動削除・一括削除は未実装です。後段の整理機能では、保全manifest・hash・対象件数を照合した上で、
成功/中止/失敗等の終端記録だけをarchiveへ移し、重複防止に必要なrequest_idと入力識別子を
tombstoneとして保持します。unknownや実行中の記録は移動・削除対象にしません。
tombstoneの保持期間・容量上限・旧クライアントからの再送時応答を決めるまで、現在の10,000件上限を維持します。
外部APIはlogin、記事参照、変更token、edit-tag、記事再参照の順です。記事の個別参照は
`POST stream/items/contents`(読取操作)、変更は`POST edit-tag`に`i`・`T`・`a`または`r`一項目を送ります。
URLや本文、資格情報を操作記録・ログへ保存しません。[FreshRSS API実装](https://github.com/FreshRSS/FreshRSS/blob/edge/p/api/greader.php)。
受入テストは、opt-inなしの拒否、同じ操作の再送、別項目の保持、送信前の権限失効、
送信直後の応答喪失、worker停止・再起動、unknownの読取専用再確認、source削除・設定変更、
通常取得との排他、他clientによる同時変更を含めます。実サービスを使わない合成fixtureで
これらを通した後、指定版FreshRSSでの受入を別途行います。
## 内蔵簡易reader
購読追加/削除、フォルダ、記事一覧、既読/未読、star、更新間隔と保持上限を初期範囲に
します。取得は背景ジョブで行い、更新失敗で購読や状態を消しません。記事本文cacheと
既読・starの保持期間を分離し、古い記事を再取得しただけで未読へ戻る挙動を防ぎます。
高度な自動分類、全文収集、reader向け互換APIはFreshRSSとの機能競争として追加しません。
初期版では全体を直列化し、上記の予算内で取得します。将来の並列化時にhost別同時数・公平性・
指数backoff/Retry-Afterを追加します。RSS 2.0とAtomを対象とし、XML外部entityは展開しません。
外部画像の同意付き表示は未実装で、現在は一律に取得・表示しません。
### OPML入出力
RSSツールバーの`⇄`から、本文ペイン内でファイル選択・確認・追加を行います。書出しは本人の内蔵購読の
名前・URL・フォルダをOPML 2.0に保存し、停止中の購読も含みます。FreshRSS接続・資格情報は含めません。
購読URLのqueryに秘密がある場合はそのURLも書き出されるため、ファイルは本人用として扱います。
既読・star・記事本文・有効状態・更新間隔のバックアップではありません。
取込はUTF-8のOPML 1.0/1.1/2.0を対象とします。OPMLは購読と階層outlineを交換する形式であり、
この実装はfeed購読部分のみ扱います。[OPML公式仕様](https://2005.opml.org/spec2.html)。
ネストしたフォルダは`親 / 子`という一つのフォルダ名へ投影します。名前のmarkupは文字情報にし、
`include`・外部outline・HTMLページの参照は取得しません。コメント扱いのoutline配下も追加対象外です。
確認表では、追加・登録済み・ファイル内重複・未対応URL/形式・名前不正を区別し、追加可能な行を選択します。
HTTPS/443と内部アドレス拒否の既存契約は維持し、ファイル取り込みを検証の迂回経路にはしません。
初期版は**追加のみ**で、置換/既存購読の削除は提供しません。有効状態と更新間隔は追加時に指定します。
保存後のfeed取得は既存の背景eventが行い、previewやimportのHTTP request中には通信しません。
上限はファイル512 KiB、記事購読候補500行、outline 1,000個、XML階層16段です。既存sourceと合わせた
32件上限を確認画面とwriter transactionの両方で検査します。既存・ファイル内重複は、hostの大小文字・
既定443 port・空pathを正規化して判定します。queryの順序、pathの大小文字、redirect先を同一視しません。
previewはsource UUID・設定versionのsnapshotを返し、適用時に一致しなければ412で再確認を求めます。
選択行の再解析、本人・Domain・有効状態の確認、source追加とreceipt保存を一つのtransactionで実行します。
同じrequest_id・同じ入力は保存結果を返し、入力が異なれば拒否します。後でsourceを削除してもreceiptを
保持するため、古い要求を再送して購読が復活することはありません。receiptはDomain・本人ごとに1,000件まで
保持し、上限時は新しいimportを拒否します。既存のWiki/タスク・既読/starは変更しません。
## Wiki保存と権利・公開 ## Wiki保存と権利・公開
既定はURL、出典、短いメモの保存です。全文保存は取得可能性・利用条件・個人用途等を確認する独立操作とし、feedが短いからといって有料記事を自動取得しません。保存先が自動公開範囲なら内容と添付の公開を明示確認します。 既定はURL、出典、短いメモの保存です。全文保存は取得可能性・利用条件・個人用途等を確認する独立操作とし、feedが短いからといって有料記事を自動取得しません。保存先が自動公開範囲なら内容と添付の公開を明示確認します。
タスク拡張が有効な場合、Wiki親Pathの初期値はDomain・本人単位の個人設定`wiki_prefix`を使います。
「個人設定 → メール・RSS取り込みの既定Wiki親Path」で設定し、メール取り込みと共通です。
RSS画面を読み込んだ時点の値を初期表示し、取り込みごとの上書き・空欄への変更を許します。
空欄でタスク作成を選んだ場合はWikiを作らず、個人設定の値を再適用しません。
未設定またはタスク拡張が無効な場合は空欄です。プロジェクトのWiki配下への暗黙の補完はRSSでは行いません。
保存時の公開範囲確認とACL検査は省略しません。既存preferencesを利用するため追加のschema変更はありません。
親Pathでは`{YYYY}`・`{MM}`・`{DD}`・`{Subject}`を使用できます。日付は取り込み時のドメインの
タイムゾーン、Subjectは編集可能な取り込み件名です。件名中の区切り文字・URI演算子・制御文字・
空白はハイフンに変換し、180バイト以内の単一Path要素へ整形します。空になる場合は`untitled`です。
未対応トークンや不正なPathは拒否します。メールcaptureでも同じ展開規則を適用します。
RSS取り込みは本文上のdialogで行い、本文を置き換えません。トークン挿入ボタンと実際の保存先を
表示し、件名・親Path・保存種別を変更した場合はプレビューを更新して公開範囲の確認をやり直します。
展開済みPathを確認後の要求へ固定し、UUIDの末尾(Wiki単独は`rss-`、タスク併設は`mail-`)で
同名記事の衝突を避けます。表示された親Path自体を記事ファイルとして上書きする仕様ではありません。
Wiki単独は親Path配下の `rss-{request UUID}` に新規作成します。既存Pathを上書きせず、Page・Path・
初回Revision・活動outbox・再送receiptを同じDB transactionで保存します。権限はtransaction内でも
再確認します。初期版の単独保存は通常Wiki空間のみで、独自所有契約を持つ`/u`・`/g`は対象外です。
タスクも作成する場合は既存capture APIの原子的なWiki+タスク保存を使います。この経路のPathは
既存契約の`mail-{request UUID}`です。途中失敗時は部分的なPage/タスクを残さず、同じ要求で再試行できます。
保存先の公開範囲確認を必須にし、取得済み本文のコピーは別チェックとします。
購読削除で明示保存したWikiやタスクは消しません。内蔵cacheを削除しても既存Wikiへの参照と
再送receiptは残します。文書payloadの不変ファイルは既存Document Storeに保存するため、DB失敗時に
未参照payloadが残る可能性は既存GC契約に従い、参照のないPage/Pathとは区別します。
## TODOと受け入れ ## TODOと受け入れ
RSS-1:read-only FreshRSS fixture。RSS-2:安全な一覧と出典付きWiki保存。RSS-3:必要なら既読/star送信。RSS-4:内蔵reader採用判断とOPML。オフライン、欠落ID、巨大feed、未知HTML、同期再送、他user情報、feed削除後の保存Page維持を試験します。 - [x] RSS-1:read-only FreshRSS connectorと合成fixture。実接続の受入は別。
- [x] RSS-2:安全な共通一覧、出典付きWiki保存、任意のタスク化、ダッシュボード。
- [x] RSS-3:opt-inの既読/star書戻し、操作receipt、背景処理、readback、結果不明の再確認、失効・競合・排他の合成試験。
- [x] RSS-3b1:未解決操作の総覧、cache窓外・削除sourceの記録、再送しない再確認、個別JSON保存。50件ページング、本人分離、readonly、1440px/390pxの合成試験。
- [ ] RSS-3b2:保全manifestの照合、終端receiptのarchive、重複防止tombstoneと保持期間・容量上限。未知結果の記録を捨てない。
- [x] RSS-4a:内蔵簡易reader、購読設定、既読・star、背景取得。
- [x] RSS-4b:OPML export/importのpreview、選択追加、重複確認、原子的な保存と再送防止。置換・状態移行は範囲外。
- [ ] RSS-5:FreshRSS実環境の接続先・版・API資格情報を指定してread-only受入。
- [ ] RSS-6:大規模利用時の公平性、host別並列上限、Retry-After/backoff、資格情報rotation UI。
- [x] RSS-7:FreshRSSの購読一覧・カテゴリを使うnavigation。同名ID分離、複数カテゴリ、cache内絞り込み、背景での原子的更新、失敗時保持。
- [x] RSS-7b:分類フォルダの継承、空フォルダ・未分類、開閉状態保存、50件ずつの購読描画、分類移動への追従。schema v6の再実行・失敗rollbackと1440px/390pxの合成試験。
- [x] RSS-7c:Shift+j/kによるフォルダ選択。空・未分類・端・購読選択中・検索維持・入力/モーダル抑止・連打を合成試験。
- [x] RSS-8a:Gecko端末描画+自前Wisp中継の方式決定。公開中継を使わず、独立・既定無効で導入する。
- [x] RSS-8b1:upstream v0.0.1/hash固定、最小埋め込みと親documentのisolation、Chromiumで合成記事の描画・遷移確認。
- [ ] RSS-8b2:Safari/iPhone SE実機、IME・コピー・find・証明書エラー・HTTPS互換性、配布license/notice整備。
- [x] RSS-8c1:認証付きWisp TCP profile、公開IP/port制限、失効・資源上限・管理者/CLI無効化・内部status。
- [ ] RSS-8c2:運用ホストの外向きfirewall、設定UIへの中継監視統合、負荷時の長時間受入。
- [x] RSS-8d1:個人設定の選択、1ページのpopup閲覧、戻る/進む/再読込/別タブ/終了。
- [ ] RSS-8d2:初回/再表示・低メモリ端末の性能評価、失敗/切断・複数タブのUX、本番配備。
隔離試験はRSS/Atom・FreshRSS login/read、欠落ID・巨大/不正feed、条件付きGET、失敗時cache保持、
本人分離・失効・readonly、二重送信・版競合・旧worker拒否、migration再実行/失敗rollback、
Wiki保存失敗の原子性を含みます。Chromiumの1440px/390pxで一覧・本文・検索・キー操作・
Wiki保存を試験します。実FreshRSS、newsboat、実機Safariの受入はこの合成試験に含めません。
## 実施段階
1. **共通契約とfixture**:Domain/本人のsource所有、provider ID、remote ID、cursor、取得予算、
最終成功/失敗と無効化を定義する。FreshRSS読取connectorを合成fixtureで先に検証し、
内蔵providerも同じ表示契約へ接続する。sourceの資格情報をブラウザ・ログへ出さない。
2. **読取UIと背景取得**:購読/フォルダ、記事の一行一覧、本文表示、出典リンクを用意する。
デスクトップでは一覧を見渡せる配置、スマホでは一覧と本文を切り替える。画面表示で外部fetchを
待たず、schedulerで有界に更新し、取得済みcacheと鮮度を表示する。source停止・失効も検証する。
3. **Wiki保存とタスク化**:タイトル・URL・出典・短いメモをpreviewし、保存先Pathを選ぶ。
既存captureと権限確認を再利用し、二重送信・保存先競合・自動公開範囲を検査する。全文保存は
独立した明示操作。Wiki単独保存とWiki+タスクの原子保存を分け、要求内の失敗では部分成功を
残さない。過去に保存済みのWikiは別のタスク作成失敗で削除しない。
4. **ダッシュボードとprovider完成**:読める記事だけのwidget、内蔵簡易readerとOPML、
FreshRSSの明示的な既読/star操作を段階的に追加する。接続先・対象版の指定後に実環境で
read-only→書込の順に受入し、read-only連携を有効化しただけで外部状態を変更しない。
各段階で無効化、件数・時間・容量上限、移行の再実行、ユーザー/Domain間の分離を試験する。
source解除時のcache清掃と、利用者が明示保存したWiki/タスクの保持は別操作とする。
+148
View File
@@ -0,0 +1,148 @@
# 実装残件台帳
更新日:2026-11-08。全体照合基準は `e65ca56`(作業履歴・日次メールの後続実装まで)。
RSS-01はその後の内蔵reader・FreshRSS読取・共通UI実装まで個別更新。
設計書の大項目をそのまま未実装機能と数えず、既存機能の続きとして着手できる単位に整理する。
実装順は本書、個別契約はリンク先、過去の経緯は[原ロードマップ](../implementation-roadmap.md)と
[実装記録](../implementation-status.md)を参照する。将来の更新でも以下のIDを再利用する。
## 調査範囲と状態の読み方
日本語設計書、原ロードマップ、実装記録と、対応するstore/HTTP handler/テストを静的照合した。
全エンドポイントの再監査や本番の再試験ではない。実データ、外部カレンダー、メールボックスには
アクセスしていない。独立した `anya/pages-cli` の現行HEAD・配布バイナリも今回の照合対象外。
- **実装済み**:記載した範囲のコードと試験がある。全機能完成・本番適用を意味しない。
- **一部実装**:基盤や初期機能はあるが、記載した後続が必要。
- **未実装**:設計・TODOの段階。API例や予約URIだけでは利用可能としない。
- **確認待ち**:実装の追加ではなく、指定端末・環境・運用での受入が必要。
- **保留**:利用者の保留指定、外部環境の選定、保存契約の合意が必要。
P0は認可・整合性・復旧の安全条件、P1は日常利用の回復性と負荷、P2は機能拡張、P3は
別サブプロジェクト。P0の記載は全項目で現に障害や脆弱性を再現したという意味ではない。
本番反映は別作業とし、今はソースと隔離fixtureを進められる。
## 未実装として数え直さないもの
| 範囲 | 確認できる実装 | 残す境界・根拠 |
| --- | --- | --- |
| 8つの命名部品 | GrammaraturからSeheweitまでの初期分離 | 分離はHA・全provider対応の完成ではない。[部品一覧](component-names.md) |
| イベントログ競合 | writer lockを先に取得、raw/bucket原子保存、完了ログ失敗で業務を再実行しない | `event_log_write.go`、`event_log_write_test.go`、`event_log_reporting_test.go`。[Dregarnuhr](dregarnuhr.md)。ログ欠落の自動回収は別件 |
| 共通outbox | Domain sequence、型別購読、checkpoint、タスクと正本の同時保存 | `activity_outbox.go`。[配送契約](outbox-delivery.md)。全producer・保持・分散配送は未完成 |
| タスク | 日別計画・持ち越し、親子、Wikiメモ、時間枠、Pomodoro、既定値、達成度・プロジェクト集計 | [生産性と連絡先](task-productivity-contacts.md)、[日別計画](task-daily-notes.md)。共有・日課・競合採否は別件 |
| 作業記録・日次メール | 作業訂正/取消/監査、配信履歴、同日手動再送、世代fence | `task_work_history*.go`、`task_digest_history*.go`。managed schema v3/v4。本番未反映 |
| オーガナイザー | 共通ナビ、個人設定、配置/サイズを保存するダッシュボード | [ダッシュボード](organizer-dashboard.md)。共有・任意widget runtimeとは別 |
| 連絡先 | 個人帳、vCard、履歴復元、trash、個人CardDAVとsync-token | [運用契約](../../operations/carddav-contacts.md)。外部CardDAV connector・共有帳・全端末互換ではない |
| DAV | 共通principal/資格情報、限定VEVENT/VTODO、プロジェクト対応、既定リスト名変更 | [所属統一](task-dav-project-unification.md)。CardDAVのsync-tokenを全CalDAV対応と読み替えない |
| グループ・公開 | `/g/` 管理、built-in管理グループ、匿名children/tree/City、明示/自動公開 | `groups_handler.go`、`groups_inheritance.go`、[公開契約](publication.md)。委譲shareと完全な権限配信は別 |
| 承認・通知 | 要求/決裁APIの限定slice、共通Inbox、アナウンス、タスク通知、Web Push | `publication_approval_api.go`。[Schutzaria](schutzaria.md)。継承reviewer・定足数・現行Revision再検査・決裁UIは残件 |
| ブログ・日記 | 作成、公開、同一Campus・同一instanceの射影、本文RSS、記事ナビ | [ブログ・日記](blog-diary.md)。遠隔射影、コメント、tDiary importは別 |
| エディタ | E1–E5、下書き、条件付き保存、競合確認、復元、言語分岐、保護HTML | [編集契約](editor-resilience.md)。全入口の同一契約・E6共同編集は未完成 |
| ストレージ・DR | Geduldhローカル不変payload、migration台帳、offline snapshot/verify/restore/手動promote | `internal/dr/snapshot.go`、`node.go`。[DR計画](dr-delivery-plan.md)。オンライン同期・自動切替ではない |
## 次の実施順
2026-11-08、利用者指定により、大きな機能を提供するサブプロジェクトを以下の順で進める。
前案の小規模な回復・基盤改善を先に一巡する順序は、この順序へ置き換える。
| 順番 | サブプロジェクト | 主な残件ID | 利用できる機能のまとまり |
| --- | --- | --- | --- |
| 1 | RSS・情報収集オーガナイザー | RSS-01 | 購読・フォルダ・記事一覧、出典付きWiki保存、タスク化、ダッシュボード連携 |
| 2 | 構造化ドキュメントと編集部品 | CSV-01、DIAGRAM-01、EDIT-01の関連部分 | CSV埋め込み/編集、図表部品挿入、nwdiag/Draw.io等のpreviewと安全な保存 |
| 3 | 料理レシピ管理 | RECIPE-01 | 材料・手順・分量変更・材料検索・買い物リスト |
| 4 | タスク・プロジェクト管理の第2段階 | TASK-01、DAV-01、REC-01/02、ORG-01の関連部分 | 日課・週次計画・capacity・依存関係・進捗・同期競合の採否 |
| 5 | 共同作業・公開承認・共有 | PUB-01、SHARE-01、COLLAB-01の関連部分 | 承認者Inbox/決裁、公開影響確認、期限付き共有、権限総覧 |
各表のP0〜P3は安全性・負荷等の性質を示す分類として残し、このサブプロジェクト順を覆す
一律の実行順にはしない。AUTH-01、OUT-01、QUEUE-01、STORE-01等は、対象機能に必要な
範囲を各段階へ組み込む。必須の認可・DB整合性・再送防止は省略せず、無関係な基盤改修を
すべて完了させるまで機能開発を止める構成にも戻さない。
最初のRSSは[着手単位](feeds.md#実施段階)に従う。内蔵は簡易正本、本格運用はFreshRSS正本と
する方針を採用。接続先・版・資格情報は別途指定し、まずprovider契約と合成fixtureで一覧・
保存フローを検証する。各サブプロジェクトはUI・API・
保存・試験が揃った単位でコミットし、本番反映は別ゲートとする。
別セッションとの保存層の競合がある場合は、該当ファイルの作業境界を合意してから進める。
## A. 整合性・回復・認可
| ID / 優先 | 状態・次の作業 | 依存・完了条件 | 詳細 |
| --- | --- | --- | --- |
| REC-01 / P1 | 一部実装:失効Domainの取り残された作業記録を終了・回復 | 現在の文書閲覧権限を再付与せず、本人の停止に必要な最小情報だけ扱う。他Domainタイトル非露出、同時停止・新規開始・失効競合を検証 | [作業記録](task-productivity-contacts.md) |
| REC-02 / P1 | 一部実装:取消からの復元、監査保持/export | REC-01と既存CAS。復元も新監査版、重複時間再検査、履歴の削除と正本の保持を分離。保持期間を決めるまで削除しない | 同上 |
| AUTH-01 / P0 | 一部実装:policy snapshot・ユーザー領域事前読込、更新時の認可fence | 入口一覧を作り、revokeと更新、alias、owner移管、公開境界移動をfixture化。接続1本でも認可callbackが別接続待ちにならない | [Schutzaria](schutzaria.md)、[認証](auth-permissions.md) |
| AUTH-02 / P0 | 一部実装:identity連結解除・メール所有・最後の管理者保護、外部group mapping | OIDC/LDAP認証自体は維持。由来付きgrant、解除・同期失効・最後の管理者の並行操作を検証 | [認証](auth-permissions.md) |
| PUB-01 / P0 | 一部実装:承認者継承/定足数/対象再検証/決裁UI、広域公開影響のページング | AUTH-01。現行source/policy/generation一致、権限失効後の決裁拒否、alias・依存Asset・移動で公開を広げない | [公開](publication.md) |
| PUSH-01 / P0 | 一部実装:claim世代をattemptから分離、購読version付き削除、Retry-After | 古いworker結果と新購読の競合、送信成功後DB失敗、lease超過を注入。配送はexactly-onceと表現しない | [Ordoschnellic](ordoschnellic.md) |
| QUEUE-01 / P0 | 一部実装:終了context/drain、汎用lease・heartbeat・fencing | 所有世代を比較して完了、旧worker遅延完了を拒否。全worker/CLIを含むwriter契約が揃うまで多重運転しない | [Dregarnuhr](dregarnuhr.md) |
| OUT-01 / P1 | 一部実装:未接続producer、consumer別再生/backoff/公平性、保持 | consumer変更時のbootstrap、一意キーtombstone、最遅必須consumerを含む安全な清掃。heads/checkpointだけ残して履歴を安易に削除しない | [outbox](outbox-delivery.md) |
| MAIL-01 / P1 | 一部実装:キュー中再送の取消、Inbox結果、保持設定/receipt整理 | attempt世代、同意、設定versionを再検査。詳細90日整理と重複防止記録の寿命を区別。SMTP結果不明を自動再送しない | [日次メール](task-digest-delivery.md) |
| STORE-01 / P0 | 一部実装:legacy schema/context整理、整合snapshot・restore契約、GC pin | migrationの再実行・途中失敗・旧データ保持・参照整合性。DB、CAS、可変正本、暗号鍵、外部副作用を一体で復旧判断 | [保存移植性](../storage-portability-and-operations.md)、[Geduldh](geduldh.md) |
## B. スケーリングと日常UX
| ID / 優先 | 状態・次の作業 | 依存・完了条件 | 詳細 |
| --- | --- | --- | --- |
| SEARCH-01 / P1 | 一部実装:現行head/Domain/言語別索引、H1 metadata、再開可能backfill | AUTH-01。候補上限を網羅性と混同せず、watermark・continuation・日本語検索・起動時間を測定。失効した結果をcacheから戻さない | [Mestionora](mestionora.md) |
| CITY-01 / P1 | 一部実装:生成由来/手動上書き/除外tombstone、局所reconcile | OUT-01の既存基盤を再利用。同入力で無変更、rename/merge後も手動名を保持、URL更新と所属の整合性 | [City](knowledge-city.md)、原ロードマップCITY-A〜D |
| NAV-01 / P1 | 一部実装:directory索引、有界children/Cityページング、共通sidebar | AUTH-01、CITY-01。隠れた件数・ラベルを出さず、Domain全件読込に依存しない。巨大階層の時間・行数・heapを測定 | [ナビ](navigation-search.md) |
| RENDER-01 / P1 | 一部実装:provider版付きcache、同一描画の集約/同時数/清掃、隔離 | 描画をforegroundより低優先にする。停止・更新・認可失効・cache破損・容量不足・外部参照を検証 | [Kunstzeal](kunstzeal.md) |
| OBS-01 / P1 | 一部実装:情報源別availability、収集共有、rollup/清掃予算、component metrics | 欠測を0としない。返却点数だけでなく走査量を制限。CPU/RAMを根拠なくproviderへ按分しない。SNMPは別選定 | [Seheweit](seheweit.md) |
| EDIT-01 / P1 | 一部実装:新規GET時のPage作成を保存まで延期、全入口CAS、block単位保護HTML/挿入palette | 既存E1–E5を回帰。Page+Path+初回Revisionを原子的に作り、閉じるだけでは空Pageを残さない。PlainText変換は欠落を明示 | [編集](editor-resilience.md)、[日常UX](priority-ux-20261031.md) |
| UX-01 / P1 | 一部実装:idle session timeout/個人短縮設定、独立CLIとの契約整理 | 絶対期限とは区別。読み取りpollだけで無期限延長しない。CLI本体の検索・転送等は独立repoを確認して別issue化 | [日常UX](priority-ux-20261031.md)、[CLI契約](remote-cli.md) |
## C. オーガナイザーと利用機能
| ID / 優先 | 状態・次の作業 | 依存・完了条件 | 詳細 |
| --- | --- | --- | --- |
| DAV-01 / P1 | 一部実装:上書き競合の候補保存、Inbox、比較/採否 | task履歴と通知を原子的に対応付け、再同期で候補を重複させない。別人・別Domainへ候補本文を漏らさない | [所属統一](task-dav-project-unification.md) |
| DAV-02 / P2 | 一部実装:VTODO時刻期限/繰返し/alarm、VEVENT temporal拡張、差分同期 | 対応profileを分離。未知propertyの保持・拒否、削除tombstone、期限切れtokenからの復旧。選定clientで認定するまで完全準拠としない | [共通DAV](caldav-common.md)、[calendar](calendar-implementation.md) |
| TASK-01 / P2 | 一部実装:日別capacity、一括計画、focus backfill、Pomodoro周期/日課 | 見積・実績・予定を混同せず、次回生成を冪等化。親子2階層と一般依存DAGを別に扱う | [時間枠](task-planning-focus.md)、[タスク](tasks-tickets.md) |
| ORG-01 / P2 | 一部実装:一般予定通知、共有タスク/アドレス帳、owner回復 | AUTH-01、OUT-01。共有相手失効、担当だけでは本文権限を増やさない、通知の重複抑止。外部CardDAV書込は別ゲート | [予定・連絡先](calendar-contacts.md) |
| DASH-01 / P2 | 一部実装:同種widget複数、共有layout、plugin widget | 個人layoutとの分離、配置版競合、widgetごとの認可/取得上限。第三者実行はEXT-01後 | [dashboard](organizer-dashboard.md) |
| SHARE-01 / P2 | 未実装:委譲share URL、期限/失効/監査、説明可能な権限総覧 | AUTH-01、PUB-01。login/adapter/federation資格情報と別目的、単一Page投影、caveat・依存物の失効を検証 | [公開](publication.md)、[Schutzaria](schutzaria.md) |
| COLLAB-01 / P2 | 一部実装:Page等の変更購読、最近閲覧/編集、集約通知。コメント/反応/評価は未実装 | OUT-01。現在権限で再投影、private履歴のopt-in/保持。inline commentはRevision変更時の孤立・再接続を扱う | [拡張](extensions.md) |
| BLOG-01 / P2 | 一部実装:安全なtDiary import、最近コメント、遠隔ブログ射影 | COLLAB-01、遠隔はFED-01。importは再開/重複抑止/dry-run。同一instance射影を再実装しない | [ブログ・日記](blog-diary.md) |
| CSV-01 / P2 | viewerのみ実装:別Documentを投影するCSV macro/editor、ODS境界 | 行列・セル・総量上限、Revision固定、権限、CSV引用/改行fixture。表示HTMLをCSV正本へ逆流させない | [表設計](extensions.md) |
| DIAGRAM-01 / P2 | 一部実装:nwdiag/Draw.io provider、slide preview/editor | RENDER-01。安全なSVG・include拒否・renderer版固定、ticket/CAS保存、print。既存Excalidraw/Krokiとは分ける | [Kunstzeal](kunstzeal.md) |
| RECIPE-01 / P2 | 未実装:構造化レシピmetadataとPage投影の初期slice | 材料・数量・未知単位を保持、人数変更は派生値、Asset認可、版競合、出典・公開確認 | [料理レシピ](recipe-management.md) |
## D. 選定・別サブプロジェクト
| ID / 優先 | 状態・次の作業 | 着手ゲート・最初の検証 | 詳細 |
| --- | --- | --- | --- |
| RSS-01 / P2 | 一部実装:内蔵簡易正本、FreshRSS読取connector、3ペインUI・HTML本文、Wiki保存・タスク化・dashboard、OPML確認付き入出力 | 残件はFreshRSS既読/star書戻し、購読/カテゴリnavigation、実接続受入、公平性/backoff。managed schema feeds v3・隔離fixture。本番未反映。二重正本・停止時の自動切替なし | [RSS](feeds.md) |
| IMAP-01 / P3 | 未実装・選定待ち:IMAP読取同期 | provider/認証/保持を決める。UIDVALIDITY/UID、添付量、外部画像、失効をfixture化。neomutt captureは実装済みで別 | [メール](mail.md) |
| TICKET-01 / P3 | 未実装:queue/番号/workflow/外部依頼者 | 共有範囲と状態遷移を定義、同時採番・失効・監査。既存個人タスクをticketと呼び替えない | [タスクとticket](tasks-tickets.md) |
| EXT-01 / P3 | built-inのみ:第三者manifest/署名/隔離runtime、停止・rollback・観測 | capability・secret・host・予算・export契約。任意コードをGoプロセス内へ直接ロードしない | [拡張](extensions.md) |
| FED-01 / P3 | 一部実装:管理設定ACK/鮮度、選択権限情報の署名付きDomain配信 | 既存group継承を保持。origin/sequence/競合/revoke/再参加を定義し、独立ローカルgrantを消さない | [Campus](campus-federation.md) |
| BACKEND-01 / P3 | 未実装:S3/SFTP、読取WebDAV/SSH、外部SQL、設定/復旧UI | STORE-01。conditional create、Range、破損・切断・遅延・mount喪失の共通試験。単なるmountを正式対応としない | [Geduldh](geduldh.md)、[運用](operations.md) |
| DR-01 / P3 | D1未実装:DR01〜13、LB/HA、controller/data authority分離 | 最初は型・pure state machine・mock probe。writer境界合意、fence設備、RPO/RTO、復旧訓練なしで自動昇格しない | [DR実施計画](dr-delivery-plan.md) |
| NETWORK-01 / P3 | 未実装:IPAM/DNS/資産管理、検疫scanner | 型付き正本、権限、dry-run→承認→apply、冪等性。スキャン/外部DNS更新を閲覧リクエスト内で行わない | [拡張](extensions.md)、[原ロードマップ](../implementation-roadmap.md) |
| CONTENT-01 / P3 | 未実装部分:他Wiki記法、widget、外部検索/Linkding、時刻・日没theme | provider単位の送信同意・上限・失効・変換欠落試験。既存壁紙色themeと時刻条件themeを区別 | [拡張](extensions.md)、[viewer/editor](viewer-editor.md) |
### 明示的に保留するもの
- **HOLD-01**:`/tasks/` 配下のメールWikiルーティング調査。利用者の棚上げ指定を維持。関連未コミットファイルを今回の実装・文書コミットへ混ぜない。
- **HOLD-02**:vim.wasm runtime改善とnativeアプリ起動。既存設定UIは残すが、自動的に再開しない。
- **HOLD-03**:全面WYSIWYG、E6 CRDT/OT共同編集。まずEDIT-01と認可/presence契約、保存量・オフライン・競合モデルを確定する。
- **HOLD-04**:本番への反映。作業履歴/日次メールのv3/v4移行を含む同世代保全と隔離復元試験、容量確認、SMTP副作用照合が必要。古いbinaryを新schemaへ向ける切戻しは禁止。
## 実装と分けて管理する受入
| ID | 確認待ち | 今回確認していない範囲 |
| --- | --- | --- |
| ACCEPT-01 | iPhone SE/Safariのカレンダー、IME、電話handoff | Chromiumの狭幅fixtureだけでは実機受入としない |
| ACCEPT-02 | Pushの背景受信・再訪・logout・VAPID rotation | 同意済みテスト端末を使う。実通知先へ勝手に送信しない |
| ACCEPT-03 | CardDAV/CalDAV clientごとの操作matrix | 過去のTasks.org動作確認は保持するが、全版・全操作の保証に広げない |
| ACCEPT-04 | 独立pages-cliの現行契約・配布物 | このrepoの旧Python版でWindows/TLS設定/配布状態を判定しない |
## 完了・コミット・文書更新の規則
変更単位ごとに契約、実装、拒否/競合/再送/上限試験を揃えてコミットする。DB変更では
旧schema fixture、migration再実行、途中失敗rollback、参照整合性、復旧方式を必要条件にする。
破壊的変更・GC・外部副作用は、単に全Goテストが通ることだけで実施可能と判定しない。
完了時は本書の該当IDと詳細書の状態を同時更新し、ソースcommit、試験、release、本番適用を
別々に記録する。旧Phaseの大項目は後続が残る限り丸ごとチェックせず、完了sliceを分ける。
残件の総数を完了率には使わない。各行は規模が異なり、着手時に実装・受入単位へ分割する。
+7 -3
View File
@@ -1,9 +1,13 @@
# PagesWiki 開発設計書 # PagesWiki 開発設計書
更新日:2026-09-19。日本語の統合設計版。実装の変更履歴ではなく、現在の境界と次の設計判断を確認するための入口です。 更新日:2026-11-08(統合版の初版は2026-09-19)。実装の変更履歴ではなく、現在の境界と次の設計判断を確認するための入口です。
## この設計書の読み方 ## この設計書の読み方
**次に実装する作業は[実装残件台帳](implementation-backlog.md)を入口にする。** `e65ca56`を基準に、実装済みsliceと後続、優先順、依存・完了条件、実機確認待ちと保留を整理した。古い大項目の未チェックを、そのまま全機能未実装と解釈しない。
2026-11-08追加:[日次タスクメールの配信履歴と手動再送](task-digest-delivery.md)。予約・要求receipt・詳細履歴の保持境界と、DB移行・復旧時の二重送信防止。
2026-11-02追加:[オーガナイザーの共通ナビゲーションと個人ダッシュボード](organizer-dashboard.md)。配置可能なウィジェット、個人設定への統合、保存・認可・読込負荷の契約。 2026-11-02追加:[オーガナイザーの共通ナビゲーションと個人ダッシュボード](organizer-dashboard.md)。配置可能なウィジェット、個人設定への統合、保存・認可・読込負荷の契約。
2026-11-01追加:[タスク作成・作業記録・達成度レポート・日次メール・個人アドレス帳](task-productivity-contacts.md)。[neomuttとCLIの操作リファレンス](../../operations/neomutt-tasks.md)も参照。 2026-11-01追加:[タスク作成・作業記録・達成度レポート・日次メール・個人アドレス帳](task-productivity-contacts.md)。[neomuttとCLIの操作リファレンス](../../operations/neomutt-tasks.md)も参照。
@@ -52,6 +56,6 @@
21. [決定事項・矛盾の整理・未決事項](/lab/pageswiki-dev/decisions) 21. [決定事項・矛盾の整理・未決事項](/lab/pageswiki-dev/decisions)
22. [原資料との対応と外部仕様](/lab/pageswiki-dev/sources) 22. [原資料との対応と外部仕様](/lab/pageswiki-dev/sources)
## 今回の範囲 ## 初版の範囲とその後の更新
今回行うのは設計の再整理と日本語設計書の配置です。オーガナイザー、外部サービス、DAVサーバー、メールアカウント、公開権限の新設は行いません。設計書の配置も匿名公開の許可とは別です。 2026-09-19の初版は設計整理と日本語設計書の配置を対象にした。その後の実装は各章の日付付き記録と残件台帳に反映する。2026-11-08の再照合は文書とソースの静的確認であり、本番データの監査・外部サービスへの接続・本番適用は行っていない。設計書の配置も匿名公開の許可とは別です。
+3 -2
View File
@@ -38,7 +38,8 @@
- [x] HTTPクライアント初期版 `pages-cli`/コマンド `pages`(2026-11-01ホストのユーザーPATHへ配置済み。実ユーザログインは未実施):configureによる接続先・既定プロファイル・言語管理、パスワード/TOTP、Bearer保持・失効、ls/cd/cat/view/get、既存Page edit/put、Path補完、メモリ内履歴、検索候補、ブラウザ引き継ぎ。詳細と原設計整合は [遠隔CLI](remote-cli.md)。既存DB直アクセスCLIは変更しない。 - [x] HTTPクライアント初期版 `pages-cli`/コマンド `pages`(2026-11-01ホストのユーザーPATHへ配置済み。実ユーザログインは未実施):configureによる接続先・既定プロファイル・言語管理、パスワード/TOTP、Bearer保持・失効、ls/cd/cat/view/get、既存Page edit/put、Path補完、メモリ内履歴、検索候補、ブラウザ引き継ぎ。詳細と原設計整合は [遠隔CLI](remote-cli.md)。既存DB直アクセスCLIは変更しない。
- [x] 専用起動コマンドとセッション保持は独立HTTPクライアント `pages` として実装。既存 `data/pageswiki-cli` のDB直接操作とは分離し、資格情報に0600の保護と失効/ログアウトを設ける。 - [x] 専用起動コマンドとセッション保持は独立HTTPクライアント `pages` として実装。既存 `data/pageswiki-cli` のDB直接操作とは分離し、資格情報に0600の保護と失効/ログアウトを設ける。
- [ ] `Ctrl+P` のfuzzy検索と、ローカル `$EDITOR` 編集。現在版/ETagを保持してアップロード時の競合を検出し、上書き・差分・キャンセルを選べるようにする。 - [x] ローカル `$EDITOR` 編集、現在Revisionを指定した保存、409時の原稿保全(分離前のHTTPクライアント)。
- [ ] `Ctrl+P` のfuzzy検索と競合の対話選択。独立CLI側の現状を確認して追跡する。サーバーの新規作成契約とは分ける。
- [x] ターミナルにURLを表示するブラウザ認証(OIDC)。`pages login --oidc [--no-browser]`、S256、5分の単回grant、確認コード/CSRF/TOTP、失効再確認、poll・発行上限を実装。2026-11-01 v0.3.0でサーバー・ホストCLIへ本番反映済み。実providerでの本人承認は利用者確認が残る。契約は[遠隔CLI](remote-cli.md)を参照。 - [x] ターミナルにURLを表示するブラウザ認証(OIDC)。`pages login --oidc [--no-browser]`、S256、5分の単回grant、確認コード/CSRF/TOTP、失効再確認、poll・発行上限を実装。2026-11-01 v0.3.0でサーバー・ホストCLIへ本番反映済み。実providerでの本人承認は利用者確認が残る。契約は[遠隔CLI](remote-cli.md)を参照。
- [ ] ターミナル表示が難しい内容はブラウザに引き継ぐ。short linkは認証を迂回しない内部リンクとし、公開share link発行とは分離する。自動公開しない。 - [ ] ターミナル表示が難しい内容はブラウザに引き継ぐ。short linkは認証を迂回しない内部リンクとし、公開share link発行とは分離する。自動公開しない。
@@ -123,7 +124,7 @@
現行 `data/pageswiki-cli` はSQLite/Document Storeへ直接アクセスする運用CLI。shell、補完、履歴、`$PAGER`/`$EDITOR` は既にあるが、Web認証を受けるリモートクライアントではない。Webユーザの認可をSQLite直アクセスへ継ぎ足さず、HTTP transportを持つ別コマンド(仮称 `pageswiki remote`)として段階導入する。 現行 `data/pageswiki-cli` はSQLite/Document Storeへ直接アクセスする運用CLI。shell、補完、履歴、`$PAGER`/`$EDITOR` は既にあるが、Web認証を受けるリモートクライアントではない。Webユーザの認可をSQLite直アクセスへ継ぎ足さず、HTTP transportを持つ別コマンド(仮称 `pageswiki remote`)として段階導入する。
2026-11-01:試作名 `data/pageswiki-remote` から、ユーザ指定によりサブプロジェクト `pages-cli`/実行名 `pages` へ整理。`pages configure` と既定値により通常操作のprofile/lang引数を不要とした。OIDCハンドオフはv0.3.0で本番反映済み。実providerでの本人承認、Ctrl+P、新規作成、強制上書きの対話選択、short linkは残件。[範囲・制限・検証](remote-cli.md)を参照。 2026-11-01:試作名 `data/pageswiki-remote` から、ユーザ指定によりサブプロジェクト `pages-cli`/実行名 `pages` へ整理。`pages configure` と既定値により通常操作のprofile/lang引数を不要とした。OIDCハンドオフはv0.3.0で本番反映・利用者確認済み。既存Pageの言語分岐と編集はv0.4.0で対応済み。新規Pageの原子的な作成は別件。2026-11-06以降のCLI本体は独立repoを正とし、Ctrl+P・転送・配布対応等の現状を旧同梱版だけで判定しない。[範囲・制限・検証](remote-cli.md)を参照。以下の番号付き項目は初期実装時の契約で、すべて未着手という意味ではない。
1. パスワード+TOTPの既存JSONログイン、失効/logout、接続先・Principal・Domain別セッション保存。秘密情報はコマンド引数/履歴/ログへ出さず、保存先0700・資格情報0600相当、TLS検証必須。 1. パスワード+TOTPの既存JSONログイン、失効/logout、接続先・Principal・Domain別セッション保存。秘密情報はコマンド引数/履歴/ログへ出さず、保存先0700・資格情報0600相当、TLS検証必須。
2. `ls/cd/view/get/edit` と現在Pathの保持。検索は認可付き・件数制限・キャンセル可能なAPIを使用し、`Ctrl+P` のfuzzy選択へ接続。端末の制御シーケンスは本文/タイトルから除去する。 2. `ls/cd/view/get/edit` と現在Pathの保持。検索は認可付き・件数制限・キャンセル可能なAPIを使用し、`Ctrl+P` のfuzzy選択へ接続。端末の制御シーケンスは本文/タイトルから除去する。
+3 -3
View File
@@ -1,6 +1,6 @@
# 6. 公開・承認・共有・匿名ナビゲーション # 6. 公開・承認・共有・匿名ナビゲーション
状態:明示的なRevision公開、Street/Spotの自動公開、匿名ナビゲーションは実装済み。承認者Inboxは受信者単位の保存・既読APIまで実装済みで、承認要求の生成・決裁UI・配送ワーカーはTODO。共有URL管理もTODO。 状態:明示的なRevision公開、Street/Spotの自動公開、匿名ナビゲーションは実装済み。承認要求の登録・限定決裁APIと受信者別Inboxは実装済み。上位方針・reviewer継承、定足数、現行Revisionの再検証、決裁UIを含む公開運用フローは未完成。共有URL管理もTODO。2026-11-08、`publication_approval_api.go`と照合。
## 三つの異なるもの ## 三つの異なるもの
@@ -42,9 +42,9 @@ Domain・Quartierもrequiredの上限制約を設定できます。automaticは
状態はdraft → requested → approved / rejected / expired / supersededを案とします。取り消し・公開撤回は別監査イベントです。上位の承認必須設定を下位が解除できないこと、自動公開との優先順位、本人承認の可否を実装前に確定します。 状態はdraft → requested → approved / rejected / expired / supersededを案とします。取り消し・公開撤回は別監査イベントです。上位の承認必須設定を下位が解除できないこと、自動公開との優先順位、本人承認の可否を実装前に確定します。
共通通知Inboxの第一段階として、`notification_inbox`にDomain・受信者・イベント・種別を保存し、同じ組合せを重複排除します。`GET /_/api/v1/notifications` は現在のPrincipalに限定して `unread`、`limit`、`before` のkeyset取得を行い、`POST/PATCH /_/api/v1/notifications/read` は同じDomain・受信者の通知だけを既読化します。通知は権限や承認を付与せず、本文や秘密を保存しません。承認要求からの生成と再表示時の権限再検査は次段階です。 共通通知Inboxの第一段階として、`notification_inbox`にDomain・受信者・イベント・種別を保存し、同じ組合せを重複排除します。`GET /_/api/v1/notifications` は現在のPrincipalに限定して `unread`、`limit`、`before` のkeyset取得を行い、`POST/PATCH /_/api/v1/notifications/read` は同じDomain・受信者の通知だけを既読化します。通知は権限や承認を付与せず、承認対象の本文や秘密を保存しません。要求登録からの限定的なInbox生成は下記のAPIに接続済み。現行対象の再検査を含む完全な決裁フローは次段階です。
承認要求の対象(Path/Object/RevisionのハッシュとJSON)、policy version、generation、期限、要求者を`publication_requests`に固定保存し、決裁履歴を`publication_decisions`へ追記します。期限付きpending要求はスケジューラが小さなbatchでexpiredへ遷移させます。レビュアーの現在権限を再検査する決裁APIとInbox UIは、まだ公開していません。 承認要求の対象(Path/Object/RevisionのハッシュとJSON)、policy version、generation、期限、要求者を`publication_requests`に固定保存し、決裁履歴を`publication_decisions`へ追記します。期限付きpending要求はスケジューラが小さなbatchでexpiredへ遷移させます。Domain管理者向けの限定決裁APIは存在しますが、委譲レビュアーの決裁UI・完全な公開承認フローを提供した状態ではありません。
認証済みのDomain管理者向けに、`POST /_/api/v1/publication/requests`でboundedな対象manifestを登録できます。idempotency keyで再送をまとめ、指定されたレビュアーがDomain管理権限を持つ場合だけInbox通知を作成します。`POST /_/api/v1/publication/requests/{uuid}/decisions`はDomainとgenerationを再検査し、承認・拒否をCASで一度だけ適用します。上位方針の継承、定足数、対象Revisionの現在性検査は、公開運用へ進む前の残タスクです。 認証済みのDomain管理者向けに、`POST /_/api/v1/publication/requests`でboundedな対象manifestを登録できます。idempotency keyで再送をまとめ、指定されたレビュアーがDomain管理権限を持つ場合だけInbox通知を作成します。`POST /_/api/v1/publication/requests/{uuid}/decisions`はDomainとgenerationを再検査し、承認・拒否をCASで一度だけ適用します。上位方針の継承、定足数、対象Revisionの現在性検査は、公開運用へ進む前の残タスクです。
+5
View File
@@ -2,6 +2,11 @@
2026-11-06:利用者向けの[クライアント契約](https://git.anya.jp/anya/pages-cli/src/branch/main/docs/client-contract.md)と[NeoMutt連携](https://git.anya.jp/anya/pages-cli/src/branch/main/docs/neomutt.md)は独立した公開pages-cliリポジトリへ移転した。本書はサーバー側のHTTP・OIDC認証契約、結合テストと導入履歴を保持する。下記のv0.3.0/v0.4.0は分離前のサーバー同梱版の番号であり、独立CLIはv0.1.0から採番する。 2026-11-06:利用者向けの[クライアント契約](https://git.anya.jp/anya/pages-cli/src/branch/main/docs/client-contract.md)と[NeoMutt連携](https://git.anya.jp/anya/pages-cli/src/branch/main/docs/neomutt.md)は独立した公開pages-cliリポジトリへ移転した。本書はサーバー側のHTTP・OIDC認証契約、結合テストと導入履歴を保持する。下記のv0.3.0/v0.4.0は分離前のサーバー同梱版の番号であり、独立CLIはv0.1.0から採番する。
2026-11-08整理:以下のPython実装・Windows未対応・TLSバイパスなし等の記述と末尾の
「次段階」は分離前の同梱版の記録であり、独立CLIの現行制約ではない。独立版の実装・配布・
残件は上記repoを正とする。今回の文書照合では独立版HEADを取得していない。
サーバー側の新規作成/CAS等の後続は[残件台帳 EDIT-01 / UX-01](implementation-backlog.md)で追跡する。
2026-11-01。合成データで検証済み。同日、本番サーバのCity更新と合わせてクライアントをホストの `/home/anya/.local/bin/pages` に配置した。実ユーザの資格情報取得・ログイン・本番Page編集は未実施。接続設定とログインは利用者が行う。[反映記録](../../operations/deployments/2026-11-01-city-pages-cli.md)。 2026-11-01。合成データで検証済み。同日、本番サーバのCity更新と合わせてクライアントをホストの `/home/anya/.local/bin/pages` に配置した。実ユーザの資格情報取得・ログイン・本番Page編集は未実施。接続設定とログインは利用者が行う。[反映記録](../../operations/deployments/2026-11-01-city-pages-cli.md)。
v0.3.0のOIDCログインは利用者の動作確認済み。v0.4.0ではOIDC承認時の追加ローカルTOTPを廃止し、CLIの `auto` 言語選択と既存Pageの新規言語版編集を実装した。2026-11-01 21:48 JSTに本番サーバー/PATH上のCLIへ反映済み。[v0.4.0反映記録](../../operations/deployments/2026-11-01-v0.4.0.md)。 v0.3.0のOIDCログインは利用者の動作確認済み。v0.4.0ではOIDC承認時の追加ローカルTOTPを廃止し、CLIの `auto` 言語選択と既存Pageの新規言語版編集を実装した。2026-11-01 21:48 JSTに本番サーバー/PATH上のCLIへ反映済み。[v0.4.0反映記録](../../operations/deployments/2026-11-01-v0.4.0.md)。
+16 -4
View File
@@ -1,14 +1,26 @@
# 20. 実装順序と受け入れ条件 # 20. 実装順序と受け入れ条件
状態:2026-09-19再整理。旧Phase番号は原ロードマップとの対応用に残し、実装は依存関係とリスク順に進めます。 状態:2026-11-08再照合。旧Phase番号は原ロードマップとの対応用に残し、実装は依存関係とリスク順に進めます。
現在の作業単位・優先順・受入条件は[実装残件台帳](implementation-backlog.md)に集約しました。
以下の日付付き追記と旧優先表は経緯を残すものです。共通outbox、限定DAV、個人連絡先、
HTML保護、Inboxなどの初期sliceは実装済みで、旧表の項目全体が新規開発という意味ではありません。
2026-11-08の利用者指定により、**RSS・情報収集 → 構造化ドキュメント/編集部品 →
料理レシピ → タスク・プロジェクト管理の第2段階 → 共同作業・公開承認・共有**の順で進めます。
必要な認可・整合性・性能対策は各機能の受入条件へ組み込みます。以前の回復・基盤改善を
先に一巡する推奨順は置き換え、下記の日付付き「次は」は当時の記録として扱います。
## 優先順 ## 優先順
2026-11-08:[日次メールの配信履歴・手動再送・保持境界](task-digest-delivery.md)をソース実装。migration v4は旧予約を保持し、詳細整理後も日別予約と要求receiptを残す。本番反映は保留して整合性検証を優先する。次は失効Domainに取り残された作業記録の回復、取消済み作業記録の復元と監査保持。共有・外部書き込みは別段階。
2026-11-08:[作業記録の訂正・取消と監査履歴、プロジェクト別集計](task-productivity-contacts.md#作業記録の訂正と取消)をソース実装。タスク一覧・ダッシュボードから履歴画面へ接続。DBのmanaged migration v3を伴うため、本番反映はバックアップと別工程にする。日次メールの配信結果・手動再送・保持契約は同日の後続実装へ進んだ。失効Domainに取り残された作業記録の回復、共有、外部書き込みは残件。
2026-11-02:[共通ナビゲーション・個人設定・ウィジェットダッシュボード](organizer-dashboard.md)を実装。タスク/カレンダー/連絡先の導線統一、トップ・ナビバーの連絡先、個人別の配置・サイズ保存。ソース実装と本番反映は区別し、共有・外部ウィジェットは後続。 2026-11-02:[共通ナビゲーション・個人設定・ウィジェットダッシュボード](organizer-dashboard.md)を実装。タスク/カレンダー/連絡先の導線統一、トップ・ナビバーの連絡先、個人別の配置・サイズ保存。ソース実装と本番反映は区別し、共有・外部ウィジェットは後続。
2026-11-02:[個人CardDAV提供と履歴復元・一括export](../../operations/carddav-contacts.md)を実装。専用資格情報、条件付き更新、変更tokenと削除tombstone、bounded REPORTを追加。09:04:17 JSTに[v0.6.0として本番2系統へ反映](../../operations/deployments/2026-11-02-v0.6.0.md)。共有アドレス帳・外部CardDAV connector・実機互換確認は同資料の後続範囲。 2026-11-02:[個人CardDAV提供と履歴復元・一括export](../../operations/carddav-contacts.md)を実装。専用資格情報、条件付き更新、変更tokenと削除tombstone、bounded REPORTを追加。09:04:17 JSTに[v0.6.0として本番2系統へ反映](../../operations/deployments/2026-11-02-v0.6.0.md)。共有アドレス帳・外部CardDAV connector・実機互換確認は同資料の後続範囲。
2026-11-01:[タスク作成・作業記録・達成度・日次メール・個人アドレス帳](task-productivity-contacts.md)を追加。Web/CLI/neomuttの共通capture、時間記録、既定値、検索コマンドを接続。作業記録の事後訂正、日次メールの再送・保持期間は同設計の後続範囲へ集約する。 2026-11-01:[タスク作成・作業記録・達成度・日次メール・個人アドレス帳](task-productivity-contacts.md)を追加。Web/CLI/neomuttの共通capture、時間記録、既定値、検索コマンドを接続。作業記録の事後訂正と日次メールの再送・保持境界は2026-11-08の実装へ進んだ。
2026-10-31:ユーザ指定により[日常操作・ブラウザ・エディタ・CLI](priority-ux-20261031.md)を次期開発の最優先とする。下記の従来機能追加より優先するが、認可・データ保全の安全条件は省略しない。TODO、受け入れ条件、復元用チェックポイントを同文書に集約する。 2026-10-31:ユーザ指定により[日常操作・ブラウザ・エディタ・CLI](priority-ux-20261031.md)を次期開発の最優先とする。下記の従来機能追加より優先するが、認可・データ保全の安全条件は省略しない。TODO、受け入れ条件、復元用チェックポイントを同文書に集約する。
@@ -34,7 +46,7 @@
| P1-3 | 検索・decode・大容量 | 現行上限と測定 | current-only検索、隔離、stream、cancel | | P1-3 | 検索・decode・大容量 | 現行上限と測定 | current-only検索、隔離、stream、cancel |
| P2-1 | 通知と承認Inbox | P0、outbox | 現在権限で再表示・決裁・失効 | | P2-1 | 通知と承認Inbox | P0、outbox | 現在権限で再表示・決裁・失効 |
| P2-2 | HTMLパーツ | round-trip fixture | macro/asset/未知HTMLを壊さない | | P2-2 | HTMLパーツ | round-trip fixture | macro/asset/未知HTMLを壊さない |
| P2-3 | local task | extension契約、原子的ジャーナル | 個人タスク/プロジェクト・Inbox通知・個人local時間枠・永続化Pomodoro・Todayの見積/時間枠/集中集約を実装済み。次は計画持ち越し、日課、共有 | | P2-3 | local task | extension契約、原子的ジャーナル | 個人タスク/プロジェクト・Inbox通知・個人local時間枠・永続化Pomodoro・日別計画と持ち越し・作業記録訂正と集計・日次メール再送を実装済み。次は失効Domainの作業記録回復、日課、共有 |
| P2-4 | RSS読取・Wiki保存 | connector共通部 | FreshRSS fixtureと安全なcopy | | P2-4 | RSS読取・Wiki保存 | connector共通部 | FreshRSS fixtureと安全なcopy |
| P2-6 | 料理レシピ管理 | Document/Asset、検索、権限 | 材料・分量・手順・調理時間・人数・タグを構造化し、単位変換と安全な共有を検証 | | P2-6 | 料理レシピ管理 | Document/Asset、検索、権限 | 材料・分量・手順・調理時間・人数・タグを構造化し、単位変換と安全な共有を検証 |
| P3 | calendar/contact/mail | import/secret/競合契約 | read-only同期、明示copy、障害復旧 | | P3 | calendar/contact/mail | import/secret/競合契約 | read-only同期、明示copy、障害復旧 |
@@ -57,7 +69,7 @@ P2以降の順序は利用価値で入れ替え可能ですが、P0の該当安
- 認証・グループ:実装あり。全endpoint監査・identity lifecycleが残り。 - 認証・グループ:実装あり。全endpoint監査・identity lifecycleが残り。
- 匿名公開・children/tree/City:実装あり。大規模影響確認、share、承認Inboxが残り。 - 匿名公開・children/tree/City:実装あり。大規模影響確認、share、承認Inboxが残り。
- ブログ・日記・公開射影・本文RSS:実装あり。遠隔射影、コメント、tDiary等が残り。 - ブログ・日記・公開射影・本文RSS:実装あり。遠隔射影、コメント、tDiary等が残り。
- オーガナイザー:カレンダーと限定CalDAV、個人タスク/プロジェクト・個別ONのInbox通知、個人local時間枠と永続化Pomodoroあり。[タスク実装契約](tasks-implementation.md)、[時間枠とPomodoro](task-planning-focus.md)参照。一般のカレンダー予定通知・共有・VTODO、RSS購読・IMAPは未実装。 - オーガナイザー:カレンダーと限定CalDAV、個人タスク/プロジェクト・VTODO同期・個別ONのInbox通知・個人local時間枠・永続化Pomodoro・日次メール・個人CardDAV・ウィジェットダッシュボードあり。[タスク実装契約](tasks-implementation.md)、[時間枠とPomodoro](task-planning-focus.md)、[タスク所属とDAV同期](task-dav-project-unification.md)参照。一般のカレンダー予定通知・共有、DAV競合候補の通知と採否選択、RSS購読・IMAPは残件。
- 全面WYSIWYG、native app起動、第三者runtime、vim.wasm改善:判断待ちまたは保留。 - 全面WYSIWYG、native app起動、第三者runtime、vim.wasm改善:判断待ちまたは保留。
## 共通のDone ## 共通のDone
+81
View File
@@ -0,0 +1,81 @@
# 日次タスクメールの配信履歴と手動再送
2026-11-08、`organizer.tasks 0.14.0` のソース実装。個人設定で配信試行を確認し、当日分を明示的に再送できる。送信は既存の `task.digest.deliver` イベントが行い、Webリクエスト内ではSMTPに接続しない。本番反映は別工程とする。
## 利用者の操作と公開範囲
入口は `/-/sys/config?panel=organizer` の「メール配信履歴・再送」。初期は閉じておき、開いたときだけ履歴を取得する。自動ポーリングは行わず、更新ボタンとページ送りを用意する。PCは表、スマホは表の領域内だけ横スクロールする。
本人・現在のDomainの履歴だけを返す。管理者権限は他人の履歴を読む根拠にならない。readonlyは閲覧だけ可能。無効化したタスク拡張へのAPIアクセスは拒否する。履歴には対象日、定期/手動/移行前の区分、状態、更新時刻、理由コードだけを表示する。本文・宛先・SMTP認証情報・生のエラーを保存しない。
再送は保存済みの配信設定が有効で、当日の配信試行が既に存在する場合だけ可能とする。初回配信の前倒しや過去日のまとめ送りは含めない。現在のタスクから本文を再構成し、現在の本人のアカウントメールアドレスへ送る。元のメールの完全な再現ではない。設定画面の未保存値は使わない。
送信済みまたは結果不明の場合は、受信状況を確認し、重複配信の可能性を了承するチェックを必須にする。送信中・待機中には次の再送を受け付けない。同じDomain・本人・日付につき、手動要求は最大5件。処理前に中止された要求も回数に含める。
## API
基点は `/_/api/v1/extension/organizer.tasks/`。既存の個人タスクAPIと同じ認証を用いる。CalDAV専用資格情報を一般APIの資格情報へ拡張しない。
| 操作 | URI | 内容 |
| --- | --- | --- |
| GET | `digest-history?limit=25&after=...` | 新しい順のitems、次のcursor、設定timezoneのtoday、当日のcurrent、手動要求数retries、配信設定enabled、retention_days |
| POST | `digest-history/retry` | request_id、day、expected_attempt、duplicate_acknowledgedを受け取り、受付成功は202とrequest_id |
一覧は1〜100件、既定50件、Webは25件。`created_ms, uuid` のkeysetを使う。cursorは256 bytes以内。expected_attemptは一覧で取得した当日のcurrent.uuidをそのまま送る。
要求IDはクライアント生成のcanonical UUID。同じ要求IDと同じ内容の再送は新たな予約を作らない。異なる内容での再利用、古いexpected_attempt、実行中、上限到達は412。対象日が現在の配信timezoneの今日でない場合や、必要な重複確認がない場合は422。配信設定OFF、readonly、権限不足は拒否する。APIはSMTP有効設定とアカウントのメール形式も確認する。
Webはネットワーク失敗時に同じ要求IDを保持して再試行する。受付済みの要求をGETで観測した場合は、再送を重ねず状態表示へ切り替える。古い要求のreceiptがある場合は、日付が過ぎたり詳細履歴が整理された後の同内容再送にも既存の受付IDを返す。新しいSMTP処理は起こさない。受付応答はメールの到達を意味しない。
## 状態とワーカー
| 状態 | 意味 | 次の扱い |
| --- | --- | --- |
| queued | 手動要求をDBで受付済み | スケジューラがclaimする |
| sending | 1つの試行が送信処理を担当 | 通常はsent、uncertain、cancelledへ遷移 |
| sent | SMTP処理が正常終了 | 受信箱への到達保証ではない。手動再送には確認が必要 |
| uncertain | SMTP結果が不明、またはワーカー中断の可能性 | 自動再送しない。手動再送には確認が必要 |
| cancelled | SMTPに渡す前に処理中止 | 原因を確認し、当日なら手動で再予約できる |
イベントは60秒周期。定期配信の候補は最大20件、手動キューは最大10件を取得し、両者を交互に処理する。全体の時間予算は30秒で、残りは次回へ持ち越す。手動キューは受付時の設定versionを固定する。claim時に権限・拡張機能・配信同意・設定version・対象日を確認し、変更されていれば送信中止にする。
SMTP直前にも、現在の権限・メールアドレス・同意version・拡張状態・対象日・試行IDを読み取りtransactionで再確認する。2分以上古いclaimでは送信を開始しない。処理の終了は現在の試行IDと状態が一致するときだけ保存し、古いワーカーが新しい試行を完了扱いにできないようにする。
10分以上sendingのままの試行は、イベントの保守処理で最大100件ずつuncertainへ移す。これは「未送信」との断定ではない。プロセス停止・長い休止・SMTPの応答喪失では送信済みの可能性がある。SMTPとDBを一つのtransactionにできないため、exactly-onceや、休止した古いプロセスからのネットワーク送信を完全に取り消す保証はしない。明示確認付きの再送でこの不確実性を扱う。
理由コードはSMTP未設定、タスク取得失敗、設定・権限変更、拡張無効化、対象日経過、SMTP結果不明、ワーカー中断に限定する。イベントログには件数だけを残す。
## DBの正本と保持境界
正本はSQLite。配信に関する以下の3つを分離する。
| テーブル | 役割 | 保持 |
| --- | --- | --- |
| organizer_digest_deliveries | Domain・本人・日付の予約と現在の試行ID/状態 | 恒久。日別の自動二重配信を防ぐ |
| organizer_digest_requests | 手動要求IDと要求内容・重複確認・受付時刻 | 恒久。同じ要求の再受付を防ぐ |
| organizer_digest_attempts | 個々の試行状態・理由・日時 | 終端状態のupdated_msから90日。queued/sendingは削除対象外 |
詳細の削除はイベント1回につき最大500件で、日時と状態のindexから選ぶ。設定や再送受付は別のwriter transactionで行い、削除と同じSQLite書き込みロックに従う。日別予約と手動receiptを削除・再作成しない。詳細が期限切れになると古い予約が指す試行の詳細行は存在しなくなるが、予約の状態と重複防止機能は残る。現時点では恒久記録の圧縮・失効は実装しない。
## 移行と復旧
task-productivityのmanaged migration v4で、既存の日別予約へattempt_uuidを追加し、試行・受付receiptのテーブルとindexを作る。既存の日別予約を削除せず、元のstateとupdated_msを保ってlegacy試行へ対応付ける。旧試行の設定version・timezoneは不明として扱い、現在の設定から過去の同意を推測しない。
DDL、旧データの対応付け、migration台帳の更新は同じtransactionで完了する。途中失敗なら旧スキーマと予約へ戻る。再実行でIDや予約を再生成しない。前段のv3を含め、旧migration定義の書き換えは行わない。
本番反映前には、書き込みを停止した整合DBバックアップと、対応するイメージを確保する。反映後にschema version、予約数、legacy試行との対応、`PRAGMA integrity_check` を確認する。旧イメージは更新DBに接続しない。forward-fixを基本とし、旧DBへ戻す場合は更新後の書き込みを保全して照合する。
特にメールはDB外で送信されるため、送信開始後のバックアップ復元だけでは重複防止を復元できない。復旧時はイベントの自動送信を止め、更新後の予約・receiptを保全して照合し、送信済みか不明な分を自動再送しないこと。移行・反映・復旧の成功と、実際の受信箱への到達は別の確認である。
## 受け入れ条件と後続
- 旧予約の全状態を保つ移行、反復、途中失敗のrollback、DB integrity check。
- 二重クリック・応答喪失後の同一要求、異内容再利用、当日制約、5回上限。
- readonly・別本人・未認証・配信OFF・権限失効・設定変更・日付経過の拒否または送信中止。
- 古い試行の送信前チェックと完了通知の拒否、SMTP結果不明の扱い。
- 詳細を削除した後も、手動receipt再送や日別自動claimで配信が復活しないこと。
- fake SMTPを用いた予約から送信・結果保存までの試験。ブラウザはPCと390px幅。
検証結果:`go test ./...`、`go vet ./...`、追加の受付競合・権限・保持境界テストの `-race`、JavaScript構文検査が成功。Chromiumの1440px・390pxで、履歴の遅延読込、重複確認、受付応答の喪失後の同一ID再試行、キュー処理後の状態表示を確認した。DB移行の反復・途中失敗のrollbackと `PRAGMA integrity_check` は隔離DBで確認。本番DBは変更していない。
後続は保持期間の管理UI、恒久receiptの安全な圧縮、当日予約の明示取消、配信結果のInbox連携、複数ワーカーの運用メトリクス。共有送信先・任意宛先・過去日の送信はこの機能へ暗黙に追加しない。
+38 -3
View File
@@ -13,6 +13,9 @@
| 実行中の記録 | GET `work` | itemがnullなら停止中 | | 実行中の記録 | GET `work` | itemがnullなら停止中 |
| 開始 | POST `work` | action=start、uuid、task_uuid。開始時刻はサーバーで記録 | | 開始 | POST `work` | action=start、uuid、task_uuid。開始時刻はサーバーで記録 |
| 終了 | POST `work` | action=stopとGETで得たitem。versionで条件付き更新 | | 終了 | POST `work` | action=stopとGETで得たitem。versionで条件付き更新 |
| 作業一覧 | GET `work-history?from=YYYY-MM-DD&to=YYYY-MM-DD&limit=50&after=...` | fromを含みtoを含まない期間と重なる本人の記録。最大367日、1ページ最大100件、取消済みを含む |
| 作業の履歴 | GET `work-history/{uuid}?before=VERSION` | 新しい版から最大100件。nextが0なら終了。それ以外は次のbeforeへ渡す |
| 作業の訂正・取消 | POST `work` | action=correctまたはcancel、uuid、version、reasonが必須。終了済み・未取消の記録のみ |
| レポート | GET `report?from=YYYY-MM-DD&to=YYYY-MM-DD` | fromを含み、toを含まない。ドメインのtimezone、最大367日 | | レポート | GET `report?from=YYYY-MM-DD&to=YYYY-MM-DD` | fromを含み、toを含まない。ドメインのtimezone、最大367日 |
| 予定を作る | POST `calendar-capture` | request_id、calendar_uuid、title、from、to。本人所有・非公開・非共有・外部sourceなしのカレンダー限定 | | 予定を作る | POST `calendar-capture` | request_id、calendar_uuid、title、from、to。本人所有・非公開・非共有・外部sourceなしのカレンダー限定 |
@@ -48,13 +51,35 @@ Webのタスク行の▶から開始し、上部の作業記録欄で終了す
深夜をまたぐ作業はローカル日付境界で分割し、夏時間の日も24時間固定にしない。Pomodoroと手動記録は別集計で、両者を合算しない。各系列は最大10,000件で、上限到達時はpartialを返して期間の絞り込みを促す。 深夜をまたぐ作業はローカル日付境界で分割し、夏時間の日も24時間固定にしない。Pomodoroと手動記録は別集計で、両者を合算しない。各系列は最大10,000件で、上限到達時はpartialを返して期間の絞り込みを促す。
### 作業記録の訂正と取消
2026-11-08に `organizer.tasks 0.13.0` としてソース実装。`/v/plan/tasks/work` で一覧と変更履歴を確認し、開始・終了をインラインで訂正する。タスク一覧とダッシュボードの達成度ウィジェットから移動できる。初期期間はドメイン時刻の今日を含む30日。ページ送りは開始時刻・UUIDのkeysetで行い、一度に全履歴を描画しない。変更履歴も開いた記録だけ取得する。スマホはラベル付き縦配置にし、操作部を44px以上とする。
訂正には `start_ms`・`end_ms`、または `start`・`end` の日時ペアを渡す。日時文字列はオフセット付きRFC 3339、あるいはドメインのタイムゾーンでの `YYYY-MM-DDTHH:mm[:ss]`。夏時間移行で存在しない・二通りに解釈できるローカル時刻は422。繰り返される時刻はAPIから明示オフセットを指定する。Webで未変更の時刻は元の瞬間をRFC 3339で送るため、ミリ秒やオフセットを失わない。
開始はUNIX epochより後、終了は開始より後かつ現在以前、訂正後の長さは31日以内。同じ本人の他の未取消記録と重なる区間は、別Domainも含めて412で拒否する。隣接区間は許可し、他Domainの記録内容は返さない。実行中は従来の終了操作を先に行う。`reason` は前後空白除去後1〜500 UTF-8 bytes、改行・NUL不可。Web入力は160文字まで。
取消は物理削除ではなく `cancelled_ms` を記録し、作業時間レポートから除外する。タスクの状態・完了ポイントは変更しない。元の時刻と全変更履歴は残る。訂正・取消後の自動復元、過去の記録の新規挿入はこの段階では扱わない。
現在値と監査snapshotは同じwriter transactionで保存する。認可・拡張機能の有効状態をロック内で再検査し、versionが一致しない更新は412。理由・変更前後の時刻・版・変更時刻を記録し、他人の記録を管理者権限だけで操作する経路は設けない。通信結果が不明な訂正・取消の再送もversionを検証し、古ければ412にする。最新値と履歴を再取得して判断する。Webは失敗時に入力を保持し、自動上書きしない。従来の開始・終了の再送契約は維持する。
移行前の記録には過去の編集履歴を捏造しない。初回の変更時に直前の値をbaselineとして捕捉する。baselineのchanged_msは捕捉時刻であり、過去の編集時刻ではない。未変更の旧記録を開いた際は現在値だけをbaselineとして表示する。
### プロジェクト別の集計
`report` の `projects` 配列にUUID・タイトル・完了数・ポイント・作業時間を返す。ダッシュボードの達成度ウィジェットに表を追加し、日付セルで選んだ日の内訳にも反映する。プロジェクトなしは独立した行とする。所属は現在のタスクから取得するため、過去時点の所属スナップショットではない。Pomodoroはプロジェクト表に合算しない。
集計は既存の上限付き取得結果を再利用し、プロジェクトごとに追加クエリを発行しない。10,000件上限でpartialとなる場合はプロジェクト表も部分集計であり、全件合計とは表示しない。
## 毎日のメール ## 毎日のメール
`/-/sys/config?panel=organizer` の個人設定で本人が有効化する。旧 `/v/plan/tasks/settings` もここへ移動する。送信先は現在の本人のアカウントメールアドレスのみ。管理者のSMTP設定が必要。時刻はHH:mmとIANA timezoneを保存する。`task.digest.deliver` イベントが60秒周期で期限到来設定を最大20件ずつ処理する。キュー停止・負荷・SMTP遅延で配信時刻が遅れる場合がある。 `/-/sys/config?panel=organizer` の個人設定で本人が有効化する。旧 `/v/plan/tasks/settings` もここへ移動する。送信先は現在の本人のアカウントメールアドレスのみ。管理者のSMTP設定が必要。時刻はHH:mmとIANA timezoneを保存する。`task.digest.deliver` イベントが60秒周期で期限到来設定を最大20件ずつ処理する。キュー停止・負荷・SMTP遅延で配信時刻が遅れる場合がある。
当日の計画・期限到来タスクを最大100件送る。件名、期限、取り組む日、見積、タスクURLを含め、メモは送らない。送信直前にアカウント状態・同意のversion・送信先を再確認する。メールによる保存・転送の可能性があるため、既定は無効。 当日の計画・期限到来タスクを最大100件送る。件名、期限、取り組む日、見積、タスクURLを含め、メモは送らない。送信直前にアカウント状態・同意のversion・送信先を再確認する。メールによる保存・転送の可能性があるため、既定は無効。
SMTPとSQLiteをまたぐexactly-onceは保証できない。送信前にDomain・本人・日付で予約し、配送結果はsent、uncertain、cancelled。予約後のクラッシュはsendingとして残す。結果不明の場合は自動再送せず、二重送信を避ける。翌日は通常通り処理し、過去日の取りこぼしをまとめて送らない。設定画面に直近結果を表示する。 SMTPとSQLiteをまたぐexactly-onceは保証できない。送信前にDomain・本人・日付で予約し、結果不明の場合は自動再送しない。翌日は通常通り処理し、過去日の取りこぼしをまとめて送らない。
2026-11-08に `organizer.tasks 0.14.0` として[配信履歴・当日分の手動再送・詳細90日保持](task-digest-delivery.md)をソース実装。個人設定の折り畳み履歴から操作する。送信済み/結果不明の再送には重複の了承が必要。詳細の保持期限後も日別予約・再送receiptを残し、重複防止を維持する。migration v4とDB外のメール送信を考慮した復旧条件は同設計を参照。
## トップ検索コマンド ## トップ検索コマンド
@@ -87,8 +112,18 @@ vCardの内容はDocumentStore snapshot、現在値と所有者・version・検
managed schemaにtask-productivityとcontactsを追加。task-productivityは既存organizer_items作成後に実行し、旧完了タスクを1回だけ集計projectionへ移す。既存スキーマ定義の改変ではない。切戻しでは旧版のschema checkが新コンポーネントを未知とするため、単に旧イメージを起動できるとは扱わない。データを消して戻さない。 managed schemaにtask-productivityとcontactsを追加。task-productivityは既存organizer_items作成後に実行し、旧完了タスクを1回だけ集計projectionへ移す。既存スキーマ定義の改変ではない。切戻しでは旧版のschema checkが新コンポーネントを未知とするため、単に旧イメージを起動できるとは扱わない。データを消して戻さない。
- 連絡先の共有アドレス帳、外部CardDAV connector、重複候補mergeは別段階。個人CardDAV提供面と外部サービスの取り込みは区別する。 - 連絡先の共有アドレス帳、外部CardDAV connector、重複候補mergeは別段階。個人CardDAV提供面と外部サービスの取り込みは区別する。
- 作業記録の事後修正・取消、失効Domainで取り残された記録の管理用終了手順、プロジェクト別集計、担当者への共有・通知は後続。 - 失効Domainで取り残された記録の管理用終了手順、取消済み記録の復元、作業監査履歴の保持期間、担当者への共有・通知は後続。
- 日次メールの手動再送・配信ログの保管期間、receiptの失効設計は後続。安易な削除で重複作成・配信を復活させない。 - 日次メールの保持期間UI、恒久receiptの圧縮・失効、配信結果のInbox連携は後続。現在は詳細90日、重複防止記録は恒久保持とする。
- vCard legacy変換・version間変換と履歴保持期間は後続。 - vCard legacy変換・version間変換と履歴保持期間は後続。
利用手順は [neomuttとタスクCLI](../../operations/neomutt-tasks.md) を参照。 利用手順は [neomuttとタスクCLI](../../operations/neomutt-tasks.md) を参照。
### 作業履歴の移行と検証
task-productivityのmanaged migration v3は、作業記録へのcancelled_ms追加、organizer_work_revisionsの作成、本人別の区間検索index追加を行う。既存の作業時刻・versionは保ち、既存記録は未取消とする。新しい監査データの正本はSQLiteで、アプリのDBバックアップに含める。
反映前に整合したDBバックアップを取得する。旧版は取消済み記録を正しく集計できず、schemaのversionも一致しないため、更新DBに旧イメージを向ける切戻しは禁止する。通常はforward-fixを選ぶ。復旧が必要ならアプリを停止し、移行前のDBと対応するイメージへ一緒に戻す。更新後の書き込みは別途保全・照合してから復旧する。本番反映は別工程。
受け入れ試験は、旧スキーマ移行の反復、本人・Domain分離、readonly・失効・無効化、版競合、重複時間、夏時間の欠落・重複、監査失敗時のrollback、並行訂正、取消による集計除外を対象とする。ブラウザはデスクトップと390px幅で訂正・取消・履歴・412時の入力保持を確認する。既存CLIの開始/終了とCalDAVの契約は変更しない。
2026-11-08の検証結果:`go test ./...`、`go vet ./...`、追加した競合・監査試験の `-race`、変更JSの構文検査が成功。Chromiumの1440px・390pxで、ダッシュボードのプロジェクト別表から履歴へ移動し、訂正・取消・監査表示・競合時の入力保持と横幅の収まりを確認した。すべて隔離fixtureによる試験で、本番移行は未実施。
+3 -3
View File
@@ -33,14 +33,14 @@ create/read/update/assign/transition/comment/manageは概念上分離します
iCalendarにはVTODOがあり、CalDAVはカレンダー資源を扱います。ただしクライアントごとのタスク・繰り返し対応を試験する必要があります。これは「CalDAVだけでチケット管理が完成する」という意味ではありません。[iCalendar](https://www.rfc-editor.org/rfc/rfc5545)、[CalDAV](https://www.rfc-editor.org/rfc/rfc4791)。 iCalendarにはVTODOがあり、CalDAVはカレンダー資源を扱います。ただしクライアントごとのタスク・繰り返し対応を試験する必要があります。これは「CalDAVだけでチケット管理が完成する」という意味ではありません。[iCalendar](https://www.rfc-editor.org/rfc/rfc5545)、[CalDAV](https://www.rfc-editor.org/rfc/rfc4791)。
最初はlocal task一覧と期限表示、次にVTODOの明示export/import、最後に選定クライアントとの同期を検討します。workflow、コメント、細かな権限をVTODOへ無理に押し込みません。失われる情報を変換警告に残します。 local task一覧と期限表示、限定VTODO/CalDAV同期は実装済みです。次は上書き競合の候補保存と採否、時刻期限・繰り返し・アラーム・差分同期を個別に扱います。workflow、コメント、細かな権限をVTODOへ無理に押し込みません。失われる情報を変換警告に残します。
## 実装TODO ## 実装TODO
- TASK-1:個人local作成/取得/更新、version競合、Page参照、所有者限定一覧は実装済み。削除は復元可能なアーカイブ、物理削除は未提供。 - TASK-1:個人local作成/取得/更新、version競合、Page参照、所有者限定一覧は実装済み。削除は復元可能なアーカイブ、物理削除は未提供。
- TASK-2:日付の期限/計画/見直し、タイトルフィルタ、archive、個別ONのInbox通知は実装済み。担当共有、通知集約、exportは未実装。 - TASK-2:日付の期限/計画/見直し、タイトルフィルタ、archive、個別ONのInbox通知は実装済み。担当共有、通知集約、exportは未実装。
- TASK-3:checklist/依存とboundedな一覧・calendar射影。 - TASK-3:2階層サブタスク、boundedな一覧、日割り工程表・個人時間枠は実装済み。一般依存グラフ、共有、一括計画・日別capacityは後続。
- TICKET-1:queue・番号予約・workflow版・権限付き遷移。 - TICKET-1:queue・番号予約・workflow版・権限付き遷移。
- TASK-4:VTODO fixture、未対応property保持、繰り返し・同期の判断。 - TASK-4:限定VTODO fixtureと同期、プロジェクト対応・既定リスト名変更は実装済み。繰り返し・時刻期限・差分同期と競合候補の通知/採否は後続。[所属統一](task-dav-project-unification.md)と[残件台帳](implementation-backlog.md)を参照。
同時採番、二重POST、完了取消、夏時間・終日、assignee失効、本文削除を受け入れ試験にします。 同時採番、二重POST、完了取消、夏時間・終日、assignee失効、本文削除を受け入れ試験にします。
+77
View File
@@ -0,0 +1,77 @@
# Gecko連携の再配布条件
PagesWiki v0.13.0は、自作の連携コードと中継サービスのソースを配布し、Geckoエンジンは同梱しません。
実験機能`viewer.gecko`は既定無効のままです。既存3イメージのリリースにエンジンを追加せず、
専用relayも上流エンジンを含まないDockerfileとして提供します。
本書は今回追加した部品の配布判断と残件を整理するもので、プロジェクト全体の法務監査ではありません。
## 部品と適用条件
| 部品 | 固定する出典・版 | ライセンスと配布時の扱い |
| --- | --- | --- |
| PagesWiki連携・Go relay | このリポジトリのリリースcommit | 自作コード。上流の実装をコピーせずWispフレーム仕様に対応。GeckoのライセンスをWiki全体へ一律適用しない |
| Go runtime / 標準ライブラリ | relayのビルドに使うGo 1.25系 | BSD-3-Clause。実ビルド環境のLICENSEを専用イメージ内`/licenses/Go.txt`へ収録 |
| golang.org/x/net | v0.35.0、WebSocket実装 | BSD-3-Clause。モジュールのLICENSEを`/licenses/golang-x-net.txt`へ収録 |
| Gecko / Mozillaの第三者コード | Firefox fork `2e1e835a5da5907e5ceef19aade58a7493f1e34d` | 主にMPL-2.0。ただし多数の個別licenseがあり、MPLだけの表示では不足。固定commitの総合noticeと該当sourceを扱う |
| gecko.js wrapper / WASM追加コード | v0.0.1 `eadcb5fa828d7e8b54ae2df13b6b3208f6553944` | 後述のMPL追加commitを確認。固定配布物にはLICENSE一式がないため、tarをそのまま再公開しない |
| Emscripten runtime / WasmFS | 6.0.1と上流のpatch | Emscripten本体はMITまたはUIUC/NCSA。libc等のruntime依存には別noticeがあるため、ツールチェーンの最上位LICENSEだけでは棚卸し完了としない |
| zstddec | 0.2.0 | npm metadataは`MIT AND BSD-3-Clause`。JS wrapperと組み込まれたdecoderの両noticeを保持 |
| Liberation fonts | 上記Firefox commit内のPDF.js standard_fonts | SIL OFL 1.1。著作権表示・OFL本文・Reserved Font Namesを保持 |
relayのruntimeイメージは`FROM scratch`とし、Goバイナリとlicenseだけを含めます。
Alpine/BusyBoxを新たに再配布する構成にはしません。`go list -deps`でrelayにリンクされる外部Go moduleは
`golang.org/x/net v0.35.0`のみと確認しています。ビルド用イメージと配布用runtimeイメージは区別します。
## 固定版とMPL宣言の関係
上流v0.0.1のcommitにはルートLICENSEがなく、翌日の
[`ac40f56c85060bd49dfc36ca60490f97dd270c50`](https://github.com/HeyPuter/firefox-wasm/commit/ac40f56c85060bd49dfc36ca60490f97dd270c50)
でMPL-2.0のLICENSEが追加されています。
[固定版からの比較](https://github.com/HeyPuter/firefox-wasm/compare/eadcb5fa828d7e8b54ae2df13b6b3208f6553944...ac40f56c85060bd49dfc36ca60490f97dd270c50)
の変更はLICENSE、Makefile、README、chrome demoの2ファイルです。
`gecko.js/js`、`gecko.js/src`、`gecko.js/lib`、`gecko.js/emsdk-patches`はこの間に変更されていません。
この事実を追加コードのMPL宣言の根拠として記録しますが、生成された全バイナリの第三者noticeを代替するものではありません。
v0.0.1の[Makefile](https://github.com/HeyPuter/firefox-wasm/blob/eadcb5fa828d7e8b54ae2df13b6b3208f6553944/Makefile)
はFirefox forkのcommit `2e1e835a5da5907e5ceef19aade58a7493f1e34d`とEmscripten 6.0.1を指定しています。
後続Makefileの参照先変更を混ぜず、再現には固定版側を使います。
固定版の[GRE縮小スクリプト](https://github.com/HeyPuter/firefox-wasm/blob/eadcb5fa828d7e8b54ae2df13b6b3208f6553944/gecko.js/stage-gre-min.sh)
は`chrome/toolkit/content/global/license.html`を明示的に除外し、LiberationのTTFをコピーします。
そのため、配布tarの存在やSHA-256一致だけでnoticeの充足を判断しません。
Wispのwire protocolは独立実装しています。固定版エンジンは独自の`gecko.js/lib/wisp-net.js`を使います。
pnpm-lockにある`@mercuryworkshop/wisp-js`はdemo側依存で、lockfileに記載があることだけを根拠に
PagesWiki relayの依存物・ライセンスとは扱いません。
## エンジンを配布する場合に必要な作業
MPLはファイル単位の義務であり、別実装のWiki全体をMPLへ変更するという意味ではありません。
一方、WASMやJavaScriptを閲覧者のブラウザへ送信することも配布として扱う必要があります。
**「イメージへ入れず、外部mountから配信すれば再配布ではない」という整理はしません。**
[Mozilla MPL FAQ Q8・Q10・Q11・Q16・Q17](https://www.mozilla.org/en-US/MPL/2.0/FAQ/)、
[MPL 2.0本文の3章](https://www.mozilla.org/en-US/MPL/2.0/)。
エンジンの本番有効化・再公開前に次を完了します。
1. 実際の配布WASM/JS/埋め込みGREと、対応するsource・patch・依存版を照合する。
2. Mozillaの固定commitの総合notice、MPL本文、Emscripten/runtime、zstddec、fontの各著作権・licenseを収録する。
Mozillaの総合noticeはbuild条件による表示があるため、未処理のHTMLを置くだけで完了としない。
3. 受領者がnoticeと対応sourceの取得先を閲覧できる画面/ファイルを用意する。
ソースは固定commitで指し、PagesWiki側でエンジンを変更した場合は変更も含める。
4. 提供するsourceとnoticeの可用性・保全、配布物とのhash対応を検査する。
5. Mozillaの商標はコードlicenseとは別に扱い、Firefoxの公式配布であるとの表現やロゴの流用をしない。
現時点では2・3・4の配布用bundleが未完成です。今回のrelease artifactにエンジンは載せず、
本番では中継用鍵・資産mount・`viewer.gecko`の有効化を追加しません。
エンジン非同梱の連携コードと、通常のRSSフォルダショートカットはリリース対象です。
## 一次資料
- [Gecko forkのLICENSEと商標注意](https://github.com/MercuryWorkshop/firefox/blob/2e1e835a5da5907e5ceef19aade58a7493f1e34d/LICENSE)
- [同commitのMozilla総合notice](https://github.com/MercuryWorkshop/firefox/blob/2e1e835a5da5907e5ceef19aade58a7493f1e34d/toolkit/content/license.html)
- [Liberationの著作権とOFL](https://github.com/MercuryWorkshop/firefox/blob/2e1e835a5da5907e5ceef19aade58a7493f1e34d/toolkit/components/pdfjs/content/web/standard_fonts/LICENSE_LIBERATION)
- [Emscripten 6.0.1 LICENSE](https://github.com/emscripten-core/emscripten/blob/6.0.1/LICENSE)
- [zstddec 0.2.0 metadata](https://registry.npmjs.org/zstddec/0.2.0)
- [Go LICENSE](https://go.dev/LICENSE)
- [golang.org/x/net v0.35.0 LICENSE](https://go.googlesource.com/net/+/refs/tags/v0.35.0/LICENSE)
+149
View File
@@ -0,0 +1,149 @@
# RSS Gecko中継の試験導入
RSS本文上で原文を読むための、端末側Gecko WASMと自前Wisp中継の実験機能です。
通常の新規タブ・iframe表示は変更しません。DBスキーマ変更はありません。
本番への自動導入・自動有効化は行いません。
## 構成と有効化条件
PagesWikiのログイン済み・activeな利用者だけが、
`/_/api/v1/extension/organizer.feeds/browser/`以下を使います。
同じDomainで`organizer.feeds`と`viewer.gecko`の両方が有効で、
本体に中継URLと共有鍵ファイルが設定されていることが必要です。
`viewer.gecko`はmanifestで既定無効です。個人設定の「RSSの記事リンクの開き方」で
Geckoを選ぶと、RSS画面だけにCOOP/COEPを追加します。選択済みでも管理者が無効化した場合は新規タブへ戻ります。
ブラウザには中継の鍵を渡しません。本体が認証後に30秒の署名付きgrantを発行して内部HTTP要求へ追加します。
WebSocketではOriginも検査し、grantは一回限りです。既存接続の認証・利用者状態・拡張の有効状態は5秒間隔で再検査します。
中継はCookie、WikiのBearer token、FreshRSSの資格情報を受け取りません。
## 固定するエンジン
[HeyPuter/firefox-wasm v0.0.1](https://github.com/HeyPuter/firefox-wasm/releases/tag/v0.0.1)を使います。
対応commitは`eadcb5fa828d7e8b54ae2df13b6b3208f6553944`です。
配布物`gecko.js-v0.0.1.tar.gz`のSHA-256:
```text
bac28056a00ef6e5fb4345117e73d3629e75aca00dac71389fabbce39c018a73
```
展開後の`gecko.js/dist/`をread-onlyで`/gecko`へmountします。起動時に次の両ファイルのhashを確認し、
欠損・改変・異なる版の混在があれば中継は起動しません。
| ファイル | SHA-256 |
| --- | --- |
| gecko.js | e5aeebffd21106eccf23b1219316221add5e95085bc5a1bfb941117cfcd75ef7 |
| gecko.wasm.zst | ebd6c75179152624d9644865e11792ed25cadc714d9e0985f412baf9c637a086 |
資産URLにも`assets/v0.0.1/`を含め、版をまたいだJS/WASMのcache混在を避けます。
初回にはJSと圧縮WASMの合計38,175,359 bytes(約36.4MiB)の転送とWASMの展開・compileが必要です。
本体のDocker buildにFirefoxのソースビルドを加えません。
`Dockerfile.gecko-relay`は自前の中継バイナリだけを含みます。エンジンは同梱・再配布しません。
**エンジンをイメージへ同梱する場合だけでなく、mountした資産を利用者へ配信する前にもnoticeと対応sourceの提供が必要です。**
固定版のMPL宣言、第三者依存物、今回の配布範囲は[再配布条件](gecko-redistribution.md)を参照してください。
v0.13.0の本番配備ではエンジンを配信せず、機能の有効化も行いません。
## 配置
任意の追加ファイル`docker-compose.gecko.yml`を通常のCompose定義に重ねます。
このoverlayは`app`だけへ接続設定を追加し、`app2`は自動変更しません。
必要な環境変数:
| 変数 | 内容 |
| --- | --- |
| PAGESWIKI_GECKO_KEY_PATH | 32文字以上の十分ランダムな共有鍵のファイル絶対Path。appとrelayへ同じread-only mount |
| PAGESWIKI_GECKO_ASSETS_DIR | hash確認した`gecko.js/dist`の絶対Path |
| PAGESWIKI_GECKO_FRONT_SUBNET | 内部接続網。既定`172.24.0.0/27`。既存LANとの重複を避ける |
| PAGESWIKI_GECKO_EGRESS_SUBNET | 中継の外向き網。既定`172.24.0.32/27` |
鍵はイメージ、Git、コマンド引数、ログへ含めません。relayはUID/GID `10001:10001`で動きます。
mount元ファイルをこのUIDまたはGIDで読み取り可能にし、他の利用者から読めない権限にしてください。
本体の`PAGESWIKI_GECKO_RELAY`は内部の固定URL、`PAGESWIKI_GECKO_KEY_FILE`はmount先です。
これらは初回参照時に読み込むため、変更・鍵ローテーション時は本体とrelayを再起動します。
設定確認・試験起動の例:
```sh
docker compose -f docker-compose.yml -f docker-compose.gecko.yml config --quiet
docker compose -f docker-compose.yml -f docker-compose.gecko.yml build gecko-relay
docker compose -f docker-compose.yml -f docker-compose.gecko.yml up -d gecko-relay app
```
本体も今回の実装を含むイメージに更新する必要があります。既存のregistry/deploy用overrideがある場合は
それも指定し、意図せず旧イメージや開発ビルドを本番へ適用しないでください。
### 外向き通信の制限
relayは公開ポートを持たず、通常のWikiネットワークやDBデータをmountしません。
ただしDocker bridgeの分離だけでは外向き通信を制限できません。試験導入前にホストまたは外部firewallで、
relayのegressから管理網・社内網・metadata・Wiki/DBホストへの到達を拒否し、公開TCP 80/443と管理されたDNSだけを許可します。
rootless Dockerとrootful Dockerでは適用点が異なるため、ホスト固有のiptablesをこのoverlayから自動変更しません。
アプリケーション側でもIPv4/IPv6のprivate、loopback、link-local、特殊用途アドレスを拒否します。
名前解決で返された全アドレスを検証してから、そのIPを直接dialします。検証後の再名前解決はしません。
UDP、80/443以外のTCP、単一ラベルのホスト名は拒否します。
これはWisp v1フレームを用いた**制限付きTCP profile**であり、Wispの全機能互換サーバーではありません。
## 上限と監視
| 対象 | 上限 |
| --- | --- |
| 同時セッション | Domain+本人ごとに1、サービス全体4 |
| 同時TCP stream | セッションごと16 |
| 接続試行 | セッションごと256 |
| 合計転送量 | セッションごと送受信合計256MiB |
| 帯域 | セッションごと送受信合計2MiB/s、初期burst 2MiB |
| セッション期限 | 15分 |
| WebSocket受信idle | 90秒 |
| 宛先接続 | 名前解決込み8秒、個別dial 2秒 |
| relayコンテナ | 256MiB、0.5 CPU、64 PIDs、read-only、capabilitiesなし |
| 閲覧プロファイル | volatileな32MiB・2,048 entries、端末のOPFSへ永続化しない |
relayの稼働状態・接続数・送受信bytes・拒否数は、コンテナ内の次の操作でJSON取得できます。
鍵はファイルから読み、標準出力には含めません。counterは再起動でリセットします。
```sh
docker compose -f docker-compose.yml -f docker-compose.gecko.yml exec gecko-relay gecko-relay --status
```
`/health`は内容なしの死活確認、`/status`は署名付き内部要求だけに応答します。
CPU・メモリはコンテナ監視を使います。設定画面の資源グラフへの統合は未完了です。
記事URL、本文、プロファイル内容、資格情報はrelayログに記録しません。
## 無効化と切り戻し
管理画面の拡張一覧で`viewer.gecko`を無効化できます。UIが使えない場合は、対象Domainを選択した
ローカル`data/pageswiki-cli`で次を実行します。
```text
extension viewer.gecko off
```
既存WebSocketは次の認証再検査時に終了します。新たな閲覧も拒否し、RSSの原文リンクは新規タブへ戻ります。
緊急時はrelayコンテナを停止します。DB移行の巻き戻しは不要です。
## 受入と制約
自動テストでは外部アクセスしない合成Wisp応答を使い、実際のGecko WASMで描画、canvasクリックによる遷移、
ビューア内の「戻る」、iframe削除後の接続終了を確認します。実FreshRSS、外部カレンダー、メール本文は使いません。
専用イメージのbuildと、`--network none`・非root・read-only・資源制限付きの一時コンテナで、
固定資産の起動時検査と`--status`も確認済みです。本番の中継・ネットワーク設定は変更していません。
```sh
go test -race ./internal/geckorelay ./internal/server -run 'TestGrant|TestDestinations|TestRelay|TestAssets|TestFeedGeckoAuthorization|TestFeedGeckoWebsocket'
PAGESWIKI_BROWSER_TEST=1 PAGESWIKI_GECKO_TEST_ASSETS=/verified/gecko.js/dist go test ./internal/server -run '^TestFeedGeckoChromium$' -count=1
node internal/server/testdata/feed-gecko-profile.mjs
```
原文クリック時にだけ起動し、閉じるとiframe・worker・接続を破棄します。タブを60秒非表示にした場合も閉じます。
再表示では静的資産のHTTP cacheを使えますが、エンジンは再起動します。
戻る/進むは最大64件のURLを再読み込みする方式です。上流の最小GREにはSessionStoreFunctionsがなく、
標準のhistory.back()が失敗するため、フォーム内容やスクロール位置を復元する履歴ではありません。
画面のcanvasは最大1920×1080、experimental WASM JITとGPUは無効です。
本番の標準機能に昇格する前に、HTTPSサイトと証明書エラーの表示、Safari/iPhone SE、IME、コピー、
find、実サイト互換性、複数タブ・起動失敗時のUX、低メモリ端末の測定、コンテナ外向きfirewallを検証します。
外部ページのiframe拒否とは別の仕組みですが、すべてのサイトでの動作や軽さを保証するものではありません。
+14
View File
@@ -121,6 +121,19 @@ Gitea 1.24 の自動ジョブトークンによるパッケージ公開は前提
アプリイメージにはソースコードが含まれるため、パッケージの公開範囲にも注意する。 アプリイメージにはソースコードが含まれるため、パッケージの公開範囲にも注意する。
## テスト用一時領域
Dockerfileのtest段階では、全Goテストを`-p 1`でパッケージ単位に直列化する。
テスト用`TMPDIR`は最大2GiBのtmpfsとし、SQLiteの一時DBをrunnerのoverlay filesystemから分離する。
コンパイラ用`GOTMPDIR`はディスク側の`/tmp`に残す。tmpfsは当該RUNの間だけ存在し、
runtime段階や本番の保存先・WAL・接続タイムアウトには影響しない。
CI run 54では、直列化後も既存の壁紙・Ordoschnellic・Seheweitのテストが
SQLite接続初期化の5秒タイムアウトで失敗した。ストレージ競合を避ける措置であり、
テスト除外・再試行による成功扱い・本番の期限緩和は行わない。
tmpfsは最大2GiBのメモリを消費し得るため、runnerにはコンパイラ等と合わせた余裕が必要。
実ファイル保存の移行確認は、CIとは別に本番DBの隔離コピーで行う。
## タグ・容量・本番利用 ## タグ・容量・本番利用
3 イメージ共通で `sha-<40桁のコミットID>` を使用し、自動 latest は付けない。 3 イメージ共通で `sha-<40桁のコミットID>` を使用し、自動 latest は付けない。
@@ -161,3 +174,4 @@ prefix の変更は `PAGESWIKI_IMAGE_PREFIX`(既定 git.anya.jp/anya)。
- [Gitea 1.24 Actions の互換性・制限](https://docs.gitea.com/1.24/usage/actions/comparison/) - [Gitea 1.24 Actions の互換性・制限](https://docs.gitea.com/1.24/usage/actions/comparison/)
- [Gitea Container Registry](https://docs.gitea.com/usage/packages/container/) - [Gitea Container Registry](https://docs.gitea.com/usage/packages/container/)
- [Docker コンテナの DNS と hosts](https://docs.docker.com/engine/network/#dns-services) - [Docker コンテナの DNS と hosts](https://docs.docker.com/engine/network/#dns-services)
- [Dockerfileの一時tmpfsマウント](https://docs.docker.com/reference/dockerfile/#run---mounttype-tmpfs)
@@ -0,0 +1,39 @@
# 壁紙連動配色のリリース
PagesWiki v0.10.0は、トップ画面の壁紙からアクセントとグラス面の配色を生成する機能を追加した。2026-11-07に本番反映済み。対象ソースは `0964fdc5b72a1ec24709d87172aaccb985e41c78`。公開APIの非互換変更とDBスキーマ変更はない。保留中のメール保存先 `/tasks` の調査変更は含めない。
## 操作と適用範囲
トップ画面右下の歯車、または `/-/sys/config?panel=home` から、色連動のオフ・アクセントのみ・背景も連動、反映量、明暗判定を選ぶ。既定は背景も連動・35%・現在の明暗を維持。グラス透明度は独立した設定を維持する。管理権限が必要なドメイン共通設定であり、ダイアログでの変更は保存前にプレビューできる。
通常Viewer・Editor・設定画面・ログイン画面には動的配色を適用しない。警告色などの意味を持つ色は変更しない。画像の追加取得はせず、表示済みの画像を64×64に縮小して描画後に解析する。色の抽出結果だけをDomain UUIDと画像SHA256単位でsessionStorageへ保存する。詳しくは[壁紙連動配色の設計](../../design/navigation-home-sidebar.md#実装済みの壁紙連動配色)を参照。
## 検証
- 手元のGo全体テスト、go vet、配色計算とリリースツールのJavaScriptテストが成功。
- 一時DBと合成画像を用いたChromiumテストで、デスクトップ・390px幅の保存、キャンセル、テーマ切替、追加取得なし、キャッシュ再利用・破損時再生成、画像エラー時の復元を確認。
- 非管理者からの設定変更は403。トップ画面以外に配色用スクリプトが混入しないことを検証。
- スマホ幅で検索+ブックマーク画面の横はみ出しを修正。
- [CI run 46](https://git.anya.jp/anya/wikipages/actions/runs/46)の初回job 60は既存の `TestAssetReferencesAreDomainScopedAndIncludeHistoricalHeads` の一時SQLite接続初期化で5秒タイムアウト。検査本体に入る前の失敗で、今回その処理の変更はない。手元で該当テストを10回連続成功後、同一コミットのjob 61を再実行し、全体テストが成功(server 170.870秒、store 84.240秒)。テスト省略やタイムアウト変更は行っていない。
CIでSQLite初期化のタイムアウトが再発する場合は、runnerのI/O負荷とパッケージ間の並列実行を調査する。本番の接続タイムアウトをこの事象だけで緩和しない。
## リリースと本番反映
[CI run 46](https://git.anya.jp/anya/wikipages/actions/runs/46)の再実行でテスト・3イメージのビルド・公開・後処理が成功。そのコミットに注釈付きタグ `v0.10.0` を付けてpushし、[release run 47](https://git.anya.jp/anya/wikipages/actions/runs/47)で再ビルドせずに昇格した。本番で取得したイメージのsource revisionとmanifest digestはリリース結果と一致する。
| イメージ | v0.10.0 manifest digest |
| --- | --- |
| pageswiki-app | `sha256:d43c5827e92ab8eef28290edd5d405f84d2c3480315d552cf5afe535bcf8a02d` |
| pageswiki-adaptor-gateway | `sha256:6882e44ec667fd4fcaecf7a21a25dfb3432a1aa8c6e54c1d0e9fabe3dd448e50` |
| pageswiki-excalidraw-viewer | `sha256:638e02520ce06c2d8267dc8514a4d6544ea8bf70f2d24a368193dad5e6aaa38d` |
23:12:21 JSTに対象4サービスを停止開始、23:13:31に最終バックアップ照合完了、23:13:37に起動完了。停止から起動まで76秒。`.env.release` をv0.10.0へ変更し、registry overlayの `up --no-build --no-deps` でapp・app2・adaptor gateway・Excalidraw viewerを更新した。ローカルlatestタグも同じイメージに揃えた。レジストリlatest、独立CLI、code-server、Kroki等は変更していない。
保全先は `.deployment-backups/release-v0.10.0-20261107/`。親ディレクトリ0700、構成コピー0600。旧 `.env`・`.env.release`・Composeと停止時点の `cutover/data`・`cutover/data2` を保持する。前回の不変バックアップとのみlink-destで共有し、checksum付き同期後の差分ゼロ、DB・WAL・SHM等のバイト一致と稼働元とのinode相違を確認した。旧v0.9.0イメージは `rollback-before-v0.10.0-20261107` タグで保持。旧バックアップや旧イメージは削除していない。完了時の空き容量は約12GiB。
切替後、4コンテナのsource revision一致・running・再起動0回、appとExcalidrawのhealthy、app2内部healthを確認した。起動直後のログでpanic・fatal・database locked・job failed等の検索パターンは0件。公開HTTPSのhealth・ログインは200、匿名のタスク画面・ホーム設定画面は303、preferences API・CardDAVは401。配信されたhome-palette.js、home-palette-core.js、home-appearance.js、home.css、config.cssはリリースソースとSHA256一致。両DBの管理対象12コンポーネントのschema checkは前後ともcurrentで、全データ完全性検査ではない。
認証後の設定操作は一時DBで試験し、本番の設定値やユーザデータを検証目的で変更していない。外部取り込みカレンダーと実メール本文を取得・表示していない。バックアップは内容を解釈しないファイルコピーとバイト照合のみ。
切戻しでは最初に書込みを停止し、切替後のデータも別途保全する。今回の新設定は旧版では利用されず、DBスキーマの差分はないが、稼働中データへバックアップを自動で上書きしない。旧構成は保全先configにある。
@@ -0,0 +1,76 @@
# RSSフォルダと取り込み操作のリリース
## 対象
v0.12.0は`f4869c7c8bd2fd48523eec2e1868d3d0750bbdf7`。
2026-11-08 14:55 JSTに本番検証を完了し、注釈付きGitタグとイメージを公開した。
既存v0.11.0タグは移動していない。
独立pages-cliと未コミットのメール保存先調査は含めない。
- [ビルドCI run 64](https://git.anya.jp/anya/pages/actions/runs/64): success。
- [リリースCI run 65](https://git.anya.jp/anya/pages/actions/runs/65): success。
- [Gitea Release v0.12.0](https://git.anya.jp/anya/pages/releases/tag/v0.12.0)。
本番`.env.release`は同コミットの`sha-`タグに固定している。
`v0.12.0`は再ビルドせず同一manifestを昇格し、3イメージすべてのdigestとrevisionを照合済み。
## 変更
- FreshRSSの分類フォルダ・購読元をツリー表示し、空フォルダ・未分類・開閉状態保存に対応。
- 記事・購読元・分類を背景で原子的に更新し、一部失敗時は前回cacheを保持。
- 既読・star書戻しの操作履歴、結果不明の再確認、JSON記録保存。
- ショートカットヘルプを`?`・`Esc`・閉じるボタンで閉じ、元の記事とstatus表示を保持。
- Wiki・タスク追加を本文上のdialogへ整理し、個人設定のWiki親Pathを初期表示。
- `{YYYY}`・`{MM}`・`{DD}`・`{Subject}`を親Pathで使用可能。日付はドメインのタイムゾーン。
- 件名を安全な単一Path要素へ整形し、保存先プレビューと公開範囲確認を表示。
- `sharing.x`の有効・無効に従うRSS記事のX共有。件名と原文URLを別タブのintentへ渡す。
- 個人設定からRSS原文・本文リンクを別タブまたは大きなiframeプレビューで開く方式を選択。
個人設定は`/-/sys/config?panel=organizer`。Wiki親Pathはメール取り込みと共通。
リンクの開き方は既定で別タブとし、設定変更はRSS画面の再読込後に反映する。
iframeはsandbox付きで、外部サイトの埋め込み禁止は回避しない。表示できない場合は別タブへ開く。
本番利用者の既定値やFreshRSS書戻し許可を自動変更しない。
詳細は[RSS設計](../../design/ja/feeds.md)を参照。
## 移行と保全
今回の本番schema差分はfeeds v5→v6。`feed_sources.remote_folders`の列追加であり、
既存記事・購読設定・送信待ちreceipt・source versionを変更しない。
Wiki親Pathとリンク表示モードは既存preferences JSONを使うため追加DDLはない。
旧版がv6を理解するとは仮定せず、単なる旧イメージへの切戻しを自動実行しない。
保全先は`.deployment-backups/release-rss-folders-20261108/`。
旧構成を`config/`、停止時点のデータを`cutover/data`・`cutover/data2`に保存した。
14:53:29 JSTに停止を開始し、14:54:38 JSTにchecksum付き同期・差分ゼロ・DBの一致を確認した。
事前コピーだけでは整合バックアップと扱っていない。
稼働元とはinodeを共有せず、既存の不変バックアップとのみ変更のないファイルを共有する。
旧イメージは`rollback-before-rss-folders-20261108`タグで保全した。
## 検証
手元のGo全体テスト・go vet、RSS関連のrace検査、1440px/390pxのChromium試験を実施。
分類の空・未分類・移動・同名ID、失敗時cache保持、schema v6のrollback・再実行を検査した。
親Pathの日付境界・日本語件名・階層逸脱防止・不正トークン拒否を検査した。
リンクプレビューは別originのローカルfixtureを実際に読み込み、sandboxと復帰を検査した。
実FreshRSS・Xへの投稿・外部カレンダー・実メール本文を検証のために取得・操作していない。
停止済みの前回バックアップから独立コピーを作り、両DBの移行前integrity_checkはok。
配備イメージをネットワーク遮断・workers無効で実際に起動し、両DBでfeeds v6への移行、
ヘルス、管理対象13コンポーネントのschema check、移行後integrity_checkのokを確認した。
schema check単体ではlegacy領域を網羅しないため、起動・SQLite検査と組み合わせた。
本番切替後は以下を確認した。
- app・app2・adaptor-gateway・excalidraw-viewerが対象revisionで稼働、restart countは0。
- 両アプリの`/health`、schema checkが正常。feeds v6、task-productivity v4。
- HTTPSの`/health`とログインは200、匿名のRSS・タスク・設定画面は303、RSS APIは401。
- 配信中の`feeds.js`・`feeds.css`・`task-productivity.js`のSHA-256がリポジトリと一致。
- 起動後ログのpanic・fatal・DB locked・event job failed・schema errorは各0件。
## 配布イメージのdigest
| イメージ(`git.anya.jp/anya/`、タグ`v0.12.0`) | manifest digest |
| --- | --- |
| pageswiki-app | `sha256:46251f9b869e47d456def4d285c3f076e0d375f2a11d69121885fca08997e6a7` |
| pageswiki-adaptor-gateway | `sha256:ee98889018f8389a655126c6a5d1046640be22cae169d97c4b27a7efa08f86c6` |
| pageswiki-excalidraw-viewer | `sha256:e7a577a8f178f625f6b13da93b046afda57c5c4c67e5e3b86cab0daf5c5f14ee` |
@@ -0,0 +1,39 @@
# PagesWiki v0.13.0
RSSフォルダのキーボード移動と、既定無効のGecko原文ビューア基盤を追加するminor releaseです。
エンジン配布を含まない範囲でリリースします。通常のRSS表示・新規タブ・sandbox iframeは維持します。
## 変更
- Shift+j/kでFreshRSSの前後の分類フォルダを選択。検索条件、未読/star絞り込みを維持。
- 認証付きの独立Wisp TCP relayと、Gecko用の本文上popup、個人設定を実装。
- relayは公開TCP 80/443限定、DNS解決後の全IP検査、固定IPへの接続、接続数・帯域・通信量・期限の制限。
- ログアウト・利用者状態・拡張無効化を既存WebSocketにも反映。
- upstream v0.0.1をhash固定。閲覧プロファイルはメモリのみ、終了時はiframeを破棄。
- 戻る/進むは最大64件のビューア内URL履歴を再読み込み。通常ブラウザのフォーム状態復元ではない。
- 拡張manifestの既定無効をサーバーとローカルCLIへ反映。
- 専用relayはscratch runtimeとし、実ビルドのGo・x/netのBSD licenseを収録。
## 再配布と本番の範囲
[再配布条件](../gecko-redistribution.md)にGecko、WASM用追加コード、Emscripten、zstddec、フォントを整理しました。
GeckoのJS/WASM/フォントをGit、通常3イメージ、release assetへ同梱しません。
relay Dockerfileと試験用Compose overlayは提供しますが、CIの配布イメージは従来の3種類です。
固定版生成物にはMozillaの総合noticeが含まれないため、エンジン配布用notice/source bundleは残件です。
外部mountからのブラウザ配信も再配布として扱い、今回の本番では鍵・asset mount・relay起動・
`viewer.gecko`の有効化を追加しません。
通常のRSSフォルダショートカットは利用できます。
## 互換性と検証
DBスキーマ変更はありません。RSSリンクモード`gecko`は既存preferences JSONの追加値です。
未構成・無効時は新規タブ表示に戻り、既存利用者の設定を自動変更しません。
旧版でpreferencesを保存する際、新しい値が残っていれば`tab`または`embedded`へ戻す必要があります。
Go全体テスト、関連race検査、CLI、配布ツール、Chromiumでの1440/390px RSS回帰と実Geckoによる
合成Wisp記事の描画・クリック遷移・戻る・終了を検証対象とします。実FreshRSS・外部カレンダー・メール本文は検証に使いません。
Safari、IME/copy/find、実HTTPSの互換性と証明書エラー、低メモリ・長時間負荷、監視UI統合、
本番の外向きfirewallは実験機能の昇格前の残件です。
構成・上限・切り戻しは[Gecko中継の試験導入](../gecko-relay.md)、機能の進捗は[RSS設計](../../design/ja/feeds.md)を参照してください。
+100
View File
@@ -0,0 +1,100 @@
# RSSリリースとFreshRSS書き戻しの本番反映
## リリース境界
注釈付きタグ`v0.11.0`は`16e572397b2b1abf096bd6bca26e2af832768b8d`を指す。
RSSの内蔵reader、OPML、FreshRSSの読み取り・購読カタログまでの区切りであり、タグは移動しない。
その後の既読・star書き戻しを含む本番反映は`61ecd1ffbdf46897d50c2f7eb7a2e1415f1bb456`。
`131a7ca`で機能を実装し、`61ecd1f`でCIのテスト一時領域だけを改善した。
2026-11-08 JSTに本番反映済み。独立pages-cliや未コミットのメール保存先調査は対象外。
## 機能と安全条件
FreshRSSを正本とし、source単位で明示的に許可した場合だけ既読・starの変更を送る。
許可は既定OFF。UI操作は永続キューへ入り、外部での確認後に表示へ反映する。
送信結果が不明になった場合は自動再送せず、利用者の再確認操作で外部状態を読む。
操作IDによる重複防止、source更新との競合、権限失効、source削除、共有leaseを扱う。
詳細と残件は[RSS設計](../../design/ja/feeds.md)を参照。
今回の本番移行には、前段のタスク作業記録訂正監査・日次ダイジェストの送信試行記録も含む。
管理対象スキーマの差分は、feedsの未導入状態からv5、task-productivityのv2からv4。
ファイル正本の一括書換えは行わない。後続版のスキーマを旧版へ自動downgradeしない。
## 検証記録
- 手元で`go test -p 1 ./...`、`go vet ./...`が成功。
- feeds関連のrace検査、1440px・390pxのChromium試験、JavaScript構文検査が成功。
- 合成FreshRSSでread/starの追加・解除、応答喪失、再確認、競合、設定変更・失効を検査。
- v5の移行失敗時rollback、再実行、既定OFF、source削除後の操作記録保持を検査。
- [CI run 53](https://git.anya.jp/anya/pages/actions/runs/53)はv0.11.0候補の全テスト・3イメージの公開に成功。
[release run 55](https://git.anya.jp/anya/pages/actions/runs/55)で既存イメージを再ビルドせずv0.11.0へ昇格した。
- CI run 54は既存3テストのSQLite接続初期化が5秒でtimeout。追加したFreshRSS試験の失敗ではない。
該当3テストを手元で各3回成功。テスト専用tmpfsを導入し、
[run 56](https://git.anya.jp/anya/pages/actions/runs/56)で全テスト・3イメージ公開が成功。
server 97.912秒、store 27.685秒。テスト省略や本番のdeadline変更は行っていない。
詳しくは[CI運用](../gitea-images.md#テスト用一時領域)を参照。
- 前回停止時バックアップを独立コピーし、ネットワーク遮断・workers無効で主/外部DBを起動。
v0.11.0候補でfeeds v4・task-productivity v4への移行、health、schema check、integrity_checkが成功。
外部インスタンスのリハーサルでは再登録を実行せず、既存DBを通常起動した。
最終イメージではコピーを旧版の状態に戻して起動し、両DBともfeeds v5・task-productivity v4まで
移行した後のhealth・schema check・SQLite integrity_checkが成功した。
外部取り込みカレンダーと実メール本文は取得・表示していない。実FreshRSSへの操作も行っていない。
DB・正本の保全はファイルコピーとバイト照合だけを用いる。実サービスへの書き戻し受入確認は残る。
## 保全・切替手順
保全先は`.deployment-backups/release-rss-20261108/`。親0700、構成コピー0600。
旧構成と停止時点の`cutover/data`・`cutover/data2`を保持する。
変更のないファイルは前回の不変バックアップとのみhardlinkを共有し、稼働元とは共有しない。
ディスク空きがDBより小さいため、今回新しく独立コピーしたpages.db(link数1を検証)だけは
停止後にchecksum付きin-place同期する。他のファイルにin-place更新を使わない。
最終checksum差分ゼロとDBのバイト一致を確認してから新しいイメージを起動する。
旧バックアップ・旧イメージは削除しない。
対象はapp・app2・adaptor-gateway・excalidraw-viewerの4サービス。
CI成功後の同一SHAの3イメージをpullし、registry overlayで`up --no-build --no-deps`する。
code-server・Kroki・独立CLI・ネットワーク構成は変更しない。
切戻しが必要な場合は書込みを停止し、切替後データも保全した上で互換性を判断する。
稼働中DBへ旧バックアップを自動復元しない。
## 本番反映結果
2026-11-08 06:20:55 JSTに停止開始、06:22:12にバックアップ最終照合・sync完了、
06:22:37に4サービスの起動確認完了。停止開始から起動確認まで102秒。
`.env.release`は`sha-61ecd1ffbdf46897d50c2f7eb7a2e1415f1bb456`を指定する。
本番のバージョン指定はv0.11.0タグそのものではなく、追加実装を含むこの固定SHAである。
| イメージ | manifest digest |
| --- | --- |
| pageswiki-app | `sha256:e452a7dbadf13c89909c5ee6a685fa39342926675dec2253747534ef6ffabfb0` |
| pageswiki-adaptor-gateway | `sha256:77efae4ef3651e12ebac6a15474448dd93bfa89a7072b8db8e296322a68a7956` |
| pageswiki-excalidraw-viewer | `sha256:45b6399438fa1b40d54c0b6dfbcc15ca1464abb03b454e8b281d94d58c80b250` |
4コンテナのsource revision一致・running・再起動0回を確認。主/外部インスタンスのhealth正常、
両DBの管理対象13コンポーネントのschema checkはcurrent。
このschema checkだけで全データや全legacy schemaの完全性を保証するものではない。
公開HTTPSのhealth・ログインは200、匿名RSS・タスク・拡張設定画面は303、RSSのsources・action APIは401。
配信feeds.jsとfeeds.cssのSHA256は配備コミットと一致する。
起動後ログのpanic・fatal・database is locked・event job failed・schema失敗の検索パターンは0件。
最終バックアップの両ルートともchecksum付きdry-runの差分はゼロ。
旧v0.10.0イメージは`rollback-before-rss-20261108`タグで保持し、ローカルlatestは新イメージに揃えた。
今回作成した隔離リハーサル用の一時コピーとコンテナは片付けたが、旧バックアップは削除していない。
完了時のディスク空きは約4GiB。その後の容量整理は次節のとおり。
FreshRSSの書き戻し許可は自動有効化していない。本番ユーザーの設定・記事状態を試験のために変更せず、
外部取り込みカレンダー・実メール・実FreshRSSへ検証用アクセスを行っていない。
## 容量整理
2026-11-08、ユーザーの削除指示を受け、旧リリースのバックアップ8世代を削除した。
対象はrelease-v0.4.0、v0.5.0、v0.6.0、v0.7.0、v0.7.1、v0.8.0、v0.8.1、v0.9.0の各ディレクトリ。
現在の切替直前を保全した`release-rss-20261108`と、その前の`release-v0.10.0-20261107`を残した。
hardlink共有ファイルも、残したバックアップの参照は維持される。開発時の小さなソース保全やrsyncログは変更していない。
9月のrsyncログを11月のバックアップの外部転送証明とは扱わず、今回の保持世代整理として削除した。
DockerはPagesWikiの指定repositoryだけを対象に、全コンテナの使用中image ID、v0.10.0の切戻し用、
v0.11.0の手元イメージを保護した。旧43 image IDに属する102タグと関連参照をforceなしで削除。
daemon全体のprune、レジストリの削除、他サービスのイメージ/volume削除は行っていない。
本番停止は不要で、空き容量は約4GiBから約61GiBへ増えた。
+3 -1
View File
@@ -21,8 +21,10 @@ type Manifest struct {
func Catalog() []Manifest { func Catalog() []Manifest {
return []Manifest{ return []Manifest{
{ID: "viewer.gecko", APIVersion: 1, Version: "0.1.0", Kind: "viewer", Scope: "domain", NameKey: "feeds.gecko", DescriptionKey: "feeds.gecko_help", Capabilities: []string{"feeds.own.read"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: false, ResourceClass: "client-compute+relay-network"},
{ID: "organizer.feeds", APIVersion: 1, Version: "0.1.0", Kind: "feeds", Scope: "domain", NameKey: "feeds.title", DescriptionKey: "feeds.help", Capabilities: []string{"feeds.own.read", "feeds.own.write"}, Events: []string{"feed.source.sync"}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "background+network+index"},
{ID: "organizer.contacts", APIVersion: 1, Version: "0.2.0", Kind: "contacts", Scope: "domain", NameKey: "contacts.title", DescriptionKey: "contacts.help", Capabilities: []string{"contacts.own.read", "contacts.own.write"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+index"}, {ID: "organizer.contacts", APIVersion: 1, Version: "0.2.0", Kind: "contacts", Scope: "domain", NameKey: "contacts.title", DescriptionKey: "contacts.help", Capabilities: []string{"contacts.own.read", "contacts.own.write"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
{ID: "organizer.tasks", APIVersion: 1, Version: "0.12.0", Kind: "tasks", Scope: "domain", NameKey: "tasks.title", DescriptionKey: "tasks.help", Capabilities: []string{"tasks.own.read", "tasks.own.write"}, Events: []string{"organizer.task.changed", "organizer.project.changed", "task.digest.deliver"}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+background+index"}, {ID: "organizer.tasks", APIVersion: 1, Version: "0.14.0", Kind: "tasks", Scope: "domain", NameKey: "tasks.title", DescriptionKey: "tasks.help", Capabilities: []string{"tasks.own.read", "tasks.own.write"}, Events: []string{"organizer.task.changed", "organizer.project.changed", "task.digest.deliver"}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+background+index"},
{ID: "organizer.calendar", APIVersion: 1, Version: "0.2.0", Kind: "calendar", Scope: "domain", NameKey: "calendar.title", DescriptionKey: "calendar.help", Capabilities: []string{"calendar.read", "calendar.write"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+index"}, {ID: "organizer.calendar", APIVersion: 1, Version: "0.2.0", Kind: "calendar", Scope: "domain", NameKey: "calendar.title", DescriptionKey: "calendar.help", Capabilities: []string{"calendar.read", "calendar.write"}, Events: []string{}, SettingsPanel: "extensions", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
{ID: "content.diary", APIVersion: 1, Version: "0.1.0", Kind: "diary", Scope: "domain", NameKey: "periodical.diary", DescriptionKey: "periodical.diary_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "diary", Builtin: true, Enabled: true, ResourceClass: "foreground+index"}, {ID: "content.diary", APIVersion: 1, Version: "0.1.0", Kind: "diary", Scope: "domain", NameKey: "periodical.diary", DescriptionKey: "periodical.diary_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "diary", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
{ID: "content.blog", APIVersion: 1, Version: "0.1.0", Kind: "blog", Scope: "domain", NameKey: "periodical.blog", DescriptionKey: "periodical.blog_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "blog", Builtin: true, Enabled: true, ResourceClass: "foreground+index"}, {ID: "content.blog", APIVersion: 1, Version: "0.1.0", Kind: "blog", Scope: "domain", NameKey: "periodical.blog", DescriptionKey: "periodical.blog_help", Capabilities: []string{"path.read", "path.write", "domain.extensions.manage"}, Events: []string{"periodical.registered", "periodical.removed"}, SettingsPanel: "blog", Builtin: true, Enabled: true, ResourceClass: "foreground+index"},
+22
View File
@@ -0,0 +1,22 @@
PagesWiki Gecko relay
This image contains the PagesWiki TCP relay, not Firefox/Gecko, gecko.js,
gecko.wasm.zst, Mozilla runtime assets or browser fonts.
Third-party code linked into the relay binary:
- Go standard library and runtime, Copyright The Go Authors.
BSD-3-Clause; full license: /licenses/Go.txt
Source and release archives: https://go.dev/dl/
- golang.org/x/net v0.35.0 (WebSocket transport), Copyright The Go Authors.
BSD-3-Clause; full license: /licenses/golang-x-net.txt
Source: https://go.googlesource.com/net/+/refs/tags/v0.35.0/
The licenses are copied from the exact toolchain/module used at build time.
The scratch runtime image does not contain Alpine, BusyBox or apk.
The PagesWiki integration source and engine redistribution assessment are in:
https://git.anya.jp/anya/pages
docs/operations/gecko-redistribution.md
Mounting and serving an independently obtained browser engine requires a
separate assessment and delivery of its applicable notices and source offer.
This relay notice is not a license grant for that engine.
+137
View File
@@ -0,0 +1,137 @@
// Package geckorelay provides a bounded TCP-only Wisp transport for Gecko.
// It is not a public general-purpose proxy: the PagesWiki frontend authenticates
// the user and signs each backend request, and every destination is checked here.
package geckorelay
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"net"
"net/netip"
"strconv"
"strings"
"time"
)
const GrantHeader = "X-PagesWiki-Gecko-Grant"
type Grant struct {
Subject string `json:"subject"`
Nonce string `json:"nonce"`
Expires int64 `json:"expires"`
}
func Sign(key string, grant Grant) string {
data, _ := json.Marshal(grant)
body := base64.RawURLEncoding.EncodeToString(data)
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(body))
return body + "." + base64.RawURLEncoding.EncodeToString(h.Sum(nil))
}
func Verify(key, token string, now time.Time) (Grant, error) {
var grant Grant
parts := strings.Split(token, ".")
if len(key) < 32 || len(token) > 2048 || len(parts) != 2 {
return grant, errors.New("invalid grant")
}
sig, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return grant, errors.New("invalid grant")
}
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(parts[0]))
if !hmac.Equal(sig, h.Sum(nil)) {
return grant, errors.New("invalid grant")
}
data, err := base64.RawURLEncoding.DecodeString(parts[0])
if err != nil {
return grant, errors.New("invalid grant")
}
if json.Unmarshal(data, &grant) != nil || grant.Subject == "" || len(grant.Subject) > 160 || len(grant.Nonce) < 16 || len(grant.Nonce) > 80 || grant.Expires <= now.Unix() || grant.Expires > now.Add(time.Minute).Unix() {
return Grant{}, errors.New("invalid grant")
}
return grant, nil
}
var denied = func() []netip.Prefix {
var result []netip.Prefix
for _, p := range []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "192.88.99.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001::/23", "2001:db8::/32", "2002::/16", "3fff::/20"} {
result = append(result, netip.MustParsePrefix(p))
}
return result
}()
func PublicIP(ip netip.Addr) bool {
ip = ip.Unmap()
if !ip.IsValid() || ip.Zone() != "" || !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
return false
}
// Only globally allocated IPv6 unicast; excludes NAT64, mapped/private and
// future address classes rather than treating unknown ranges as public.
if ip.Is6() && !netip.MustParsePrefix("2000::/3").Contains(ip) {
return false
}
for _, p := range denied {
if p.Contains(ip) {
return false
}
}
return true
}
func ValidDestination(host string, port uint16) bool {
if port != 80 && port != 443 {
return false
}
if ip, err := netip.ParseAddr(host); err == nil {
return PublicIP(ip)
}
host = strings.TrimSuffix(strings.ToLower(host), ".")
if len(host) == 0 || len(host) > 253 || host == "localhost" || strings.HasSuffix(host, ".localhost") || !strings.Contains(host, ".") {
return false
}
for _, label := range strings.Split(host, ".") {
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
return false
}
for _, c := range label {
if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-') {
return false
}
}
}
return true
}
type lookupFunc func(context.Context, string, string) ([]netip.Addr, error)
type dialFunc func(context.Context, string, string) (net.Conn, error)
func dialPublic(ctx context.Context, host string, port uint16, lookup lookupFunc, dial dialFunc) (net.Conn, error) {
if !ValidDestination(host, port) {
return nil, errors.New("blocked destination")
}
ips, err := lookup(ctx, "ip", host)
if err != nil || len(ips) == 0 || len(ips) > 16 {
return nil, errors.New("destination lookup failed")
}
for _, ip := range ips {
if !PublicIP(ip) {
return nil, errors.New("blocked destination")
}
}
for _, ip := range ips {
conn, e := dial(ctx, "tcp", net.JoinHostPort(ip.Unmap().String(), strconv.Itoa(int(port))))
if e == nil {
return conn, nil
}
if ctx.Err() != nil {
break
}
}
return nil, errors.New("destination unavailable")
}
+33
View File
@@ -0,0 +1,33 @@
package geckorelay
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
)
const AssetsPrefix = "/assets/v0.0.1/"
// Hashes of the two unmodified files in upstream v0.0.1. The release archive
// itself is pinned in docs/operations/gecko-relay.md. Refuse mixed engine/glue.
func VerifyAssets(dir string) error {
for name, want := range map[string]string{
"gecko.js": "e5aeebffd21106eccf23b1219316221add5e95085bc5a1bfb941117cfcd75ef7",
"gecko.wasm.zst": "ebd6c75179152624d9644865e11792ed25cadc714d9e0985f412baf9c637a086",
} {
f, err := os.Open(filepath.Join(dir, name))
if err != nil {
return fmt.Errorf("required Gecko asset unavailable: %s", name)
}
h := sha256.New()
_, err = io.Copy(h, io.LimitReader(f, 64<<20))
f.Close()
if err != nil || hex.EncodeToString(h.Sum(nil)) != want {
return fmt.Errorf("Gecko asset checksum mismatch: %s", name)
}
}
return nil
}
+352
View File
@@ -0,0 +1,352 @@
package geckorelay
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"io"
"net"
"net/http"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/net/websocket"
)
const (
window = 8
maxFrame = 256 << 10
maxStreams = 16
maxBytes = 256 << 20
bytesPerSecond = 2 << 20
)
type Relay struct {
Key, Assets string
mu sync.Mutex
active map[string]bool
used map[string]int64
rx, tx, rejected atomic.Int64
// Overridden only by package tests; deployment always resolves and pins public IPs.
dial func(context.Context, string, uint16) (net.Conn, error)
}
func New(key, assets string) (*Relay, error) {
if len(key) < 32 {
return nil, errors.New("relay key must contain at least 32 bytes")
}
r := &Relay{Key: key, Assets: assets, active: map[string]bool{}, used: map[string]int64{}}
r.dial = func(ctx context.Context, host string, port uint16) (net.Conn, error) {
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
defer cancel()
return dialPublic(ctx, host, port, net.DefaultResolver.LookupNetIP, (&net.Dialer{Timeout: 2 * time.Second}).DialContext)
}
return r, nil
}
func (r *Relay) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Cache-Control", "no-store")
if req.URL.Path == "/health" && req.Method == "GET" {
io.WriteString(w, "ok")
return
}
grant, err := Verify(r.Key, req.Header.Get(GrantHeader), time.Now())
if err != nil {
http.Error(w, "forbidden", 403)
return
}
if req.Method != "GET" || req.URL.RawQuery != "" {
http.Error(w, "invalid request", 400)
return
}
if req.URL.Path == "/status" {
r.mu.Lock()
active := len(r.active)
r.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{"active_sessions": active, "received_bytes": r.rx.Load(), "sent_bytes": r.tx.Load(), "rejected": r.rejected.Load(), "profile": "tcp-80-443", "max_sessions": 4, "max_streams": maxStreams, "bytes_per_second": bytesPerSecond, "max_session_bytes": maxBytes})
return
}
if req.URL.Path == AssetsPrefix+"gecko.js" || req.URL.Path == AssetsPrefix+"gecko.wasm.zst" {
w.Header().Set("Cross-Origin-Resource-Policy", "same-origin")
w.Header().Set("Cache-Control", "private, max-age=86400, immutable")
if strings.HasSuffix(req.URL.Path, ".js") {
w.Header().Set("Content-Type", "text/javascript")
} else {
w.Header().Set("Content-Type", "application/octet-stream")
}
http.ServeFile(w, req, filepath.Join(r.Assets, filepath.Base(req.URL.Path)))
return
}
if req.URL.Path != "/wisp/" {
http.NotFound(w, req)
return
}
r.mu.Lock()
now := time.Now().Unix()
for nonce, expires := range r.used {
if expires <= now {
delete(r.used, nonce)
}
}
if r.active[grant.Subject] || len(r.active) >= 4 || r.used[grant.Nonce] > now || len(r.used) >= 256 {
r.mu.Unlock()
r.rejected.Add(1)
http.Error(w, "session limit", 429)
return
}
r.active[grant.Subject] = true
r.used[grant.Nonce] = grant.Expires
r.mu.Unlock()
defer func() { r.mu.Lock(); delete(r.active, grant.Subject); r.mu.Unlock() }()
wsServer := websocket.Server{Handshake: func(c *websocket.Config, req *http.Request) error {
// Only the authenticated PagesWiki proxy can sign a grant. It validates
// the browser Origin before forwarding, not the untrusted client header.
if req.Header.Get("Origin") == "" {
return errors.New("origin required")
}
return nil
}, Handler: func(ws *websocket.Conn) { r.serve(ws) }}
wsServer.ServeHTTP(w, req)
}
type stream struct {
data chan []byte
done chan struct{}
once sync.Once
mu sync.Mutex
conn net.Conn
}
func (s *stream) close() {
s.once.Do(func() {
close(s.done)
s.mu.Lock()
if s.conn != nil {
s.conn.Close()
}
s.mu.Unlock()
})
}
func packet(kind byte, id uint32, data []byte) []byte {
b := make([]byte, 5+len(data))
b[0] = kind
binary.LittleEndian.PutUint32(b[1:], id)
copy(b[5:], data)
return b
}
func credit() []byte { b := make([]byte, 4); binary.LittleEndian.PutUint32(b, window); return b }
func (r *Relay) serve(ws *websocket.Conn) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
defer cancel()
defer ws.Close()
ws.MaxPayloadBytes = maxFrame + 5
var mu, sendMu, budgetMu sync.Mutex
streams := map[uint32]*stream{}
var workers sync.WaitGroup
var spent int64
tokens := float64(bytesPerSecond)
last := time.Now()
account := func(n int) bool {
budgetMu.Lock()
defer budgetMu.Unlock()
spent += int64(n)
if spent > maxBytes {
return false
}
now := time.Now()
tokens += now.Sub(last).Seconds() * bytesPerSecond
if tokens > bytesPerSecond {
tokens = bytesPerSecond
}
last = now
tokens -= float64(n)
if tokens < 0 {
timer := time.NewTimer(time.Duration(-tokens / bytesPerSecond * float64(time.Second)))
defer timer.Stop()
select {
case <-timer.C:
tokens = 0
last = time.Now()
case <-ctx.Done():
return false
}
}
return true
}
send := func(kind byte, id uint32, data []byte) error {
sendMu.Lock()
defer sendMu.Unlock()
ws.SetWriteDeadline(time.Now().Add(10 * time.Second))
return websocket.Message.Send(ws, packet(kind, id, data))
}
defer func() {
cancel()
mu.Lock()
for _, s := range streams {
s.close()
}
mu.Unlock()
workers.Wait()
}()
go func() { <-ctx.Done(); ws.Close() }()
if send(3, 0, credit()) != nil {
return
}
connections := 0
for {
ws.SetReadDeadline(time.Now().Add(90 * time.Second))
var b []byte
if websocket.Message.Receive(ws, &b) != nil {
return
}
if len(b) < 5 || !account(len(b)) {
return
}
r.rx.Add(int64(len(b)))
kind, id := b[0], binary.LittleEndian.Uint32(b[1:])
if id == 0 {
return
}
mu.Lock()
s := streams[id]
mu.Unlock()
switch kind {
case 1:
connections++
if s != nil || len(b) < 9 || len(b) > 261 || connections > 256 {
return
}
port := binary.LittleEndian.Uint16(b[6:8])
host := string(b[8:])
if b[5] != 1 || !ValidDestination(host, port) {
r.rejected.Add(1)
if send(4, id, []byte{0x48}) != nil {
return
}
continue
}
mu.Lock()
full := len(streams) >= maxStreams
mu.Unlock()
if full {
r.rejected.Add(1)
if send(4, id, []byte{0x49}) != nil {
return
}
continue
}
s = &stream{data: make(chan []byte, window), done: make(chan struct{})}
mu.Lock()
streams[id] = s
mu.Unlock()
workers.Add(1)
go func(id uint32, s *stream, host string, port uint16) {
defer workers.Done()
defer func() { s.close(); mu.Lock(); delete(streams, id); mu.Unlock() }()
dialCtx, stop := context.WithCancel(ctx)
defer stop()
go func() {
select {
case <-s.done:
stop()
case <-dialCtx.Done():
}
}()
conn, e := r.dial(dialCtx, host, port)
if e != nil {
r.rejected.Add(1)
send(4, id, []byte{0x48})
return
}
s.mu.Lock()
s.conn = conn
s.mu.Unlock()
select {
case <-s.done:
conn.Close()
return
default:
}
readerDone := make(chan struct{})
defer func() { conn.Close(); <-readerDone }()
go func() {
defer close(readerDone)
defer s.close()
buffer := make([]byte, 64<<10)
for {
conn.SetReadDeadline(time.Now().Add(60 * time.Second))
n, e := conn.Read(buffer)
if n > 0 {
if !account(n) {
cancel()
return
}
r.tx.Add(int64(n))
if send(2, id, buffer[:n]) != nil {
cancel()
return
}
}
if e != nil {
send(4, id, []byte{2})
return
}
}
}()
count := 0
for {
select {
case <-ctx.Done():
return
case <-s.done:
return
case data := <-s.data:
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if _, e = io.Copy(conn, bytes.NewReader(data)); e != nil {
send(4, id, []byte{3})
return
}
count++
if count == window {
count = 0
if send(3, id, credit()) != nil {
cancel()
return
}
}
}
}
}(id, s, host, port)
case 2:
if s == nil {
continue
}
select {
case <-s.done:
case s.data <- b[5:]:
default:
r.rejected.Add(1)
s.close()
if send(4, id, []byte{0x49}) != nil {
return
}
}
case 4:
if len(b) != 6 {
return
}
if s != nil {
s.close()
}
default:
return
}
}
}
+284
View File
@@ -0,0 +1,284 @@
package geckorelay
import (
"context"
"encoding/binary"
"io"
"net"
"net/http"
"net/http/httptest"
"net/netip"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"golang.org/x/net/websocket"
)
const testKey = "synthetic-relay-key-at-least-32-bytes-long"
func TestAssetsPin(t *testing.T) {
dir := t.TempDir()
if VerifyAssets(dir) == nil {
t.Fatal("missing assets accepted")
}
for _, name := range []string{"gecko.js", "gecko.wasm.zst"} {
if err := os.WriteFile(filepath.Join(dir, name), []byte("untrusted"), 0600); err != nil {
t.Fatal(err)
}
}
if VerifyAssets(dir) == nil {
t.Fatal("unverified assets accepted")
}
if assets := os.Getenv("PAGESWIKI_GECKO_TEST_ASSETS"); assets != "" {
if err := VerifyAssets(assets); err != nil {
t.Fatal(err)
}
}
}
func TestGrant(t *testing.T) {
now := time.Now()
g := Grant{"domain:actor", "0123456789abcdef", now.Add(30 * time.Second).Unix()}
token := Sign(testKey, g)
if got, e := Verify(testKey, token, now); e != nil || got != g {
t.Fatal(got, e)
}
for _, bad := range []string{token + "x", "", strings.Repeat("x", 2049), Sign(testKey, Grant{g.Subject, g.Nonce, now.Add(2 * time.Minute).Unix()}), Sign(testKey, Grant{g.Subject, g.Nonce, now.Unix()})} {
if _, e := Verify(testKey, bad, now); e == nil {
t.Fatal("accepted invalid grant")
}
}
if _, e := Verify("other-key-at-least-32-bytes-long!!", token, now); e == nil {
t.Fatal("wrong key")
}
}
func TestDestinations(t *testing.T) {
for _, host := range []string{"127.0.0.1", "localhost", "host.localhost", "10.0.0.1", "169.254.169.254", "100.64.0.1", "192.0.2.2", "198.19.0.1", "::1", "::ffff:127.0.0.1", "64:ff9b::808:808", "2002:808:808::1", "2001:db8::1", "3fff::1", "fd00::1", "fe80::1%eth0", "bad/host", "example.org\x00.evil", "singlelabel"} {
if ValidDestination(host, 443) {
t.Fatalf("allowed %q", host)
}
}
for _, host := range []string{"example.org", "EXAMPLE.org.", "8.8.8.8", "2606:4700:4700::1111"} {
if !ValidDestination(host, 443) {
t.Fatal("blocked", host)
}
}
if ValidDestination("example.org", 22) {
t.Fatal("port allowed")
}
called := false
dial := func(_ context.Context, _, addr string) (net.Conn, error) {
called = true
if addr != "8.8.8.8:443" {
t.Fatal("unvalidated DNS re-resolution", addr)
}
a, b := net.Pipe()
b.Close()
return a, nil
}
lookup := func(_ context.Context, _, _ string) ([]netip.Addr, error) {
return []netip.Addr{netip.MustParseAddr("8.8.8.8"), netip.MustParseAddr("127.0.0.1")}, nil
}
if _, err := dialPublic(context.Background(), "example.org", 443, lookup, dial); err == nil || called {
t.Fatal("mixed DNS allowed")
}
lookup = func(_ context.Context, _, _ string) ([]netip.Addr, error) {
return []netip.Addr{netip.MustParseAddr("8.8.8.8")}, nil
}
c, err := dialPublic(context.Background(), "example.org", 443, lookup, dial)
if err != nil || !called {
t.Fatal(err)
}
c.Close()
}
func TestRelayFramesAndLimits(t *testing.T) {
r, _ := New(testKey, t.TempDir())
r.dial = func(ctx context.Context, host string, port uint16) (net.Conn, error) {
a, b := net.Pipe()
go func() { defer b.Close(); io.Copy(b, b) }()
return a, nil
}
s := httptest.NewServer(r)
defer s.Close()
grant := Sign(testKey, Grant{"domain:actor", "nonce-first-connection", time.Now().Add(30 * time.Second).Unix()})
dial := func(token string) (*websocket.Conn, error) {
cfg, _ := websocket.NewConfig("ws"+strings.TrimPrefix(s.URL, "http")+"/wisp/", "https://pages.example")
cfg.Header.Set(GrantHeader, token)
return websocket.DialConfig(cfg)
}
if c, e := dial(""); e == nil {
c.Close()
t.Fatal("anonymous websocket")
}
c, err := dial(grant)
if err != nil {
t.Fatal(err)
}
defer c.Close()
c.SetDeadline(time.Now().Add(5 * time.Second))
receive := func() []byte {
var p []byte
if e := websocket.Message.Receive(c, &p); e != nil {
t.Fatal(e)
}
return p
}
p := receive()
if p[0] != 3 || binary.LittleEndian.Uint32(p[1:]) != 0 || binary.LittleEndian.Uint32(p[5:]) != window {
t.Fatal(p)
}
if second, e := dial(grant); e == nil {
second.Close()
t.Fatal("duplicate actor/replayed grant")
}
connect := func(id uint32, host string, port uint16, kind byte) {
body := []byte{kind, byte(port), byte(port >> 8)}
body = append(body, host...)
if e := websocket.Message.Send(c, packet(1, id, body)); e != nil {
t.Fatal(e)
}
}
connect(1, "127.0.0.1", 80, 1)
if p = receive(); p[0] != 4 || p[5] != 0x48 {
t.Fatal(p)
}
connect(2, "example.org", 443, 2)
if p = receive(); p[0] != 4 {
t.Fatal("UDP accepted")
}
connect(3, "example.org", 443, 1)
for n := 0; n < window; n++ {
if e := websocket.Message.Send(c, packet(2, 3, []byte("hello"))); e != nil {
t.Fatal(e)
}
}
got := 0
creditSeen := false
for got < window*5 || !creditSeen {
p = receive()
switch p[0] {
case 2:
got += len(p) - 5
case 3:
creditSeen = true
default:
t.Fatal(p)
}
}
// Wisp credits count DATA frames, including empty payloads.
for n := 0; n < window; n++ {
if e := websocket.Message.Send(c, packet(2, 3, nil)); e != nil {
t.Fatal(e)
}
}
if p = receive(); p[0] != 3 || binary.LittleEndian.Uint32(p[1:]) != 3 {
t.Fatal("empty frame credit", p)
}
websocket.Message.Send(c, packet(4, 3, []byte{2}))
websocket.Message.Send(c, packet(9, 4, nil))
var b []byte
if websocket.Message.Receive(c, &b) == nil && b[0] != 4 {
t.Fatal("invalid packet accepted")
}
resp, e := http.Get(s.URL + "/status")
if e != nil {
t.Fatal(e)
}
resp.Body.Close()
if resp.StatusCode != 403 {
t.Fatal("unprotected metrics")
}
}
func TestRelayStreamBoundAndCancellation(t *testing.T) {
r, _ := New(testKey, t.TempDir())
var dialing atomic.Int64
r.dial = func(ctx context.Context, host string, port uint16) (net.Conn, error) {
dialing.Add(1)
defer dialing.Add(-1)
<-ctx.Done()
return nil, ctx.Err()
}
server := httptest.NewServer(r)
defer server.Close()
cfg, _ := websocket.NewConfig("ws"+strings.TrimPrefix(server.URL, "http")+"/wisp/", "https://pages.example")
cfg.Header.Set(GrantHeader, Sign(testKey, Grant{"domain:actor", "stream-bound-nonce", time.Now().Add(30 * time.Second).Unix()}))
c, err := websocket.DialConfig(cfg)
if err != nil {
t.Fatal(err)
}
defer c.Close()
c.SetDeadline(time.Now().Add(5 * time.Second))
var frame []byte
if err = websocket.Message.Receive(c, &frame); err != nil {
t.Fatal(err)
}
for id := 1; id <= maxStreams+1; id++ {
if err = websocket.Message.Send(c, packet(1, uint32(id), append([]byte{1, 80, 0}, []byte("example.org")...))); err != nil {
t.Fatal(err)
}
}
if err = websocket.Message.Receive(c, &frame); err != nil || len(frame) != 6 || frame[0] != 4 || binary.LittleEndian.Uint32(frame[1:]) != maxStreams+1 || frame[5] != 0x49 {
t.Fatal("stream cap", frame, err)
}
c.Close()
for n := 0; n < 100; n++ {
r.mu.Lock()
active := len(r.active)
r.mu.Unlock()
if active == 0 && dialing.Load() == 0 {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatal("closing session did not cancel pending dials")
}
func TestRelaySessionBoundAndReplay(t *testing.T) {
r, _ := New(testKey, t.TempDir())
server := httptest.NewServer(r)
defer server.Close()
dial := func(subject, nonce string) (*websocket.Conn, error) {
cfg, _ := websocket.NewConfig("ws"+strings.TrimPrefix(server.URL, "http")+"/wisp/", "https://pages.example")
cfg.Header.Set(GrantHeader, Sign(testKey, Grant{subject, nonce, time.Now().Add(30 * time.Second).Unix()}))
return websocket.DialConfig(cfg)
}
var conns []*websocket.Conn
for _, subject := range []string{"one", "two", "three", "four"} {
c, e := dial(subject, "unique-session-nonce-"+subject)
if e != nil {
t.Fatal(e)
}
conns = append(conns, c)
defer c.Close()
}
if c, e := dial("five", "unique-session-nonce-five"); e == nil {
c.Close()
t.Fatal("global cap exceeded")
}
conns[0].Close()
for n := 0; n < 100; n++ {
r.mu.Lock()
active := len(r.active)
r.mu.Unlock()
if active == 3 {
break
}
time.Sleep(10 * time.Millisecond)
}
if c, e := dial("one", "unique-session-nonce-one"); e == nil {
c.Close()
t.Fatal("replayed after close")
}
c, e := dial("five", "unique-session-nonce-retry")
if e != nil {
t.Fatal("slot not released", e)
}
c.Close()
}
+1
View File
@@ -16,6 +16,7 @@ func (s *Server) registeredEventHandlers() *dregarnuhr.Registry {
s.eventHandlersOnce.Do(func() { s.eventHandlersOnce.Do(func() {
registry := &dregarnuhr.Registry{} registry := &dregarnuhr.Registry{}
handlers := map[string]dregarnuhr.Handler{ handlers := map[string]dregarnuhr.Handler{
store.FeedSyncEventType: s.syncFeedSources,
store.TaskDigestEventType: s.deliverTaskDigests, store.TaskDigestEventType: s.deliverTaskDigests,
store.NotificationPushEventType: func(ctx context.Context, job *store.EventJob) error { store.NotificationPushEventType: func(ctx context.Context, job *store.EventJob) error {
return s.deliverNotificationPushContext(ctx, job) return s.deliverNotificationPushContext(ctx, job)
+3
View File
@@ -58,6 +58,9 @@ func (s *Server) startEventScheduler() {
if s.store == nil { if s.store == nil {
return return
} }
if err := s.store.EnsureFeedEvent(); err != nil {
log.Printf("feed scheduler initialization failed")
}
if err := s.store.EnsureBuiltinResourceEvent(); err != nil { if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
log.Printf("resource monitor initialization failed: %v", err) log.Printf("resource monitor initialization failed: %v", err)
} }
+13 -3
View File
@@ -10,16 +10,26 @@ import (
const systemExtensionStateKey = "system_extension_states" const systemExtensionStateKey = "system_extension_states"
func (s *Server) systemExtensionEnabled(domainUUID, id string) bool { func (s *Server) systemExtensionEnabled(domainUUID, id string) bool {
defaultEnabled := true
for _, manifest := range systemext.Catalog() {
if manifest.ID == id {
defaultEnabled = manifest.Enabled
break
}
}
raw, err := s.store.GetConfigInDomain(domainUUID, systemExtensionStateKey) raw, err := s.store.GetConfigInDomain(domainUUID, systemExtensionStateKey)
if err != nil || strings.TrimSpace(raw) == "" { if err != nil || strings.TrimSpace(raw) == "" {
return true return defaultEnabled
} }
var states map[string]bool var states map[string]bool
if json.Unmarshal([]byte(raw), &states) != nil { if json.Unmarshal([]byte(raw), &states) != nil {
return true return defaultEnabled
} }
enabled, ok := states[id] enabled, ok := states[id]
return !ok || enabled if !ok {
return defaultEnabled
}
return enabled
} }
func (s *Server) setSystemExtensionEnabled(domainUUID, id string, enabled bool) error { func (s *Server) setSystemExtensionEnabled(domainUUID, id string, enabled bool) error {
+257
View File
@@ -0,0 +1,257 @@
package server
import (
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"pageswiki/internal/store"
)
const feedsAPI = "/_/api/v1/extension/organizer.feeds/"
func feedRoute(p string) bool { return p == "/v/feeds" || strings.HasPrefix(p, feedsAPI) }
func (s *Server) handleFeeds(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "private, no-store")
domain := s.domainUUIDForRequest(r)
if !s.systemExtensionEnabled(domain, "organizer.feeds") {
http.NotFound(w, r)
return
}
actor, active := s.calendarActor(r)
if actor == "" {
s.authRequired(w, r)
return
}
if r.Method != "GET" && r.Method != "HEAD" && !active {
taskError(w, store.ErrAccessDenied)
return
}
if strings.HasPrefix(r.URL.Path, geckoAPI) {
s.handleFeedBrowser(w, r, domain, actor, active)
return
}
if r.URL.Path == "/v/feeds" {
if r.Method != "GET" && r.Method != "HEAD" {
w.WriteHeader(405)
return
}
if _, e := s.store.FeedSources(r.Context(), domain, actor); e != nil {
taskError(w, e)
return
}
prefix := ""
linkMode := "tab"
if s.systemExtensionEnabled(domain, "organizer.tasks") {
preferences, e := s.store.TaskPreferences(r.Context(), domain, actor)
if e != nil {
taskError(w, e)
return
}
prefix = preferences.WikiPrefix
linkMode = preferences.RSSLinkMode
}
if linkMode == "gecko" {
if active && s.geckoAvailable(domain) {
geckoIsolation(w)
} else {
linkMode = "tab"
}
}
s.renderWithContext(w, r, "feeds.gohtml", map[string]any{"Title": "RSS", "FeedWritable": active, "FeedWikiPrefix": prefix, "FeedLinkMode": linkMode, "FeedShareX": s.systemExtensionEnabled(domain, "sharing.x")})
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
}
switch {
case key == "capture-preview" && r.Method == "POST":
var input struct {
Prefix string `json:"prefix"`
Title string `json:"title"`
}
if !taskJSON(w, r, &input) {
return
}
prefix, e := store.ExpandWikiPrefix(input.Prefix, input.Title, time.Now().In(s.getLocation(domain)))
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]string{"prefix": prefix})
return
case key == "catalog" && r.Method == "GET":
catalog, e := s.store.FeedCatalogSnapshot(r.Context(), domain, actor, r.URL.Query().Get("source"))
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, catalog)
return
case key == "note" && r.Method == "POST":
var input struct {
RequestID string `json:"request_id"`
Source string `json:"source"`
ID string `json:"id"`
Path string `json:"path"`
Title string `json:"title"`
Body string `json:"body"`
Confirm bool `json:"confirm_publication"`
}
if !taskJSONLimit(w, r, &input, 128<<10) {
return
}
if !input.Confirm {
taskError(w, store.ErrTaskInvalid)
return
}
for _, capability := range []string{"read", "create", "write"} {
if s.authorizePathRequest(r, capability, input.Path) != nil {
taskError(w, store.ErrAccessDenied)
return
}
}
page, e := s.store.CaptureFeedNote(r.Context(), domain, actor, input.RequestID, input.Source, input.ID, input.Path, input.Title, input.Body)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 201, map[string]string{"page_uuid": page})
return
case key == "sources" && r.Method == "GET":
sources, e := s.store.FeedSources(r.Context(), domain, actor)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"sources": sources})
return
case key == "sources" && r.Method == "POST":
var input struct {
store.FeedSource
Username string `json:"username"`
Password string `json:"password"`
}
if !taskJSON(w, r, &input) {
return
}
if !validCalendarSourceURL(input.URL) || len(input.Username) > 256 || len(input.Password) > 4096 {
taskError(w, store.ErrTaskInvalid)
return
}
if input.Provider == "freshrss" {
u, _ := url.Parse(input.URL)
if u.RawQuery != "" || !strings.HasSuffix(strings.TrimRight(u.Path, "/"), "/greader.php") {
taskError(w, store.ErrTaskInvalid)
return
}
}
if input.UUID == "" {
input.UUID = uuid.NewString()
}
if input.Version == 0 && input.Provider == "freshrss" {
if input.Username == "" || input.Password == "" {
taskError(w, store.ErrTaskInvalid)
return
}
cipher, e := s.store.SealFeedCredential(domain, actor, input.UUID, input.Username, input.Password)
if e != nil {
taskError(w, e)
return
}
input.Credential = cipher
} else if input.Username != "" || input.Password != "" {
taskError(w, store.ErrTaskInvalid)
return
}
v, e := s.store.SaveFeedSource(r.Context(), domain, actor, input.FeedSource)
if e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"source": v})
return
case strings.HasPrefix(key, "sources/") && r.Method == "POST":
parts := strings.Split(key, "/")
if len(parts) != 3 {
http.NotFound(w, r)
return
}
var input struct {
Version int64 `json:"version"`
}
if !taskJSON(w, r, &input) {
return
}
if e := s.store.FeedSourceAction(r.Context(), domain, actor, parts[1], parts[2], input.Version); e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]bool{"ok": true})
return
case key == "items" && r.Method == "GET":
q := r.URL.Query()
before := int64(-1)
if q.Has("before") {
var e error
before, e = strconv.ParseInt(q.Get("before"), 10, 64)
if e != nil || before < 0 {
taskError(w, store.ErrTaskInvalid)
return
}
}
items, e := s.store.FeedItems(r.Context(), domain, actor, q.Get("source"), q.Get("filter"), q.Get("q"), before, q.Get("key"), q.Get("feed"), q.Get("category"), q.Get("category_mode"))
if e != nil {
taskError(w, e)
return
}
more := len(items) > 100
if more {
items = items[:100]
}
writeEventJSON(w, 200, map[string]any{"items": items, "more": more})
return
case key == "item" && r.Method == "GET":
v, e := s.store.FeedItem(r.Context(), domain, actor, r.URL.Query().Get("source"), r.URL.Query().Get("id"))
if e != nil {
taskError(w, e)
return
}
// Defense in depth for restored/older caches, not just freshly fetched data.
v.BodyHTML = feedHTML(v.BodyHTML, v.URL)
writeEventJSON(w, 200, map[string]any{"item": v})
return
case key == "state" && r.Method == "POST":
var input struct {
Source string `json:"source"`
ID string `json:"id"`
Read bool `json:"read"`
Starred bool `json:"starred"`
Version int64 `json:"version"`
}
if !taskJSON(w, r, &input) {
return
}
if e := s.store.SetFeedState(r.Context(), domain, actor, input.Source, input.ID, input.Read, input.Starred, input.Version); e != nil {
taskError(w, e)
return
}
writeEventJSON(w, 200, map[string]any{"version": input.Version + 1})
return
}
http.NotFound(w, r)
}
func init() {
translations["en"]["feeds.title"] = "RSS"
translations["ja"]["feeds.title"] = "RSS"
translations["en"]["feeds.help"] = "Private local feeds and FreshRSS integration with optional read/star updates"
translations["ja"]["feeds.help"] = "個人用の簡易RSSとFreshRSS連携(既読・star変更は任意)"
}
+174
View File
@@ -0,0 +1,174 @@
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
}
+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")
}
}
})
}
}
}
@@ -0,0 +1,54 @@
package server
import (
"context"
"encoding/json"
"strings"
"testing"
)
func TestRSSLinkPreference(t *testing.T) {
s, token, domain, actor, _ := feedFixture(t)
p, err := s.store.TaskPreferences(context.Background(), domain, actor)
if err != nil || p.RSSLinkMode != "tab" {
t.Fatal(p.RSSLinkMode, err)
}
p.RSSLinkMode = "embedded"
if w := taskRequest(s, token, "PUT", "preferences", p, ""); w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
p, err = s.store.TaskPreferences(context.Background(), domain, actor)
if err != nil || p.RSSLinkMode != "embedded" {
t.Fatal(p.RSSLinkMode, err)
}
p.RSSLinkMode = "gecko"
if w := taskRequest(s, token, "PUT", "preferences", p, ""); w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
p.RSSLinkMode = "unsafe"
if w := taskRequest(s, token, "PUT", "preferences", p, ""); w.Code != 422 {
t.Fatal(w.Code, w.Body)
}
}
func TestFeedCapturePreview(t *testing.T) {
s, token, _, _, _ := feedFixture(t)
for _, tt := range []struct {
prefix string
status int
}{{"/notes/{YYYY}/{MM}/{DD}/{Subject}", 200}, {"/notes/{unknown}", 422}, {"/a/../b", 422}} {
w := feedRequestTest(s, token, "POST", "capture-preview", map[string]string{"prefix": tt.prefix, "title": "日本語 / test"})
if w.Code != tt.status {
t.Fatal(w.Code, w.Body)
}
if w.Code == 200 {
var data map[string]string
if e := json.Unmarshal(w.Body.Bytes(), &data); e != nil || strings.ContainsAny(data["prefix"], "{}") || !strings.HasSuffix(data["prefix"], "/日本語---test") {
t.Fatal(data, e)
}
}
}
if w := feedRequestTest(s, "", "POST", "capture-preview", map[string]string{"prefix": "/notes"}); w.Code != 401 {
t.Fatal(w.Code)
}
}
+162
View File
@@ -0,0 +1,162 @@
package server
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
"testing"
"github.com/google/uuid"
"pageswiki/internal/store"
)
const testFeedCatalog = `{"subscriptions":[{"id":"feed/1","title":"Same title","categories":[{"id":"user/alice/label/Tech","label":"技術"}]},{"id":"feed/2","title":"Same title","categories":[{"id":"user/alice/label/Other","label":"技術"}]},{"id":"feed/3","title":"Empty feed","categories":[]}]}`
func TestFreshRSSCatalogValidation(t *testing.T) {
catalog, e := parseFreshRSSCatalog([]byte(testFeedCatalog))
if e != nil || len(catalog) != 3 || catalog[0].Categories[0].ID == catalog[1].Categories[0].ID {
t.Fatal(catalog, e)
}
for _, raw := range []string{`{}`, `{"subscriptions":null}`, `{"subscriptions":[{"id":""}]}`, `{"subscriptions":[{"id":"feed/1"},{"id":"feed/1"}]}`, `{"subscriptions":[{"id":"feed/1","categories":[{"id":"cat","label":"A"},{"id":"cat","label":"A"}]}]}`, `{"subscriptions":[{"id":"feed/1","categories":[{"id":"cat","label":"A"}]},{"id":"feed/2","categories":[{"id":"cat","label":"B"}]}]}`} {
if _, e := parseFreshRSSCatalog([]byte(raw)); e == nil {
t.Fatal("accepted", raw)
}
}
if c, e := parseFreshRSSCatalog([]byte(`{"subscriptions":[]}`)); e != nil || len(c) != 0 {
t.Fatal(c, e)
}
big := make([]store.FeedRemoteSubscription, 1001)
raw, _ := json.Marshal(map[string]any{"subscriptions": big})
if _, e := parseFreshRSSCatalog(raw); e == nil {
t.Fatal("unbounded catalog")
}
}
func TestFeedCatalogBackgroundSnapshotIsolation(t *testing.T) {
s, token, domain, actor, local := feedFixture(t)
ctx := context.Background()
if e := s.store.FeedSourceAction(ctx, domain, actor, local.UUID, "delete", local.Version); e != nil {
t.Fatal(e)
}
v := store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "FreshRSS", URL: "https://example.org/api/greader.php", Enabled: 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)
}
calls := 0
failCatalog := false
failFolders := false
client := &http.Client{Transport: feedTransportFunc(func(r *http.Request) (*http.Response, error) {
calls++
body := ""
switch {
case strings.HasSuffix(r.URL.Path, "/accounts/ClientLogin"):
if r.Method != "POST" {
t.Error("login method")
}
body = "Auth=synthetic"
case strings.HasSuffix(r.URL.Path, "/stream/contents/reading-list"):
if r.Method != "GET" || r.URL.Query().Get("n") != "200" {
t.Error("article request")
}
body = `{"items":[{"id":"a","title":"First","origin":{"title":"Same title","streamId":"feed/1"},"categories":["user/alice/label/Tech"]},{"id":"b","title":"Second","origin":{"title":"Same title","streamId":"feed/2"},"categories":["user/alice/label/Other"]}]}`
if failCatalog || failFolders {
body = `{"items":[]}`
}
case strings.HasSuffix(r.URL.Path, "/subscription/list"):
if r.Method != "GET" || r.Header.Get("Authorization") != "GoogleLogin auth=synthetic" {
t.Error("catalog request")
}
if failCatalog {
return nil, errors.New("synthetic timeout")
}
body = testFeedCatalog
case strings.HasSuffix(r.URL.Path, "/tag/list"):
if failFolders {
return nil, errors.New("synthetic folder failure")
}
body = `{"tags":[{"id":"user/-/label/Empty/Folder","type":"folder"},{"id":"user/-/label/Not a folder","type":"tag"}]}`
default:
t.Fatalf("unexpected external request %s %s", r.Method, r.URL.Path)
}
return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))}, nil
})}
run := func() {
t.Helper()
if _, e := s.store.DB.Exec(`UPDATE feed_sources SET next_ms=0 WHERE uuid=?`, v.UUID); e != nil {
t.Fatal(e)
}
if e := s.syncFeedSourcesWithClient(ctx, &store.EventJob{}, client); e != nil {
t.Fatal(e)
}
}
run()
if calls != 4 {
t.Fatal("fetch count", calls)
}
w := feedRequestTest(s, token, "GET", "catalog?source="+v.UUID, nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), "Empty feed") || strings.Contains(w.Body.String(), "secret") {
t.Fatal(w.Code, w.Body)
}
for _, scope := range []string{"feed=feed%2F1", "category=" + url.QueryEscape("user/alice/label/Tech")} {
w = feedRequestTest(s, token, "GET", "items?source="+v.UUID+"&"+scope, nil)
var data struct {
Items []store.FeedItem `json:"items"`
}
json.Unmarshal(w.Body.Bytes(), &data)
if w.Code != 200 || len(data.Items) != 1 || data.Items[0].ID != "a" {
t.Fatal(scope, w.Code, w.Body)
}
}
w = feedRequestTest(s, token, "GET", "items?feed=feed%2F1", nil)
if w.Code != 422 {
t.Fatal("unscoped remote ID", w.Code, w.Body)
}
other, e := s.store.CreatePasswordAccount(domain, "catalogother", "long test password", "Other", "")
if e != nil {
t.Fatal(e)
}
otherToken, e := s.store.CreateAuthSession(domain, other.UUID)
if e != nil {
t.Fatal(e)
}
if w = feedRequestTest(s, otherToken, "GET", "catalog?source="+v.UUID, nil); w.Code == 200 {
t.Fatal("cross-owner catalog leak")
}
if w = feedRequestTest(s, "", "GET", "catalog?source="+v.UUID, nil); w.Code == 200 {
t.Fatal("anonymous catalog leak")
}
if _, e := s.store.FeedCatalog(ctx, uuid.NewString(), actor, v.UUID); e == nil {
t.Fatal("cross-domain catalog leak")
}
if calls != 4 {
t.Fatal("foreground external request")
}
failFolders = true
run()
kept, e := s.store.FeedCatalogSnapshot(ctx, domain, actor, v.UUID)
if e != nil || len(kept.Folders) != 3 || len(kept.Subscriptions) != 3 {
t.Fatal("folder failure erased snapshot", kept, e)
}
failFolders = false
failCatalog = true
run()
catalog, e := s.store.FeedCatalog(ctx, domain, actor, v.UUID)
if e != nil || len(catalog) != 3 {
t.Fatal("lost catalog", catalog, e)
}
items, e := s.store.FeedItems(ctx, domain, actor, v.UUID, "", "", -1, "")
if e != nil || len(items) != 2 {
t.Fatal("lost articles", items, e)
}
if e := s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil {
t.Fatal(e)
}
if _, e := s.store.FeedCatalog(ctx, domain, actor, v.UUID); !errors.Is(e, store.ErrAccessDenied) {
t.Fatal("disabled read", e)
}
}
+573
View File
@@ -0,0 +1,573 @@
package server
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"html"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/microcosm-cc/bluemonday"
xhtml "golang.org/x/net/html"
"golang.org/x/net/html/charset"
"pageswiki/internal/store"
)
var errFeedInvalid = errors.New("invalid_feed")
var feedTextPolicy = bluemonday.StrictPolicy()
var feedHTMLPolicy = func() *bluemonday.Policy {
p := bluemonday.NewPolicy()
p.AllowElements("p", "div", "span", "br", "hr", "h1", "h2", "h3", "h4", "h5", "h6", "strong", "b", "em", "i", "u", "s", "del", "ins", "sub", "sup", "mark", "small", "blockquote", "pre", "code", "kbd", "samp", "ul", "ol", "li", "dl", "dt", "dd", "table", "caption", "thead", "tbody", "tfoot", "tr", "th", "td", "a")
p.AllowAttrs("href", "target", "rel").OnElements("a")
p.AllowAttrs("colspan", "rowspan", "scope").OnElements("th", "td")
p.AllowStandardURLs()
p.AllowRelativeURLs(false)
return p
}()
// Keep article structure without granting fetched HTML script, style, form,
// embedding or automatic network capabilities. Wiki capture still uses Body.
func feedHTML(raw, base string) string {
if raw == "" || len(raw) > 128<<10 {
return ""
}
z := xhtml.NewTokenizer(strings.NewReader(raw))
var out strings.Builder
markup := false
for {
typeOf := z.Next()
if typeOf == xhtml.ErrorToken {
if z.Err() != io.EOF {
return ""
}
break
}
if typeOf == xhtml.StartTagToken || typeOf == xhtml.SelfClosingTagToken {
markup = true
tok := z.Token()
if tok.Data == "a" {
attrs := []xhtml.Attribute{}
for _, attr := range tok.Attr {
if attr.Key == "href" {
if link := feedLink(base, attr.Val); link != "" {
attrs = append(attrs, xhtml.Attribute{Key: "href", Val: link})
}
}
}
tok.Attr = append(attrs, xhtml.Attribute{Key: "target", Val: "_blank"}, xhtml.Attribute{Key: "rel", Val: "noopener noreferrer"})
}
out.WriteString(tok.String())
} else {
out.Write(z.Raw())
}
}
if !markup {
return ""
}
clean := feedHTMLPolicy.Sanitize(out.String())
if len(clean) > 128<<10 {
return ""
}
return clean
}
func feedText(raw string, limit int) string {
// Plain text for fallback and inert Wiki/task capture.
for _, tag := range []string{"</p>", "</div>", "<br>", "<br/>", "<br />", "</li>"} {
raw = strings.ReplaceAll(raw, tag, tag+"\n")
}
text := strings.TrimSpace(html.UnescapeString(feedTextPolicy.Sanitize(raw)))
if len(text) > limit {
text = text[:limit-16]
for !utf8.ValidString(text) {
text = text[:len(text)-1]
}
text += "\n[… truncated]"
}
return text
}
func feedLink(base, raw string) string {
if strings.TrimSpace(raw) == "" || len(raw) > 4096 {
return ""
}
u, e := url.Parse(strings.TrimSpace(raw))
if e != nil {
return ""
}
b, e := url.Parse(base)
if e != nil {
return ""
}
u = b.ResolveReference(u)
if (u.Scheme != "https" && u.Scheme != "http") || u.Hostname() == "" || u.User != nil {
return ""
}
result := u.String()
if len(result) > 4096 {
return ""
}
return result
}
func feedDate(v string) int64 {
for _, layout := range []string{time.RFC3339Nano, time.RFC1123Z, time.RFC1123, time.RFC822Z, time.RFC822, "2006-01-02T15:04:05-0700"} {
if t, e := time.Parse(layout, v); e == nil && t.UnixMilli() > 0 && t.Before(time.Now().AddDate(1, 0, 0)) {
return t.UnixMilli()
}
}
return 0
}
type rssEntry struct {
ID string `xml:"guid"`
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Content string `xml:"encoded"`
Date string `xml:"pubDate"`
Category string `xml:"category"`
}
type atomText struct {
Type string `xml:"type,attr"`
Text string `xml:",chardata"`
Inner string `xml:",innerxml"`
}
func (v atomText) body() (string, bool) {
if v.Type == "xhtml" {
return v.Inner, true
}
return v.Text, v.Type == "html"
}
type atomEntry struct {
ID string `xml:"id"`
Title string `xml:"title"`
Links []struct {
Href string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
} `xml:"link"`
Summary atomText `xml:"summary"`
Content atomText `xml:"content"`
Published string `xml:"published"`
Updated string `xml:"updated"`
Category struct {
Term string `xml:"term,attr"`
} `xml:"category"`
}
func parseFeed(raw []byte, base string) ([]store.FeedItem, error) {
if len(raw) > 4<<20 {
return nil, errors.New("limit")
}
d := xml.NewDecoder(bytes.NewReader(raw))
d.CharsetReader = charset.NewReaderLabel
root := ""
title := ""
depth := 0
out := []store.FeedItem{}
seen := map[string]bool{}
for {
tok, e := d.Token()
if e == io.EOF {
break
}
if e != nil {
return nil, errFeedInvalid
}
switch t := tok.(type) {
case xml.Directive:
return nil, errFeedInvalid // DTD/entities have no role in a feed.
case xml.StartElement:
depth++
if depth == 1 && root != "" {
return nil, errFeedInvalid
}
if depth > 64 {
return nil, errFeedInvalid
}
if root == "" {
root = t.Name.Local
if root != "rss" && root != "feed" {
return nil, errFeedInvalid
}
}
var item store.FeedItem
isItem := false
htmlBody := false
if root == "rss" && t.Name.Local == "item" && depth == 3 {
var v rssEntry
if d.DecodeElement(&v, &t) != nil {
return nil, errFeedInvalid
}
depth--
body := v.Content
if body == "" {
body = v.Description
}
item = store.FeedItem{ID: v.ID, Title: v.Title, URL: feedLink(base, v.Link), Body: body, PublishedMS: feedDate(v.Date), Folder: v.Category}
isItem = true
htmlBody = true
} else if root == "feed" && t.Name.Local == "entry" && depth == 2 {
var v atomEntry
if d.DecodeElement(&v, &t) != nil {
return nil, errFeedInvalid
}
depth--
body, isHTML := v.Content.body()
if body == "" {
body, isHTML = v.Summary.body()
}
htmlBody = isHTML
link := ""
for _, l := range v.Links {
if l.Rel == "" || l.Rel == "alternate" {
link = feedLink(base, l.Href)
break
}
}
date := v.Published
if date == "" {
date = v.Updated
}
item = store.FeedItem{ID: v.ID, Title: v.Title, URL: link, Body: body, PublishedMS: feedDate(date), Folder: v.Category.Term}
isItem = true
} else if t.Name.Local == "title" && ((root == "rss" && depth == 3) || (root == "feed" && depth == 2)) {
if d.DecodeElement(&title, &t) != nil {
return nil, errFeedInvalid
}
depth--
}
if isItem {
if len(out) >= 500 {
return nil, errors.New("limit")
}
item.Title = feedText(item.Title, 2048)
if htmlBody {
item.BodyHTML = feedHTML(item.Body, base)
item.Body = feedText(item.Body, 32768)
} else {
// Atom type=text is literal, even when it contains HTML-looking text.
item.Body = feedText(html.EscapeString(item.Body), 32768)
}
item.Feed = feedText(title, 512)
item.Folder = feedText(item.Folder, 512)
if item.ID == "" {
key := item.URL
if key == "" {
key = item.Title + "\x00" + strconv.FormatInt(item.PublishedMS, 10) + "\x00" + item.Body
}
h := sha256.Sum256([]byte(key))
item.ID = "sha256:" + hex.EncodeToString(h[:])
}
if len(item.ID) > 1024 || seen[item.ID] {
return nil, errFeedInvalid
}
seen[item.ID] = true
out = append(out, item)
}
case xml.EndElement:
depth--
}
}
if root == "" || depth != 0 {
return nil, errFeedInvalid
}
return out, nil
}
func feedRequest(ctx context.Context, client *http.Client, method, target, auth string, form url.Values, headers map[string]string) ([]byte, http.Header, int, error) {
var body io.Reader
if form != nil {
body = strings.NewReader(form.Encode())
}
r, e := http.NewRequestWithContext(ctx, method, target, body)
if e != nil {
return nil, nil, 0, errFeedInvalid
}
if auth != "" {
r.Header.Set("Authorization", "GoogleLogin auth="+auth)
}
if form != nil {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
r.Header.Set("User-Agent", "PagesWiki-Feeds/0.1")
for k, v := range headers {
r.Header.Set(k, v)
}
res, e := client.Do(r)
if e != nil {
return nil, nil, 0, errors.New("network")
}
defer res.Body.Close()
if res.StatusCode == 401 || res.StatusCode == 403 {
return nil, nil, res.StatusCode, errors.New("credentials")
}
if res.StatusCode == 304 {
return nil, res.Header, 304, nil
}
if res.StatusCode != 200 {
return nil, nil, res.StatusCode, errors.New("remote_error")
}
raw, e := io.ReadAll(io.LimitReader(res.Body, (4<<20)+1))
if e != nil {
return nil, nil, 0, errors.New("network")
}
if len(raw) > 4<<20 {
return nil, nil, 0, errors.New("limit")
}
return raw, res.Header, res.StatusCode, nil
}
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 "", e
}
auth := ""
for _, line := range strings.Split(string(raw), "\n") {
if strings.HasPrefix(line, "Auth=") {
auth = strings.TrimSpace(strings.TrimPrefix(line, "Auth="))
}
}
if auth == "" || len(auth) > 4096 || strings.ContainsAny(auth, "\r\n") {
return "", errors.New("credentials")
}
return auth, nil
}
func fetchFreshRSS(ctx context.Context, client *http.Client, v store.FeedSource, user, password string, catalogs ...*store.FeedCatalogSnapshot) ([]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
}
raw, _, _, e = feedRequest(ctx, client, "GET", base+"/reader/api/0/tag/list?output=json", auth, nil, nil)
if e != nil {
return nil, e
}
folders, e := parseFreshRSSFolders(raw)
if e != nil {
return nil, e
}
folders, e = store.FeedFolders(catalog, folders)
if e != nil {
return nil, errFeedInvalid
}
snapshot := store.FeedCatalogSnapshot{Subscriptions: catalog, Folders: folders}
if _, _, e = snapshot.Encode(); e != nil {
return nil, errFeedInvalid
}
*catalogs[0] = snapshot
}
return items, nil
}
func parseFreshRSSItems(raw []byte, base string) ([]store.FeedItem, error) {
var response struct {
Items []struct {
ID string `json:"id"`
Title string `json:"title"`
Published int64 `json:"published"`
Categories []string `json:"categories"`
Origin struct {
Title string `json:"title"`
StreamID string `json:"streamId"`
} `json:"origin"`
Alternate []struct {
Href string `json:"href"`
} `json:"alternate"`
Summary struct {
Content string `json:"content"`
} `json:"summary"`
Content struct {
Content string `json:"content"`
} `json:"content"`
} `json:"items"`
}
if json.Unmarshal(raw, &response) != nil || response.Items == nil || len(response.Items) > 200 {
return nil, errFeedInvalid
}
out := []store.FeedItem{}
seen := map[string]bool{}
for _, i := range response.Items {
if i.ID == "" || len(i.ID) > 1024 || seen[i.ID] {
return nil, errFeedInvalid
}
seen[i.ID] = true
if (i.Origin.StreamID != "" && !store.ValidFeedRemoteID(i.Origin.StreamID)) || len(i.Categories) > 64 {
return nil, errFeedInvalid
}
item := store.FeedItem{ID: i.ID, Title: feedText(i.Title, 2048), Feed: feedText(i.Origin.Title, 512), RemoteFeed: i.Origin.StreamID, RemoteCategories: []string{}}
if i.Published > 0 && i.Published < time.Now().AddDate(1, 0, 0).Unix() {
item.PublishedMS = i.Published * 1000
}
body := i.Summary.Content
if body == "" {
body = i.Content.Content
}
item.Body = feedText(body, 32768)
item.BodyHTML = feedHTML(body, base)
if len(i.Alternate) > 0 {
item.URL = feedLink(base, i.Alternate[0].Href)
}
for _, c := range i.Categories {
if !store.ValidFeedRemoteID(c) {
return nil, errFeedInvalid
}
if strings.HasSuffix(c, "/state/com.google/read") {
item.Read = true
}
if strings.HasSuffix(c, "/state/com.google/starred") {
item.Starred = true
}
if pos := strings.Index(c, "/label/"); pos >= 0 {
item.Folder = feedText(c[pos+7:], 512)
item.RemoteCategories = append(item.RemoteCategories, c)
}
}
out = append(out, item)
}
return out, nil
}
func parseFreshRSSCatalog(raw []byte) ([]store.FeedRemoteSubscription, error) {
if len(raw) > 4<<20 || !utf8.Valid(raw) {
return nil, errFeedInvalid
}
var response struct {
Subscriptions []store.FeedRemoteSubscription `json:"subscriptions"`
}
if json.Unmarshal(raw, &response) != nil || response.Subscriptions == nil {
return nil, errFeedInvalid
}
if len(response.Subscriptions) > 1000 {
return nil, errors.New("limit")
}
for i := range response.Subscriptions {
sub := &response.Subscriptions[i]
sub.Title = feedText(sub.Title, 512)
if sub.Categories == nil {
sub.Categories = []store.FeedCategory{}
}
for j := range sub.Categories {
sub.Categories[j].Label = feedText(sub.Categories[j].Label, 512)
}
}
if _, e := store.FeedCatalogJSON(response.Subscriptions); e != nil {
return nil, errFeedInvalid
}
return response.Subscriptions, nil
}
func (s *Server) syncFeedSources(ctx context.Context, job *store.EventJob) error {
client := calendarSourceClient()
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
defer client.CloseIdleConnections()
return s.syncFeedSourcesWithClient(ctx, job, client)
}
// Tests inject a synthetic transport; production always uses the DNS-pinned,
// public HTTPS-only transport above, never a user-configurable HTTP client.
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
}
done, failed := 0, 0
for _, v := range sources {
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
}
v, e = s.store.ClaimFeed(ctx, v)
if e != nil {
continue
}
child, stop := context.WithTimeout(ctx, 8*time.Second)
var items []store.FeedItem
var catalog store.FeedCatalogSnapshot
etag, modified := v.ETag, v.Modified
unchanged := false
if !validCalendarSourceURL(v.URL) {
e = errFeedInvalid
} else if v.Provider == "freshrss" {
var user, password string
user, password, e = s.store.OpenFeedCredential(v)
if e == nil {
items, e = fetchFreshRSS(child, client, v, user, password, &catalog)
} else {
e = errors.New("credentials")
}
} else {
var raw []byte
var h http.Header
var status int
raw, h, status, e = feedRequest(child, client, "GET", v.URL, "", nil, map[string]string{"If-None-Match": v.ETag, "If-Modified-Since": v.Modified})
if e == nil {
unchanged = status == 304
if !unchanged {
items, e = parseFeed(raw, v.URL)
etag, modified = h.Get("ETag"), h.Get("Last-Modified")
}
}
}
stop()
if e == nil && (len(etag) > 1024 || len(modified) > 1024) {
e = errors.New("limit")
etag, modified = v.ETag, v.Modified
items = nil
}
code := ""
if e != nil {
code = e.Error()
switch code {
case "network", "invalid_feed", "credentials", "remote_error", "limit":
default:
code = "invalid_feed"
}
failed++
} else {
done++
}
if e = s.store.CompleteFeed(ctx, v, items, etag, modified, code, unchanged, catalog); e != nil && !errors.Is(e, store.ErrAccessDenied) && !errors.Is(e, store.ErrTaskConflict) {
return e
}
}
s.addEventProgressLog(ctx, job, fmt.Sprintf("feed sources updated=%d failed=%d", done, failed))
return nil
}
+42
View File
@@ -0,0 +1,42 @@
package server
import (
"encoding/json"
"strings"
"unicode/utf8"
"pageswiki/internal/store"
)
func parseFreshRSSFolders(raw []byte) ([]store.FeedCategory, error) {
if len(raw) > 4<<20 || !utf8.Valid(raw) {
return nil, errFeedInvalid
}
var response struct {
Tags []struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"tags"`
}
if json.Unmarshal(raw, &response) != nil || response.Tags == nil || len(response.Tags) > 10000 {
return nil, errFeedInvalid
}
folders := []store.FeedCategory{}
seen := map[string]bool{}
for _, tag := range response.Tags {
// Article tags and system state tags are not subscription folders.
if tag.Type != "folder" {
continue
}
prefix, label, ok := strings.Cut(tag.ID, "/label/")
if !ok || !strings.HasPrefix(prefix, "user/") || len(prefix) <= 5 || label == "" || !store.ValidFeedRemoteID(tag.ID) || seen[tag.ID] {
return nil, errFeedInvalid
}
seen[tag.ID] = true
folders = append(folders, store.FeedCategory{ID: tag.ID, Label: feedText(label, 512)})
}
if _, e := store.FeedFolders(nil, folders); e != nil {
return nil, errFeedInvalid
}
return folders, nil
}
+95
View File
@@ -0,0 +1,95 @@
package server
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
"testing"
"pageswiki/internal/store"
)
func TestFreshRSSFoldersParsing(t *testing.T) {
folders, e := parseFreshRSSFolders([]byte(`{"tags":[{"id":"user/-/state/com.google/starred"},{"id":"user/-/label/ArticleTag","type":"tag"},{"id":"user/-/label/技術/Linux","type":"folder"}]}`))
if e != nil || len(folders) != 1 || folders[0].Label != "技術/Linux" {
t.Fatal(folders, e)
}
for _, raw := range []string{`{}`, `{"tags":null}`, `{"tags":[{"id":"invalid","type":"folder"}]}`, `{"tags":[{"id":"user/-/label/","type":"folder"}]}`, `{"tags":[{"id":"user/-/label/A","type":"folder"},{"id":"user/-/label/A","type":"folder"}]}`} {
if _, e = parseFreshRSSFolders([]byte(raw)); e == nil {
t.Fatal("invalid folder accepted", raw)
}
}
tags := []map[string]string{}
for i := 0; i < 1001; i++ {
tags = append(tags, map[string]string{"id": fmt.Sprintf("user/-/label/%d", i), "type": "folder"})
}
raw, _ := json.Marshal(map[string]any{"tags": tags})
if _, e = parseFreshRSSFolders(raw); e == nil {
t.Fatal("unbounded folders")
}
if _, e = store.FeedFolders([]store.FeedRemoteSubscription{{ID: "feed/1", Categories: []store.FeedCategory{{ID: "cat", Label: "A"}}}}, []store.FeedCategory{{ID: "cat", Label: "B"}}); e == nil {
t.Fatal("conflicting labels")
}
large := store.FeedCatalogSnapshot{}
for i := 0; i < 1000; i++ {
large.Subscriptions = append(large.Subscriptions, store.FeedRemoteSubscription{ID: fmt.Sprintf("feed/%d", i), Title: strings.Repeat("s", 512)})
large.Folders = append(large.Folders, store.FeedCategory{ID: fmt.Sprintf("user/-/label/%d", i), Label: strings.Repeat("f", 512)})
}
if _, e := store.FeedCatalogJSON(large.Subscriptions); e != nil {
t.Fatal("catalog alone should fit", e)
}
if _, _, e := large.Encode(); e != store.ErrTaskDAVLimit {
t.Fatal("combined snapshot limit", e)
}
}
func TestFeedFolderMembershipAndUncategorized(t *testing.T) {
s, token, domain, actor, source := feedActionFixture(t)
ctx := context.Background()
if _, e := s.store.DB.Exec(`UPDATE feed_sources SET next_ms=0 WHERE uuid=?`, source.UUID); e != nil {
t.Fatal(e)
}
sources, e := s.store.FeedSources(ctx, domain, actor)
if e != nil {
t.Fatal(e)
}
for _, v := range sources {
if v.UUID == source.UUID {
source = v
}
}
claim, e := s.store.ClaimFeed(ctx, source)
if e != nil {
t.Fatal(e)
}
cat := store.FeedCategory{ID: "user/-/label/New/Folder", Label: "New/Folder"}
snapshot := store.FeedCatalogSnapshot{Subscriptions: []store.FeedRemoteSubscription{{ID: "feed/1", Title: "Moved", Categories: []store.FeedCategory{cat}}, {ID: "feed/2", Title: "Unclassified", Categories: []store.FeedCategory{}}}, Folders: []store.FeedCategory{{ID: "user/-/label/Empty", Label: "Empty"}}}
items := []store.FeedItem{{ID: "a", Title: "Moved", RemoteFeed: "feed/1", RemoteCategories: []string{"user/-/label/Old"}}, {ID: "b", Title: "Unclassified", RemoteFeed: "feed/2", RemoteCategories: []string{"user/-/label/ArticleTag"}}, {ID: "c", Title: "Unknown subscription", RemoteFeed: "feed/unknown"}}
if e = s.store.CompleteFeed(ctx, claim, items, "", "", "", false, snapshot); e != nil {
t.Fatal(e)
}
for _, test := range []struct {
query string
want string
}{{"category=" + url.QueryEscape(cat.ID), "a"}, {"category_mode=uncategorized", "b"}, {"category=user%2F-%2Flabel%2FOld", ""}, {"category=user%2F-%2Flabel%2FEmpty", ""}} {
w := feedRequestTest(s, token, "GET", "items?source="+source.UUID+"&"+test.query, nil)
var data struct {
Items []store.FeedItem `json:"items"`
}
json.Unmarshal(w.Body.Bytes(), &data)
if w.Code != 200 || (test.want == "" && len(data.Items) != 0) || (test.want != "" && (len(data.Items) != 1 || data.Items[0].ID != test.want)) {
t.Fatal(test.query, w.Code, w.Body)
}
}
for _, query := range []string{"category_mode=uncategorized", "source=" + source.UUID + "&category_mode=bad", "source=" + source.UUID + "&category_mode=uncategorized&category=" + url.QueryEscape(cat.ID)} {
if w := feedRequestTest(s, token, "GET", "items?"+query, nil); w.Code != 422 {
t.Fatal(query, w.Code)
}
}
got, e := s.store.FeedCatalogSnapshot(ctx, domain, actor, source.UUID)
if e != nil || len(got.Folders) != 2 {
t.Fatal(got, e)
}
}
+139
View File
@@ -0,0 +1,139 @@
package server
import (
"context"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
"pageswiki/internal/geckorelay"
)
const geckoAPI = feedsAPI + "browser/"
type geckoProxy struct {
origin *url.URL
key string
transport *http.Transport
}
func (s *Server) geckoConfig() *geckoProxy {
s.geckoOnce.Do(func() {
u, err := url.Parse(os.Getenv("PAGESWIKI_GECKO_RELAY"))
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || (u.Path != "" && u.Path != "/") {
return
}
key, err := os.ReadFile(os.Getenv("PAGESWIKI_GECKO_KEY_FILE"))
if err != nil || len(strings.TrimSpace(string(key))) < 32 {
return
}
s.gecko = &geckoProxy{origin: u, key: strings.TrimSpace(string(key)), transport: &http.Transport{Proxy: nil, DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext, ResponseHeaderTimeout: 10 * time.Second, TLSHandshakeTimeout: 5 * time.Second, IdleConnTimeout: 30 * time.Second, MaxIdleConnsPerHost: 4}}
})
return s.gecko
}
func (s *Server) geckoAvailable(domain string) bool {
return s.systemExtensionEnabled(domain, "viewer.gecko") && s.geckoConfig() != nil
}
func geckoIsolation(w http.ResponseWriter) {
w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
w.Header().Set("Cross-Origin-Resource-Policy", "same-origin")
}
func (s *Server) handleFeedBrowser(w http.ResponseWriter, r *http.Request, domain, actor string, active bool) {
if !active || !s.geckoAvailable(domain) {
http.NotFound(w, r)
return
}
if r.Method != "GET" || r.URL.RawQuery != "" {
http.Error(w, "invalid request", 400)
return
}
path := strings.TrimPrefix(r.URL.Path, geckoAPI)
switch path {
case "":
geckoIsolation(w)
w.Header().Set("Content-Security-Policy", "default-src 'none'; script-src 'self' blob: 'wasm-unsafe-eval'; worker-src 'self' blob:; connect-src 'self' data:; style-src 'self'; img-src 'self' data:; frame-ancestors 'self'; base-uri 'none'; form-action 'none'")
w.Header().Set("Referrer-Policy", "no-referrer")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, geckoShell)
return
case "assets/v0.0.1/gecko.js", "assets/v0.0.1/gecko.wasm.zst":
case "wisp/":
// WebSocket cookies alone are not CSRF protection.
if r.Header.Get("Origin") != externalURL(r, "") || !strings.EqualFold(r.Header.Get("Upgrade"), "websocket") {
http.Error(w, "forbidden", 403)
return
}
default:
http.NotFound(w, r)
return
}
cfg := s.geckoConfig()
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
if path == "wisp/" {
token := s.authTokenFromRequest(r)
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
p, err := s.store.PrincipalForSession(domain, token)
if err != nil || p.UUID != actor || p.State != "active" || !s.systemExtensionEnabled(domain, "organizer.feeds") || !s.geckoAvailable(domain) {
cancel()
return
}
}
}
}()
}
proxy := &httputil.ReverseProxy{Transport: cfg.transport, Rewrite: func(p *httputil.ProxyRequest) {
p.SetURL(cfg.origin)
p.Out.URL.Path = "/" + path
p.Out.URL.RawPath = ""
// No ambient credentials, forwarding headers, target URL or user-supplied
// grant crosses this boundary. The relay origin is operator-configured.
keep := http.Header{}
for _, h := range []string{"Upgrade", "Connection", "Sec-WebSocket-Key", "Sec-WebSocket-Version", "Origin", "Range", "If-None-Match", "If-Modified-Since"} {
if value := p.Out.Header.Get(h); value != "" {
keep.Set(h, value)
}
}
keep.Set(geckorelay.GrantHeader, geckorelay.Sign(cfg.key, geckorelay.Grant{Subject: domain + ":" + actor, Nonce: randomURLToken(24), Expires: time.Now().Add(30 * time.Second).Unix()}))
p.Out.Header = keep
}, ModifyResponse: func(resp *http.Response) error {
resp.Header.Del("Set-Cookie")
resp.Header.Set("Cross-Origin-Resource-Policy", "same-origin")
if resp.Header.Get("Cache-Control") == "" {
resp.Header.Set("Cache-Control", "private, no-store")
}
return nil
}, ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
w.Header().Set("Cache-Control", "private, no-store")
http.Error(w, "browser relay unavailable", http.StatusBadGateway)
}}
// ReverseProxy adds upstream headers rather than replacing existing values.
// Do not accidentally append the RSS API's no-store to versioned assets.
w.Header().Del("Cache-Control")
proxy.ServeHTTP(w, r.WithContext(ctx))
}
const geckoShell = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="/static/css/feed-gecko.css?v=1"><title>Gecko</title></head><body><p id="status" role="status"></p><canvas id="screen" tabindex="0" aria-label="Original article"></canvas><script type="module" src="/static/js/feed-gecko.js?v=1"></script></body></html>`
func init() {
translations["en"]["feeds.gecko"] = "Gecko RSS browser (experimental)"
translations["ja"]["feeds.gecko"] = "Gecko RSSブラウザ(実験的)"
translations["en"]["feeds.gecko_help"] = "Requires a separately deployed authenticated relay. Public HTTP/HTTPS only; browsing uses the relay's IP address."
translations["ja"]["feeds.gecko_help"] = "認証付き中継サービスの別途配置が必要です。公開HTTP/HTTPSのみ。閲覧先には中継サーバーのIPが使われます。"
}
+125
View File
@@ -0,0 +1,125 @@
package server
import (
"context"
"encoding/binary"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"golang.org/x/net/websocket"
"pageswiki/internal/geckorelay"
"pageswiki/internal/store"
)
// The real engine runs against a synthetic Wisp HTTP responder. This test never
// dials an article server, production source, DNS name or external calendar.
func TestFeedGeckoChromium(t *testing.T) {
assets := os.Getenv("PAGESWIKI_GECKO_TEST_ASSETS")
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" || assets == "" {
t.Skip("opt-in real Gecko assets")
}
var connections atomic.Int64
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := geckorelay.Verify(geckoTestKey, r.Header.Get(geckorelay.GrantHeader), time.Now()); err != nil {
http.Error(w, "bad grant", 403)
return
}
if strings.HasPrefix(r.URL.Path, "/assets/") {
http.ServeFile(w, r, filepath.Join(assets, filepath.Base(r.URL.Path)))
return
}
if r.URL.Path != "/wisp/" {
http.NotFound(w, r)
return
}
websocket.Handler(func(ws *websocket.Conn) {
connections.Add(1)
defer connections.Add(-1)
packet := func(kind byte, id uint32, data []byte) []byte {
out := make([]byte, 5+len(data))
out[0] = kind
binary.LittleEndian.PutUint32(out[1:], id)
copy(out[5:], data)
return out
}
websocket.Message.Send(ws, packet(3, 0, []byte{8, 0, 0, 0}))
requests := map[uint32]string{}
for {
var p []byte
if websocket.Message.Receive(ws, &p) != nil || len(p) < 5 {
return
}
id := binary.LittleEndian.Uint32(p[1:])
switch p[0] {
case 1:
requests[id] = ""
case 2:
requests[id] += string(p[5:])
if !strings.Contains(requests[id], "\r\n\r\n") {
continue
}
body := `<!doctype html><body style="margin:0;background:rgb(255,0,0)"><a style="display:block;width:300px;height:100px;background:blue;color:white" href="http://fixture.invalid/next">Synthetic next page</a><h1>Private fixture</h1>`
if strings.HasPrefix(requests[id], "GET /next ") {
body = `<!doctype html><body style="margin:0;background:rgb(0,255,0)"><h1>Second synthetic page</h1>`
}
response := fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(body), body)
websocket.Message.Send(ws, packet(2, id, []byte(response)))
websocket.Message.Send(ws, packet(4, id, []byte{2}))
delete(requests, id)
case 4:
delete(requests, id)
}
}
}).ServeHTTP(w, r)
}))
defer backend.Close()
s, token, domain, actor, source := feedFixture(t)
configureTestGecko(t, s, domain, backend.URL)
p, e := s.store.TaskPreferences(context.Background(), domain, actor)
if e != nil {
t.Fatal(e)
}
p.RSSLinkMode = "gecko"
if _, e = s.store.SaveTaskPreferences(context.Background(), domain, actor, p, time.Now()); e != nil {
t.Fatal(e)
}
claim, e := s.store.ClaimFeed(context.Background(), source)
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(context.Background(), claim, []store.FeedItem{{ID: "synthetic", Title: "Gecko synthetic fixture", URL: "http://fixture.invalid/first", Body: "No external network access."}}, "", "", "", false); e != nil {
t.Fatal(e)
}
handler := s.handler()
web := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Origin") == "http://"+r.Host {
r.Header.Set("Origin", "http://localhost")
}
r.Host = "localhost"
r.Header.Set("Authorization", "Bearer "+token)
handler.ServeHTTP(w, r)
}))
defer web.Close()
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second)
defer cancel()
out, e := exec.CommandContext(ctx, "node", "internal/server/testdata/feed-gecko-browser.mjs", web.URL+"/v/feeds", t.TempDir(), "/tmp/pageswiki-feed-gecko.png").CombinedOutput()
if e != nil {
t.Fatalf("browser: %v\n%s", e, out)
}
io.WriteString(os.Stdout, string(out))
for i := 0; i < 30 && connections.Load() != 0; i++ {
time.Sleep(100 * time.Millisecond)
}
if connections.Load() != 0 {
t.Fatal("viewer close left relay session alive")
}
}
+190
View File
@@ -0,0 +1,190 @@
package server
import (
"context"
"encoding/binary"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"golang.org/x/net/websocket"
"pageswiki/internal/geckorelay"
)
const geckoTestKey = "synthetic-server-relay-key-at-least-32-bytes"
func configureTestGecko(t *testing.T, s *Server, domain, origin string) {
t.Helper()
path := filepath.Join(t.TempDir(), "key")
if err := os.WriteFile(path, []byte(geckoTestKey), 0600); err != nil {
t.Fatal(err)
}
t.Setenv("PAGESWIKI_GECKO_RELAY", origin)
t.Setenv("PAGESWIKI_GECKO_KEY_FILE", path)
if err := s.setSystemExtensionEnabled(domain, "viewer.gecko", true); err != nil {
t.Fatal(err)
}
}
func TestFeedGeckoAuthorizationAndHeaders(t *testing.T) {
s, token, domain, actor, _ := feedFixture(t)
if s.systemExtensionEnabled(domain, "viewer.gecko") {
t.Fatal("experimental relay enabled by default")
}
for _, raw := range []string{"bad json", `{"organizer.feeds":true}`} {
if err := s.store.SetConfigInDomain(domain, systemExtensionStateKey, raw); err != nil {
t.Fatal(err)
}
if s.systemExtensionEnabled(domain, "viewer.gecko") {
t.Fatal("invalid/absent config enabled relay")
}
}
var requests atomic.Int64
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests.Add(1)
g, err := geckorelay.Verify(geckoTestKey, r.Header.Get(geckorelay.GrantHeader), time.Now())
if err != nil || g.Subject != domain+":"+actor {
t.Error("missing bound grant")
}
for _, h := range []string{"Cookie", "Authorization", "X-Forwarded-For", "Forwarded", "Referer"} {
if r.Header.Get(h) != "" {
t.Error("credential/header leaked", h)
}
}
if r.URL.Path != "/assets/v0.0.1/gecko.js" {
t.Error("path", r.URL.Path)
}
w.Header().Set("Set-Cookie", "forbidden=1")
w.Header().Set("Cache-Control", "private, max-age=86400, immutable")
io.WriteString(w, "export const fixture=true;")
}))
defer upstream.Close()
configureTestGecko(t, s, domain, upstream.URL)
r := httptest.NewRequest("GET", "http://localhost"+geckoAPI+"assets/v0.0.1/gecko.js", nil)
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Cookie", "private=secret")
r.Header.Set("X-Forwarded-For", "secret")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code != 200 || requests.Load() != 1 || w.Header().Get("Set-Cookie") != "" {
t.Fatal(w.Code, w.Body)
}
if len(w.Header().Values("Cache-Control")) != 1 || strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
t.Fatal("asset cache disabled", w.Header())
}
for _, tc := range []struct {
path, method string
code int
}{{"", "GET", 200}, {"wisp/", "GET", 403}, {"assets/v0.0.1/gecko.js?target=https://example.org", "GET", 400}, {"unknown", "GET", 404}, {"assets/v0.0.1/gecko.js", "POST", 400}} {
w = feedRequestTest(s, token, tc.method, "browser/"+tc.path, nil)
if w.Code != tc.code {
t.Fatalf("%s %s: %d %s", tc.method, tc.path, w.Code, w.Body)
}
if tc.path == "" && w.Header().Get("Cross-Origin-Embedder-Policy") != "require-corp" {
t.Fatal("not isolated")
}
}
if w = feedRequestTest(s, "", "GET", "browser/assets/v0.0.1/gecko.js", nil); w.Code == 200 {
t.Fatal("anonymous")
}
for _, origin := range []string{"https://evil.example", "null", "http://localhost.evil.example"} {
r := httptest.NewRequest("GET", "http://localhost"+geckoAPI+"wisp/", nil)
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Origin", origin)
r.Header.Set("Upgrade", "websocket")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code != 403 {
t.Fatal("cross-site websocket", origin, w.Code)
}
}
prefs, err := s.store.TaskPreferences(context.Background(), domain, actor)
if err != nil {
t.Fatal(err)
}
prefs.RSSLinkMode = "gecko"
if _, err = s.store.SaveTaskPreferences(context.Background(), domain, actor, prefs, time.Now()); err != nil {
t.Fatal(err)
}
page := func() *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", "http://localhost/v/feeds", nil)
r.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
w = page()
if w.Code != 200 || w.Header().Get("Cross-Origin-Opener-Policy") != "same-origin" || !strings.Contains(w.Body.String(), `data-link-mode="gecko"`) {
t.Fatal("isolated page", w.Code)
}
if err = s.setSystemExtensionEnabled(domain, "viewer.gecko", false); err != nil {
t.Fatal(err)
}
w = page()
if w.Header().Get("Cross-Origin-Opener-Policy") != "" || !strings.Contains(w.Body.String(), `data-link-mode="tab"`) {
t.Fatal("disabled fallback")
}
if w = feedRequestTest(s, token, "GET", "browser/", nil); w.Code != 404 {
t.Fatal("disabled shell", w.Code)
}
if err = s.setSystemExtensionEnabled(domain, "viewer.gecko", true); err != nil {
t.Fatal(err)
}
if _, err = s.store.DB.Exec(`UPDATE principals SET state='readonly' WHERE uuid=?`, actor); err != nil {
t.Fatal(err)
}
if w = feedRequestTest(s, token, "GET", "browser/", nil); w.Code != 404 {
t.Fatal("readonly browser", w.Code)
}
}
func TestFeedGeckoWebsocketRevocation(t *testing.T) {
for _, disable := range []bool{false, true} {
t.Run(map[bool]string{false: "logout", true: "disable"}[disable], func(t *testing.T) {
s, token, domain, _, _ := feedFixture(t)
relay, err := geckorelay.New(geckoTestKey, t.TempDir())
if err != nil {
t.Fatal(err)
}
upstream := httptest.NewServer(relay)
defer upstream.Close()
configureTestGecko(t, s, domain, upstream.URL)
web := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "localhost"
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer web.Close()
cfg, _ := websocket.NewConfig("ws"+strings.TrimPrefix(web.URL, "http")+geckoAPI+"wisp/", "http://localhost")
cfg.Header.Set("Authorization", "Bearer "+token)
conn, err := websocket.DialConfig(cfg)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(12 * time.Second))
var frame []byte
if err = websocket.Message.Receive(conn, &frame); err != nil || len(frame) != 9 || frame[0] != 3 || binary.LittleEndian.Uint32(frame[1:]) != 0 {
t.Fatal(frame, err)
}
if disable {
err = s.setSystemExtensionEnabled(domain, "viewer.gecko", false)
} else {
err = s.store.RevokeAuthSession(token)
}
if err != nil {
t.Fatal(err)
}
start := time.Now()
err = websocket.Message.Receive(conn, &frame)
if err == nil || time.Since(start) > 10*time.Second {
t.Fatal("session not promptly closed", err)
}
})
}
}
+56
View File
@@ -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
}
+260
View File
@@ -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)
}
}
+275
View File
@@ -0,0 +1,275 @@
package server
import (
"encoding/xml"
"io"
"net/http"
"strings"
"pageswiki/internal/store"
)
const feedOPMLMaxBytes = 512 << 10
type feedOPMLRow struct {
store.FeedSubscription
Index int `json:"index"`
Status string `json:"status"`
}
// Parse only the uploaded document. OPML includes and HTML links are never
// fetched. Every token participates in the depth/outline/DTD checks.
func parseFeedOPML(raw string) ([]feedOPMLRow, error) {
if len(raw) > feedOPMLMaxBytes {
return nil, store.ErrTaskDAVLimit
}
d := xml.NewDecoder(strings.NewReader(strings.TrimPrefix(raw, "\ufeff")))
// JSON and the browser's File.text() carry UTF-8, not the original legacy
// bytes. Reject other XML encodings instead of silently corrupting names.
type frame struct {
name xml.Name
folder string
commented bool
}
stack := []frame{}
rows := []feedOPMLRow{}
root, head, body, outlines := 0, 0, 0, 0
for {
token, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return nil, store.ErrTaskInvalid
}
switch t := token.(type) {
case xml.Directive:
return nil, store.ErrTaskInvalid
case xml.CharData:
if len(stack) == 0 && strings.TrimSpace(string(t)) != "" {
return nil, store.ErrTaskInvalid
}
case xml.StartElement:
if len(stack) >= 16 {
return nil, store.ErrTaskDAVLimit
}
attrs := map[string]string{}
seen := map[xml.Name]bool{}
for _, a := range t.Attr {
if seen[a.Name] {
return nil, store.ErrTaskInvalid
}
seen[a.Name] = true
if a.Name.Space == "" {
attrs[a.Name.Local] = a.Value
}
}
f := frame{name: t.Name}
if len(stack) == 0 {
root++
if root != 1 || t.Name.Space != "" || t.Name.Local != "opml" || (attrs["version"] != "1.0" && attrs["version"] != "1.1" && attrs["version"] != "2.0") {
return nil, store.ErrTaskInvalid
}
} else {
parent := stack[len(stack)-1]
f.folder = parent.folder
f.commented = parent.commented
if len(stack) == 1 && t.Name.Space == "" {
switch t.Name.Local {
case "head":
head++
if head != 1 || body != 0 {
return nil, store.ErrTaskInvalid
}
case "body":
body++
if head != 1 || body != 1 {
return nil, store.ErrTaskInvalid
}
default:
return nil, store.ErrTaskInvalid
}
}
inBody := len(stack) >= 2 && stack[1].name == (xml.Name{Local: "body"})
if inBody && t.Name.Space == "" && parent.name.Space == "" && (parent.name.Local == "body" || parent.name.Local == "outline") && t.Name.Local != "outline" {
return nil, store.ErrTaskInvalid
}
if inBody && t.Name.Space == "" && t.Name.Local == "outline" && parent.name.Space == "" && (parent.name.Local == "body" || parent.name.Local == "outline") {
outlines++
if outlines > 1000 {
return nil, store.ErrTaskDAVLimit
}
f.commented = f.commented || strings.EqualFold(attrs["isComment"], "true")
label := attrs["text"]
if label == "" {
label = attrs["title"]
}
label = feedText(label, 1024)
kind := strings.ToLower(attrs["type"])
if attrs["xmlUrl"] != "" || kind != "" {
if len(rows) >= 500 {
return nil, store.ErrTaskDAVLimit
}
row := feedOPMLRow{FeedSubscription: store.FeedSubscription{Title: label, Folder: f.folder, URL: strings.TrimSpace(attrs["xmlUrl"])}, Index: len(rows), Status: "add"}
switch {
case f.commented:
row.Status = "commented"
case kind != "" && kind != "rss" && kind != "atom":
row.Status = "unsupported_type"
case !validCalendarSourceURL(row.URL):
row.Status = "unsupported_url"
case label == "" || len(label) > 200 || len(f.folder) > 200:
row.Status = "invalid_fields"
}
rows = append(rows, row)
} else if label != "" {
if f.folder != "" {
f.folder += " / "
}
f.folder += label
}
}
}
stack = append(stack, f)
case xml.EndElement:
if len(stack) == 0 {
return nil, store.ErrTaskInvalid
}
stack = stack[:len(stack)-1]
}
}
if root != 1 || head != 1 || body != 1 || len(stack) != 0 {
return nil, store.ErrTaskInvalid
}
return rows, nil
}
func classifyFeedOPML(rows []feedOPMLRow, sources []store.FeedSource) {
existing := map[string]bool{}
seen := map[string]bool{}
for _, s := range sources {
if s.Provider == "local" {
existing[store.FeedURLKey(s.URL)] = true
}
}
for i := range rows {
row := &rows[i]
if row.Status != "add" {
continue
}
key := store.FeedURLKey(row.URL)
if existing[key] {
row.Status = "existing"
} else if seen[key] {
row.Status = "duplicate"
}
seen[key] = true
}
}
type feedOPMLOutline struct {
Text string `xml:"text,attr"`
Title string `xml:"title,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
URL string `xml:"xmlUrl,attr,omitempty"`
Children []feedOPMLOutline `xml:"outline"`
}
func encodeFeedOPML(sources []store.FeedSource) ([]byte, error) {
var doc struct {
XMLName xml.Name `xml:"opml"`
Version string `xml:"version,attr"`
Title string `xml:"head>title"`
Outlines []feedOPMLOutline `xml:"body>outline"`
}
doc.Version = "2.0"
doc.Title = "PagesWiki RSS"
groups := map[string]int{}
for _, source := range sources {
if source.Provider != "local" {
continue
}
entry := feedOPMLOutline{Text: source.Title, Title: source.Title, Type: "rss", URL: source.URL}
if source.Folder == "" {
doc.Outlines = append(doc.Outlines, entry)
continue
}
i, ok := groups[source.Folder]
if !ok {
i = len(doc.Outlines)
groups[source.Folder] = i
doc.Outlines = append(doc.Outlines, feedOPMLOutline{Text: source.Folder})
}
doc.Outlines[i].Children = append(doc.Outlines[i].Children, entry)
}
raw, err := xml.MarshalIndent(doc, "", " ")
return append([]byte(xml.Header), raw...), err
}
func (s *Server) handleFeedOPML(w http.ResponseWriter, r *http.Request, domain, actor, key string) {
if key == "opml/export" && r.Method == http.MethodGet {
sources, err := s.store.FeedSources(r.Context(), domain, actor)
if err != nil {
taskError(w, err)
return
}
raw, err := encodeFeedOPML(sources)
if err != nil {
taskError(w, err)
return
}
w.Header().Set("Content-Type", "text/x-opml; charset=utf-8")
w.Header().Set("Content-Disposition", `attachment; filename="pageswiki-feeds.opml"`)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Write(raw)
return
}
if r.Method != http.MethodPost || (key != "opml/preview" && key != "opml/import") {
http.NotFound(w, r)
return
}
var input struct {
OPML string `json:"opml"`
Selected []int `json:"selected"`
RequestID string `json:"request_id"`
Expected string `json:"expected"`
Enabled bool `json:"enabled"`
Interval int `json:"interval_seconds"`
}
if !taskJSONLimit(w, r, &input, 2<<20) {
return
}
rows, err := parseFeedOPML(input.OPML)
if err != nil {
taskError(w, err)
return
}
if key == "opml/preview" {
sources, err := s.store.FeedSources(r.Context(), domain, actor)
if err != nil {
taskError(w, err)
return
}
classifyFeedOPML(rows, sources)
writeEventJSON(w, 200, map[string]any{"rows": rows, "expected": store.FeedSourcesSnapshot(sources), "available": 32 - len(sources)})
return
}
// Reparse on apply; client-supplied titles, URLs or row eligibility are not trusted.
classifyFeedOPML(rows, nil)
seen := map[int]bool{}
plan := store.FeedImport{RequestID: input.RequestID, Expected: input.Expected, Enabled: input.Enabled, Interval: input.Interval}
for _, index := range input.Selected {
if index < 0 || index >= len(rows) || seen[index] || rows[index].Status != "add" {
taskError(w, store.ErrTaskInvalid)
return
}
seen[index] = true
plan.Entries = append(plan.Entries, rows[index].FeedSubscription)
}
ids, err := s.store.ImportFeedSubscriptions(r.Context(), domain, actor, plan)
if err != nil {
taskError(w, err)
return
}
writeEventJSON(w, 201, map[string]any{"source_ids": ids})
}
+240
View File
@@ -0,0 +1,240 @@
package server
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"testing"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func opmlDoc(outlines string) string {
return `<?xml version="1.0"?><opml version="2.0"><head><title>Test</title></head><body>` + outlines + `</body></opml>`
}
func opmlEntry(name, link string) string {
return `<outline text="` + name + `" type="rss" xmlUrl="` + link + `"/>`
}
func TestFeedOPMLParserAndExport(t *testing.T) {
for _, version := range []string{"1.0", "1.1", "2.0"} {
raw := strings.Replace(opmlDoc(`<outline text="技術"><outline text="Go">`+opmlEntry("A &amp; B", "https://example.org/feed?a=1&amp;b=2")+`</outline></outline>`), `version="2.0"`, `version="`+version+`"`, 1)
rows, err := parseFeedOPML(raw)
if err != nil || len(rows) != 1 || rows[0].Title != "A & B" || rows[0].Folder != "技術 / Go" || rows[0].URL != "https://example.org/feed?a=1&b=2" || rows[0].Status != "add" {
t.Fatal(rows, err)
}
}
for _, bad := range []string{
strings.Replace(opmlDoc(""), `version="1.0"?>`, `version="1.0" encoding="iso-8859-1"?>`, 1),
`<!DOCTYPE opml [<!ENTITY external SYSTEM "file:///etc/passwd">]>` + opmlDoc(""),
opmlDoc("") + opmlDoc(""), opmlDoc("") + "garbage", `<opml version="2.0"><body/></opml>`,
opmlDoc(`<outline text="x" text="y"/>`), opmlDoc(strings.Repeat(`<outline text="deep">`, 20) + strings.Repeat(`</outline>`, 20)),
opmlDoc(strings.Repeat(opmlEntry("x", "https://example.org"), 501)), strings.Repeat("x", feedOPMLMaxBytes+1),
opmlDoc(`<unexpected>` + opmlEntry("x", "https://example.org") + `</unexpected>`),
} {
if _, err := parseFeedOPML(bad); err == nil {
t.Fatal("accepted invalid OPML")
}
}
sources := []store.FeedSource{{Provider: "local", Title: "A & B", Folder: "技術 / Go", URL: "https://example.org/feed?a=1&b=2"}, {Provider: "local", Title: "Paused", Enabled: false, URL: "https://example.org/paused"}, {Provider: "freshrss", Title: "Remote", URL: "https://private.example.org/api/greader.php", Credential: "secret"}}
raw, err := encodeFeedOPML(sources)
if err != nil || strings.Contains(string(raw), "private.example.org") || strings.Contains(string(raw), "secret") {
t.Fatal("export leak", err)
}
rows, err := parseFeedOPML(string(raw))
if err != nil || len(rows) != 2 || rows[0].Folder != "技術 / Go" || rows[0].Title != "A & B" {
t.Fatal("roundtrip", string(raw), rows, err)
}
raw, err = encodeFeedOPML(nil)
if err != nil {
t.Fatal(err)
}
if rows, err = parseFeedOPML(string(raw)); err != nil || len(rows) != 0 {
t.Fatal("empty export", string(raw), err)
}
}
type opmlPreview struct {
Rows []feedOPMLRow `json:"rows"`
Expected string `json:"expected"`
Available int `json:"available"`
}
func previewOPML(t *testing.T, s *Server, token, raw string) opmlPreview {
t.Helper()
w := feedRequestTest(s, token, "POST", "opml/preview", map[string]string{"opml": raw})
var preview opmlPreview
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &preview) != nil {
t.Fatal(w.Code, w.Body)
}
return preview
}
func TestFeedOPMLPreviewImportReplayAndOwnership(t *testing.T) {
s, token, domain, actor, source := feedFixture(t)
raw := opmlDoc(`<outline text="日本語">` + opmlEntry("New", "https://example.org/new") + opmlEntry("Existing", "https://EXAMPLE.org:443/feed.xml") + opmlEntry("Duplicate", "https://example.org/new") + opmlEntry("Private", "https://127.0.0.1/feed") + `<outline text="Skipped" isComment="true">` + opmlEntry("Hidden", "https://example.org/hidden") + `</outline><outline type="include" text="Include" url="https://example.org/external.opml"/>` + opmlEntry("", "https://example.org/empty-title") + `</outline>`)
p := previewOPML(t, s, token, raw)
if p.Available != 31 || len(p.Rows) != 7 {
t.Fatal(p)
}
for i, want := range []string{"add", "existing", "duplicate", "unsupported_url", "commented", "unsupported_type", "invalid_fields"} {
if p.Rows[i].Status != want {
t.Fatal(i, p.Rows[i])
}
}
all, _ := s.store.FeedSources(context.Background(), domain, actor)
if len(all) != 1 {
t.Fatal("preview wrote sources")
}
for _, index := range []int{2, 3, 4, 5, 6} {
w := feedRequestTest(s, token, "POST", "opml/import", map[string]any{"opml": raw, "expected": p.Expected, "selected": []int{index}, "request_id": uuid.NewString(), "interval_seconds": 1800})
if w.Code != 422 {
t.Fatal("ineligible row accepted", index, w.Code)
}
}
input := map[string]any{"opml": raw, "expected": p.Expected, "selected": []int{0}, "request_id": uuid.NewString(), "enabled": false, "interval_seconds": 1800}
w := feedRequestTest(s, token, "POST", "opml/import", input)
if w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
original := w.Body.String()
if retry := feedRequestTest(s, token, "POST", "opml/import", input); retry.Code != 201 || retry.Body.String() != original {
t.Fatal("retry", retry.Code, retry.Body)
}
input["enabled"] = true
if retry := feedRequestTest(s, token, "POST", "opml/import", input); retry.Code != 412 {
t.Fatal("changed retry", retry.Code)
}
input["enabled"] = false
all, _ = s.store.FeedSources(context.Background(), domain, actor)
if len(all) != 2 {
t.Fatal("duplicate import", len(all))
}
for _, v := range all {
if v.UUID != source.UUID {
if v.Enabled || v.Title != "New" || v.Folder != "日本語" {
t.Fatal(v)
}
if err := s.store.FeedSourceAction(context.Background(), domain, actor, v.UUID, "delete", v.Version); err != nil {
t.Fatal(err)
}
}
}
if retry := feedRequestTest(s, token, "POST", "opml/import", input); retry.Code != 201 || retry.Body.String() != original {
t.Fatal("deleted retry", retry.Code, retry.Body)
}
all, _ = s.store.FeedSources(context.Background(), domain, actor)
if len(all) != 1 {
t.Fatal("resurrected source")
}
w = feedRequestTest(s, token, "GET", "opml/export", nil)
if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Disposition"), "attachment") || !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
t.Fatal(w.Code, w.Header())
}
other, err := s.store.CreatePasswordAccount(domain, "opmlother", "long test password", "Other", "")
if err != nil {
t.Fatal(err)
}
otherToken, err := s.store.CreateAuthSession(domain, other.UUID)
if err != nil {
t.Fatal(err)
}
w = feedRequestTest(s, otherToken, "GET", "opml/export", nil)
if w.Code != 200 || strings.Contains(w.Body.String(), source.URL) {
t.Fatal("owner leak", w.Code, w.Body)
}
if w = feedRequestTest(s, "", "GET", "opml/export", nil); w.Code == 200 {
t.Fatal("anonymous export")
}
if err = s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
t.Fatal(err)
}
token, err = s.store.CreateAuthSession(domain, actor)
if err != nil {
t.Fatal(err)
}
if w = feedRequestTest(s, token, "GET", "opml/export", nil); w.Code != 200 {
t.Fatal("readonly export", w.Code)
}
if w = feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 403 {
t.Fatal("readonly mutation", w.Code)
}
if err = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); err != nil {
t.Fatal(err)
}
if w = feedRequestTest(s, token, "GET", "opml/export", nil); w.Code != 404 {
t.Fatal("disabled export", w.Code)
}
}
func TestFeedOPMLAtomicityLimitsAndConcurrentApply(t *testing.T) {
s, token, domain, actor, source := feedFixture(t)
raw := opmlDoc(opmlEntry("A", "https://example.org/a") + opmlEntry("B", "https://example.org/b"))
p := previewOPML(t, s, token, raw)
input := map[string]any{"opml": raw, "expected": p.Expected, "selected": []int{0, 1}, "request_id": uuid.NewString(), "interval_seconds": 1800}
if _, err := s.store.DB.Exec(`CREATE TRIGGER fail_feed_import BEFORE INSERT ON feed_import_receipts BEGIN SELECT RAISE(ABORT,'synthetic failure'); END`); err != nil {
t.Fatal(err)
}
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 500 {
t.Fatal(w.Code, w.Body)
}
all, _ := s.store.FeedSources(context.Background(), domain, actor)
if len(all) != 1 {
t.Fatal("partial source insert")
}
if _, err := s.store.DB.Exec(`DROP TRIGGER fail_feed_import`); err != nil {
t.Fatal(err)
}
// A configuration edit invalidates the preview; neither new source is applied.
source.Title = "Changed"
if _, err := s.store.SaveFeedSource(context.Background(), domain, actor, source); err != nil {
t.Fatal(err)
}
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 412 {
t.Fatal("stale preview", w.Code)
}
p = previewOPML(t, s, token, raw)
input["expected"] = p.Expected
var wg sync.WaitGroup
codes := make(chan int, 2)
for range 2 {
wg.Add(1)
go func() { defer wg.Done(); codes <- feedRequestTest(s, token, "POST", "opml/import", input).Code }()
}
wg.Wait()
close(codes)
for code := range codes {
if code != 201 {
t.Fatal("concurrent retry", code)
}
}
all, _ = s.store.FeedSources(context.Background(), domain, actor)
if len(all) != 3 {
t.Fatal("duplicate concurrent import")
}
entries := ""
selected := []int{}
for i := 0; i < 32; i++ {
entries += opmlEntry(fmt.Sprint(i), fmt.Sprintf("https://example.org/large/%d", i))
selected = append(selected, i)
}
raw = opmlDoc(entries)
p = previewOPML(t, s, token, raw)
input = map[string]any{"opml": raw, "expected": p.Expected, "selected": selected, "request_id": uuid.NewString(), "interval_seconds": 1800}
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 507 {
t.Fatal("capacity", w.Code, w.Body)
}
all, _ = s.store.FeedSources(context.Background(), domain, actor)
if len(all) != 3 {
t.Fatal("partial limit import")
}
for _, selection := range [][]int{{-1}, {99}, {0, 0}, {}} {
input["selected"] = selection
if w := feedRequestTest(s, token, "POST", "opml/import", input); w.Code != 422 {
t.Fatal("invalid selection", selection, w.Code)
}
}
}
+653
View File
@@ -0,0 +1,653 @@
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func feedFixture(t *testing.T) (*Server, string, string, string, store.FeedSource) {
t.Helper()
s, token, domain, actor := taskFixture(t)
source, e := s.store.SaveFeedSource(context.Background(), domain, actor, store.FeedSource{UUID: uuid.NewString(), Provider: "local", Title: "Synthetic news", Folder: "Tech", URL: "https://example.org/feed.xml", Enabled: true, Interval: 300})
if e != nil {
t.Fatal(e)
}
sources, e := s.store.FeedSources(context.Background(), domain, actor)
if e != nil || len(sources) != 1 {
t.Fatal(e)
}
source = sources[0]
return s, token, domain, actor, source
}
func feedRequestTest(s *Server, token, method, key string, body any) *httptest.ResponseRecorder {
raw, _ := json.Marshal(body)
r := httptest.NewRequest(method, "http://localhost"+feedsAPI+key, strings.NewReader(string(raw)))
r.Header.Set("Content-Type", "application/json")
if token != "" {
r.Header.Set("Authorization", "Bearer "+token)
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
func TestFeedReaderStateIsolationAndFencing(t *testing.T) {
s, token, domain, actor, source := feedFixture(t)
ctx := context.Background()
claim, e := s.store.ClaimFeed(ctx, source)
if e != nil {
t.Fatal(e)
}
if _, e = s.store.ClaimFeed(ctx, source); !errors.Is(e, store.ErrTaskConflict) {
t.Fatal("double claim", e)
}
item := store.FeedItem{ID: "one", Title: "日本語 news", Body: "safe text", URL: "https://example.org/one", PublishedMS: 1}
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{item}, "e1", "", "", false); e != nil {
t.Fatal(e)
}
if w := feedRequestTest(s, token, "GET", "items", nil); w.Code != 200 || !strings.Contains(w.Body.String(), "日本語") {
t.Fatal(w.Code, w.Body)
}
if w := feedRequestTest(s, "", "GET", "items", nil); w.Code == 200 {
t.Fatal("anonymous")
}
if _, e = s.store.FeedItem(ctx, domain, uuid.NewString(), source.UUID, "one"); !errors.Is(e, store.ErrAccessDenied) {
t.Fatal(e)
}
state := map[string]any{"source": source.UUID, "id": "one", "read": true, "starred": true, "version": 0}
if w := feedRequestTest(s, token, "POST", "state", state); w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
if w := feedRequestTest(s, token, "POST", "state", state); w.Code != 412 {
t.Fatal("stale state", w.Code, w.Body)
}
got, e := s.store.FeedItem(ctx, domain, actor, source.UUID, "one")
if e != nil || !got.Read || !got.Starred || got.Version != 1 {
t.Fatal(got, e)
}
if e = s.store.CompleteFeed(ctx, claim, nil, "", "", "", false); !errors.Is(e, store.ErrTaskConflict) {
t.Fatal("old completion", e)
}
// Re-fetch must preserve local read/star; config edits invalidate in-flight work.
source.Version = 1
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
if e != nil {
t.Fatal(e)
}
sources, _ := s.store.FeedSources(ctx, domain, actor)
claim, e = s.store.ClaimFeed(ctx, sources[0])
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{item}, "", "", "", false); e != nil {
t.Fatal(e)
}
got, _ = s.store.FeedItem(ctx, domain, actor, source.UUID, "one")
if !got.Read || !got.Starred {
t.Fatal("lost state")
}
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
if e != nil {
t.Fatal(e)
}
sources, _ = s.store.FeedSources(ctx, domain, actor)
claim, e = s.store.ClaimFeed(ctx, sources[0])
if e != nil {
t.Fatal(e)
}
source.Enabled = false
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{item}, "", "", "", false); !errors.Is(e, store.ErrTaskConflict) {
t.Fatal("disabled late commit", e)
}
if e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil {
t.Fatal(e)
}
if _, e = s.store.FeedItems(ctx, domain, actor, "", "", "", -1, ""); !errors.Is(e, store.ErrAccessDenied) {
t.Fatal(e)
}
if w := feedRequestTest(s, token, "GET", "items", nil); w.Code != 404 {
t.Fatal(w.Code)
}
}
func TestFeedParserSafetyAndFormats(t *testing.T) {
rss := `<?xml version="1.0"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>News</title><item><guid>id</guid><title>日本語</title><link>/one</link><content:encoded><![CDATA[<p>Hello</p><script>alert(1)</script><img src="https://evil.invalid/pixel"><b>world</b>]]></content:encoded><pubDate>Fri, 06 Nov 2026 10:00:00 +0000</pubDate></item></channel></rss>`
items, e := parseFeed([]byte(rss), "https://example.org/feed")
if e != nil || len(items) != 1 || items[0].URL != "https://example.org/one" || strings.Contains(items[0].Body, "alert") || strings.Contains(items[0].Body, "pixel") || items[0].PublishedMS == 0 {
t.Fatal(items, e)
}
atom := `<feed xmlns="http://www.w3.org/2005/Atom"><title>Atom</title><entry><id>urn:one</id><title>One</title><link href="https://example.org/one"/><updated>2026-11-01T10:00:00Z</updated><content type="html">&lt;p&gt;Body&lt;/p&gt;</content></entry></feed>`
items, e = parseFeed([]byte(atom), "https://example.org")
if e != nil || len(items) != 1 || items[0].Body != "Body" {
t.Fatal(items, e)
}
for _, raw := range []string{`<!DOCTYPE rss [<!ENTITY x SYSTEM "file:///etc/passwd">]><rss/>`, `<html>error</html>`, `<rss/><rss/>`, `<rss><channel><item><guid>x</guid></item><item><guid>x</guid></item></channel></rss>`, strings.Repeat("x", (4<<20)+1)} {
if _, e = parseFeed([]byte(raw), "https://example.org"); e == nil {
t.Fatal("accepted malformed feed")
}
}
if feedLink("https://example.org", "javascript:alert(1)") != "" || feedLink("https://example.org", "https://user:pass@example.org") != "" {
t.Fatal("unsafe link")
}
items, e = parseFeed([]byte(`<rss><channel><item><title>No ID or link</title><description>First body</description></item><item><title>No ID or link</title><description>Second body</description></item></channel></rss>`), "https://example.org/feed")
if e != nil || len(items) != 2 || items[0].ID == items[1].ID || items[0].URL != "" {
t.Fatal("missing IDs must not collapse to the feed URL", items, e)
}
}
func TestFreshRSSReadOnlyContract(t *testing.T) {
requests := []string{}
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests = append(requests, r.Method+" "+r.URL.Path)
switch r.URL.Path {
case "/api/greader.php/accounts/ClientLogin":
if r.Method != "POST" {
t.Error("login method")
}
r.ParseForm()
if r.Form.Get("Email") != "reader" || r.Form.Get("Passwd") != "api-secret" {
t.Error("login form")
}
fmt.Fprint(w, "SID=x\nAuth=reader/token\n")
case "/api/greader.php/reader/api/0/stream/contents/reading-list":
if r.Header.Get("Authorization") != "GoogleLogin auth=reader/token" || r.URL.Query().Get("n") != "200" {
t.Error("request contract")
}
fmt.Fprint(w, `{"items":[{"id":"tag:1","title":"Title","summary":{"content":"<p>Text</p>"},"published":1793500000,"categories":["user/reader/state/com.google/read","user/reader/state/com.google/starred","user/reader/label/Tech"],"origin":{"title":"Feed"},"alternate":[{"href":"https://example.org/article"}]}]}`)
default:
t.Error("unexpected mutation", r.URL.Path)
w.WriteHeader(404)
}
}))
defer remote.Close()
items, e := fetchFreshRSS(context.Background(), remote.Client(), store.FeedSource{URL: remote.URL + "/api/greader.php"}, "reader", "api-secret")
if e != nil || len(items) != 1 || !items[0].Read || !items[0].Starred || items[0].Folder != "Tech" || items[0].Body != "Text" || items[0].BodyHTML != "<p>Text</p>" || len(requests) != 2 {
t.Fatal(items, e, requests)
}
for _, bad := range []string{"http://example.org/feed", "https://127.0.0.1/feed", "https://10.0.0.1/feed", "https://example.org:8443/feed", "https://user:pass@example.org/feed"} {
if validCalendarSourceURL(bad) {
t.Fatal("SSRF URL", bad)
}
}
}
func TestFeedHTMLFormattingAndIsolation(t *testing.T) {
raw := `<h2>Title</h2><p class="x" style="position:fixed" onclick="bad()"><strong>Bold</strong></p><table><tr><td colspan="2">Cell</td></tr></table><a href="../story" target="_top">Story</a><a href="javascript:bad()">bad</a><script>bad()</script><style>@import '/__feed_leak';</style><form action="/__feed_leak"><input autofocus></form><img src="/__feed_leak"><iframe src="/__feed_leak"></iframe><svg onload="bad()"></svg>`
clean := feedHTML(raw, "https://example.org/news/feed.xml")
for _, want := range []string{`<h2>Title</h2>`, `<strong>Bold</strong>`, `<table>`, `colspan="2"`, `href="https://example.org/story"`, `target="_blank"`, `noopener`} {
if !strings.Contains(clean, want) {
t.Fatal("format lost", want, clean)
}
}
for _, bad := range []string{"<script", "<style", "<form", "<input", "<img", "<iframe", "<svg", "onclick", "javascript:", "position:", "_top", "__feed_leak"} {
if strings.Contains(clean, bad) {
t.Fatal("unsafe markup", bad, clean)
}
}
if feedHTML(clean, "https://example.org/news/feed.xml") != clean {
t.Fatal("sanitizer not stable")
}
if feedHTML(strings.Repeat("<p>x</p>", 17000), "https://example.org") != "" {
t.Fatal("HTML cap")
}
for _, kind := range []string{"text", "html", "xhtml"} {
content := `&lt;b&gt;bold&lt;/b&gt;`
if kind == "xhtml" {
content = `<div xmlns="http://www.w3.org/1999/xhtml"><b>bold</b></div>`
}
items, e := parseFeed([]byte(`<feed xmlns="http://www.w3.org/2005/Atom"><entry><id>x</id><content type="`+kind+`">`+content+`</content></entry></feed>`), "https://example.org")
if e != nil || len(items) != 1 {
t.Fatal(e)
}
if kind == "text" {
if items[0].BodyHTML != "" || items[0].Body != "<b>bold</b>" {
t.Fatal("literal Atom text", items)
}
} else if !strings.Contains(items[0].BodyHTML, "<b>bold</b>") {
t.Fatal("Atom markup", kind, items)
}
}
}
func TestFeedPaginationFailureAndDelete(t *testing.T) {
s, token, domain, actor, source := feedFixture(t)
ctx := context.Background()
claim, e := s.store.ClaimFeed(ctx, source)
if e != nil {
t.Fatal(e)
}
items := []store.FeedItem{}
for i := 0; i < 205; i++ {
items = append(items, store.FeedItem{ID: fmt.Sprintf("id-%03d", i), Title: "Title", Body: "Body", PublishedMS: 100})
}
if e = s.store.CompleteFeed(ctx, claim, items, "old", "", "", false); e != nil {
t.Fatal(e)
}
seen := map[string]bool{}
before := int64(-1)
key := ""
for {
batch, e := s.store.FeedItems(ctx, domain, actor, "", "", "", before, key)
if e != nil {
t.Fatal(e)
}
more := len(batch) > 100
if more {
batch = batch[:100]
}
for _, item := range batch {
if seen[item.ID] {
t.Fatal("duplicate")
}
seen[item.ID] = true
}
if !more {
break
}
last := batch[len(batch)-1]
before = last.PublishedMS
key = last.Source + ":" + last.ID
}
if len(seen) != 205 {
t.Fatal(len(seen))
}
source, e = s.store.SaveFeedSource(ctx, domain, actor, source)
if e != nil {
t.Fatal(e)
}
sources, _ := s.store.FeedSources(ctx, domain, actor)
claim, e = s.store.ClaimFeed(ctx, sources[0])
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(ctx, claim, nil, "", "", "network", false); e != nil {
t.Fatal(e)
}
if _, e = s.store.FeedItem(ctx, domain, actor, source.UUID, "id-001"); e != nil {
t.Fatal("cache lost on failure", e)
}
if w := feedRequestTest(s, token, "POST", "sources/"+source.UUID+"/delete", map[string]int64{"version": source.Version}); w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
var count int
if e = s.store.DB.QueryRow(`SELECT count(*) FROM feed_items`).Scan(&count); e != nil || count != 0 {
t.Fatal("cascade", count, e)
}
var integrity string
if e = s.store.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); e != nil || integrity != "ok" {
t.Fatal(integrity, e)
}
}
func TestFeedSourceAPIAndCredentialBinding(t *testing.T) {
s, token, domain, actor, _ := feedFixture(t)
input := map[string]any{"uuid": uuid.NewString(), "provider": "freshrss", "title": "Remote", "url": "https://rss.example.org/api/greader.php", "interval_seconds": 300, "enabled": true, "username": "reader", "password": "top-secret"}
w := feedRequestTest(s, token, "POST", "sources", input)
if w.Code != 200 || strings.Contains(w.Body.String(), "secret") {
t.Fatal(w.Code, w.Body)
}
// Each HTTP retry seals a different ciphertext, but identical credentials and
// request UUID must still return the same source, not create a duplicate.
if retry := feedRequestTest(s, token, "POST", "sources", input); retry.Code != 200 || retry.Body.String() != w.Body.String() {
t.Fatal("source replay", retry.Code, retry.Body)
}
input["password"] = "changed-secret"
if retry := feedRequestTest(s, token, "POST", "sources", input); retry.Code != 412 {
t.Fatal("changed source replay", retry.Code, retry.Body)
}
input["password"] = "top-secret"
sources, e := s.store.FeedSources(context.Background(), domain, actor)
if e != nil {
t.Fatal(e)
}
if len(sources) != 2 {
t.Fatal("duplicate source", len(sources))
}
var remote store.FeedSource
for _, v := range sources {
if v.Provider == "freshrss" {
remote = v
}
}
u, p, e := s.store.OpenFeedCredential(remote)
if e != nil || u != "reader" || p != "top-secret" {
t.Fatal("credential", e)
}
remote.Owner = uuid.NewString()
if _, _, e = s.store.OpenFeedCredential(remote); e == nil {
t.Fatal("credential rebound")
}
if w = feedRequestTest(s, token, "GET", "sources", nil); w.Code != 200 || strings.Contains(w.Body.String(), "top-secret") || strings.Contains(w.Body.String(), "credential") {
t.Fatal(w.Code, w.Body)
}
input["url"] = "https://127.0.0.1/api/greader.php"
if w = feedRequestTest(s, token, "POST", "sources", input); w.Code != 422 {
t.Fatal(w.Code, w.Body)
}
}
func TestFeedFreshRSSAuthorityAndRevocation(t *testing.T) {
s, token, domain, actor, local := feedFixture(t)
ctx := context.Background()
v, e := s.store.SaveFeedSource(ctx, domain, actor, store.FeedSource{UUID: uuid.NewString(), Provider: "freshrss", Title: "FreshRSS", URL: "https://example.org/api/greader.php", Enabled: true, Interval: 300})
if e != nil {
t.Fatal(e)
}
claim := func() store.FeedSource {
t.Helper()
// Simulate the next scheduler tick without a wall-clock wait.
if _, err := s.store.DB.Exec(`UPDATE feed_sources SET next_ms=0 WHERE uuid=?`, v.UUID); err != nil {
t.Fatal(err)
}
all, err := s.store.FeedSources(ctx, domain, actor)
if err != nil {
t.Fatal(err)
}
for _, source := range all {
if source.UUID == v.UUID {
v = source
}
}
c, err := s.store.ClaimFeed(ctx, v)
if err != nil {
t.Fatal(err)
}
return c
}
c := claim()
if e = s.store.CompleteFeed(ctx, c, []store.FeedItem{{ID: "removed", Title: "Removed"}, {ID: "kept", Title: "Kept", Read: true, Starred: true}}, "", "", "", false); e != nil {
t.Fatal(e)
}
if e = s.store.SetFeedState(ctx, domain, actor, v.UUID, "kept", false, false, 0); !errors.Is(e, store.ErrAccessDenied) {
t.Fatal("remote state mutation", e)
}
c = claim()
if e = s.store.CompleteFeed(ctx, c, []store.FeedItem{{ID: "kept", Title: "Changed elsewhere", Read: false}}, "", "", "", false); e != nil {
t.Fatal(e)
}
items, e := s.store.FeedItems(ctx, domain, actor, v.UUID, "", "", -1, "")
if e != nil || len(items) != 1 || items[0].Read || items[0].Starred || items[0].Title != "Changed elsewhere" {
t.Fatal("non-authoritative cache", items, e)
}
c = claim()
if e = s.store.SetPrincipalState(domain, actor, "readonly"); e != nil {
t.Fatal(e)
}
// Changing principal state invalidates the previous session.
token, e = s.store.CreateAuthSession(domain, actor)
if e != nil {
t.Fatal(e)
}
if _, e = s.store.FeedItems(ctx, domain, actor, "", "", "", -1, ""); e != nil {
t.Fatal("readonly read", e)
}
if e = s.store.CompleteFeed(ctx, c, nil, "", "", "", false); !errors.Is(e, store.ErrAccessDenied) {
t.Fatal("late revoked worker", e)
}
if w := feedRequestTest(s, token, "POST", "sources/"+local.UUID+"/delete", map[string]int64{"version": 1}); w.Code != 403 {
t.Fatal("readonly API", w.Code, w.Body)
}
if due, err := s.store.DueFeedSources(ctx); err != nil || len(due) != 0 {
t.Fatal("inactive source starves queue", due, err)
}
if e = s.store.SetPrincipalState(domain, actor, "active"); e != nil {
t.Fatal(e)
}
if e = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); e != nil {
t.Fatal(e)
}
if due, err := s.store.DueFeedSources(ctx); err != nil || len(due) != 0 {
t.Fatal("disabled extension starves queue", due, err)
}
}
func TestFeedNoteAtomicReceiptAndPermissions(t *testing.T) {
s, token, domain, _, source := feedFixture(t)
ctx := context.Background()
claim, e := s.store.ClaimFeed(ctx, source)
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(ctx, claim, []store.FeedItem{{ID: "note", Title: "Title", Body: "Body"}}, "", "", "", false); e != nil {
t.Fatal(e)
}
id := uuid.NewString()
input := map[string]any{"request_id": id, "source": source.UUID, "id": "note", "path": "/notes/rss-" + id, "title": "RSS title", "body": "Source: https://example.org\n{{children}} <script>bad()</script>", "confirm_publication": true}
w := feedRequestTest(s, token, "POST", "note", input)
if w.Code != 201 {
t.Fatal(w.Code, w.Body)
}
original := w.Body.String()
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 201 || w.Body.String() != original {
t.Fatal("replay", w.Code, w.Body)
}
var response map[string]string
json.Unmarshal(w.Body.Bytes(), &response)
rev, e := s.store.GetLatestRevision(response["page_uuid"])
if e != nil || strings.Contains(rev.Body, "{{children}}") || strings.Contains(rev.Body, "<script>") {
t.Fatal(rev, e)
}
input["body"] = "changed"
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 412 {
t.Fatal("changed replay", w.Code, w.Body)
}
input["request_id"] = uuid.NewString()
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 409 {
t.Fatal("path exists", w.Code, w.Body)
}
input["path"] = "/u/other/rss"
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 403 {
t.Fatal("reserved user space", w.Code, w.Body)
}
// Roll back page/path when the durable receipt cannot be committed.
if _, e = s.store.DB.Exec(`CREATE TRIGGER fail_feed_receipt BEFORE INSERT ON feed_note_receipts BEGIN SELECT RAISE(ABORT,'synthetic failure'); END`); e != nil {
t.Fatal(e)
}
input["path"] = "/notes/rollback"
if w = feedRequestTest(s, token, "POST", "note", input); w.Code != 500 {
t.Fatal(w.Code, w.Body)
}
var n int
if e = s.store.DB.QueryRow(`SELECT count(*) FROM paths WHERE domain_uuid=? AND path='/notes/rollback'`, domain).Scan(&n); e != nil || n != 0 {
t.Fatal("partial note", n, e)
}
}
type feedTransportFunc func(*http.Request) (*http.Response, error)
func (f feedTransportFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestFeedBackgroundConditionalFetch(t *testing.T) {
s, _, domain, actor, source := feedFixture(t)
ctx := context.Background()
step := 0
client := &http.Client{Transport: feedTransportFunc(func(r *http.Request) (*http.Response, error) {
step++
if r.URL.String() != source.URL || r.Method != "GET" {
t.Fatal("unexpected request", r.Method, r.URL)
}
deadline, ok := r.Context().Deadline()
if !ok || time.Until(deadline) > 8*time.Second {
t.Fatal("unbounded fetch")
}
switch step {
case 1:
return &http.Response{StatusCode: 200, Header: http.Header{"Etag": {`"feed-1"`}}, Body: io.NopCloser(strings.NewReader(`<rss><channel><item><guid>id</guid><title>First</title></item></channel></rss>`))}, nil
case 2:
if r.Header.Get("If-None-Match") != `"feed-1"` {
t.Fatal("missing conditional GET")
}
return &http.Response{StatusCode: 304, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(""))}, nil
default:
return nil, errors.New("synthetic failure containing a secret URL; must not be persisted")
}
})}
for tick := 1; tick <= 3; tick++ {
if _, e := s.store.DB.Exec(`UPDATE feed_sources SET next_ms=0 WHERE uuid=?`, source.UUID); e != nil {
t.Fatal(e)
}
if e := s.syncFeedSourcesWithClient(ctx, &store.EventJob{UUID: uuid.NewString(), Type: store.FeedSyncEventType}, client); e != nil {
t.Fatal(e)
}
if step != tick {
t.Fatal("wrong fetch count", step, tick)
}
item, e := s.store.FeedItem(ctx, domain, actor, source.UUID, "id")
if e != nil || item.Title != "First" {
t.Fatal("cache not preserved", item, e)
}
all, e := s.store.FeedSources(ctx, domain, actor)
if e != nil || len(all) != 1 || all[0].ETag != `"feed-1"` || all[0].Lease != "" || all[0].NextMS <= time.Now().UnixMilli() {
t.Fatal("worker metadata", all, e)
}
if tick == 3 && all[0].Error != "network" {
t.Fatal("secret error leaked", all[0].Error)
}
if e := s.syncFeedSourcesWithClient(ctx, &store.EventJob{UUID: uuid.NewString()}, client); e != nil {
t.Fatal(e)
}
if step != tick {
t.Fatal("cooldown ignored")
}
}
}
func TestFeedsChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, actor, source := feedFixture(t)
prefs, err := s.store.TaskPreferences(context.Background(), domain, actor)
if err != nil {
t.Fatal(err)
}
prefs.WikiPrefix = "/notes/{YYYY}/{MM}/{DD}/{Subject}"
prefs.RSSLinkMode = "embedded"
if _, err = s.store.SaveTaskPreferences(context.Background(), domain, actor, prefs, time.Now()); err != nil {
t.Fatal(err)
}
claim, e := s.store.ClaimFeed(context.Background(), source)
if e != nil {
t.Fatal(e)
}
items := []store.FeedItem{}
for i := 0; i < 120; i++ {
items = append(items, store.FeedItem{ID: fmt.Sprint(i), Title: fmt.Sprintf("%03d 日本語のRSS記事 — リーダーの密度とキーボード操作", i), Body: "安全な本文です。\n\n<script>display as text</script> {{children}}\n\nSource content stays inert.", Feed: "技術ニュース", URL: "https://example.org/article", PublishedMS: int64(1793500000000 + i)})
}
items[119].BodyHTML = `<h2>HTMLの記事本文</h2><p><strong>強調</strong>や<a href="/article">リンク</a>をそのまま読めます。</p><ul><li>見出しと箇条書き</li><li>テーマに沿った表示</li></ul><table><tr><th>項目</th><th>内容</th></tr><tr><td>本文</td><td>HTML表示</td></tr></table><script>window.rssAttack=1</script><img src="/__feed_leak" onerror="window.rssAttack=1"><style>body{display:none}</style>`
if e = s.store.CompleteFeed(context.Background(), claim, items, "", "", "", false); e != nil {
t.Fatal(e)
}
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)
}
all, e := s.store.FeedSources(context.Background(), domain, actor)
if e != nil {
t.Fatal(e)
}
for _, v := range all {
if v.UUID == fresh.UUID {
fresh = v
}
}
claim, e = s.store.ClaimFeed(context.Background(), fresh)
if e != nil {
t.Fatal(e)
}
catalog, e := parseFreshRSSCatalog([]byte(testFeedCatalog))
if e != nil {
t.Fatal(e)
}
if e = s.store.CompleteFeed(context.Background(), claim, []store.FeedItem{{ID: "catalog-a", Title: "Catalog first", RemoteFeed: "feed/1", RemoteCategories: []string{"user/alice/label/Tech"}}, {ID: "catalog-b", Title: "Catalog second", RemoteFeed: "feed/2", RemoteCategories: []string{"user/alice/label/Other"}}}, "", "", "", false, store.FeedCatalogSnapshot{Subscriptions: catalog, Folders: []store.FeedCategory{{ID: "user/-/label/Empty/Folder", Label: "Empty/Folder"}}}); e != nil {
t.Fatal(e)
}
s.store.BackgroundJobsDisabled = true
s = New(s.store)
script, e := os.ReadFile("internal/server/testdata/feeds.js")
if e != nil {
t.Fatal(e)
}
handler := s.handler()
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/embedded" {
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, `<h1>Isolated external article fixture</h1><script>parent.postMessage('rss-external-fixture-loaded','*')</script>`)
return
}
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/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>")
return
}
handler.ServeHTTP(w, r)
}))
defer web.Close()
for _, width := range []string{"1440", "390"} {
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 {
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()
cancel()
if e != nil {
t.Fatalf("browser %s: %v %s", width, e, out)
}
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ func NormalizePath(
// Hosted adapter assets use directory-relative module and worker URLs. // Hosted adapter assets use directory-relative module and worker URLs.
// DAV collections also require the slash: removing it conflicts with // DAV collections also require the slash: removing it conflicts with
// ServeMux's collection redirect and causes an infinite redirect loop. // ServeMux's collection redirect and causes an infinite redirect loop.
if strings.HasPrefix(p, "/_/adaptor/") || strings.HasPrefix(p, "/_/api/v1/caldav/") || strings.HasPrefix(p, "/_/api/v1/carddav/") { if strings.HasPrefix(p, "/_/adaptor/") || strings.HasPrefix(p, "/_/api/v1/caldav/") || strings.HasPrefix(p, "/_/api/v1/carddav/") || strings.HasPrefix(p, "/_/api/v1/extension/organizer.feeds/browser/") {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
@@ -24,3 +24,18 @@ func TestNormalizePathDAVCollections(t *testing.T) {
t.Fatal("Wiki canonicalization changed", w.Code, w.Header()) t.Fatal("Wiki canonicalization changed", w.Code, w.Header())
} }
} }
func TestNormalizePathGeckoWebsocket(t *testing.T) {
for _, path := range []string{"/_/api/v1/extension/organizer.feeds/browser/", "/_/api/v1/extension/organizer.feeds/browser/wisp/"} {
w := httptest.NewRecorder()
NormalizePath(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != path {
t.Fatal(r.URL.Path)
}
w.WriteHeader(401)
})).ServeHTTP(w, httptest.NewRequest("GET", path, nil))
if w.Code != 401 || w.Header().Get("Location") != "" {
t.Fatal("redirected browser endpoint", w.Code)
}
}
}
+4
View File
@@ -93,6 +93,8 @@ func (s *Server) handleDashboardData(w http.ResponseWriter, r *http.Request, act
func organizerSection(path string) string { func organizerSection(path string) string {
switch { switch {
case feedRoute(path):
return "feeds"
case path == "/v/plan/dashboard": case path == "/v/plan/dashboard":
return "dashboard" return "dashboard"
case strings.HasPrefix(path, "/-/sys/config"): case strings.HasPrefix(path, "/-/sys/config"):
@@ -108,6 +110,8 @@ func organizerSection(path string) string {
} }
func init() { func init() {
translations["en"]["task.work_history"] = "Work history"
translations["ja"]["task.work_history"] = "作業記録"
for lang, entries := range map[string]map[string]string{ for lang, entries := range map[string]map[string]string{
"en": {"organizer.title": "Organizer", "organizer.dashboard": "Dashboard", "organizer.settings": "Personal settings", "organizer.contacts": "Contacts", "organizer.private": "Preferences apply only to you in this Domain. System administration is separate.", "organizer.calendar_defaults": "Default calendars", "organizer.open": "Open"}, "en": {"organizer.title": "Organizer", "organizer.dashboard": "Dashboard", "organizer.settings": "Personal settings", "organizer.contacts": "Contacts", "organizer.private": "Preferences apply only to you in this Domain. System administration is separate.", "organizer.calendar_defaults": "Default calendars", "organizer.open": "Open"},
"ja": {"organizer.title": "オーガナイザー", "organizer.dashboard": "ダッシュボード", "organizer.settings": "個人設定", "organizer.contacts": "連絡先", "organizer.private": "このドメイン内の自分だけに適用する設定です。システムの管理設定とは別です。", "organizer.calendar_defaults": "既定のカレンダー", "organizer.open": "開く"}, "ja": {"organizer.title": "オーガナイザー", "organizer.dashboard": "ダッシュボード", "organizer.settings": "個人設定", "organizer.contacts": "連絡先", "organizer.private": "このドメイン内の自分だけに適用する設定です。システムの管理設定とは別です。", "organizer.calendar_defaults": "既定のカレンダー", "organizer.open": "開く"},
+1
View File
@@ -68,6 +68,7 @@ func (s *Server) renderWithContext(
data["CalendarEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.calendar") data["CalendarEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.calendar")
data["TasksEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.tasks") data["TasksEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.tasks")
data["ContactsEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.contacts") data["ContactsEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.contacts")
data["FeedsEnabled"] = s.systemExtensionEnabled(domainUUID, "organizer.feeds")
data["OrganizerSection"] = organizerSection(r.URL.Path) data["OrganizerSection"] = organizerSection(r.URL.Path)
if r.URL.Path == "/" { if r.URL.Path == "/" {
data["IsHome"] = true data["IsHome"] = true
+3 -1
View File
@@ -21,6 +21,8 @@ import (
) )
type Server struct { type Server struct {
geckoOnce sync.Once
gecko *geckoProxy
calendarOnce sync.Once calendarOnce sync.Once
calendarRepo *calendar.Repository calendarRepo *calendar.Repository
calendarErr error calendarErr error
@@ -351,7 +353,7 @@ func (s *Server) handler() http.Handler {
domainHandler = h domainHandler = h
h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { h = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
background := r.URL.Query().Get("preview") == "1" || background := r.URL.Query().Get("preview") == "1" ||
strings.HasPrefix(r.URL.Path, "/v/preview-cache/") strings.HasPrefix(r.URL.Path, "/v/preview-cache/") || r.URL.Path == feedsAPI+"browser/wisp/"
if !background { if !background {
s.foregroundRequests.Add(1) s.foregroundRequests.Add(1)
defer s.foregroundRequests.Add(-1) defer s.foregroundRequests.Add(-1)
+96 -53
View File
@@ -38,67 +38,110 @@ func sendTaskDigestSMTP(ctx context.Context, config store.AuthProviderConfig, re
func (s *Server) deliverTaskDigests(ctx context.Context, job *store.EventJob) error { func (s *Server) deliverTaskDigests(ctx context.Context, job *store.EventJob) error {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second) ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel() defer cancel()
if _, err := s.store.MaintainTaskDigests(ctx, time.Now()); err != nil {
return err
}
queued, err := s.store.QueuedTaskDigests(ctx)
if err != nil {
return err
}
due, err := s.store.DueTaskDigests(ctx, time.Now()) due, err := s.store.DueTaskDigests(ctx, time.Now())
if err != nil { if err != nil {
return err return err
} }
sent, uncertain := 0, 0 sent, uncertain, checked := 0, 0, 0
for _, candidate := range due { deliver := func(claim store.TaskDigestClaim) error {
state, reason := s.deliverTaskDigest(ctx, claim)
if state == "sent" {
sent++
}
if state == "uncertain" {
uncertain++
}
checked++
finish, stop := context.WithTimeout(context.Background(), 2*time.Second)
defer stop()
err := s.store.FinishTaskDigestReason(finish, claim, state, reason, time.Now())
// A reclaimed generation cannot complete a newer attempt.
if errors.Is(err, store.ErrTaskConflict) {
return nil
}
return err
}
// Alternate automatic delivery and manual retries within the shared time
// budget, so a retry backlog cannot monopolize scheduled deliveries.
for i := 0; i < max(len(due), len(queued)); i++ {
for _, manual := range []bool{false, true} {
if ctx.Err() != nil {
break
}
var claim store.TaskDigestClaim
var e error
if manual {
if i >= len(queued) {
continue
}
claim, e = s.store.ClaimQueuedTaskDigest(ctx, queued[i], time.Now())
} else {
if i >= len(due) {
continue
}
claim, e = s.store.ClaimTaskDigest(ctx, due[i], time.Now())
}
if errors.Is(e, sql.ErrNoRows) {
continue
}
if e != nil {
return e
}
if e = deliver(claim); e != nil {
return e
}
}
if ctx.Err() != nil { if ctx.Err() != nil {
break break
} }
claim, e := s.store.ClaimTaskDigest(ctx, candidate, time.Now())
if errors.Is(e, sql.ErrNoRows) {
continue
}
if e != nil {
return e
}
state := "cancelled"
if s.systemExtensionEnabled(claim.Domain, "organizer.tasks") {
config, e := s.store.AuthProviderConfig(claim.Domain, "smtp")
if e == nil && config.Enabled {
items, next, e := s.store.ListTasks(ctx, claim.Domain, claim.Owner, store.TaskQuery{Kind: "task", View: "today", Today: claim.Day, Limit: 100})
if e == nil {
var body strings.Builder
host, _ := s.store.GetDomainEffectiveHost(claim.Domain)
base := ""
if host != "" && !strings.ContainsAny(host, "/\\\r\n\t @") {
base = "https://" + host
}
fmt.Fprintf(&body, "Tasks / やることリスト — %s (%s)\n\n", claim.Day, claim.Timezone)
if len(items) == 0 {
body.WriteString("No tasks due or planned today. / 今日の対象タスクはありません。\n")
}
for _, item := range items {
fmt.Fprintf(&body, "- %s\n due/期限: %s | planned/予定: %s | estimate/見積: %d min\n %s/v/plan/task/%s\n", item.Title, item.DueOn, item.PlannedOn, item.Estimate, base, item.UUID)
}
if next != "" {
body.WriteString("\nFirst 100 items only. / 最初の100件です。続きはタスク画面で確認してください。\n")
}
// Check consent and current account email immediately before send.
if recipient, e := s.store.TaskDigestRecipient(ctx, claim); e == nil {
state = "uncertain"
if e = sendTaskDigestSMTP(ctx, config, recipient, claim.Day, body.String()); e == nil {
state = "sent"
sent++
} else {
uncertain++
}
}
}
}
}
// Persist delivery outcome even if the scheduler context was cancelled.
finish, stop := context.WithTimeout(context.Background(), 2*time.Second)
e = s.store.FinishTaskDigest(finish, claim, state)
stop()
if e != nil {
return e
}
} }
if len(due) > 0 { if checked > 0 {
s.addEventProgressLog(ctx, job, fmt.Sprintf("task digest checked=%d sent=%d uncertain=%d", len(due), sent, uncertain)) s.addEventProgressLog(ctx, job, fmt.Sprintf("task digest checked=%d sent=%d uncertain=%d", checked, sent, uncertain))
} }
return nil return nil
} }
func (s *Server) deliverTaskDigest(ctx context.Context, claim store.TaskDigestClaim) (string, string) {
if !s.systemExtensionEnabled(claim.Domain, "organizer.tasks") {
return "cancelled", "extension_disabled"
}
config, err := s.store.AuthProviderConfig(claim.Domain, "smtp")
if err != nil || !config.Enabled {
return "cancelled", "smtp_unavailable"
}
items, next, err := s.store.ListTasks(ctx, claim.Domain, claim.Owner, store.TaskQuery{Kind: "task", View: "today", Today: claim.Day, Limit: 100})
if err != nil {
return "cancelled", "tasks_unavailable"
}
var body strings.Builder
host, _ := s.store.GetDomainEffectiveHost(claim.Domain)
base := ""
if host != "" && !strings.ContainsAny(host, "/\\\r\n\t @") {
base = "https://" + host
}
fmt.Fprintf(&body, "Tasks / やることリスト — %s (%s)\n\n", claim.Day, claim.Timezone)
if len(items) == 0 {
body.WriteString("No tasks due or planned today. / 今日の対象タスクはありません。\n")
}
for _, item := range items {
fmt.Fprintf(&body, "- %s\n due/期限: %s | planned/予定: %s | estimate/見積: %d min\n %s/v/plan/task/%s\n", item.Title, item.DueOn, item.PlannedOn, item.Estimate, base, item.UUID)
}
if next != "" {
body.WriteString("\nFirst 100 items only. / 最初の100件です。続きはタスク画面で確認してください。\n")
}
recipient, err := s.store.TaskDigestRecipient(ctx, claim)
if err != nil {
return "cancelled", "consent_changed"
}
if err = sendTaskDigestSMTP(ctx, config, recipient, claim.Day, body.String()); err != nil {
return "uncertain", "transport_uncertain"
}
return "sent", ""
}
+82
View File
@@ -0,0 +1,82 @@
package server
import (
"database/sql"
"errors"
"net/http"
"strconv"
"time"
"pageswiki/internal/store"
)
func (s *Server) handleTaskDigestHistory(w http.ResponseWriter, r *http.Request, actor string, active bool, parts []string) {
domain := s.domainUUIDForRequest(r)
if len(parts) == 1 && r.Method == "GET" {
limit := 50
if raw := r.URL.Query().Get("limit"); raw != "" {
v, e := strconv.Atoi(raw)
if e != nil {
taskError(w, store.ErrTaskInvalid)
return
}
limit = v
}
result, err := s.store.TaskDigestHistory(r.Context(), domain, actor, r.URL.Query().Get("after"), limit, time.Now())
if err != nil {
taskError(w, err)
return
}
writeEventJSON(w, 200, result)
return
}
if len(parts) == 2 && parts[1] == "retry" && r.Method == "POST" {
if !active {
taskError(w, store.ErrAccessDenied)
return
}
var input store.TaskDigestRetry
if !taskJSON(w, r, &input) {
return
}
if id, err := s.store.TaskDigestRetryReceipt(r.Context(), domain, actor, input); err == nil {
writeEventJSON(w, 202, map[string]string{"request_id": id})
return
} else if !errors.Is(err, sql.ErrNoRows) {
taskError(w, err)
return
}
principal, err := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
if err != nil {
taskError(w, store.ErrAccessDenied)
return
}
if _, err = smtpMailbox(principal.Email); err != nil {
writeEventJSON(w, 422, map[string]string{"error": "email_required"})
return
}
config, err := s.store.AuthProviderConfig(domain, "smtp")
if err != nil || !config.Enabled {
writeEventJSON(w, 422, map[string]string{"error": "smtp_unavailable"})
return
}
id, err := s.store.QueueTaskDigestRetry(r.Context(), domain, actor, input, time.Now())
if err != nil {
taskError(w, err)
return
}
writeEventJSON(w, 202, map[string]string{"request_id": id})
return
}
if len(parts) == 1 {
w.Header().Set("Allow", "GET")
w.WriteHeader(405)
return
}
if len(parts) == 2 && parts[1] == "retry" {
w.Header().Set("Allow", "POST")
w.WriteHeader(405)
return
}
http.NotFound(w, r)
}
+447
View File
@@ -0,0 +1,447 @@
package server
import (
"context"
"database/sql"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func digestHistoryFixture(t *testing.T) (*Server, string, string, string, time.Time, store.TaskDigestClaim) {
t.Helper()
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
now := time.Now().UTC()
p := store.TaskPreferences{Complexity: 1, DigestEnabled: true, DigestTime: "00:00", Timezone: "UTC"}
if _, err := s.store.SaveTaskPreferences(ctx, domain, actor, p, now.Add(-24*time.Hour)); err != nil {
t.Fatal(err)
}
if _, err := s.store.DB.Exec(`UPDATE principals SET email='reader@example.test' WHERE uuid=?`, actor); err != nil {
t.Fatal(err)
}
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: "127.0.0.1:1", SMTPFrom: "wiki@example.test"}); err != nil {
t.Fatal(err)
}
c, err := s.store.ClaimTaskDigest(ctx, store.TaskDigestDue{Domain: domain, Owner: actor}, now)
if err != nil {
t.Fatal(err)
}
return s, token, domain, actor, now, c
}
func TestTaskDigestHistoryRetryGenerationAndConsent(t *testing.T) {
s, token, domain, actor, now, c := digestHistoryFixture(t)
ctx := context.Background()
if recipient, err := s.store.TaskDigestRecipient(ctx, c); err != nil || recipient != "reader@example.test" {
t.Fatal(recipient, err)
}
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
post := func(v store.TaskDigestRetry) int {
return taskRequest(s, token, "POST", "digest-history/retry", v, "").Code
}
if got := post(input); got != 412 {
t.Fatal("retried active delivery", got)
}
if err := s.store.FinishTaskDigest(ctx, c, "sent"); err != nil {
t.Fatal(err)
}
if got := post(input); got != 422 {
t.Fatal("missing duplicate acknowledgment", got)
}
input.DuplicateAcknowledged = true
var wg sync.WaitGroup
results := make(chan int, 2)
for range 2 {
wg.Add(1)
go func() { defer wg.Done(); results <- post(input) }()
}
wg.Wait()
close(results)
for code := range results {
if code != 202 {
t.Fatal("idempotent retry", code)
}
}
q, err := s.store.QueuedTaskDigests(ctx)
if err != nil || len(q) != 1 || q[0].AttemptUUID != input.RequestID {
t.Fatal(q, err)
}
if err = s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: false, SMTPAddress: "127.0.0.1:1", SMTPFrom: "wiki@example.test"}); err != nil {
t.Fatal(err)
}
if got := post(input); got != 202 {
t.Fatal("accepted receipt lost after SMTP disabled", got)
}
if err = s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: "127.0.0.1:1", SMTPFrom: "wiki@example.test"}); err != nil {
t.Fatal(err)
}
changed := input
changed.DuplicateAcknowledged = false
if got := post(changed); got != 412 {
t.Fatal("changed replay", got)
}
changed = input
changed.RequestID = uuid.NewString()
if got := post(changed); got != 412 {
t.Fatal("second queued request", got)
}
newClaim, err := s.store.ClaimQueuedTaskDigest(ctx, q[0], now)
if err != nil {
t.Fatal(err)
}
if _, err = s.store.ClaimQueuedTaskDigest(ctx, q[0], now); !errors.Is(err, sql.ErrNoRows) {
t.Fatal("double claim", err)
}
if err = s.store.FinishTaskDigest(ctx, c, "cancelled"); !errors.Is(err, store.ErrTaskConflict) {
t.Fatal("old generation finished new attempt", err)
}
if _, err = s.store.TaskDigestRecipient(ctx, c); err == nil {
t.Fatal("old generation can send")
}
if _, err = s.store.TaskDigestRecipient(ctx, newClaim); err != nil {
t.Fatal(err)
}
p, err := s.store.TaskPreferences(ctx, domain, actor)
if err != nil {
t.Fatal(err)
}
p.DigestEnabled = false
if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, now); err != nil {
t.Fatal(err)
}
if _, err = s.store.TaskDigestRecipient(ctx, newClaim); err == nil {
t.Fatal("opt out ignored")
}
if err = s.store.FinishTaskDigestReason(ctx, newClaim, "cancelled", "consent_changed", now); err != nil {
t.Fatal(err)
}
w := taskRequest(s, token, "GET", "digest-history?limit=1", nil, "")
if w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
var page store.TaskDigestHistory
if err = json.Unmarshal(w.Body.Bytes(), &page); err != nil || len(page.Items) != 1 || page.Next == "" || page.Retries != 1 || page.Current.UUID != input.RequestID {
t.Fatal(page, err)
}
w = taskRequest(s, token, "GET", "digest-history?limit=1&after="+page.Next, nil, "")
if w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
for _, suffix := range []string{"?limit=101", "?after=bad", "?limit=NaN"} {
if w = taskRequest(s, token, "GET", "digest-history"+suffix, nil, ""); w.Code != 422 {
t.Fatal(suffix, w.Code)
}
}
if strings.Contains(w.Body.String(), "reader@example.test") {
t.Fatal("email in history")
}
}
func TestTaskDigestHistoryQueueExpiryAndPermissions(t *testing.T) {
for _, change := range []string{"preference", "extension", "suspended", "tomorrow"} {
t.Run(change, func(t *testing.T) {
s, _, domain, actor, now, c := digestHistoryFixture(t)
ctx := context.Background()
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
t.Fatal(err)
}
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err != nil {
t.Fatal(err)
}
q, err := s.store.QueuedTaskDigests(ctx)
if err != nil {
t.Fatal(err)
}
switch change {
case "preference":
p, e := s.store.TaskPreferences(ctx, domain, actor)
if e != nil {
t.Fatal(e)
}
p.Estimate++
_, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, now)
case "extension":
err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`)
case "suspended":
_, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=? AND principal_uuid=?`, domain, actor)
case "tomorrow":
now = now.Add(24 * time.Hour)
}
if err != nil {
t.Fatal(err)
}
if _, err = s.store.ClaimQueuedTaskDigest(ctx, q[0], now); !errors.Is(err, sql.ErrNoRows) {
t.Fatal("cancelled queue returned live claim", err)
}
var state string
if err = s.store.DB.QueryRow(`SELECT state FROM organizer_digest_attempts WHERE uuid=?`, input.RequestID).Scan(&state); err != nil || state != "cancelled" {
t.Fatal(state, err)
}
})
}
s, token, domain, actor, now, c := digestHistoryFixture(t)
ctx := context.Background()
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
t.Fatal(err)
}
other, err := s.store.CreatePasswordAccount(domain, "digest-other", "a long test password", "Other", "")
if err != nil {
t.Fatal(err)
}
otherToken, err := s.store.CreateAuthSession(domain, other.UUID)
if err != nil {
t.Fatal(err)
}
w := taskRequest(s, otherToken, "GET", "digest-history", nil, "")
if w.Code != 200 || !strings.Contains(w.Body.String(), `"items":[]`) {
t.Fatal("history privacy", w.Code, w.Body)
}
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
if _, err = s.store.QueueTaskDigestRetry(ctx, domain, other.UUID, input, now); err == nil {
t.Fatal("retried other's mail")
}
if err = s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
t.Fatal(err)
}
token, err = s.store.CreateAuthSession(domain, actor)
if err != nil {
t.Fatal(err)
}
if w = taskRequest(s, token, "POST", "digest-history/retry", input, ""); w.Code != 403 {
t.Fatal(w.Code, w.Body)
}
if w = taskRequest(s, token, "GET", "digest-history", nil, ""); w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
if w = taskRequest(s, "", "GET", "digest-history", nil, ""); w.Code == 200 {
t.Fatal("anonymous history")
}
}
func TestTaskDigestHistoryRetentionAndStaleWorker(t *testing.T) {
s, _, domain, actor, now, c := digestHistoryFixture(t)
ctx := context.Background()
// Keep synthetic stale-worker time within the same date even near midnight.
now = time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.UTC)
if _, err := s.store.DB.Exec(`UPDATE organizer_digest_attempts SET updated_ms=? WHERE uuid=?`, now.UnixMilli(), c.AttemptUUID); err != nil {
t.Fatal(err)
}
if _, err := s.store.MaintainTaskDigests(ctx, now.Add(11*time.Minute)); err != nil {
t.Fatal(err)
}
page, err := s.store.TaskDigestHistory(ctx, domain, actor, "", 50, now)
if err != nil || page.Current.State != "uncertain" {
t.Fatal(page, err)
}
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID, DuplicateAcknowledged: true}
if _, err = s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now.Add(12*time.Minute)); err != nil {
t.Fatal(err)
}
q, err := s.store.QueuedTaskDigests(ctx)
if err != nil {
t.Fatal(err)
}
claim, err := s.store.ClaimQueuedTaskDigest(ctx, q[0], now.Add(12*time.Minute))
if err != nil {
t.Fatal(err)
}
if err = s.store.FinishTaskDigest(ctx, c, "sent"); !errors.Is(err, store.ErrTaskConflict) {
t.Fatal("stale completion", err)
}
if err = s.store.FinishTaskDigestReason(ctx, claim, "sent", "", now.Add(12*time.Minute)); err != nil {
t.Fatal(err)
}
deleted, err := s.store.MaintainTaskDigests(ctx, now.Add(91*24*time.Hour))
if err != nil || deleted != 2 {
t.Fatal("retention", deleted, err)
}
var reservations, receipts int
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&reservations); err != nil {
t.Fatal(err)
}
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&receipts); err != nil || reservations != 1 || receipts != 1 {
t.Fatal("deduplication lost", reservations, receipts, err)
}
// Even an old request replay after retention returns its receipt without requeueing.
if id, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now.Add(91*24*time.Hour)); err != nil || id != input.RequestID {
t.Fatal(id, err)
}
q, err = s.store.QueuedTaskDigests(ctx)
if err != nil || len(q) != 0 {
t.Fatal("retention resurrected mail", q, err)
}
if _, err = s.store.DB.Exec(`UPDATE organizer_task_preferences SET next_digest_ms=? WHERE domain_uuid=? AND owner_uuid=?`, now.Add(-time.Minute).UnixMilli(), domain, actor); err != nil {
t.Fatal(err)
}
if _, err = s.store.ClaimTaskDigest(ctx, store.TaskDigestDue{Domain: domain, Owner: actor}, now); !errors.Is(err, sql.ErrNoRows) {
t.Fatal("reservation lost", err)
}
}
func TestTaskDigestHistorySMTPWorker(t *testing.T) {
s, _, domain, actor, now, c := digestHistoryFixture(t)
ctx := context.Background()
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
t.Fatal(err)
}
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err != nil {
t.Fatal(err)
}
address, _, done := fakeSMTP(t, "", false)
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: address, SMTPFrom: "wiki@example.test"}); err != nil {
t.Fatal(err)
}
if err := s.deliverTaskDigests(ctx, &store.EventJob{}); err != nil {
t.Fatal(err)
}
delivery := <-done
if !strings.Contains(delivery.message, "Subject: PagesWiki tasks "+c.Day) {
t.Fatal("message missing")
}
page, err := s.store.TaskDigestHistory(ctx, domain, actor, "", 50, now)
if err != nil || page.Current.State != "sent" || len(page.Items) != 2 {
t.Fatal(page, err)
}
if err := s.deliverTaskDigests(ctx, &store.EventJob{}); err != nil {
t.Fatal(err)
}
}
func TestTaskDigestHistoryLimitsAndAtomicQueue(t *testing.T) {
s, _, domain, actor, now, c := digestHistoryFixture(t)
ctx := context.Background()
if err := s.store.FinishTaskDigest(ctx, c, "cancelled"); err != nil {
t.Fatal(err)
}
input := store.TaskDigestRetry{RequestID: uuid.NewString(), Day: c.Day, ExpectedAttempt: c.AttemptUUID}
if _, err := s.store.DB.Exec(`CREATE TRIGGER fail_digest_queue BEFORE INSERT ON organizer_digest_attempts WHEN NEW.kind='manual' BEGIN SELECT RAISE(ABORT,'test queue failure'); END`); err != nil {
t.Fatal(err)
}
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err == nil {
t.Fatal("injected failure ignored")
}
var count int
if err := s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_requests`).Scan(&count); err != nil || count != 0 {
t.Fatal("partial receipt", count, err)
}
if _, err := s.store.DB.Exec(`DROP TRIGGER fail_digest_queue`); err != nil {
t.Fatal(err)
}
for i := 0; i < 5; i++ {
input.RequestID = uuid.NewString()
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); err != nil {
t.Fatal(i, err)
}
q, err := s.store.QueuedTaskDigests(ctx)
if err != nil || len(q) != 1 {
t.Fatal(q, err)
}
claim, err := s.store.ClaimQueuedTaskDigest(ctx, q[0], now)
if err != nil {
t.Fatal(err)
}
if err = s.store.FinishTaskDigest(ctx, claim, "cancelled"); err != nil {
t.Fatal(err)
}
input.ExpectedAttempt = claim.AttemptUUID
}
input.RequestID = uuid.NewString()
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); !errors.Is(err, store.ErrTaskConflict) {
t.Fatal("retry cap", err)
}
input.Day = now.AddDate(0, 0, -1).Format("2006-01-02")
if _, err := s.store.QueueTaskDigestRetry(ctx, domain, actor, input, now); !errors.Is(err, store.ErrTaskInvalid) {
t.Fatal("past day accepted", err)
}
// A bounded retention pass leaves queued rows and durable guards untouched.
_, err := s.store.DB.Exec(`WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<501) INSERT INTO organizer_digest_attempts SELECT 'old-'||i,'d','u','2000-01-01','scheduled','sent',1,'UTC','',1,1 FROM n`)
if err != nil {
t.Fatal(err)
}
deleted, err := s.store.MaintainTaskDigests(ctx, now)
if err != nil || deleted != 500 {
t.Fatal("unbounded retention", deleted, err)
}
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_digest_attempts WHERE uuid LIKE 'old-%'`).Scan(&count); err != nil || count != 1 {
t.Fatal(count, err)
}
var integrity string
if err = s.store.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); err != nil || integrity != "ok" {
t.Fatal(integrity, err)
}
}
func TestTaskDigestHistoryChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, actor, now, c := digestHistoryFixture(t)
s.store.BackgroundJobsDisabled = true
s = New(s.store)
if err := s.store.FinishTaskDigest(context.Background(), c, "uncertain"); err != nil {
t.Fatal(err)
}
address, _, done := fakeSMTP(t, "", false)
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: address, SMTPFrom: "wiki@example.test"}); err != nil {
t.Fatal(err)
}
script, err := os.ReadFile("internal/server/testdata/task-digest-history.js")
if err != nil {
t.Fatal(err)
}
handler := s.handler()
server := 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" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte("<!doctype html><body style='margin:0'><script>" + string(script) + "</script></body>"))
return
}
if r.URL.Path == "/fixture/tick" && r.Method == "POST" {
if e := s.deliverTaskDigests(r.Context(), &store.EventJob{}); e != nil {
http.Error(w, "worker failed", 500)
return
}
w.WriteHeader(204)
return
}
handler.ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
defer cancel()
width := os.Getenv("DIGEST_HISTORY_WIDTH")
if width == "" {
width = "1440"
}
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir(), os.Getenv("DIGEST_HISTORY_SCREENSHOT"), width, "850"}
if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil {
t.Fatalf("browser: %v\n%s", err, out)
}
select {
case delivery := <-done:
if !strings.Contains(delivery.message, "Subject: PagesWiki tasks "+c.Day) {
t.Fatal("mail missing")
}
case <-ctx.Done():
t.Fatal("SMTP not observed")
}
page, err := s.store.TaskDigestHistory(context.Background(), domain, actor, "", 50, now)
if err != nil || page.Retries != 1 || len(page.Items) != 2 || page.Current.State != "sent" {
t.Fatal(page, err)
}
}
+17 -3
View File
@@ -155,7 +155,7 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
taskError(w, err) taskError(w, err)
return return
} }
writeEventJSON(w, 200, map[string]any{"preferences": p, "defaults": defaultTask(p, time.Now().In(s.getLocation(domain))), "timezone": s.getTimezoneName(domain), "last_digest": last}) writeEventJSON(w, 200, map[string]any{"preferences": p, "defaults": defaultTask(p, time.Now().In(s.getLocation(domain))), "timezone": s.getTimezoneName(domain), "last_digest": last, "gecko_available": s.geckoAvailable(domain)})
return return
} }
if r.Method == "PUT" { if r.Method == "PUT" {
@@ -256,7 +256,8 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
writeEventJSON(w, 422, map[string]string{"error": "wiki_prefix_required"}) writeEventJSON(w, 422, map[string]string{"error": "wiki_prefix_required"})
return return
} }
if !store.ValidTaskWikiPrefix(prefix) { prefix, err = store.ExpandWikiPrefix(prefix, item.Title, time.Now().In(s.getLocation(domain)))
if err != nil {
taskError(w, store.ErrTaskInvalid) taskError(w, store.ErrTaskInvalid)
return return
} }
@@ -301,11 +302,24 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request,
var input struct { var input struct {
store.TaskWork store.TaskWork
Action string `json:"action"` Action string `json:"action"`
Reason string `json:"reason"`
Start string `json:"start"`
End string `json:"end"`
} }
if !taskJSON(w, r, &input) { if !taskJSON(w, r, &input) {
return return
} }
v, err := s.store.ChangeTaskWork(r.Context(), domain, actor, input.Action, input.TaskWork, time.Now()) if input.Action == "correct" && (input.Start != "" || input.End != "") {
a, e1 := parseWorkInstant(input.Start, s.getLocation(domain))
b, e2 := parseWorkInstant(input.End, s.getLocation(domain))
if e1 != nil || e2 != nil {
taskError(w, store.ErrTaskInvalid)
return
}
input.StartMS = a.UnixMilli()
input.EndMS = b.UnixMilli()
}
v, err := s.store.ChangeTaskWork(r.Context(), domain, actor, input.Action, input.TaskWork, time.Now(), input.Reason)
if err != nil { if err != nil {
taskError(w, err) taskError(w, err)
return return
@@ -222,6 +222,9 @@ func TestTaskWorkReportSplitsMidnightAndCompletion(t *testing.T) {
if err != nil || report.Days[0].Points != 7 || report.Days[0].Completed != 1 { if err != nil || report.Days[0].Points != 7 || report.Days[0].Completed != 1 {
t.Fatal("points", report, err) t.Fatal("points", report, err)
} }
if len(report.Projects) != 1 || report.Projects[0].UUID != "" || report.Projects[0].Points != 7 || report.Projects[0].Completed != 1 {
t.Fatal("unassigned project totals", report.Projects)
}
item.Status = "todo" item.Status = "todo"
if _, err = s.store.SaveTask(ctx, domain, actor, item, item.Version); err != nil { if _, err = s.store.SaveTask(ctx, domain, actor, item, item.Version); err != nil {
t.Fatal(err) t.Fatal(err)
+95
View File
@@ -0,0 +1,95 @@
package server
import (
"net/http"
"pageswiki/internal/store"
"strconv"
"time"
)
func parseWorkInstant(raw string, loc *time.Location) (time.Time, error) {
if v, err := time.Parse(time.RFC3339Nano, raw); err == nil {
return v, nil
}
for _, format := range []string{"2006-01-02T15:04:05", "2006-01-02T15:04"} {
v, err := time.ParseInLocation(format, raw, loc)
if err != nil || v.Format(format) != raw {
continue
}
// A repeated wall time needs an explicit offset. Do not silently choose
// the earlier/later occurrence when a browser datetime-local cannot express it.
for _, delta := range []time.Duration{-24 * time.Hour, -2 * time.Hour, -time.Hour, -30 * time.Minute, 30 * time.Minute, time.Hour, 2 * time.Hour, 24 * time.Hour} {
if v.Add(delta).In(loc).Format(format) == raw {
return time.Time{}, store.ErrTaskInvalid
}
}
return v, nil
}
return time.Time{}, store.ErrTaskInvalid
}
func (s *Server) handleTaskWorkHistory(w http.ResponseWriter, r *http.Request, actor string, parts []string) {
if r.Method != "GET" {
w.Header().Set("Allow", "GET")
w.WriteHeader(405)
return
}
domain := s.domainUUIDForRequest(r)
if len(parts) == 2 && parts[1] != "" {
var before int64
if raw := r.URL.Query().Get("before"); raw != "" {
var err error
before, err = strconv.ParseInt(raw, 10, 64)
if err != nil || before < 0 {
taskError(w, store.ErrTaskInvalid)
return
}
}
items, err := s.store.TaskWorkRevisions(r.Context(), domain, actor, parts[1], before)
if err != nil {
taskError(w, err)
return
}
var next int64
if len(items) == 100 {
next = items[len(items)-1].Item.Version
}
writeEventJSON(w, 200, map[string]any{"items": items, "next": next})
return
}
if len(parts) != 1 {
http.NotFound(w, r)
return
}
now := time.Now().In(s.getLocation(domain))
end := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
start := end.AddDate(0, 0, -30)
for _, v := range []struct {
key string
target *time.Time
}{{"from", &start}, {"to", &end}} {
if raw := r.URL.Query().Get(v.key); raw != "" {
date, err := time.ParseInLocation("2006-01-02", raw, now.Location())
if err != nil || date.Format("2006-01-02") != raw {
taskError(w, store.ErrTaskInvalid)
return
}
*v.target = date
}
}
limit := 50
if raw := r.URL.Query().Get("limit"); raw != "" {
var err error
limit, err = strconv.Atoi(raw)
if err != nil {
taskError(w, store.ErrTaskInvalid)
return
}
}
result, err := s.store.TaskWorkHistory(r.Context(), domain, actor, start, end, r.URL.Query().Get("after"), limit)
if err != nil {
taskError(w, err)
return
}
writeEventJSON(w, 200, map[string]any{"items": result.Items, "next": result.Next, "from": start.Format("2006-01-02"), "to": end.Format("2006-01-02"), "timezone": now.Location().String()})
}
+343
View File
@@ -0,0 +1,343 @@
package server
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"pageswiki/internal/store"
)
func recordedWork(t *testing.T, s *Server, domain, actor, task string, start time.Time) store.TaskWork {
t.Helper()
v, err := s.store.ChangeTaskWork(context.Background(), domain, actor, "start", store.TaskWork{UUID: uuid.NewString(), TaskUUID: task, CancelledMS: 99}, start)
if err != nil || v.CancelledMS != 0 {
t.Fatal(v, err)
}
v, err = s.store.ChangeTaskWork(context.Background(), domain, actor, "stop", v, start.Add(time.Hour))
if err != nil {
t.Fatal(err)
}
return v
}
func TestWorkHistoryCorrectionCancellationAndReports(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
project, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("project", "Project A"), 0)
if err != nil {
t.Fatal(err)
}
item := newTaskRecord("task", "Audited work")
item.ProjectUUID = project.UUID
item, err = s.store.SaveTask(ctx, domain, actor, item, 0)
if err != nil {
t.Fatal(err)
}
start := time.Date(2026, 1, 2, 12, 0, 0, 0, time.UTC)
first := recordedWork(t, s, domain, actor, item.UUID, start)
second := recordedWork(t, s, domain, actor, item.UUID, start.Add(2*time.Hour))
post := func(v store.TaskWork, action, reason string) int {
return taskRequest(s, token, "POST", "work", map[string]any{"uuid": v.UUID, "task_uuid": v.TaskUUID, "version": v.Version, "action": action, "reason": reason, "start_ms": v.StartMS, "end_ms": v.EndMS}, "").Code
}
if got := post(first, "correct", ""); got != 422 {
t.Fatal("missing reason", got)
}
bad := first
bad.EndMS = second.EndMS
if got := post(bad, "correct", "overlap"); got != 412 {
t.Fatal("overlap", got)
}
bad = first
bad.EndMS = bad.StartMS
if got := post(bad, "correct", "empty"); got != 422 {
t.Fatal("empty interval", got)
}
first.EndMS = start.Add(2 * time.Hour).UnixMilli() // adjacency is valid
if got := post(first, "correct", "Finished later"); got != 200 {
t.Fatal("correct", got)
}
if got := post(first, "correct", "stale mutation"); got != 412 {
t.Fatal("stale version", got)
}
first.Version++
report, err := s.store.TaskReport(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), time.Now())
if err != nil || len(report.Projects) != 1 || report.Projects[0].Title != "Project A" || report.Projects[0].WorkMS != 3*3600000 {
t.Fatal(report, err)
}
if got := post(first, "cancel", "Duplicate recording"); got != 200 {
t.Fatal("cancel", got)
}
report, err = s.store.TaskReport(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), time.Now())
if err != nil || report.Projects[0].WorkMS != 3600000 {
t.Fatal("cancelled counted", report, err)
}
audit, err := s.store.TaskWorkRevisions(ctx, domain, actor, first.UUID, 0)
if err != nil || len(audit) != 4 || audit[0].Action != "cancel" || audit[1].Action != "correct" || audit[2].Action != "stop" || audit[3].Action != "start" || audit[2].Item.EndMS != start.Add(time.Hour).UnixMilli() {
t.Fatal(audit, err)
}
page, err := s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), "", 1)
if err != nil || len(page.Items) != 1 || page.Items[0].UUID != second.UUID || page.Next == "" {
t.Fatal(page, err)
}
page, err = s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), start.Add(5*time.Hour), page.Next, 1)
if err != nil || len(page.Items) != 1 || page.Items[0].UUID != first.UUID || page.Items[0].CancelledMS == 0 || page.Next != "" {
t.Fatal(page, err)
}
for _, query := range []string{"limit=101", "after=bad", "from=2026-01-02&to=2026-01-01", "from=2020-01-01&to=2026-01-01"} {
if w := taskRequest(s, token, "GET", "work-history?"+query, nil, ""); w.Code != 422 {
t.Fatal(query, w.Code, w.Body)
}
}
if w := taskRequest(s, token, "GET", "work-history/"+first.UUID+"?before=3", nil, ""); w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
if w := taskRequest(s, token, "POST", "work-history", nil, ""); w.Code != 405 {
t.Fatal(w.Code, w.Body)
}
stranger, err := s.store.CreatePasswordAccount(domain, "history-stranger", "a long test password", "Stranger", "")
if err != nil {
t.Fatal(err)
}
strangerToken, err := s.store.CreateAuthSession(domain, stranger.UUID)
if err != nil {
t.Fatal(err)
}
if w := taskRequest(s, strangerToken, "GET", "work-history/"+first.UUID, nil, ""); w.Code != 404 {
t.Fatal("history leaked", w.Code, w.Body)
}
if w := taskRequest(s, strangerToken, "GET", "work-history?from=2026-01-01&to=2026-01-04", nil, ""); w.Code != 200 || !strings.Contains(w.Body.String(), `"items":[]`) {
t.Fatal("list leaked", w.Code, w.Body)
}
if err = s.store.SetPrincipalState(domain, actor, "readonly"); err != nil {
t.Fatal(err)
}
token, err = s.store.CreateAuthSession(domain, actor)
if err != nil {
t.Fatal(err)
}
if got := post(second, "cancel", "No permission"); got != 403 {
t.Fatal("readonly mutation", got)
}
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
t.Fatal(err)
}
if w := taskRequest(s, token, "GET", "work-history", nil, ""); w.Code != 404 {
t.Fatal("disabled extension", w.Code)
}
}
func TestWorkInstantTimezone(t *testing.T) {
for _, tc := range []struct {
zone, input string
valid bool
}{{"Asia/Tokyo", "2026-01-02T12:30", true}, {"America/New_York", "2026-03-08T02:30", false}, {"America/New_York", "2026-11-01T01:30", false}, {"America/New_York", "2026-11-01T01:30:00-04:00", true}, {"Australia/Lord_Howe", "2026-04-05T01:45", false}, {"UTC", "bad", false}} {
loc, err := time.LoadLocation(tc.zone)
if err != nil {
t.Fatal(err)
}
_, err = parseWorkInstant(tc.input, loc)
if (err == nil) != tc.valid {
t.Fatal(tc, err)
}
}
}
func TestWorkHistoryFencingAndAtomicAudit(t *testing.T) {
s, _, domain, actor := taskFixture(t)
ctx := context.Background()
item, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Fenced work"), 0)
if err != nil {
t.Fatal(err)
}
start := time.Now().Add(-10 * time.Hour)
v := recordedWork(t, s, domain, actor, item.UUID, start)
// Simulate a pre-upgrade session: capture its old value, never invent older versions.
if _, err = s.store.DB.Exec(`DELETE FROM organizer_work_revisions WHERE work_uuid=?`, v.UUID); err != nil {
t.Fatal(err)
}
if _, err = s.store.DB.Exec(`CREATE TRIGGER fail_work_audit BEFORE INSERT ON organizer_work_revisions WHEN NEW.action='correct' BEGIN SELECT RAISE(ABORT,'test audit unavailable'); END`); err != nil {
t.Fatal(err)
}
edit := v
edit.EndMS += 60000
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, "correct", edit, time.Now(), "must rollback"); err == nil {
t.Fatal("audit failure accepted")
}
var version int64
if err = s.store.DB.QueryRow(`SELECT version FROM organizer_work_sessions WHERE uuid=?`, v.UUID).Scan(&version); err != nil || version != 2 {
t.Fatal("partial mutation", version, err)
}
if _, err = s.store.DB.Exec(`DROP TRIGGER fail_work_audit`); err != nil {
t.Fatal(err)
}
// Two concurrent corrections must not overwrite one another.
var wg sync.WaitGroup
results := make(chan error, 2)
for range 2 {
wg.Add(1)
go func() {
defer wg.Done()
_, e := s.store.ChangeTaskWork(ctx, domain, actor, "correct", edit, time.Now(), "racing correction")
results <- e
}()
}
wg.Wait()
close(results)
wins, conflicts := 0, 0
for err := range results {
if err == nil {
wins++
} else if errors.Is(err, store.ErrTaskConflict) {
conflicts++
} else {
t.Fatal(err)
}
}
if wins != 1 || conflicts != 1 {
t.Fatal(wins, conflicts)
}
history, err := s.store.TaskWorkRevisions(ctx, domain, actor, v.UUID, 0)
if err != nil || len(history) != 2 || history[1].Action != "baseline" || history[1].Item.EndMS != v.EndMS {
t.Fatal(history, err)
}
// Cross-Domain intervals belong to the same person, but never appear in this list.
d, err := s.store.GetDomainByUUID(domain)
if err != nil {
t.Fatal(err)
}
if err = s.store.CreateDomain(d.CampusUUID, "work-other", "Other", "work-other", "local", "", "", true); err != nil {
t.Fatal(err)
}
domains, err := s.store.ListDomains()
if err != nil {
t.Fatal(err)
}
other := ""
for _, d := range domains {
if d.Name == "work-other" {
other = d.UUID
}
}
if other == "" {
t.Fatal("missing domain")
}
if _, err = s.store.DB.Exec(`INSERT OR IGNORE INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(?,?,?,'active',0)`, uuid.NewString(), other, actor); err != nil {
t.Fatal(err)
}
otherTask, err := s.store.SaveTask(ctx, other, actor, newTaskRecord("task", "Private other work"), 0)
if err != nil {
t.Fatal(err)
}
foreign := recordedWork(t, s, other, actor, otherTask.UUID, start.Add(2*time.Hour))
edit = history[0].Item
edit.EndMS = foreign.EndMS
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, "correct", edit, time.Now(), "overlaps other domain"); !errors.Is(err, store.ErrTaskConflict) {
t.Fatal(err)
}
if _, err = s.store.TaskWorkRevisions(ctx, domain, actor, foreign.UUID, 0); err == nil {
t.Fatal("cross-domain audit leaked")
}
page, err := s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), time.Now(), "", 50)
if err != nil || len(page.Items) != 1 {
t.Fatal(page, err)
}
active, err := s.store.ChangeTaskWork(ctx, domain, actor, "start", store.TaskWork{UUID: uuid.NewString(), TaskUUID: item.UUID}, start.Add(5*time.Hour))
if err != nil {
t.Fatal(err)
}
for _, action := range []string{"correct", "cancel"} {
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, action, active, time.Now(), "active recording"); !errors.Is(err, store.ErrTaskConflict) {
t.Fatal(action, err)
}
}
if _, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=? AND principal_uuid=?`, domain, actor); err != nil {
t.Fatal(err)
}
if _, err = s.store.ChangeTaskWork(ctx, domain, actor, "cancel", history[0].Item, time.Now(), "revoked"); !errors.Is(err, store.ErrAccessDenied) {
t.Fatal("revocation not fenced", err)
}
}
func TestWorkHistoryChromium(t *testing.T) {
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
t.Skip("opt-in Chromium")
}
s, token, domain, actor := taskFixture(t)
s.store.BackgroundJobsDisabled = true
s = New(s.store)
item, err := s.store.SaveTask(context.Background(), domain, actor, newTaskRecord("task", "Browser work record"), 0)
if err != nil {
t.Fatal(err)
}
v := recordedWork(t, s, domain, actor, item.UUID, time.Now().Add(-3*time.Hour))
script, err := os.ReadFile("internal/server/testdata/task-work-history.js")
if err != nil {
t.Fatal(err)
}
handler := s.handler()
server := 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" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte("<!doctype html><body style='margin:0'><script>" + string(script) + "</script></body>"))
return
}
handler.ServeHTTP(w, r)
}))
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
defer cancel()
width := os.Getenv("WORK_HISTORY_WIDTH")
if width == "" {
width = "1440"
}
args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir(), os.Getenv("WORK_HISTORY_SCREENSHOT"), width, "850"}
if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil {
t.Fatalf("browser: %v\n%s", err, out)
}
history, err := s.store.TaskWorkRevisions(context.Background(), domain, actor, v.UUID, 0)
if err != nil || len(history) != 4 || history[0].Action != "cancel" || history[1].Action != "correct" {
t.Fatal(history, err)
}
}
func TestWorkHistoryLocalTimestampAPI(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
item, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Timestamp"), 0)
if err != nil {
t.Fatal(err)
}
start := time.Date(2026, 1, 2, 12, 0, 0, 123000000, time.UTC)
v := recordedWork(t, s, domain, actor, item.UUID, start)
input := map[string]any{"uuid": v.UUID, "version": v.Version, "action": "correct", "reason": "API timestamp", "start": start.Format(time.RFC3339Nano), "end": start.Add(90 * time.Minute).Format(time.RFC3339Nano)}
w := taskRequest(s, token, "POST", "work", input, "")
if w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
var result struct {
Item store.TaskWork `json:"item"`
}
if err = json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
page, err := s.store.TaskWorkHistory(ctx, domain, actor, start.Add(-time.Hour), start.Add(3*time.Hour), "", 50)
if err != nil || page.Items[0].StartMS != start.UnixMilli() {
t.Fatal(page, err)
}
if w = taskRequest(s, token, "GET", "work-history?"+url.Values{"from": {"2026-01-02"}, "to": {"2026-01-03"}}.Encode(), nil, ""); w.Code != 200 {
t.Fatal(w.Code, w.Body)
}
}
+17
View File
@@ -20,6 +20,9 @@ import (
const tasksAPI = "/_/api/v1/extension/organizer.tasks/" const tasksAPI = "/_/api/v1/extension/organizer.tasks/"
func taskRoute(p string) bool { func taskRoute(p string) bool {
if feedRoute(p) {
return true
}
if p == "/v/plan/dashboard" { if p == "/v/plan/dashboard" {
return true return true
} }
@@ -30,6 +33,8 @@ func taskRoute(p string) bool {
} }
func (s *Server) registerTaskRoutes() { func (s *Server) registerTaskRoutes() {
s.mux.HandleFunc("/v/feeds", s.handleFeeds)
s.mux.HandleFunc(feedsAPI, s.handleFeeds)
s.mux.HandleFunc("/v/plan/dashboard", s.handleOrganizerDashboard) s.mux.HandleFunc("/v/plan/dashboard", s.handleOrganizerDashboard)
s.mux.HandleFunc(contactDAV, s.handleContactDAV) s.mux.HandleFunc(contactDAV, s.handleContactDAV)
s.mux.HandleFunc(strings.TrimSuffix(contactDAV, "/"), s.handleContactDAV) s.mux.HandleFunc(strings.TrimSuffix(contactDAV, "/"), s.handleContactDAV)
@@ -98,6 +103,10 @@ func (s *Server) handleTaskPage(w http.ResponseWriter, r *http.Request) {
return return
} }
domain := s.domainUUIDForRequest(r) domain := s.domainUUIDForRequest(r)
if r.URL.Path == "/v/plan/tasks/work" {
s.renderWithContext(w, r, "task_work_history.gohtml", map[string]any{"TaskWritable": active, "Timezone": s.getTimezoneName(domain)})
return
}
if r.URL.Path == "/v/plan/tasks/report" || r.URL.Path == "/v/plan/tasks/settings" { if r.URL.Path == "/v/plan/tasks/report" || r.URL.Path == "/v/plan/tasks/settings" {
s.handleTaskProductivityPage(w, r, active) s.handleTaskProductivityPage(w, r, active)
return return
@@ -212,6 +221,14 @@ func (s *Server) handleTasksAPI(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(ctx) r = r.WithContext(ctx)
domain := s.domainUUIDForRequest(r) domain := s.domainUUIDForRequest(r)
parts := strings.Split(strings.TrimPrefix(r.URL.Path, tasksAPI), "/") parts := strings.Split(strings.TrimPrefix(r.URL.Path, tasksAPI), "/")
if parts[0] == "digest-history" {
s.handleTaskDigestHistory(w, r, actor, active, parts)
return
}
if parts[0] == "work-history" {
s.handleTaskWorkHistory(w, r, actor, parts)
return
}
if len(parts) == 1 && (parts[0] == "dashboard" || parts[0] == "progress" || parts[0] == "calendar-capture" || parts[0] == "capture" || parts[0] == "preferences" || parts[0] == "work" || parts[0] == "report") { if len(parts) == 1 && (parts[0] == "dashboard" || parts[0] == "progress" || parts[0] == "calendar-capture" || parts[0] == "capture" || parts[0] == "preferences" || parts[0] == "work" || parts[0] == "report") {
s.handleTaskProductivity(w, r, actor, active, parts[0]) s.handleTaskProductivity(w, r, actor, active, parts[0])
return return
+40
View File
@@ -0,0 +1,40 @@
import {spawn} from 'node:child_process';
import {writeFile} from 'node:fs/promises';
import {setTimeout as delay} from 'node:timers/promises';
const [url,profile,screenshot]=process.argv.slice(2);
const browser=spawn('chromium',['--headless','--no-sandbox','--disable-gpu','--disable-background-networking','--no-proxy-server','--remote-debugging-port=0',`--user-data-dir=${profile}`,'about:blank']);
process.on('SIGTERM',()=>{browser.kill();process.exit(1);});
let socket,logs=[];
try {
const endpoint=await new Promise((resolve,reject)=>{let text='';browser.stderr.on('data',c=>{text+=c;const match=text.match(/DevTools listening on (ws:\/\/[^\s]+)/);if(match)resolve(match[1]);});browser.on('error',reject);browser.on('exit',code=>reject(Error('Browser exited '+code)));});
socket=new WebSocket(endpoint);await new Promise((resolve,reject)=>{socket.onopen=resolve;socket.onerror=reject;});
let id=0;const pending=new Map();
socket.onmessage=e=>{const m=JSON.parse(e.data),p=pending.get(m.id);if(p){pending.delete(m.id);m.error?p.reject(Error(JSON.stringify(m.error))):p.resolve(m.result);}else if(['Runtime.exceptionThrown','Log.entryAdded','Runtime.consoleAPICalled'].includes(m.method))logs.push(JSON.stringify(m.params));};
const send=(method,params={},sessionId)=>new Promise((resolve,reject)=>{pending.set(++id,{resolve,reject});socket.send(JSON.stringify({id,method,params,sessionId}));});
const {targetId}=await send('Target.createTarget',{url:'about:blank'}),{sessionId}=await send('Target.attachToTarget',{targetId,flatten:true});
const call=(method,params)=>send(method,params,sessionId);
const evaluate=async expression=>{const r=await call('Runtime.evaluate',{expression,returnByValue:true,awaitPromise:true});if(r.exceptionDetails)throw Error(JSON.stringify(r.exceptionDetails));return r.result.value;};
const wait=async(expression,seconds=15)=>{for(let n=0;n<seconds*5;n++){if(await evaluate(expression))return;await delay(200);}if(screenshot){const {data}=await call('Page.captureScreenshot',{format:'png'});await writeFile(screenshot,Buffer.from(data,'base64'));}throw Error('Timeout: '+expression+'\n'+logs.slice(-30).join('\n'));};
await call('Runtime.enable');await call('Log.enable');await call('Page.enable');
// Only synthetic fixture diagnostics; production does not log article URLs.
await call('Page.addScriptToEvaluateOnNewDocument',{source:`if(location.pathname.endsWith('/browser/'))import('/_/api/v1/extension/organizer.feeds/browser/assets/v0.0.1/gecko.js').then(({Gecko})=>{const init=Gecko.prototype.init;Gecko.prototype.init=function(){this.opts.printErr=console.warn;this.opts.print=console.log;return init.call(this);};});`});
await call('Emulation.setDeviceMetricsOverride',{width:1280,height:850,deviceScaleFactor:1,mobile:false});await call('Page.navigate',{url});
await wait(`document.querySelector('.feed-row')`);
if(!await evaluate('crossOriginIsolated'))throw Error('Parent not isolated');
await evaluate(`window.geckoReady=false;addEventListener('message',e=>{if(e.data?.type==='pageswiki-gecko'&&e.data.ready)geckoReady=true;});document.querySelector('.feed-row').click()`);
await wait(`document.querySelector('.feed-original')`);await evaluate(`document.querySelector('.feed-original').click()`);
await wait(`document.querySelector('.feed-external iframe')?.contentWindow.crossOriginIsolated`);
await wait(`geckoReady || document.querySelector('.feed-external iframe')?.contentDocument?.querySelector('#status')?.textContent.includes('Viewer failed')`,100);
if(!await evaluate('geckoReady'))throw Error('Gecko startup failed\n'+logs.slice(-8).join('\n'));
const pixel=`(()=>{const c=document.querySelector('.feed-external iframe').contentDocument.querySelector('canvas');return Array.from(c.getContext('2d').getImageData(400,200,1,1).data)})()`;
await wait(`${pixel}[0]===255 && ${pixel}[1]===0`);
const rect=await evaluate(`(()=>{const r=document.querySelector('.feed-external iframe').getBoundingClientRect();return {x:r.x+100,y:r.y+50}})()`);
await call('Input.dispatchMouseEvent',{type:'mousePressed',...rect,button:'left',buttons:1,clickCount:1});await call('Input.dispatchMouseEvent',{type:'mouseReleased',...rect,button:'left',buttons:0,clickCount:1});
await wait(`${pixel}[0]===0 && ${pixel}[1]===255`);
await wait(`document.querySelector('.feed-external a').href==='http://fixture.invalid/next'`);
await evaluate(`document.querySelector('.feed-external button').click()`);
await wait(`${pixel}[0]===255 && ${pixel}[1]===0`);
if(screenshot){const {data}=await call('Page.captureScreenshot',{format:'png'});await writeFile(screenshot,Buffer.from(data,'base64'));}
await evaluate(`document.querySelector('.feed-external').close()`);await wait(`!document.querySelector('.feed-external')`);
console.log('Real Gecko: isolation, Wisp article render, click navigation, viewer-local back and teardown passed');
}finally{socket?.close();browser.kill();await new Promise(resolve=>browser.once('exit',resolve));await delay(300);}
+17
View File
@@ -0,0 +1,17 @@
import {memoryProfile} from '../../../static/js/feed-gecko-profile.mjs';
import assert from 'node:assert/strict';
const p=memoryProfile(10);
await p.mkdir('cache');await p.writeFile('cache/a',new Uint8Array([1,2,3]));
const copy=await p.readFile('cache/a');copy[0]=99;
assert.equal((await p.readFile('cache/a'))[0],1);
assert.deepEqual(await p.readdir(''),['cache/']);
assert.deepEqual(await p.readdir('cache'),['a']);
await p.rename('cache','new');assert.equal(await p.stat('cache'),null);
assert.equal((await p.readFile('new/a')).length,3);
await p.writeFile('b',new Uint8Array(5));await p.rename('new/a','b');
await p.writeFile('c',new Uint8Array(7));
await assert.rejects(p.writeFile('d',new Uint8Array(1)));
for(const path of ['../secret','/absolute','new/../x','a\\b','a\0b'])await assert.rejects(p.stat(path));
await p.unlink('b');await p.writeFile('d',new Uint8Array(3));
assert.equal(await memoryProfile().stat('d'),null);
console.log('Ephemeral profile bounds, isolation and rename passed');
+114
View File
@@ -0,0 +1,114 @@
(async()=>{
const delay=ms=>new Promise(r=>setTimeout(r,ms)),assert=(ok,msg)=>{if(!ok)throw Error(msg);};
const wait=async check=>{for(let i=0;i<200;i++){if(check())return;await delay(30);}throw Error('Timeout: '+check);};
const frame=document.createElement('iframe');frame.style='display:block;width:100%;height:100vh;border:0';document.body.append(frame);
try{
await new Promise(resolve=>{frame.onload=resolve;frame.src='/v/feeds';});const d=frame.contentDocument,w=frame.contentWindow;
// Let fixture requests finish before destroying a viewport's browser context.
const fetch=w.fetch.bind(w);let pendingRequests=0;
w.fetch=async(...args)=>{pendingRequests++;try{const response=await fetch(...args);await response.clone().arrayBuffer();return response;}finally{pendingRequests--;}};
for(const key of Object.keys(w.localStorage))if(key.startsWith('pageswiki.feed-folders.v1:'))w.localStorage.removeItem(key);
await wait(()=>d.querySelectorAll('.feed-row').length===100);
assert(d.documentElement.scrollWidth<=w.innerWidth,'Viewport overflow');
assert(d.title.startsWith('RSS ·'),'RSS title');assert(d.getElementById('feed-status').textContent==='','Unrequested status copy');assert(w.getComputedStyle(d.getElementById('feed-status')).display==='none','Empty status takes space');
for(const a of d.querySelectorAll('.organizer-nav a')){assert(a.getAttribute('aria-label'),'Unnamed navigation');if(w.innerWidth<=700)assert(w.getComputedStyle(a.querySelector('span')).display==='none','Mobile tab label visible');}
d.getElementById('feed-rows').focus();d.dispatchEvent(new w.KeyboardEvent('keydown',{key:'j',bubbles:true}));
await wait(()=>d.querySelector('#feed-reader .feed-body'));const html=d.querySelector('#feed-reader .feed-body').shadowRoot;assert(html?.querySelector('h2')&&html.querySelector('table')&&html.querySelector('strong'),'HTML not rendered');assert(!html.querySelector('script,img,iframe,form'),'Active article HTML');assert(!w.rssAttack,'Script executed');assert(html.querySelector('a').target==='_blank','Article link target');assert(d.querySelector('.feed-original svg')&&!d.querySelector('.feed-original').textContent,'Original link not icon');
const share=d.querySelector('.feed-share-x'),intent=new URL(share.href);assert(share.target==='_blank'&&share.rel.includes('noopener')&&intent.searchParams.get('url')==='https://example.org/article'&&intent.searchParams.get('text').includes('日本語のRSS記事')&&!intent.searchParams.get('text').includes('https://'),'RSS share URL or duplicate URL in text');
assert(d.getElementById('feeds-app').dataset.linkMode==='embedded','Link preference missing');assert(!d.querySelector('.feed-external iframe'),'External page fetched before click');const externalURL=new URL('/fixture/embedded',w.location.href);externalURL.hostname='localhost';let externalLoaded=0;w.addEventListener('message',e=>{if(e.data==='rss-external-fixture-loaded'&&e.origin==='null')externalLoaded++;});const original=d.querySelector('.feed-original');original.href=externalURL.href;original.click();await wait(()=>d.querySelector('.feed-external[open] iframe'));await wait(()=>externalLoaded===1);let previewFrame=d.querySelector('.feed-external iframe');assert(previewFrame.src===externalURL.href&&previewFrame.getAttribute('sandbox')==='allow-scripts'&&previewFrame.referrerPolicy==='no-referrer','Unsafe external frame');assert(d.querySelector('.feed-external a').target==='_blank','Missing fallback');d.querySelector('.feed-external button').click();await wait(()=>!d.querySelector('.feed-external'));assert(d.querySelector('#feed-reader .feed-body'),'External preview lost article');const articleLink=html.querySelector('a');articleLink.href=externalURL.href;articleLink.click();await wait(()=>externalLoaded===2);d.querySelector('.feed-external').close();await wait(()=>!d.querySelector('.feed-external'));
const help=d.getElementById('feed-help'),helpButton=d.querySelector('[aria-controls="feed-help"]'),key=k=>d.dispatchEvent(new w.KeyboardEvent('keydown',{key:k,bubbles:true}));
d.getElementById('feed-status').textContent='Synthetic status';key('?');await wait(()=>!help.hidden);assert(helpButton.getAttribute('aria-expanded')==='true','Help toggle state');key('Escape');assert(help.hidden&&d.querySelector('.feed-layout').classList.contains('reading'),'Closing help changed reader');assert(d.getElementById('feed-status').textContent==='Synthetic status','Help erased status');key('?');await wait(()=>!help.hidden);key('?');await wait(()=>help.hidden);helpButton.click();await wait(()=>!help.hidden);help.querySelector('button').click();await wait(()=>help.hidden);assert(helpButton.getAttribute('aria-expanded')==='false','Help state not reset');d.getElementById('feed-status').textContent='';
d.querySelectorAll('.feed-row')[1].click();await wait(()=>d.querySelector('#feed-reader .feed-body')&&!d.querySelector('#feed-reader .feed-body').shadowRoot);assert(d.querySelector('#feed-reader .feed-body').textContent.includes('<script>'),'Literal text not preserved');d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body')?.shadowRoot);
d.dispatchEvent(new w.KeyboardEvent('keydown',{key:'*',bubbles:true}));await wait(()=>d.querySelector('.feed-row[aria-selected=true]').textContent.includes('★'));
d.dispatchEvent(new w.KeyboardEvent('keydown',{key:'Escape',bubbles:true}));await wait(()=>!d.querySelector('.feed-layout').classList.contains('reading'));
d.getElementById('feed-more').click();await wait(()=>d.querySelectorAll('.feed-row').length===122);
d.dispatchEvent(new w.KeyboardEvent('keydown',{key:'/',bubbles:true}));await wait(()=>d.activeElement.type==='search');d.activeElement.value='119';d.activeElement.dispatchEvent(new w.Event('input',{bubbles:true}));await wait(()=>d.querySelectorAll('.feed-row').length===1);
d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body'));
d.querySelector('#feed-reader .feed-actions button:last-child').click();await wait(()=>d.querySelector('#feed-reader form'));
assert(!['rgba(0, 0, 0, 0)','transparent'].includes(w.getComputedStyle(d.querySelector('.feed-capture')).backgroundColor),'Capture dialog background transparent');
const form=d.querySelector('#feed-reader form'),inputs=form.querySelectorAll('input');assert(inputs[2].value==='/notes/{YYYY}/{MM}/{DD}/{Subject}','Wiki default not filled');assert(form.closest('dialog').open&&d.querySelector('#feed-reader .feed-body'),'Capture replaced article');assert(form.querySelector('a[href="/-/sys/config?panel=organizer"]'),'Default settings link missing');await wait(()=>form.querySelector('output').textContent.includes('/rss-'));assert(!form.querySelector('output').textContent.includes('{'),'Template not expanded');inputs[2].value='/notes/feed-ui';inputs[2].dispatchEvent(new w.Event('input'));await wait(()=>form.querySelector('output').textContent.startsWith('/notes/feed-ui/rss-'));inputs[5].checked=true;form.requestSubmit();await wait(()=>d.querySelector('#feed-reader a[href^="/v/uuid:"]'));d.querySelector('.feed-capture button').click();await wait(()=>!d.querySelector('.feed-capture'));assert(d.querySelector('#feed-reader .feed-body'),'Closing capture lost article');
const search=d.querySelector('#feed-toolbar input');search.value='';search.dispatchEvent(new w.Event('input',{bubbles:true}));await wait(()=>d.querySelectorAll('.feed-row').length===100);d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body'));
assert(d.documentElement.scrollWidth<=w.innerWidth,'Reader overflow');
d.getElementById('feed-opml').click();await wait(()=>d.getElementById('feed-opml-file'));
const opml='<opml version="2.0"><head><title>Import</title></head><body><outline text="技術"><outline type="rss" text="Import '+w.innerWidth+'" xmlUrl="https://example.org/ui-'+w.innerWidth+'"/><outline type="rss" text="Existing" xmlUrl="https://example.org/feed.xml"/><outline type="rss" text="Private" xmlUrl="https://127.0.0.1/feed"/></outline></body></opml>';
const choose=()=>{const transfer=new w.DataTransfer();transfer.items.add(new w.File([opml],'feeds.opml',{type:'text/x-opml'}));const f=d.getElementById('feed-opml-file');f.files=transfer.files;f.dispatchEvent(new w.Event('change',{bubbles:true}));};choose();
await wait(()=>d.querySelectorAll('#feed-opml-preview tbody tr').length===3);const checks=d.querySelectorAll('#feed-opml-preview tbody input');assert(checks[0].checked&&!checks[0].disabled&&checks[1].disabled&&checks[2].disabled,'Import eligibility');
assert(d.documentElement.scrollWidth<=w.innerWidth,'OPML overflow');d.querySelector('.feed-import-options input[type=checkbox]').checked=false;d.getElementById('feed-opml-preview').requestSubmit();await wait(()=>!d.getElementById('feed-opml-preview'));
assert(d.getElementById('feed-sources').textContent.includes('Import '+w.innerWidth),'Imported source missing');d.getElementById('feed-opml').click();await wait(()=>d.getElementById('feed-opml-file'));choose();await wait(()=>d.querySelectorAll('#feed-opml-preview tbody tr').length===3);assert(d.querySelector('#feed-opml-preview tbody input').disabled,'Duplicate after import');
assert(d.documentElement.scrollWidth<=w.innerWidth,'OPML preview overflow');
const freshButton=()=>[...d.querySelectorAll('.feed-source>button:first-child')].find(b=>b.textContent==='FreshRSS fixture');freshButton().click();await wait(()=>d.querySelectorAll('.feed-folder').length===4&&d.querySelectorAll('.feed-row').length===2);
const folder=id=>[...d.querySelectorAll('.feed-folder')].find(n=>n.dataset.category===id),tech=()=>folder('user/alice/label/Tech');
const folderKey=(key,target=d,extra={})=>target.dispatchEvent(new w.KeyboardEvent('keydown',{key,shiftKey:true,bubbles:true,cancelable:true,...extra}));
const activeFolder=()=>d.querySelector('.feed-folder-name[aria-current="true"]');
const folderNames=()=>[...d.querySelectorAll('.feed-folder-name')];
assert(help.textContent.includes('Shift+j/k'),'Folder shortcuts missing from help');
// All subscriptions has no folder: start at either end, including empty/uncategorized.
folderKey('K');await wait(()=>activeFolder()===folderNames().at(-1)&&d.querySelectorAll('.feed-row').length===0);
folderKey('J');await delay(80);assert(activeFolder()===folderNames().at(-1),'Folder navigation wrapped past end');
folderKey('K');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')&&d.querySelector('.feed-row')?.textContent.includes('Catalog first'));
assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false','Keyboard navigation expanded folder');
folderKey('k');await wait(()=>activeFolder()===folderNames()[1]&&d.querySelector('.feed-row')?.textContent.includes('Catalog second'));
folderKey('K');await wait(()=>activeFolder()===folderNames()[0]&&d.querySelectorAll('.feed-row').length===0);
folderKey('K');await delay(80);assert(activeFolder()===folderNames()[0],'Folder navigation wrapped before start');
const articleFilter=d.querySelector('#feed-toolbar select');articleFilter.value='starred';articleFilter.dispatchEvent(new w.Event('change'));await delay(80);
folderKey('J');await wait(()=>activeFolder()===folderNames()[1]);await delay(80);assert(articleFilter.value==='starred'&&d.querySelectorAll('.feed-row').length===0,'Folder navigation lost starred filter');
articleFilter.value='';articleFilter.dispatchEvent(new w.Event('change'));await wait(()=>d.querySelector('.feed-row')?.textContent.includes('Catalog second'));
folderKey('K');await wait(()=>activeFolder()===folderNames()[0]&&d.querySelectorAll('.feed-row').length===0);
const keep=activeFolder();for(const extra of [{ctrlKey:true},{metaKey:true},{altKey:true},{isComposing:true}])folderKey('J',d,extra);
const catalogFind=d.querySelector('.feed-catalog input');folderKey('J',catalogFind);
const editable=d.createElement('div');editable.setAttribute('contenteditable','');editable.innerHTML='<span>editing</span>';d.body.append(editable);folderKey('J',editable.firstChild);editable.remove();
await delay(80);assert(activeFolder()===keep,'Modifier or editable input triggered folder shortcut');
// Preserve article filters while moving from a selected subscription to the next folder.
tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('[data-feed="feed/1"]'));
tech().querySelector('[data-feed="feed/1"]').click();await wait(()=>d.querySelector('.feed-row')?.textContent.includes('Catalog first'));
const globalSearch=d.querySelector('#feed-toolbar input');globalSearch.value='Catalog';globalSearch.dispatchEvent(new w.Event('input',{bubbles:true}));await delay(220);
folderKey('K');await wait(()=>activeFolder()===folderNames()[1]&&d.querySelector('.feed-row')?.textContent.includes('Catalog second'));
assert(globalSearch.value==='Catalog'&&!d.querySelector('button[data-feed][aria-current="true"]'),'Folder navigation lost search or kept subscription filter');
d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body'));
const selectedTitle=d.querySelector('#feed-reader h1').textContent;
d.querySelector('#feed-reader .feed-actions button:last-child').click();await wait(()=>d.querySelector('.feed-capture[open]'));
folderKey('J');await delay(80);assert(activeFolder()===folderNames()[1]&&d.querySelector('#feed-reader h1').textContent===selectedTitle,'Shortcut escaped capture modal');
d.querySelector('.feed-capture').close();await wait(()=>!d.querySelector('.feed-capture'));
folderKey('J');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')&&d.querySelector('.feed-row')?.textContent.includes('Catalog first'));
assert(!d.querySelector('.feed-layout').classList.contains('reading')&&!d.querySelector('#feed-reader .feed-body'),'Folder navigation left old article open');
catalogFind.value='Other';catalogFind.dispatchEvent(new w.Event('input',{bubbles:true}));assert(!activeFolder(),'Catalog filter setup');
folderKey('K');await wait(()=>activeFolder()===folderNames()[1]&&d.querySelector('.feed-row')?.textContent.includes('Catalog second'));
assert(d.querySelector('.feed-catalog input').value==='','Folder navigation left selected folder hidden by catalog filter');
globalSearch.value='';globalSearch.dispatchEvent(new w.Event('input',{bubbles:true}));await delay(220);
// Rapid repeats must settle on the latest folder rather than an older request.
folderKey('J');folderKey('J');folderKey('K');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')&&d.querySelector('.feed-row')?.textContent.includes('Catalog first'));await delay(100);
assert(d.querySelectorAll('.feed-row').length===1&&d.querySelector('.feed-row').textContent.includes('Catalog first'),'Stale folder request won');
tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false');
tech().querySelector('.feed-folder-name').click();await wait(()=>d.querySelectorAll('.feed-row').length===1&&d.querySelector('.feed-row').textContent.includes('Catalog first'));
assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false','Selecting folder expands it unexpectedly');tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('button[data-feed="feed/1"]'));
const empty=folder('user/-/label/Empty/Folder');assert(empty.querySelector('.feed-folder-name span').textContent==='Empty/Folder','Slash label split');empty.querySelector('.feed-folder-toggle').click();assert(!empty.querySelector('button[data-feed]'),'Empty folder fabricated a feed');empty.querySelector('.feed-folder-name').click();await wait(()=>d.querySelectorAll('.feed-row').length===0);
d.querySelector('.feed-folder[data-mode="uncategorized"] .feed-folder-name').click();await wait(()=>d.querySelectorAll('.feed-row').length===0);
d.querySelector('.feed-catalog .feed-tree-all').click();await wait(()=>d.querySelectorAll('.feed-row').length===2);
[...d.querySelectorAll('.feed-source>button:first-child')].find(b=>b.textContent==='Synthetic news').click();await wait(()=>!d.querySelector('.feed-catalog'));freshButton().click();await wait(()=>tech()?.querySelector('button[data-feed="feed/1"]'));assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='true','Folder expansion not retained');
folder('user/alice/label/Other').querySelector('.feed-folder-toggle').click();await wait(()=>d.querySelector('.feed-subscriptions button[data-feed="feed/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})});
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');
const previousHistory=d.getElementById('feed-history-panel');d.getElementById('feed-history').click();await wait(()=>d.getElementById('feed-history-panel')!==previousHistory&&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');
const probe=document.createElement('iframe');probe.hidden=true;document.body.append(probe);await new Promise(resolve=>{probe.onload=resolve;probe.src='/v/feeds?source='+encodeURIComponent(source.uuid);});await wait(()=>probe.contentDocument.querySelector('.feed-folder[data-category="user/alice/label/Tech"] .feed-folder-toggle')?.getAttribute('aria-expanded')==='true'&&probe.contentDocument.querySelectorAll('.feed-row').length===2);probe.remove();
const previousCatalog=d.querySelector('.feed-catalog');freshButton().click();await wait(()=>d.querySelector('.feed-catalog')!==previousCatalog&&tech()?.querySelector('button[data-feed="feed/1"]')&&d.querySelectorAll('.feed-row').length===2);if(w.innerWidth<=700){d.querySelector('#feed-toolbar button').click();await wait(()=>d.querySelector('.feed-layout').classList.contains('sources-open'));assert(w.getComputedStyle(d.getElementById('feed-sources')).display!=='none','Mobile folders hidden');}assert(d.documentElement.scrollWidth<=w.innerWidth,'Folder tree overflow');await wait(()=>pendingRequests===0);await delay(100);document.body.dataset.vimReady='1';
}catch(e){const out=document.createElement('pre');out.id='error';out.textContent=e.stack;document.body.append(out);}
})();
+28
View File
@@ -0,0 +1,28 @@
(async()=>{
const assert=(ok,msg)=>{if(!ok)throw Error(msg);},delay=ms=>new Promise(r=>setTimeout(r,ms));
const wait=async check=>{for(let n=0;n<240;n++){if(check())return;await delay(25);}throw Error('Timed out: '+check);};
const frame=document.createElement('iframe');frame.style='display:block;width:100%;height:100vh;border:0';document.body.append(frame);
try{
await new Promise(resolve=>{frame.onload=resolve;frame.src='/-/sys/config?panel=organizer';});
const w=frame.contentWindow,d=frame.contentDocument;await wait(()=>d.querySelector('#task-digest-history details'));
const root=d.getElementById('task-digest-history'),details=root.querySelector('details');
assert(!details.open,'History not lazy');assert(!w.performance.getEntriesByType('resource').some(x=>x.name.includes('/digest-history')),'History eagerly fetched');
details.open=true;await wait(()=>root.querySelector('.digest-retry form'));
const form=root.querySelector('.digest-retry form'),ack=form.querySelector('input');
assert(ack.required&&!ack.checked,'Uncertain delivery must require confirmation');assert(!form.checkValidity(),'Unchecked acknowledgment valid');ack.checked=true;
const original=w.fetch,requests=[];let lose=true;
w.fetch=async(url,options)=>{if(String(url).endsWith('/digest-history/retry')){requests.push(JSON.parse(options.body));const response=await original(url,options);if(lose){lose=false;await response.text();throw new TypeError('Simulated lost response');}return response;}return original(url,options);};
form.requestSubmit();await wait(()=>root.querySelector('[role=status]').textContent&&!form.querySelector('button').disabled);
assert(root.contains(form),'Lost response removed retry controls');form.requestSubmit();
await wait(()=>!root.querySelector('.digest-retry form')&&!root.querySelector('.productivity-controls button').disabled);
assert(requests.length===2&&requests[0].request_id===requests[1].request_id,'Retry generated a second request ID');
assert(root.querySelectorAll('tbody tr').length===2,'Duplicate attempt created');
const tick=await fetch('/fixture/tick',{method:'POST'});assert(tick.ok,'Worker failed');
root.querySelector('.productivity-controls button').click();await wait(()=>root.querySelector('.digest-retry form'));
assert(root.querySelector('.digest-retry input').required,'Sent retry must require confirmation');
assert(root.textContent.includes('メールサーバー受付済み')||root.textContent.includes('Accepted by mail server'),'Sent status missing');
assert(!root.textContent.includes('reader@example.test'),'Recipient leaked in history');
assert(d.documentElement.scrollWidth<=w.innerWidth,'Settings overflow viewport');
details.scrollIntoView({block:'start'});document.body.dataset.vimReady='1';
}catch(error){const out=document.createElement('pre');out.id='error';out.textContent=error.stack;document.body.append(out);}
})();
+34
View File
@@ -0,0 +1,34 @@
(async()=>{
const assert=(ok,msg)=>{if(!ok)throw Error(msg);},delay=ms=>new Promise(r=>setTimeout(r,ms));
const wait=async check=>{for(let n=0;n<200;n++){if(check())return;await delay(25);}throw Error('Timed out: '+check);};
const frame=document.createElement('iframe');frame.style='display:block;width:100%;height:100vh;border:0';document.body.append(frame);
try{
await new Promise(resolve=>{frame.onload=resolve;frame.src='/v/plan/dashboard';});
await wait(()=>frame.contentDocument.querySelector('.productivity-table tbody tr'));
const reportTable=frame.contentDocument.querySelector('.productivity-table');
assert(reportTable.querySelectorAll('thead th').length===4,'Project aggregate table missing');
assert(reportTable.querySelector('tbody tr').lastElementChild.textContent==='1.00','Project duration incorrect');
const historyLink=frame.contentDocument.querySelector('a[href="/v/plan/tasks/work"]');assert(historyLink,'Dashboard history link missing');
await new Promise(resolve=>{frame.onload=resolve;historyLink.click();});
const w=frame.contentWindow,d=frame.contentDocument;
await wait(()=>d.querySelector('.work-history-table tbody button'));
assert(d.documentElement.scrollWidth<=w.innerWidth,'History overflows viewport');
d.querySelector('.work-history-table tbody button').click();
let form=d.querySelector('.work-history-edit');assert(form,'Inline correction missing');
const inputs=form.querySelectorAll('input');inputs[2].value='Browser correction';
form.requestSubmit();await wait(()=>!d.querySelector('.work-history-edit')&&!d.getElementById('work-history-list').inert);
d.querySelector('#work-history-list details').open=true;await wait(()=>d.querySelector('#work-history-list details strong'));
assert(d.querySelector('#work-history-list details').textContent.includes('Browser correction'),'Audit missing');
const originalFetch=w.fetch;w.fetch=async(url,options)=>options?.method==='POST'?new w.Response(JSON.stringify({error:'conflict'}),{status:412}):originalFetch(url,options);
d.querySelector('.work-history-table tbody button').click();form=d.querySelector('.work-history-edit');form.querySelector('input[type=text]').value='Retain my input';form.requestSubmit();
await wait(()=>form.querySelector('[role=status]').textContent);assert(form.querySelector('input[type=text]').value==='Retain my input','Conflict discarded input');
w.fetch=originalFetch;w.confirm=()=>true;form.querySelector('button[type=button]').click();
d.querySelectorAll('.work-history-table tbody tr:first-child button')[1].click();form=d.querySelector('.work-history-edit');form.querySelector('input').value='Browser cancellation';form.requestSubmit();await wait(()=>!d.querySelector('.work-history-edit')&&!d.getElementById('work-history-list').inert);
assert(!d.querySelector('.work-history-table tbody tr:first-child button'),'Cancelled work remains editable');
d.querySelector('#work-history-list details').open=true;await wait(()=>d.querySelector('#work-history-list details strong'));
assert(d.querySelector('#work-history-list details').textContent.includes('Browser cancellation'),'Cancellation audit missing');
const report=await (await fetch('/_/api/v1/extension/organizer.tasks/report')).json();assert(report.days.every(day=>day.work_ms===0),'Cancelled time remains in report');
assert(d.documentElement.scrollWidth<=w.innerWidth,'Expanded history overflows viewport');
document.body.dataset.vimReady='1';
}catch(error){const out=document.createElement('pre');out.id='error';out.textContent=error.stack;document.body.append(out);}
})();
+569
View File
@@ -0,0 +1,569 @@
package store
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
"pageswiki/internal/db"
)
const FeedSyncEventType = "feed.source.sync"
var feedMigrations = []db.SQLMigration{{Version: 1, Name: "private_feed_reader", Statements: []string{
`CREATE TABLE feed_sources(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,provider TEXT NOT NULL CHECK(provider IN ('local','freshrss')),title TEXT NOT NULL,folder TEXT NOT NULL,url TEXT NOT NULL,credential TEXT NOT NULL DEFAULT '',version INTEGER NOT NULL,enabled INTEGER NOT NULL,interval_seconds INTEGER NOT NULL,next_ms INTEGER NOT NULL DEFAULT 0,lease TEXT NOT NULL DEFAULT '',lease_until INTEGER NOT NULL DEFAULT 0,last_ms INTEGER NOT NULL DEFAULT 0,error TEXT NOT NULL DEFAULT '',etag TEXT NOT NULL DEFAULT '',modified TEXT NOT NULL DEFAULT '')`,
`CREATE INDEX feed_sources_owner ON feed_sources(domain_uuid,owner_uuid,uuid)`,
`CREATE INDEX feed_sources_due ON feed_sources(enabled,next_ms,uuid)`,
`CREATE TABLE feed_items(source_uuid TEXT NOT NULL REFERENCES feed_sources(uuid) ON DELETE CASCADE,id TEXT NOT NULL,title TEXT NOT NULL,url TEXT NOT NULL,body TEXT NOT NULL,published_ms INTEGER NOT NULL,feed TEXT NOT NULL,folder TEXT NOT NULL,is_read INTEGER NOT NULL,starred INTEGER NOT NULL,PRIMARY KEY(source_uuid,id))`,
`CREATE INDEX feed_items_date ON feed_items(source_uuid,published_ms DESC,id DESC)`,
`CREATE TABLE feed_states(source_uuid TEXT NOT NULL REFERENCES feed_sources(uuid) ON DELETE CASCADE,id TEXT NOT NULL,is_read INTEGER NOT NULL,starred INTEGER NOT NULL,version INTEGER NOT NULL,PRIMARY KEY(source_uuid,id))`,
`CREATE TRIGGER feed_source_delete AFTER DELETE ON feed_sources BEGIN DELETE FROM feed_items WHERE source_uuid=old.uuid; DELETE FROM feed_states WHERE source_uuid=old.uuid; END`,
`CREATE TABLE feed_note_receipts(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,request_id TEXT NOT NULL,input_hash TEXT NOT NULL,page_uuid TEXT NOT NULL,PRIMARY KEY(domain_uuid,owner_uuid,request_id))`,
}, Assertions: []string{schemaColumns("feed_sources", "uuid,domain_uuid,owner_uuid,provider,title,folder,url,credential,version,enabled,interval_seconds,next_ms,lease,lease_until,last_ms,error,etag,modified"), schemaColumns("feed_items", "source_uuid,id,title,url,body,published_ms,feed,folder,is_read,starred"), schemaUnique("feed_items", "source_uuid,id"), schemaUnique("feed_states", "source_uuid,id"), schemaUnique("feed_note_receipts", "domain_uuid,owner_uuid,request_id"), `SELECT 1 WHERE NOT EXISTS(SELECT 1 FROM sqlite_master WHERE type='trigger' AND name='feed_source_delete')`}}, {
Version: 2, Name: "feed_html_body", Statements: []string{
`ALTER TABLE feed_items ADD COLUMN body_html TEXT NOT NULL DEFAULT ''`,
// Text-only caches cannot recover markup from a 304. Request one full
// background fetch while fencing workers started before this migration.
`UPDATE feed_sources SET etag='',modified='',next_ms=0,lease='',lease_until=0,version=version+1`,
}, Assertions: []string{schemaColumns("feed_items", "body_html")},
}, {
Version: 3, Name: "feed_import_receipts", Statements: []string{
`CREATE TABLE feed_import_receipts(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,request_id TEXT NOT NULL,input_hash TEXT NOT NULL,result TEXT NOT NULL,PRIMARY KEY(domain_uuid,owner_uuid,request_id))`,
}, Assertions: []string{schemaUnique("feed_import_receipts", "domain_uuid,owner_uuid,request_id")},
}, {
Version: 4, Name: "freshrss_catalog", Statements: []string{
`ALTER TABLE feed_sources ADD COLUMN remote_catalog TEXT NOT NULL DEFAULT '[]'`,
`ALTER TABLE feed_items ADD COLUMN remote_feed TEXT NOT NULL DEFAULT ''`,
`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")},
}, {
Version: 6, Name: "freshrss_empty_folders", Statements: []string{
`ALTER TABLE feed_sources ADD COLUMN remote_folders TEXT NOT NULL DEFAULT '[]'`,
}, Assertions: []string{schemaColumns("feed_sources", "remote_folders")},
}}
// CaptureFeedNote creates an inert Wiki note and receipt atomically. It does not
// mutate the feed or create a task, and rechecks grants under the writer lock.
func (s *Store) CaptureFeedNote(ctx context.Context, domain, owner, request, source, id, target, title, body string) (string, error) {
if _, e := uuid.Parse(request); e != nil {
return "", ErrTaskInvalid
}
if !ValidTaskWikiPrefix(target) || target == "/" || len(target) > 500 || len(title) > 300 || title == "" || len(body) > 65536 {
return "", ErrTaskInvalid
}
// User/group spaces have additional ownership/publication contracts. Initial
// capture supports ordinary Wiki paths only, rather than bypassing those caps.
first := strings.Split(strings.TrimPrefix(target, "/"), "/")[0]
if first == "u" || first == "g" {
return "", ErrAccessDenied
}
raw, _ := json.Marshal([]string{source, id, target, title, body})
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)
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)
if basis != nil && !errors.Is(basis, ErrAccessDenied) {
return basis
}
if !caps.Decide(target, capability, basis == nil) {
return ErrAccessDenied
}
}
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)
if e == nil {
if oldHash != hash {
return ErrTaskConflict
}
return nil
}
if !errors.Is(e, sql.ErrNoRows) {
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 {
return e
}
if !exists {
return sql.ErrNoRows
}
item := TaskRecord{Title: title}
if e = s.createCapturedTaskWiki(ctx, tx, domain, owner, &item, target, body); e != nil {
return e
}
page = item.PageUUID
_, e = tx.ExecContext(ctx, `INSERT INTO feed_note_receipts VALUES(?,?,?,?,?)`, domain, owner, request, hash, page)
return e
})
if e == nil {
s.InvalidatePathCaches()
}
return page, e
}
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:"-"`
WriteEnabled bool `json:"write_enabled"`
}
type FeedItem struct {
Source string `json:"source"`
ID string `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
Body string `json:"body,omitempty"`
BodyHTML string `json:"body_html,omitempty"`
PublishedMS int64 `json:"published_ms"`
Feed string `json:"feed"`
Folder string `json:"folder"`
Read bool `json:"read"`
Starred bool `json:"starred"`
Version int64 `json:"version"`
Provider string `json:"provider"`
RemoteFeed string `json:"remote_feed,omitempty"`
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,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, &v.WriteEnabled)
return
}
func 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 {
return e
}
var raw string
e := q.QueryRowContext(ctx, `SELECT value FROM config WHERE domain_uuid=? AND key='system_extension_states'`, domain).Scan(&raw)
if e != nil && !errors.Is(e, sql.ErrNoRows) {
return e
}
states := map[string]bool{}
if raw != "" && json.Unmarshal([]byte(raw), &states) != nil {
return ErrAccessDenied
}
if enabled, ok := states["organizer.feeds"]; ok && !enabled {
return ErrAccessDenied
}
return nil
}
func (s *Store) feedWrite(ctx context.Context, domain, owner string, fn func(*sql.Tx) error) error {
if s.ReadOnly {
return ErrReadOnly
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
tx, e := s.DB.BeginTx(ctx, nil)
if e != nil {
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 {
return e
}
if e = feedActor(ctx, tx, domain, owner, true); e != nil {
return e
}
if e = fn(tx); e != nil {
return e
}
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 {
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)
if e != nil {
return nil, e
}
defer rows.Close()
out := []FeedSource{}
for rows.Next() {
v, e := scanFeedSource(rows)
if e != nil {
return nil, e
}
out = append(out, v)
}
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
}
v.Title = strings.TrimSpace(v.Title)
v.Folder = strings.TrimSpace(v.Folder)
if v.Title == "" || len(v.Title) > 200 || len(v.Folder) > 200 || len(v.URL) > 4096 || !utf8.ValidString(v.Title+v.Folder) || v.Interval < 300 || v.Interval > 86400 || (v.Provider != "local" && v.Provider != "freshrss") {
return v, ErrTaskInvalid
}
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))
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
}
if old.Credential != v.Credential {
candidate := v
candidate.Domain = domain
candidate.Owner = owner
u, p, e := s.OpenFeedCredential(candidate)
if e != nil {
return ErrTaskConflict
}
ou, op, e := s.OpenFeedCredential(old)
if e != nil || u != ou || p != op {
return ErrTaskConflict
}
}
return nil
}
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
}
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)
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))
if e != nil {
return e
}
// Changing remote identity requires a new source, not reassignment of cached IDs.
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)
if e != nil {
return e
}
n, _ := result.RowsAffected()
if n != 1 {
return ErrTaskConflict
}
return nil
})
v.Version++
v.Credential = ""
return v, e
}
func (s *Store) FeedSourceAction(ctx context.Context, domain, owner, id, action string, version int64) error {
return s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
var result sql.Result
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())
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)
default:
return ErrTaskInvalid
}
if e != nil {
return e
}
n, _ := result.RowsAffected()
if n != 1 {
return ErrTaskConflict
}
return nil
})
}
func (s *Store) FeedItems(ctx context.Context, domain, owner, source, filter, query string, before int64, beforeID string, scope ...string) ([]FeedItem, error) {
remoteFeed, category := "", ""
if len(scope) > 3 {
return nil, ErrTaskInvalid
}
if len(scope) > 0 {
remoteFeed = scope[0]
}
if len(scope) > 1 {
category = scope[1]
}
mode := ""
if len(scope) > 2 {
mode = scope[2]
}
if mode != "" && (mode != "uncategorized" || source == "" || category != "") {
return nil, ErrTaskInvalid
}
if (remoteFeed != "" && !ValidFeedRemoteID(remoteFeed)) || (category != "" && !ValidFeedRemoteID(category)) || ((remoteFeed != "" || category != "") && source == "") {
return nil, ErrTaskInvalid
}
if len(query) > 200 || len(beforeID) > 1200 {
return nil, ErrTaskInvalid
}
if e := 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}
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)`
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...)
if e != nil {
return nil, e
}
defer rows.Close()
out := []FeedItem{}
for rows.Next() {
var v FeedItem
if e = rows.Scan(&v.Source, &v.ID, &v.Title, &v.URL, &v.PublishedMS, &v.Feed, &v.Folder, &v.Read, &v.Starred, &v.Version, &v.Provider); e != nil {
return nil, e
}
out = append(out, v)
}
return out, rows.Err()
}
func (s *Store) FeedItem(ctx context.Context, domain, owner, source, id string) (FeedItem, error) {
var v FeedItem
if e := 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)
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 {
return e
}
if provider != "local" {
return ErrAccessDenied
}
if version == 0 {
var n int
if e := tx.QueryRowContext(ctx, `SELECT count(*) FROM feed_states WHERE source_uuid=?`, 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)
if e != nil {
return e
}
n, _ := result.RowsAffected()
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)
if e != nil {
return e
}
n, _ = result.RowsAffected()
if n != 1 {
return ErrTaskConflict
}
return nil
})
}
func (s *Store) SealFeedCredential(domain, owner, id, user, password string) (string, error) {
b, _ := json.Marshal([]string{"feed-source-v1", domain, owner, id, user, password})
return s.encryptAuthSecret(b)
}
func (s *Store) OpenFeedCredential(v FeedSource) (string, string, error) {
b, e := s.decryptAuthSecret(v.Credential)
if e != nil {
return "", "", e
}
var f []string
if json.Unmarshal(b, &f) != nil || len(f) != 6 || f[0] != "feed-source-v1" || f[1] != v.Domain || f[2] != v.Owner || f[3] != v.UUID {
return "", "", ErrAccessDenied
}
return f[4], f[5], nil
}
func (s *Store) EnsureFeedEvent() error {
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)
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())
if e != nil {
return nil, e
}
defer rows.Close()
out := []FeedSource{}
for rows.Next() {
v, e := scanFeedSource(rows)
if e != nil {
return nil, e
}
out = append(out, v)
}
return out, rows.Err()
}
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)
if e != nil {
return e
}
n, _ := result.RowsAffected()
if n != 1 {
return ErrTaskConflict
}
return nil
})
return v, e
}
// Failed fetches preserve the previous cache. Version and claim token fence stale workers.
func (s *Store) CompleteFeed(ctx context.Context, v FeedSource, items []FeedItem, etag, modified, code string, unchanged bool, catalogs ...FeedCatalogSnapshot) error {
if len(catalogs) > 1 {
return ErrTaskInvalid
}
var catalog, folders string
if len(catalogs) == 1 {
var e error
catalog, folders, e = catalogs[0].Encode()
if e != nil {
return e
}
if v.Provider == "freshrss" {
membership := map[string][]string{}
for _, sub := range catalogs[0].Subscriptions {
for _, c := range sub.Categories {
membership[sub.ID] = append(membership[sub.ID], c.ID)
}
}
items = append([]FeedItem(nil), items...)
for n := range items {
items[n].RemoteCategories = membership[items[n].RemoteFeed]
}
}
}
if len(items) > 500 || len(etag) > 1024 || len(modified) > 1024 {
return ErrTaskInvalid
}
switch code {
case "", "network", "invalid_feed", "credentials", "remote_error", "limit":
default:
return ErrTaskInvalid
}
for _, i := range items {
if (i.RemoteFeed != "" && !ValidFeedRemoteID(i.RemoteFeed)) || len(i.RemoteCategories) > 64 {
return ErrTaskInvalid
}
for _, c := range i.RemoteCategories {
if !ValidFeedRemoteID(c) {
return ErrTaskInvalid
}
}
if i.ID == "" || len(i.ID) > 1024 || len(i.Title) > 2048 || len(i.URL) > 4096 || len(i.Body) > 32768 || len(i.BodyHTML) > 128<<10 || len(i.Feed) > 512 || len(i.Folder) > 512 || i.PublishedMS < 0 {
return ErrTaskInvalid
}
}
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)
if e != nil {
return e
}
n, _ := result.RowsAffected()
if n != 1 {
return ErrTaskConflict
}
if code != "" || unchanged {
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 {
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 {
return e
}
}
for _, i := range items {
categories := i.RemoteCategories
if categories == nil {
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))
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)
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.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)
})
}
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
})
}
+150
View File
@@ -0,0 +1,150 @@
package store
import (
"context"
"encoding/json"
"strings"
"unicode/utf8"
)
// Remote IDs are opaque identities, not URLs to fetch or display labels.
type FeedCategory struct {
ID string `json:"id"`
Label string `json:"label"`
}
type FeedRemoteSubscription struct {
ID string `json:"id"`
Title string `json:"title"`
Categories []FeedCategory `json:"categories"`
}
type FeedCatalogSnapshot struct {
Subscriptions []FeedRemoteSubscription `json:"subscriptions"`
Folders []FeedCategory `json:"folders"`
}
// Merge membership-derived folders with explicit folders, including empty
// ones. IDs, not labels or slash-delimited paths, are the identity boundary.
func FeedFolders(catalog []FeedRemoteSubscription, folders []FeedCategory) ([]FeedCategory, error) {
out := []FeedCategory{}
seen := map[string]string{}
add := func(c FeedCategory) error {
if !ValidFeedRemoteID(c.ID) || len(c.Label) > 512 || !utf8.ValidString(c.Label) {
return ErrTaskInvalid
}
if label, ok := seen[c.ID]; ok {
if label != c.Label {
return ErrTaskInvalid
}
return nil
}
seen[c.ID] = c.Label
out = append(out, c)
if len(out) > 1000 {
return ErrTaskDAVLimit
}
return nil
}
if len(folders) > 1000 {
return nil, ErrTaskDAVLimit
}
for _, c := range folders {
if e := add(c); e != nil {
return nil, e
}
}
for _, sub := range catalog {
for _, c := range sub.Categories {
if e := add(c); e != nil {
return nil, e
}
}
}
return out, nil
}
func ValidFeedRemoteID(id string) bool {
return id != "" && len(id) <= 2048 && utf8.ValidString(id) && !strings.ContainsAny(id, "\x00\r\n")
}
// Bound both the number of entries and the encoded snapshot independently of
// the HTTP response limit. Invalid snapshots must not erase a good cache.
func FeedCatalogJSON(catalog []FeedRemoteSubscription) (string, error) {
if len(catalog) > 1000 {
return "", ErrTaskDAVLimit
}
seen := map[string]bool{}
labels := map[string]string{}
for _, sub := range catalog {
if !ValidFeedRemoteID(sub.ID) || seen[sub.ID] || len(sub.Title) > 512 || !utf8.ValidString(sub.Title) || len(sub.Categories) > 64 {
return "", ErrTaskInvalid
}
seen[sub.ID] = true
cats := map[string]bool{}
for _, c := range sub.Categories {
if !ValidFeedRemoteID(c.ID) || cats[c.ID] || len(c.Label) > 512 || !utf8.ValidString(c.Label) {
return "", ErrTaskInvalid
}
if label, ok := labels[c.ID]; ok && label != c.Label {
return "", ErrTaskInvalid
}
cats[c.ID], labels[c.ID] = true, c.Label
}
}
if catalog == nil {
catalog = []FeedRemoteSubscription{}
}
raw, e := json.Marshal(catalog)
if e != nil {
return "", e
}
if len(raw) > 1<<20 {
return "", ErrTaskDAVLimit
}
return string(raw), nil
}
// Validate the complete snapshot before persisting or reporting fetch success.
func (v FeedCatalogSnapshot) Encode() (string, string, error) {
catalog, e := FeedCatalogJSON(v.Subscriptions)
if e != nil {
return "", "", e
}
folders, e := FeedFolders(v.Subscriptions, v.Folders)
if e != nil {
return "", "", e
}
raw, e := json.Marshal(folders)
if e != nil {
return "", "", e
}
if len(raw)+len(catalog) > 1<<20 {
return "", "", ErrTaskDAVLimit
}
return catalog, string(raw), nil
}
func (s *Store) FeedCatalog(ctx context.Context, domain, owner, source string) ([]FeedRemoteSubscription, error) {
v, e := s.FeedCatalogSnapshot(ctx, domain, owner, source)
return v.Subscriptions, e
}
func (s *Store) FeedCatalogSnapshot(ctx context.Context, domain, owner, source string) (FeedCatalogSnapshot, error) {
v := FeedCatalogSnapshot{}
if e := feedActor(ctx, s.DB, domain, owner, false); e != nil {
return v, e
}
var raw, folders string
if e := s.DB.QueryRowContext(ctx, `SELECT remote_catalog,remote_folders FROM feed_sources WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, source, domain, owner).Scan(&raw, &folders); e != nil {
return v, e
}
if e := json.Unmarshal([]byte(raw), &v.Subscriptions); e != nil {
return v, e
}
if e := json.Unmarshal([]byte(folders), &v.Folders); e != nil {
return v, e
}
var e error
v.Folders, e = FeedFolders(v.Subscriptions, v.Folders)
return v, e
}
+109
View File
@@ -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)
}
+148
View File
@@ -0,0 +1,148 @@
package store
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/url"
"sort"
"strings"
"unicode/utf8"
"github.com/google/uuid"
)
type FeedSubscription struct {
Title string `json:"title"`
Folder string `json:"folder"`
URL string `json:"url"`
}
// Only normalize syntactic equivalence; never reorder query parameters, change
// path case or follow redirects to decide identity.
func FeedURLKey(raw string) string {
u, err := url.Parse(strings.TrimSpace(raw))
if err != nil {
return raw
}
u.Scheme = strings.ToLower(u.Scheme)
u.Host = strings.ToLower(u.Host)
if u.Scheme == "https" && u.Port() == "443" {
u.Host = strings.TrimSuffix(u.Host, ":443")
}
if u.Path == "" {
u.Path = "/"
}
u.Fragment = ""
return u.String()
}
func FeedSourcesSnapshot(sources []FeedSource) string {
versions := make([]string, 0, len(sources))
for _, s := range sources {
versions = append(versions, fmt.Sprintf("%s:%d", s.UUID, s.Version))
}
sort.Strings(versions)
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(versions, "\n"))))
}
type FeedImport struct {
RequestID string `json:"request_id"`
Expected string `json:"expected"`
Entries []FeedSubscription `json:"entries"`
Enabled bool `json:"enabled"`
Interval int `json:"interval_seconds"`
}
// Append-only import: source inserts and the durable retry receipt commit as a
// unit. A retry after later source deletion returns its receipt, never resurrects
// the deleted subscription. Authorization is checked even on receipt replay.
func (s *Store) ImportFeedSubscriptions(ctx context.Context, domain, owner string, input FeedImport) ([]string, error) {
if _, err := uuid.Parse(input.RequestID); err != nil {
return nil, ErrTaskInvalid
}
if len(input.Expected) != 64 || len(input.Entries) == 0 || len(input.Entries) > 32 || input.Interval < 300 || input.Interval > 86400 {
return nil, ErrTaskInvalid
}
keys := map[string]bool{}
for _, entry := range input.Entries {
u, err := url.Parse(entry.URL)
if err != nil || u.Scheme != "https" || u.Hostname() == "" || u.User != nil || u.Fragment != "" || (u.Port() != "" && u.Port() != "443") || len(entry.URL) > 4096 || strings.TrimSpace(entry.Title) == "" || len(entry.Title) > 200 || len(entry.Folder) > 200 || !utf8.ValidString(entry.Title+entry.Folder+entry.URL) {
return nil, ErrTaskInvalid
}
key := FeedURLKey(entry.URL)
if keys[key] {
return nil, ErrTaskInvalid
}
keys[key] = true
}
raw, _ := json.Marshal(input)
hash := fmt.Sprintf("%x", sha256.Sum256(raw))
ids := []string{}
err := s.feedWrite(ctx, domain, owner, func(tx *sql.Tx) error {
var oldHash, result string
err := tx.QueryRowContext(ctx, `SELECT input_hash,result FROM feed_import_receipts WHERE domain_uuid=? AND owner_uuid=? AND request_id=?`, domain, owner, input.RequestID).Scan(&oldHash, &result)
if err == nil {
if oldHash != hash {
return ErrTaskConflict
}
return json.Unmarshal([]byte(result), &ids)
}
if !errors.Is(err, sql.ErrNoRows) {
return err
}
rows, err := tx.QueryContext(ctx, `SELECT uuid,version,provider,url FROM feed_sources WHERE domain_uuid=? AND owner_uuid=?`, domain, owner)
if err != nil {
return err
}
existing := []FeedSource{}
for rows.Next() {
var source FeedSource
if err = rows.Scan(&source.UUID, &source.Version, &source.Provider, &source.URL); err != nil {
rows.Close()
return err
}
existing = append(existing, source)
}
err = rows.Err()
rows.Close()
if err != nil {
return err
}
if FeedSourcesSnapshot(existing) != input.Expected {
return ErrTaskConflict
}
if len(existing)+len(input.Entries) > 32 {
return ErrTaskDAVLimit
}
for _, source := range existing {
if source.Provider == "local" && keys[FeedURLKey(source.URL)] {
return ErrTaskConflict
}
}
var receipts int
if err = tx.QueryRowContext(ctx, `SELECT count(*) FROM feed_import_receipts WHERE domain_uuid=? AND owner_uuid=?`, domain, owner).Scan(&receipts); err != nil {
return err
}
if receipts >= 1000 {
return ErrTaskDAVLimit
}
for _, entry := range input.Entries {
id := uuid.NewString()
if _, err = tx.ExecContext(ctx, `INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,version,enabled,interval_seconds) VALUES(?,?,?,'local',?,?,?,1,?,?)`, id, domain, owner, entry.Title, entry.Folder, entry.URL, input.Enabled, input.Interval); err != nil {
return err
}
ids = append(ids, id)
}
encoded, _ := json.Marshal(ids)
_, err = tx.ExecContext(ctx, `INSERT INTO feed_import_receipts VALUES(?,?,?,?,?)`, domain, owner, input.RequestID, hash, string(encoded))
return err
})
if err != nil {
return nil, err
}
return ids, nil
}
+228
View File
@@ -0,0 +1,228 @@
package store
import (
"context"
"testing"
"pageswiki/internal/db"
)
func TestFeedMigrationAtomicRepeatAndCleanup(t *testing.T) {
st := newSchemaTestStore(t)
ctx := context.Background()
// Fail after creating the tables, and verify the entire migration rolls back.
broken := append([]db.SQLMigration(nil), feedMigrations...)
broken[0].Statements = append(append([]string(nil), feedMigrations[0].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`)
if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil {
t.Fatal("expected injected failure")
}
var count int
if e := st.DB.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name='feed_sources'`).Scan(&count); e != nil || count != 0 {
t.Fatal("partial schema", count, e)
}
// Upgrade an existing v1 cache, not just an empty database.
if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:1]); 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','local','title','','https://example.org',1,1,300); INSERT INTO feed_items VALUES('s','i','title','','body',0,'','',0,0); INSERT INTO feed_states VALUES('s','i',1,1,1)`); e != nil {
t.Fatal(e)
}
if _, e := st.DB.Exec(`UPDATE feed_sources SET etag='old',modified='date',next_ms=999,lease='old-worker',lease_until=999`); e != nil {
t.Fatal(e)
}
broken = append([]db.SQLMigration(nil), feedMigrations...)
broken[1].Statements = append(append([]string(nil), feedMigrations[1].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`)
if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil {
t.Fatal("expected v2 failure")
}
if e := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('feed_items') WHERE name='body_html'`).Scan(&count); e != nil || count != 0 {
t.Fatal("partial v2 schema", count, e)
}
var oldETag string
if e := st.DB.QueryRow(`SELECT etag FROM feed_sources WHERE uuid='s'`).Scan(&oldETag); e != nil || oldETag != "old" {
t.Fatal("partial v2 metadata", oldETag, e)
}
for range 2 {
if e := st.ensureSchemaComponent("feeds"); e != nil {
t.Fatal(e)
}
}
var body, markup, etag, lease string
var version, next int64
if e := st.DB.QueryRow(`SELECT body,body_html FROM feed_items WHERE source_uuid='s'`).Scan(&body, &markup); e != nil || body != "body" || markup != "" {
t.Fatal("body migration", body, markup, e)
}
if e := st.DB.QueryRow(`SELECT version,next_ms,etag,lease FROM feed_sources WHERE uuid='s'`).Scan(&version, &next, &etag, &lease); e != nil || version != 2 || next != 0 || etag != "" || lease != "" {
t.Fatal("refetch/fence", version, next, etag, lease, e)
}
if e := st.DB.QueryRow(`SELECT count(*) FROM feed_items`).Scan(&count); e != nil || count != 1 {
t.Fatal("lost data", e)
}
if _, e := st.DB.Exec(`PRAGMA foreign_keys=OFF; DELETE FROM feed_sources WHERE uuid='s'`); e != nil {
t.Fatal(e)
}
for _, table := range []string{"feed_items", "feed_states"} {
if e := st.DB.QueryRow(`SELECT count(*) FROM ` + table).Scan(&count); e != nil || count != 0 {
t.Fatal("orphan", table, count, e)
}
}
var result string
if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&result); e != nil || result != "ok" {
t.Fatal(result, e)
}
}
func TestFeedImportReceiptMigrationRollbackAndRepeat(t *testing.T) {
st := newSchemaTestStore(t)
ctx := context.Background()
if err := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:2]); err != nil {
t.Fatal(err)
}
if _, err := st.DB.Exec(`INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,version,enabled,interval_seconds) VALUES('s','d','u','local','title','','https://example.org',7,1,300)`); err != nil {
t.Fatal(err)
}
broken := append([]db.SQLMigration(nil), feedMigrations...)
broken[2].Statements = append(append([]string(nil), feedMigrations[2].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`)
if err := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); err == nil {
t.Fatal("expected v3 failure")
}
var count int
if err := st.DB.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name='feed_import_receipts'`).Scan(&count); err != nil || count != 0 {
t.Fatal("partial migration", count, err)
}
if err := st.ensureSchemaComponent("feeds"); err != nil {
t.Fatal(err)
}
if _, err := st.DB.Exec(`INSERT INTO feed_import_receipts VALUES('d','u','request','hash','["s"]')`); err != nil {
t.Fatal(err)
}
if err := st.ensureSchemaComponent("feeds"); err != nil {
t.Fatal(err)
}
if err := st.DB.QueryRow(`SELECT version FROM feed_sources WHERE uuid='s'`).Scan(&count); err != nil || count != 7 {
t.Fatal("unrelated data changed", count, err)
}
if _, err := st.DB.Exec(`DELETE FROM feed_sources WHERE uuid='s'`); err != nil {
t.Fatal(err)
}
if err := st.DB.QueryRow(`SELECT count(*) FROM feed_import_receipts`).Scan(&count); err != nil || count != 1 {
t.Fatal("receipt must survive deletion", count, err)
}
}
func TestFeedCatalogMigrationRollbackAndRepeat(t *testing.T) {
st := newSchemaTestStore(t)
ctx := context.Background()
if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:3]); 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,lease,next_ms) VALUES('s','d','u','freshrss','Title','','https://example.org',7,1,300,'old',999); INSERT INTO feed_items(source_uuid,id,title,url,body,published_ms,feed,folder,is_read,starred,body_html) VALUES('s','i','Title','','Body',1,'','',1,1,'<p>Body</p>')`); e != nil {
t.Fatal(e)
}
broken := append([]db.SQLMigration(nil), feedMigrations...)
broken[3].Statements = append(append([]string(nil), feedMigrations[3].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_items') WHERE name='remote_feed'`).Scan(&count); e != nil || count != 0 {
t.Fatal("partial migration", count, e)
}
if e := st.DB.QueryRow(`SELECT version FROM feed_sources WHERE uuid='s'`).Scan(&count); e != nil || count != 7 {
t.Fatal("partial fence", count, e)
}
for range 2 {
if e := st.ensureSchemaComponent("feeds"); e != nil {
t.Fatal(e)
}
}
var version, next int
var catalog, lease, body string
var read, star bool
if e := st.DB.QueryRow(`SELECT version,next_ms,lease,remote_catalog FROM feed_sources WHERE uuid='s'`).Scan(&version, &next, &lease, &catalog); e != nil || version != 8 || next != 0 || lease != "" || catalog != "[]" {
t.Fatal(version, next, lease, catalog, e)
}
if e := st.DB.QueryRow(`SELECT body_html,is_read,starred FROM feed_items WHERE source_uuid='s'`).Scan(&body, &read, &star); e != nil || body != "<p>Body</p>" || !read || !star {
t.Fatal("lost old cache", body, read, star, e)
}
var integrity string
if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); e != nil || integrity != "ok" {
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)
}
}
func TestFeedFoldersMigrationRollbackAndRepeat(t *testing.T) {
st := newSchemaTestStore(t)
ctx := context.Background()
if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:5]); e != nil {
t.Fatal(e)
}
const catalog = `[{"id":"feed/1","title":"Title","categories":[{"id":"category","label":"Folder"}]}]`
if _, e := st.DB.Exec(`INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,version,enabled,interval_seconds,lease,next_ms,remote_catalog) VALUES('s','d','u','freshrss','Title','','https://example.org',7,1,300,'worker',999,?);
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,'queued',1)`, catalog); e != nil {
t.Fatal(e)
}
broken := append([]db.SQLMigration(nil), feedMigrations...)
broken[5].Statements = append(append([]string(nil), feedMigrations[5].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='remote_folders'`).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)
}
}
var version, next int
var got, folders, lease, status string
if e := st.DB.QueryRow(`SELECT version,next_ms,lease,remote_catalog,remote_folders FROM feed_sources WHERE uuid='s'`).Scan(&version, &next, &lease, &got, &folders); e != nil || version != 7 || next != 999 || lease != "worker" || got != catalog || folders != "[]" {
t.Fatal("existing state changed", version, next, lease, got, folders, e)
}
if e := st.DB.QueryRow(`SELECT status FROM feed_actions WHERE request_id='r'`).Scan(&status); e != nil || status != "queued" {
t.Fatal("pending action changed", status, e)
}
if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&status); e != nil || status != "ok" {
t.Fatal(status, e)
}
}
+2 -2
View File
@@ -21,13 +21,13 @@ func DefaultDashboard() []DashboardWidget {
} }
func ValidateDashboard(items []DashboardWidget) error { func ValidateDashboard(items []DashboardWidget) error {
if len(items) > 6 { if len(items) > 7 {
return ErrTaskInvalid return ErrTaskInvalid
} }
seen := map[string]bool{} seen := map[string]bool{}
for _, item := range items { for _, item := range items {
switch item.ID { switch item.ID {
case "progress", "today", "tomorrow", "tasks", "schedule", "activity": case "progress", "today", "tomorrow", "tasks", "schedule", "activity", "feeds":
default: default:
return ErrTaskInvalid return ErrTaskInvalid
} }
+1
View File
@@ -12,6 +12,7 @@ import (
// Once released, add a new version; never edit an applied definition. // Once released, add a new version; never edit an applied definition.
// Auth, City, tasks and calendars remain in their existing migration paths. // Auth, City, tasks and calendars remain in their existing migration paths.
var managedStoreSchemas = map[string][]db.SQLMigration{ var managedStoreSchemas = map[string][]db.SQLMigration{
"feeds": feedMigrations,
"contacts": contactMigrations, "contacts": contactMigrations,
"task-productivity": taskProductivityMigrations, "task-productivity": taskProductivityMigrations,
"cli-auth": cliAuthMigrations, "cli-auth": cliAuthMigrations,
+82
View File
@@ -110,6 +110,88 @@ func TestDashboardUpgradePreservesTaskPreferences(t *testing.T) {
} }
} }
func TestWorkHistoryUpgradePreservesLegacySessions(t *testing.T) {
st := newSchemaTestStore(t)
if err := db.ApplySQLiteMigrations(context.Background(), st.DB, "task-productivity", taskProductivityMigrations[:2]); err != nil {
t.Fatal(err)
}
if _, err := st.DB.Exec(`INSERT INTO organizer_work_sessions VALUES('w','d','u','t',100,200,2)`); err != nil {
t.Fatal(err)
}
for range 2 {
if err := st.ensureSchemaComponent("task-productivity"); err != nil {
t.Fatal(err)
}
}
var start, end, version, cancelled int64
if err := st.DB.QueryRow(`SELECT start_ms,end_ms,version,cancelled_ms FROM organizer_work_sessions WHERE uuid='w'`).Scan(&start, &end, &version, &cancelled); err != nil || start != 100 || end != 200 || version != 2 || cancelled != 0 {
t.Fatal(start, end, version, cancelled, err)
}
var count int
if err := st.DB.QueryRow(`SELECT count(*) FROM organizer_work_revisions`).Scan(&count); err != nil || count != 0 {
t.Fatal("invented history", count, err)
}
}
func TestDigestHistoryUpgradePreservesReservations(t *testing.T) {
st := newSchemaTestStore(t)
ctx := context.Background()
if err := db.ApplySQLiteMigrations(ctx, st.DB, "task-productivity", taskProductivityMigrations[:3]); err != nil {
t.Fatal(err)
}
for i, state := range []string{"sent", "sending", "uncertain", "cancelled"} {
if _, err := st.DB.Exec(`INSERT INTO organizer_digest_deliveries VALUES(?,?,?, ?,?)`, "d", state, "2026-11-01", state, 100+i); err != nil {
t.Fatal(err)
}
}
for range 2 {
if err := st.ensureSchemaComponent("task-productivity"); err != nil {
t.Fatal(err)
}
}
var count int
if err := st.DB.QueryRow(`SELECT count(*) FROM organizer_digest_deliveries d JOIN organizer_digest_attempts a ON a.uuid=d.attempt_uuid AND a.domain_uuid=d.domain_uuid AND a.owner_uuid=d.owner_uuid AND a.day=d.day AND a.state=d.state AND a.updated_ms=d.updated_ms WHERE a.kind='legacy' AND a.preference_version=0`).Scan(&count); err != nil || count != 4 {
t.Fatal("reservation migration", count, err)
}
var integrity string
if err := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); err != nil || integrity != "ok" {
t.Fatal(integrity, err)
}
}
func TestDigestHistoryMigrationRollback(t *testing.T) {
st := newSchemaTestStore(t)
ctx := context.Background()
if err := db.ApplySQLiteMigrations(ctx, st.DB, "task-productivity", taskProductivityMigrations[:3]); err != nil {
t.Fatal(err)
}
if _, err := st.DB.Exec(`INSERT INTO organizer_digest_deliveries VALUES('d','u','2026-11-01','sent',123)`); err != nil {
t.Fatal(err)
}
steps := append([]db.SQLMigration(nil), taskProductivityMigrations...)
steps[3].Statements = append(append([]string(nil), steps[3].Statements...), `INVALID MIGRATION STATEMENT`)
if err := db.ApplySQLiteMigrations(ctx, st.DB, "task-productivity", steps); err == nil {
t.Fatal("failed migration committed")
}
var columns, ledger, attempts int
if err := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('organizer_digest_deliveries')`).Scan(&columns); err != nil || columns != 5 {
t.Fatal("DDL not rolled back", columns, err)
}
if err := st.DB.QueryRow(`SELECT count(*) FROM schema_migration_steps WHERE component='task-productivity'`).Scan(&ledger); err != nil || ledger != 3 {
t.Fatal(ledger, err)
}
if err := st.DB.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name='organizer_digest_attempts'`).Scan(&attempts); err != nil || attempts != 0 {
t.Fatal(attempts, err)
}
var state string
if err := st.DB.QueryRow(`SELECT state FROM organizer_digest_deliveries`).Scan(&state); err != nil || state != "sent" {
t.Fatal(state, err)
}
if err := st.ensureSchemaComponent("task-productivity"); err != nil {
t.Fatal("retry migration", err)
}
}
func TestManagedSchemasFreshRepeatAndReadOnly(t *testing.T) { func TestManagedSchemasFreshRepeatAndReadOnly(t *testing.T) {
for component := range managedStoreSchemas { for component := range managedStoreSchemas {
t.Run(component, func(t *testing.T) { t.Run(component, func(t *testing.T) {
+3
View File
@@ -111,6 +111,9 @@ func NewWithDocumentRoot(
if err := st.ensureSchemaComponent("contacts"); err != nil { if err := st.ensureSchemaComponent("contacts"); err != nil {
panic(err) panic(err)
} }
if err := st.ensureSchemaComponent("feeds"); err != nil {
panic(err)
}
if err := st.EnsureBrowserNavigationSchema(); err != nil { if err := st.EnsureBrowserNavigationSchema(); err != nil {
panic(err) panic(err)
} }
+29 -1
View File
@@ -35,6 +35,34 @@ func ValidTaskWikiPrefix(p string) bool {
return true return true
} }
// Expand only the documented placeholders. Subject is one safe path component,
// never a source of separators or URI operators. Callers choose the timezone.
func ExpandWikiPrefix(template, subject string, now time.Time) (string, error) {
if len(template) > 500 || !utf8.ValidString(template) || !utf8.ValidString(subject) {
return "", ErrTaskInvalid
}
name := strings.Map(func(r rune) rune {
if unicode.IsControl(r) || unicode.IsSpace(r) || strings.ContainsRune(`/\?#%:{}<>"|`, r) {
return '-'
}
return r
}, subject)
name = strings.Trim(name, " .-_")
for len(name) > 180 {
_, n := utf8.DecodeLastRuneInString(name)
name = name[:len(name)-n]
}
name = strings.Trim(name, " .-_")
if name == "" {
name = "untitled"
}
result := strings.NewReplacer("{YYYY}", now.Format("2006"), "{MM}", now.Format("01"), "{DD}", now.Format("02"), "{Subject}", name).Replace(template)
if strings.ContainsAny(result, "{}") || !ValidTaskWikiPrefix(result) {
return "", ErrTaskInvalid
}
return result, nil
}
// The CLI derives the request UUID from origin, user and Message-ID (or a raw // The CLI derives the request UUID from origin, user and Message-ID (or a raw
// mail hash). Keep subjects in the title, never in the URL's identity segment. // mail hash). Keep subjects in the title, never in the URL's identity segment.
func TaskCaptureWikiPath(prefix, title, request string) string { func TaskCaptureWikiPath(prefix, title, request string) string {
@@ -73,7 +101,7 @@ func (s *Store) createCapturedTaskWiki(ctx context.Context, tx *sql.Tx, domain,
} }
content := "<h1>" + escape(item.Title) + "</h1>\n<pre>" + escape(body) + "</pre>\n" content := "<h1>" + escape(item.Title) + "</h1>\n<pre>" + escape(body) + "</pre>\n"
now := time.Now() now := time.Now()
rev := &Revision{UUID: uuid.NewString(), ObjectUUID: item.PageUUID, DocumentHash: revisionHash(content), Language: "und", ContentType: "text/html; charset=utf-8", Body: content, Message: "Capture mail as task Wiki", CreatedAt: now.Unix(), CreatedAtMS: now.UnixMilli()} rev := &Revision{UUID: uuid.NewString(), ObjectUUID: item.PageUUID, DocumentHash: revisionHash(content), Language: "und", ContentType: "text/html; charset=utf-8", Body: content, Message: "Capture attributed text as Wiki", CreatedAt: now.Unix(), CreatedAtMS: now.UnixMilli()}
if err := s.persistDocument(rev.DocumentHash, rev.Body); err != nil { if err := s.persistDocument(rev.DocumentHash, rev.Body); err != nil {
return err return err
} }
+36 -12
View File
@@ -6,6 +6,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"time" "time"
"github.com/google/uuid"
) )
const TaskDigestEventType = "task.digest.deliver" const TaskDigestEventType = "task.digest.deliver"
@@ -16,9 +18,10 @@ type TaskDigestDue struct {
} }
type TaskDigestClaim struct { type TaskDigestClaim struct {
TaskDigestDue TaskDigestDue
Day string Day string
Version int64 Version int64
Timezone string Timezone string
AttemptUUID string
} }
func (s *Store) EnsureTaskDigestEvent() error { func (s *Store) EnsureTaskDigestEvent() error {
@@ -124,11 +127,17 @@ func (s *Store) ClaimTaskDigest(ctx context.Context, due TaskDigestDue, now time
} }
return out, sql.ErrNoRows return out, sql.ErrNoRows
} }
res, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_digest_deliveries VALUES(?,?,?,'sending',?)`, due.Domain, due.Owner, out.Day, now.UnixMilli()) out.AttemptUUID = uuid.NewString()
res, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_digest_deliveries(domain_uuid,owner_uuid,day,state,updated_ms,attempt_uuid) VALUES(?,?,?,'sending',?,?)`, due.Domain, due.Owner, out.Day, now.UnixMilli(), out.AttemptUUID)
if err != nil { if err != nil {
return out, err return out, err
} }
n, _ = res.RowsAffected() n, _ = res.RowsAffected()
if n == 1 {
if err = insertDigestAttempt(ctx, tx, out, "scheduled", "sending", now); err != nil {
return out, err
}
}
if err = tx.Commit(); err != nil { if err = tx.Commit(); err != nil {
return out, err return out, err
} }
@@ -139,12 +148,20 @@ func (s *Store) ClaimTaskDigest(ctx context.Context, due TaskDigestDue, now time
} }
func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (string, error) { func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (string, error) {
if err := taskActor(ctx, s.DB, c.Domain, c.Owner, true); err != nil { if s.ReadOnly {
return "", ErrReadOnly
}
tx, err := s.DB.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return "", err
}
defer tx.Rollback()
if err := taskActor(ctx, tx, c.Domain, c.Owner, true); err != nil {
return "", err return "", err
} }
var raw, email string var raw, email string
var version int64 var version int64
err := s.DB.QueryRowContext(ctx, `SELECT f.data,f.version,p.email FROM organizer_task_preferences f JOIN principals p ON p.uuid=f.owner_uuid WHERE f.domain_uuid=? AND f.owner_uuid=?`, c.Domain, c.Owner).Scan(&raw, &version, &email) err = tx.QueryRowContext(ctx, `SELECT f.data,f.version,p.email FROM organizer_task_preferences f JOIN principals p ON p.uuid=f.owner_uuid JOIN organizer_digest_deliveries d ON d.domain_uuid=f.domain_uuid AND d.owner_uuid=f.owner_uuid JOIN organizer_digest_attempts a ON a.uuid=d.attempt_uuid WHERE f.domain_uuid=? AND f.owner_uuid=? AND d.day=? AND d.attempt_uuid=? AND d.state='sending' AND a.state='sending' AND a.updated_ms>?`, c.Domain, c.Owner, c.Day, c.AttemptUUID, time.Now().Add(-2*time.Minute).UnixMilli()).Scan(&raw, &version, &email)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -152,15 +169,22 @@ func (s *Store) TaskDigestRecipient(ctx context.Context, c TaskDigestClaim) (str
if json.Unmarshal([]byte(raw), &p) != nil || !p.DigestEnabled || version != c.Version { if json.Unmarshal([]byte(raw), &p) != nil || !p.DigestEnabled || version != c.Version {
return "", ErrAccessDenied return "", ErrAccessDenied
} }
return email, nil loc, err := time.LoadLocation(c.Timezone)
if err != nil || time.Now().In(loc).Format("2006-01-02") != c.Day {
return "", ErrAccessDenied
}
on, err := taskReminderExtensionEnabled(ctx, tx, c.Domain)
if err != nil {
return "", err
}
if !on {
return "", ErrAccessDenied
}
return email, tx.Commit()
} }
func (s *Store) FinishTaskDigest(ctx context.Context, c TaskDigestClaim, state string) error { func (s *Store) FinishTaskDigest(ctx context.Context, c TaskDigestClaim, state string) error {
if state != "sent" && state != "uncertain" && state != "cancelled" { return s.FinishTaskDigestReason(ctx, c, state, "", time.Now())
return ErrTaskInvalid
}
_, err := s.DB.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND state='sending'`, state, time.Now().UnixMilli(), c.Domain, c.Owner, c.Day)
return err
} }
func (s *Store) LastTaskDigest(ctx context.Context, domain, actor string) (map[string]any, error) { func (s *Store) LastTaskDigest(ctx context.Context, domain, actor string) (map[string]any, error) {
+382
View File
@@ -0,0 +1,382 @@
package store
import (
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"errors"
"time"
"github.com/google/uuid"
)
const TaskDigestRetentionDays = 90
type TaskDigestAttempt struct {
UUID string `json:"uuid"`
Day string `json:"day"`
Kind string `json:"kind"`
State string `json:"state"`
Reason string `json:"reason"`
Timezone string `json:"timezone"`
CreatedMS int64 `json:"created_ms"`
UpdatedMS int64 `json:"updated_ms"`
}
type TaskDigestRetry struct {
RequestID string `json:"request_id"`
Day string `json:"day"`
ExpectedAttempt string `json:"expected_attempt"`
DuplicateAcknowledged bool `json:"duplicate_acknowledged"`
}
type TaskDigestHistory struct {
Items []TaskDigestAttempt `json:"items"`
Next string `json:"next"`
Today string `json:"today"`
Current *TaskDigestAttempt `json:"current"`
Retries int `json:"retries"`
Enabled bool `json:"enabled"`
RetentionDays int `json:"retention_days"`
}
const digestAttemptColumns = `uuid,day,kind,state,reason,timezone,created_ms,updated_ms`
func scanDigestAttempt(row interface{ Scan(...any) error }) (TaskDigestAttempt, error) {
var v TaskDigestAttempt
err := row.Scan(&v.UUID, &v.Day, &v.Kind, &v.State, &v.Reason, &v.Timezone, &v.CreatedMS, &v.UpdatedMS)
return v, err
}
func insertDigestAttempt(ctx context.Context, tx *sql.Tx, c TaskDigestClaim, kind, state string, now time.Time) error {
_, err := tx.ExecContext(ctx, `INSERT INTO organizer_digest_attempts(uuid,domain_uuid,owner_uuid,day,kind,state,preference_version,timezone,created_ms,updated_ms) VALUES(?,?,?,?,?,?,?,?,?,?)`, c.AttemptUUID, c.Domain, c.Owner, c.Day, kind, state, c.Version, c.Timezone, now.UnixMilli(), now.UnixMilli())
return err
}
// Receipt lookup is read-only. It lets clients confirm an accepted request even
// when SMTP is subsequently disabled; it never reactivates a delivery.
func (s *Store) TaskDigestRetryReceipt(ctx context.Context, domain, actor string, input TaskDigestRetry) (string, error) {
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
return "", err
}
id, err := uuid.Parse(input.RequestID)
if err != nil || id.String() != input.RequestID {
return "", ErrTaskInvalid
}
var day, expected string
var ack bool
err = s.DB.QueryRowContext(ctx, `SELECT day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, input.RequestID, domain, actor).Scan(&day, &expected, &ack)
if err != nil {
return "", err
}
if day != input.Day || expected != input.ExpectedAttempt || ack != input.DuplicateAcknowledged {
return "", ErrTaskConflict
}
return input.RequestID, nil
}
// A retry is durable queue admission, never an SMTP send in a browser request.
// Keep request receipts and daily reservations even after detail retention.
func (s *Store) QueueTaskDigestRetry(ctx context.Context, domain, actor string, input TaskDigestRetry, now time.Time) (string, error) {
if s.ReadOnly {
return "", ErrReadOnly
}
id, err := uuid.Parse(input.RequestID)
if err != nil || id.String() != input.RequestID {
return "", ErrTaskInvalid
}
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return "", err
}
defer tx.Rollback()
if err = taskProductivityWriter(ctx, tx, domain, actor); err != nil {
return "", err
}
var oldDomain, oldOwner, oldDay, oldExpected string
var oldAck bool
err = tx.QueryRowContext(ctx, `SELECT domain_uuid,owner_uuid,day,expected_attempt,duplicate_ack FROM organizer_digest_requests WHERE uuid=?`, input.RequestID).Scan(&oldDomain, &oldOwner, &oldDay, &oldExpected, &oldAck)
if err == nil {
if oldDomain != domain || oldOwner != actor || oldDay != input.Day || oldExpected != input.ExpectedAttempt || oldAck != input.DuplicateAcknowledged {
return "", ErrTaskConflict
}
return input.RequestID, tx.Commit()
}
if !errors.Is(err, sql.ErrNoRows) {
return "", err
}
var raw string
if err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, domain, actor).Scan(&raw); err != nil {
return "", err
}
var p TaskPreferences
if err = json.Unmarshal([]byte(raw), &p); err != nil {
return "", err
}
loc, err := time.LoadLocation(p.Timezone)
if err != nil {
return "", err
}
if !p.DigestEnabled {
return "", ErrAccessDenied
}
if input.Day != now.In(loc).Format("2006-01-02") {
return "", ErrTaskInvalid
}
var state, current string
if err = tx.QueryRowContext(ctx, `SELECT state,attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, input.Day).Scan(&state, &current); err != nil {
return "", err
}
if current != input.ExpectedAttempt || state == "queued" || state == "sending" {
return "", ErrTaskConflict
}
if state != "sent" && state != "uncertain" && state != "cancelled" {
return "", ErrTaskConflict
}
if (state == "sent" || state == "uncertain") && !input.DuplicateAcknowledged {
return "", ErrTaskInvalid
}
var count int
if err = tx.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, input.Day).Scan(&count); err != nil {
return "", err
}
if count >= 5 {
return "", ErrTaskConflict
}
_, err = tx.ExecContext(ctx, `INSERT INTO organizer_digest_requests VALUES(?,?,?,?,?,?,?)`, input.RequestID, domain, actor, input.Day, input.ExpectedAttempt, input.DuplicateAcknowledged, now.UnixMilli())
if err != nil {
return "", err
}
c := TaskDigestClaim{TaskDigestDue: TaskDigestDue{domain, actor}, Day: input.Day, Version: p.Version, Timezone: p.Timezone, AttemptUUID: input.RequestID}
if err = insertDigestAttempt(ctx, tx, c, "manual", "queued", now); err != nil {
return "", err
}
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='queued',attempt_uuid=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=?`, c.AttemptUUID, now.UnixMilli(), domain, actor, input.Day); err != nil {
return "", err
}
return c.AttemptUUID, tx.Commit()
}
func (s *Store) QueuedTaskDigests(ctx context.Context) ([]TaskDigestClaim, error) {
rows, err := s.DB.QueryContext(ctx, `SELECT uuid,domain_uuid,owner_uuid,day,preference_version,timezone FROM organizer_digest_attempts WHERE state='queued' ORDER BY updated_ms,uuid LIMIT 10`)
if err != nil {
return nil, err
}
defer rows.Close()
out := []TaskDigestClaim{}
for rows.Next() {
var c TaskDigestClaim
if err = rows.Scan(&c.AttemptUUID, &c.Domain, &c.Owner, &c.Day, &c.Version, &c.Timezone); err != nil {
return nil, err
}
out = append(out, c)
}
return out, rows.Err()
}
func (s *Store) ClaimQueuedTaskDigest(ctx context.Context, c TaskDigestClaim, now time.Time) (TaskDigestClaim, error) {
if s.ReadOnly {
return c, ErrReadOnly
}
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return c, err
}
defer tx.Rollback()
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='sending',updated_ms=? WHERE uuid=? AND domain_uuid=? AND owner_uuid=? AND state='queued' AND EXISTS(SELECT 1 FROM organizer_digest_deliveries d WHERE d.domain_uuid=? AND d.owner_uuid=? AND d.day=? AND d.attempt_uuid=? AND d.state='queued')`, now.UnixMilli(), c.AttemptUUID, c.Domain, c.Owner, c.Domain, c.Owner, c.Day, c.AttemptUUID)
if err != nil {
return c, err
}
n, err := res.RowsAffected()
if err != nil {
return c, err
}
if n != 1 {
return c, sql.ErrNoRows
}
// Reload all claim fields from the queue, not from an earlier enumeration.
if err = tx.QueryRowContext(ctx, `SELECT day,preference_version,timezone FROM organizer_digest_attempts WHERE uuid=?`, c.AttemptUUID).Scan(&c.Day, &c.Version, &c.Timezone); err != nil {
return c, err
}
state, reason := "sending", ""
allowed := taskActor(ctx, tx, c.Domain, c.Owner, true)
if allowed != nil && !errors.Is(allowed, ErrAccessDenied) {
return c, allowed
}
on, err := taskReminderExtensionEnabled(ctx, tx, c.Domain)
if err != nil {
return c, err
}
var raw string
err = tx.QueryRowContext(ctx, `SELECT data FROM organizer_task_preferences WHERE domain_uuid=? AND owner_uuid=?`, c.Domain, c.Owner).Scan(&raw)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return c, err
}
var p TaskPreferences
if err == nil {
if err = json.Unmarshal([]byte(raw), &p); err != nil {
return c, err
}
}
if allowed != nil || !on || !p.DigestEnabled || p.Version != c.Version {
state, reason = "cancelled", "consent_changed"
}
loc, e := time.LoadLocation(c.Timezone)
if e != nil || now.In(loc).Format("2006-01-02") != c.Day {
state, reason = "cancelled", "day_expired"
}
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=?,reason=? WHERE uuid=?`, state, reason, c.AttemptUUID); err != nil {
return c, err
}
if _, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND attempt_uuid=?`, state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID); err != nil {
return c, err
}
if err = tx.Commit(); err != nil {
return c, err
}
if state != "sending" {
return c, sql.ErrNoRows
}
return c, nil
}
func (s *Store) FinishTaskDigestReason(ctx context.Context, c TaskDigestClaim, state, reason string, now time.Time) error {
if s.ReadOnly {
return ErrReadOnly
}
if state != "sent" && state != "uncertain" && state != "cancelled" {
return ErrTaskInvalid
}
switch reason {
case "", "smtp_unavailable", "tasks_unavailable", "consent_changed", "transport_uncertain", "extension_disabled":
default:
return ErrTaskInvalid
}
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
res, err := tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state=?,updated_ms=? WHERE domain_uuid=? AND owner_uuid=? AND day=? AND attempt_uuid=? AND state='sending'`, state, now.UnixMilli(), c.Domain, c.Owner, c.Day, c.AttemptUUID)
if err != nil {
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return ErrTaskConflict
}
res, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state=?,reason=?,updated_ms=? WHERE uuid=? AND state='sending'`, state, reason, now.UnixMilli(), c.AttemptUUID)
if err != nil {
return err
}
n, err = res.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return ErrTaskConflict
}
return tx.Commit()
}
func (s *Store) TaskDigestHistory(ctx context.Context, domain, actor, after string, limit int, now time.Time) (TaskDigestHistory, error) {
out := TaskDigestHistory{Items: []TaskDigestAttempt{}, RetentionDays: TaskDigestRetentionDays}
p, err := s.TaskPreferences(ctx, domain, actor)
if err != nil {
return out, err
}
loc, err := time.LoadLocation(p.Timezone)
if err != nil {
return out, err
}
out.Today = now.In(loc).Format("2006-01-02")
out.Enabled = p.DigestEnabled
if limit < 1 || limit > 100 {
return out, ErrTaskInvalid
}
query := `SELECT ` + digestAttemptColumns + ` FROM organizer_digest_attempts WHERE domain_uuid=? AND owner_uuid=?`
args := []any{domain, actor}
if after != "" {
if len(after) > 256 {
return out, ErrTaskInvalid
}
var cursor struct {
MS int64
ID string
}
raw, e := base64.RawURLEncoding.DecodeString(after)
if e != nil || json.Unmarshal(raw, &cursor) != nil || cursor.MS < 0 || len(cursor.ID) > 80 || cursor.ID == "" {
return out, ErrTaskInvalid
}
query += ` AND (created_ms,uuid)<(?,?)`
args = append(args, cursor.MS, cursor.ID)
}
query += ` ORDER BY created_ms DESC,uuid DESC LIMIT ?`
args = append(args, limit+1)
rows, err := s.DB.QueryContext(ctx, query, args...)
if err != nil {
return out, err
}
for rows.Next() {
v, e := scanDigestAttempt(rows)
if e != nil {
rows.Close()
return out, e
}
out.Items = append(out.Items, v)
}
err = rows.Err()
rows.Close()
if err != nil {
return out, err
}
if len(out.Items) > limit {
out.Items = out.Items[:limit]
last := out.Items[limit-1]
raw, _ := json.Marshal(struct {
MS int64
ID string
}{last.CreatedMS, last.UUID})
out.Next = base64.RawURLEncoding.EncodeToString(raw)
}
current, e := scanDigestAttempt(s.DB.QueryRowContext(ctx, `SELECT `+digestAttemptColumns+` FROM organizer_digest_attempts WHERE uuid=(SELECT attempt_uuid FROM organizer_digest_deliveries WHERE domain_uuid=? AND owner_uuid=? AND day=?)`, domain, actor, out.Today))
if e == nil {
out.Current = &current
} else if !errors.Is(e, sql.ErrNoRows) {
return out, e
}
err = s.DB.QueryRowContext(ctx, `SELECT count(*) FROM organizer_digest_requests WHERE domain_uuid=? AND owner_uuid=? AND day=?`, domain, actor, out.Today).Scan(&out.Retries)
return out, err
}
// Only detailed terminal attempts expire. Reservations and request receipts are
// permanent deduplication state; retention must never delete them.
func (s *Store) MaintainTaskDigests(ctx context.Context, now time.Time) (int64, error) {
if s.ReadOnly {
return 0, ErrReadOnly
}
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_attempts SET state='uncertain',reason='worker_interrupted',updated_ms=? WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='sending' AND updated_ms<? ORDER BY updated_ms,uuid LIMIT 100)`, now.UnixMilli(), now.Add(-10*time.Minute).UnixMilli())
if err != nil {
return 0, err
}
_, err = tx.ExecContext(ctx, `UPDATE organizer_digest_deliveries SET state='uncertain',updated_ms=? WHERE state='sending' AND attempt_uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state='uncertain' AND reason='worker_interrupted' AND updated_ms=?)`, now.UnixMilli(), now.UnixMilli())
if err != nil {
return 0, err
}
res, err := tx.ExecContext(ctx, `DELETE FROM organizer_digest_attempts WHERE uuid IN (SELECT uuid FROM organizer_digest_attempts WHERE state IN ('sent','uncertain','cancelled') AND updated_ms<? ORDER BY updated_ms,uuid LIMIT 500)`, now.Add(-TaskDigestRetentionDays*24*time.Hour).UnixMilli())
if err != nil {
return 0, err
}
n, err := res.RowsAffected()
if err != nil {
return 0, err
}
return n, tx.Commit()
}
+127 -31
View File
@@ -5,6 +5,7 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"sort"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@@ -28,12 +29,42 @@ var taskProductivityMigrations = []db.SQLMigration{{Version: 1, Name: "task_capt
schemaIndex("organizer_work_sessions", "organizer_work_period", "domain_uuid,owner_uuid,start_ms,uuid"), schemaIndex("organizer_work_sessions", "organizer_work_period", "domain_uuid,owner_uuid,start_ms,uuid"),
}}, {Version: 2, Name: "personal_dashboard_layout", Statements: []string{ }}, {Version: 2, Name: "personal_dashboard_layout", Statements: []string{
`CREATE TABLE organizer_dashboard_layouts(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,version INTEGER NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain_uuid,owner_uuid))`, `CREATE TABLE organizer_dashboard_layouts(domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,version INTEGER NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain_uuid,owner_uuid))`,
}, Assertions: []string{schemaColumns("organizer_dashboard_layouts", "domain_uuid,owner_uuid,version,data"), schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid")}}} }, Assertions: []string{schemaColumns("organizer_dashboard_layouts", "domain_uuid,owner_uuid,version,data"), schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid")}}, {
Version: 3, Name: "work_corrections_and_audit",
AddColumns: []db.SQLiteColumn{{Table: "organizer_work_sessions", Name: "cancelled_ms", Declaration: "INTEGER NOT NULL DEFAULT 0"}},
Statements: []string{`CREATE TABLE organizer_work_revisions(work_uuid TEXT NOT NULL,version INTEGER NOT NULL,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,action TEXT NOT NULL,reason TEXT NOT NULL,changed_ms INTEGER NOT NULL,data TEXT NOT NULL,PRIMARY KEY(work_uuid,version))`, `CREATE INDEX organizer_work_owner_period ON organizer_work_sessions(owner_uuid,start_ms,uuid)`},
Assertions: []string{schemaColumns("organizer_work_sessions", "uuid,domain_uuid,owner_uuid,task_uuid,start_ms,end_ms,version,cancelled_ms"), schemaUnique("organizer_work_revisions", "work_uuid,version"), schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid")},
}, {
Version: 4, Name: "digest_attempts_and_retry_receipts",
Statements: []string{
`ALTER TABLE organizer_digest_deliveries ADD COLUMN attempt_uuid TEXT NOT NULL DEFAULT ''`,
`CREATE INDEX organizer_digest_delivery_state ON organizer_digest_deliveries(state,attempt_uuid)`,
`CREATE TABLE organizer_digest_attempts(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,day TEXT NOT NULL,kind TEXT NOT NULL,state TEXT NOT NULL,preference_version INTEGER NOT NULL,timezone TEXT NOT NULL,reason TEXT NOT NULL DEFAULT '',created_ms INTEGER NOT NULL,updated_ms INTEGER NOT NULL)`,
`CREATE INDEX organizer_digest_history ON organizer_digest_attempts(domain_uuid,owner_uuid,created_ms,uuid)`,
`CREATE INDEX organizer_digest_pending ON organizer_digest_attempts(state,updated_ms,uuid)`,
`CREATE TABLE organizer_digest_requests(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,day TEXT NOT NULL,expected_attempt TEXT NOT NULL,duplicate_ack INTEGER NOT NULL,created_ms INTEGER NOT NULL)`,
`CREATE INDEX organizer_digest_request_day ON organizer_digest_requests(domain_uuid,owner_uuid,day)`,
`UPDATE organizer_digest_deliveries SET attempt_uuid='legacy-'||lower(hex(randomblob(16)))`,
`INSERT INTO organizer_digest_attempts SELECT attempt_uuid,domain_uuid,owner_uuid,day,'legacy',state,0,'','legacy',updated_ms,updated_ms FROM organizer_digest_deliveries`,
}, Assertions: []string{
schemaColumns("organizer_work_sessions", "uuid,domain_uuid,owner_uuid,task_uuid,start_ms,end_ms,version,cancelled_ms"),
schemaUnique("organizer_work_revisions", "work_uuid,version"),
schemaUnique("organizer_dashboard_layouts", "domain_uuid,owner_uuid"),
schemaColumns("organizer_digest_deliveries", "domain_uuid,owner_uuid,day,state,updated_ms,attempt_uuid"),
schemaUnique("organizer_digest_deliveries", "domain_uuid,owner_uuid,day"),
schemaColumns("organizer_digest_attempts", "uuid,domain_uuid,owner_uuid,day,kind,state,preference_version,timezone,reason,created_ms,updated_ms"),
schemaUnique("organizer_digest_attempts", "uuid"),
schemaIndex("organizer_digest_attempts", "organizer_digest_pending", "state,updated_ms,uuid"),
schemaColumns("organizer_digest_requests", "uuid,domain_uuid,owner_uuid,day,expected_attempt,duplicate_ack,created_ms"),
schemaUnique("organizer_digest_requests", "uuid"),
},
}}
type TaskPreferences struct { type TaskPreferences struct {
Version int64 `json:"version"` Version int64 `json:"version"`
ProjectUUID string `json:"project_uuid"` ProjectUUID string `json:"project_uuid"`
WikiPrefix string `json:"wiki_prefix"` WikiPrefix string `json:"wiki_prefix"`
RSSLinkMode string `json:"rss_link_mode"`
AssigneeUUID string `json:"assignee_uuid"` AssigneeUUID string `json:"assignee_uuid"`
DueDays *int `json:"due_days"` DueDays *int `json:"due_days"`
StartDays *int `json:"start_days"` StartDays *int `json:"start_days"`
@@ -50,7 +81,7 @@ type TaskPreferences struct {
} }
func (s *Store) TaskPreferences(ctx context.Context, domain, actor string) (TaskPreferences, error) { func (s *Store) TaskPreferences(ctx context.Context, domain, actor string) (TaskPreferences, error) {
p := TaskPreferences{AssigneeUUID: actor, Complexity: 1, ReminderTime: "09:00", DigestTime: "09:00", Timezone: "UTC"} p := TaskPreferences{AssigneeUUID: actor, Complexity: 1, ReminderTime: "09:00", DigestTime: "09:00", Timezone: "UTC", RSSLinkMode: "tab"}
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil { if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
return p, err return p, err
} }
@@ -90,7 +121,13 @@ func (s *Store) SaveTaskPreferences(ctx context.Context, domain, actor string, p
if s.ReadOnly { if s.ReadOnly {
return p, ErrReadOnly return p, ErrReadOnly
} }
if !ValidTaskWikiPrefix(p.WikiPrefix) { if _, err := ExpandWikiPrefix(p.WikiPrefix, "subject", now); err != nil {
return p, ErrTaskInvalid
}
if p.RSSLinkMode == "" {
p.RSSLinkMode = "tab"
}
if p.RSSLinkMode != "tab" && p.RSSLinkMode != "embedded" && p.RSSLinkMode != "gecko" {
return p, ErrTaskInvalid return p, ErrTaskInvalid
} }
// Older clients do not send this field. Preserve their default clock. // Older clients do not send this field. Preserve their default clock.
@@ -235,20 +272,21 @@ func (s *Store) CaptureTaskWithWiki(ctx context.Context, domain, actor, request,
} }
type TaskWork struct { type TaskWork struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
TaskUUID string `json:"task_uuid"` TaskUUID string `json:"task_uuid"`
StartMS int64 `json:"start_ms"` StartMS int64 `json:"start_ms"`
EndMS int64 `json:"end_ms"` EndMS int64 `json:"end_ms"`
Version int64 `json:"version"` Version int64 `json:"version"`
CancelledMS int64 `json:"cancelled_ms,omitempty"`
} }
func scanTaskWork(row interface{ Scan(...any) error }) (TaskWork, error) { func scanTaskWork(row interface{ Scan(...any) error }) (TaskWork, error) {
var v TaskWork var v TaskWork
err := row.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version) err := row.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &v.CancelledMS)
return v, err return v, err
} }
const taskWorkColumns = `uuid,task_uuid,start_ms,end_ms,version` const taskWorkColumns = `uuid,task_uuid,start_ms,end_ms,version,cancelled_ms`
func (s *Store) ActiveTaskWork(ctx context.Context, domain, actor string) (*TaskWork, error) { func (s *Store) ActiveTaskWork(ctx context.Context, domain, actor string) (*TaskWork, error) {
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil { if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
@@ -261,7 +299,7 @@ func (s *Store) ActiveTaskWork(ctx context.Context, domain, actor string) (*Task
return &v, err return &v, err
} }
func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string, v TaskWork, now time.Time) (TaskWork, error) { func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string, v TaskWork, now time.Time, reasons ...string) (TaskWork, error) {
if s.ReadOnly { if s.ReadOnly {
return v, ErrReadOnly return v, ErrReadOnly
} }
@@ -280,6 +318,16 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
if err != nil && !errors.Is(err, sql.ErrNoRows) { if err != nil && !errors.Is(err, sql.ErrNoRows) {
return v, err return v, err
} }
if action == "correct" || action == "cancel" {
if err != nil {
return v, err
}
reason := ""
if len(reasons) > 0 {
reason = reasons[0]
}
return correctTaskWork(ctx, tx, domain, actor, action, reason, v, old, now)
}
switch action { switch action {
case "start": case "start":
if v.Version != 0 { if v.Version != 0 {
@@ -300,6 +348,7 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
} }
v.StartMS = now.UnixMilli() v.StartMS = now.UnixMilli()
v.EndMS = 0 v.EndMS = 0
v.CancelledMS = 0
v.Version = 1 v.Version = 1
res, e := tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_work_sessions(uuid,domain_uuid,owner_uuid,task_uuid,start_ms) VALUES(?,?,?,?,?)`, v.UUID, domain, actor, v.TaskUUID, v.StartMS) res, e := tx.ExecContext(ctx, `INSERT OR IGNORE INTO organizer_work_sessions(uuid,domain_uuid,owner_uuid,task_uuid,start_ms) VALUES(?,?,?,?,?)`, v.UUID, domain, actor, v.TaskUUID, v.StartMS)
if e != nil { if e != nil {
@@ -313,6 +362,9 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
if err != nil { if err != nil {
return v, err return v, err
} }
if old.CancelledMS != 0 {
return v, ErrTaskConflict
}
if old.EndMS > 0 { if old.EndMS > 0 {
if v.Version == old.Version || v.Version == old.Version-1 { if v.Version == old.Version || v.Version == old.Version-1 {
return old, tx.Commit() return old, tx.Commit()
@@ -332,6 +384,14 @@ func (s *Store) ChangeTaskWork(ctx context.Context, domain, actor, action string
default: default:
return v, ErrTaskInvalid return v, ErrTaskInvalid
} }
if action == "stop" {
if err = auditTaskWork(ctx, tx, domain, actor, "baseline", "", old, now); err != nil {
return v, err
}
}
if err = auditTaskWork(ctx, tx, domain, actor, action, "", v, now); err != nil {
return v, err
}
return v, tx.Commit() return v, tx.Commit()
} }
@@ -344,19 +404,21 @@ type TaskReportDay struct {
FocusMS int64 `json:"focus_ms"` FocusMS int64 `json:"focus_ms"`
} }
type TaskReportItem struct { type TaskReportItem struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
Title string `json:"title"` Title string `json:"title"`
ProjectUUID string `json:"project_uuid"` ProjectUUID string `json:"project_uuid"`
WorkMS int64 `json:"work_ms"` ProjectTitle string `json:"project_title"`
Points int `json:"points"` WorkMS int64 `json:"work_ms"`
Completed bool `json:"completed"` Points int `json:"points"`
Completed bool `json:"completed"`
} }
type TaskReport struct { type TaskReport struct {
Days []TaskReportDay `json:"days"` Days []TaskReportDay `json:"days"`
Tasks []TaskReportItem `json:"tasks"` Tasks []TaskReportItem `json:"tasks"`
Sessions []TaskWork `json:"sessions"` Sessions []TaskWork `json:"sessions"`
Partial bool `json:"partial"` Partial bool `json:"partial"`
Timezone string `json:"timezone"` Timezone string `json:"timezone"`
Projects []TaskReportProject `json:"projects"`
} }
func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end, now time.Time) (TaskReport, error) { func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end, now time.Time) (TaskReport, error) {
@@ -373,7 +435,7 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
out.Days = append(out.Days, TaskReportDay{Date: day.Format("2006-01-02")}) out.Days = append(out.Days, TaskReportDay{Date: day.Format("2006-01-02")})
} }
byTask := map[string]int{} byTask := map[string]int{}
rows, err := s.DB.QueryContext(ctx, `SELECT m.completed_ms,m.estimate,m.complexity,m.task_uuid,COALESCE(i.title,''),COALESCE(i.project_uuid,'') FROM organizer_task_metrics m LEFT JOIN organizer_items i ON i.domain_uuid=m.domain_uuid AND i.owner_uuid=m.owner_uuid AND i.uuid=m.task_uuid WHERE m.domain_uuid=? AND m.owner_uuid=? AND m.completed_ms>=? AND m.completed_ms<? ORDER BY m.completed_ms,m.task_uuid LIMIT 10001`, domain, actor, start.UnixMilli(), end.UnixMilli()) rows, err := s.DB.QueryContext(ctx, `SELECT m.completed_ms,m.estimate,m.complexity,m.task_uuid,COALESCE(i.title,''),COALESCE(i.project_uuid,''),COALESCE(p.title,'') FROM organizer_task_metrics m LEFT JOIN organizer_items i ON i.domain_uuid=m.domain_uuid AND i.owner_uuid=m.owner_uuid AND i.uuid=m.task_uuid LEFT JOIN organizer_items p ON p.uuid=i.project_uuid AND p.domain_uuid=m.domain_uuid AND p.owner_uuid=m.owner_uuid AND p.kind='project' WHERE m.domain_uuid=? AND m.owner_uuid=? AND m.completed_ms>=? AND m.completed_ms<? ORDER BY m.completed_ms,m.task_uuid LIMIT 10001`, domain, actor, start.UnixMilli(), end.UnixMilli())
if err != nil { if err != nil {
return out, err return out, err
} }
@@ -381,8 +443,8 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
for rows.Next() { for rows.Next() {
var ms int64 var ms int64
var estimate, complexity int var estimate, complexity int
var id, title, project string var id, title, project, projectTitle string
if err = rows.Scan(&ms, &estimate, &complexity, &id, &title, &project); err != nil { if err = rows.Scan(&ms, &estimate, &complexity, &id, &title, &project, &projectTitle); err != nil {
rows.Close() rows.Close()
return out, err return out, err
} }
@@ -397,7 +459,7 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
d.Estimate += estimate d.Estimate += estimate
d.Points += 1 + ((estimate+29)/30)*max(1, complexity) d.Points += 1 + ((estimate+29)/30)*max(1, complexity)
byTask[id] = len(out.Tasks) byTask[id] = len(out.Tasks)
out.Tasks = append(out.Tasks, TaskReportItem{UUID: id, Title: title, ProjectUUID: project, Completed: true, Points: 1 + ((estimate+29)/30)*max(1, complexity)}) out.Tasks = append(out.Tasks, TaskReportItem{UUID: id, Title: title, ProjectUUID: project, ProjectTitle: projectTitle, Completed: true, Points: 1 + ((estimate+29)/30)*max(1, complexity)})
} }
} }
err = rows.Err() err = rows.Err()
@@ -428,15 +490,15 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
} }
return total return total
} }
rows, err = s.DB.QueryContext(ctx, `SELECT w.uuid,w.task_uuid,w.start_ms,w.end_ms,w.version,COALESCE(i.title,''),COALESCE(i.project_uuid,'') FROM organizer_work_sessions w LEFT JOIN organizer_items i ON i.domain_uuid=w.domain_uuid AND i.owner_uuid=w.owner_uuid AND i.uuid=w.task_uuid WHERE w.domain_uuid=? AND w.owner_uuid=? AND w.start_ms<? AND (w.end_ms=0 OR w.end_ms>?) ORDER BY w.start_ms DESC,w.uuid LIMIT 10001`, domain, actor, end.UnixMilli(), start.UnixMilli()) rows, err = s.DB.QueryContext(ctx, `SELECT w.uuid,w.task_uuid,w.start_ms,w.end_ms,w.version,COALESCE(i.title,''),COALESCE(i.project_uuid,''),COALESCE(p.title,'') FROM organizer_work_sessions w LEFT JOIN organizer_items i ON i.domain_uuid=w.domain_uuid AND i.owner_uuid=w.owner_uuid AND i.uuid=w.task_uuid LEFT JOIN organizer_items p ON p.uuid=i.project_uuid AND p.domain_uuid=w.domain_uuid AND p.owner_uuid=w.owner_uuid AND p.kind='project' WHERE w.cancelled_ms=0 AND w.domain_uuid=? AND w.owner_uuid=? AND w.start_ms<? AND (w.end_ms=0 OR w.end_ms>?) ORDER BY w.start_ms DESC,w.uuid LIMIT 10001`, domain, actor, end.UnixMilli(), start.UnixMilli())
if err != nil { if err != nil {
return out, err return out, err
} }
n = 0 n = 0
for rows.Next() { for rows.Next() {
var v TaskWork var v TaskWork
var title, project string var title, project, projectTitle string
if err = rows.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &title, &project); err != nil { if err = rows.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &title, &project, &projectTitle); err != nil {
rows.Close() rows.Close()
return out, err return out, err
} }
@@ -458,7 +520,7 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
if !ok { if !ok {
index = len(out.Tasks) index = len(out.Tasks)
byTask[v.TaskUUID] = index byTask[v.TaskUUID] = index
out.Tasks = append(out.Tasks, TaskReportItem{UUID: v.TaskUUID, Title: title, ProjectUUID: project}) out.Tasks = append(out.Tasks, TaskReportItem{UUID: v.TaskUUID, Title: title, ProjectUUID: project, ProjectTitle: projectTitle})
} }
out.Tasks[index].WorkMS += elapsed out.Tasks[index].WorkMS += elapsed
} }
@@ -485,5 +547,39 @@ func (s *Store) TaskReport(ctx context.Context, domain, actor string, start, end
} }
addSpan(a, b, true) addSpan(a, b, true)
} }
return out, rows.Err() if err = rows.Err(); err != nil {
return out, err
}
groups := map[string]*TaskReportProject{}
for _, task := range out.Tasks {
p := groups[task.ProjectUUID]
if p == nil {
p = &TaskReportProject{UUID: task.ProjectUUID, Title: task.ProjectTitle}
groups[task.ProjectUUID] = p
}
if task.Completed {
p.Completed++
}
p.Points += task.Points
p.WorkMS += task.WorkMS
}
out.Projects = []TaskReportProject{}
for _, p := range groups {
out.Projects = append(out.Projects, *p)
}
sort.Slice(out.Projects, func(i, j int) bool {
if out.Projects[i].Title != out.Projects[j].Title {
return out.Projects[i].Title < out.Projects[j].Title
}
return out.Projects[i].UUID < out.Projects[j].UUID
})
return out, nil
}
type TaskReportProject struct {
UUID string `json:"uuid"`
Title string `json:"title"`
Completed int `json:"completed"`
Points int `json:"points"`
WorkMS int64 `json:"work_ms"`
} }
+186
View File
@@ -0,0 +1,186 @@
package store
import (
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
)
type TaskWorkRevision struct {
Item TaskWork `json:"item"`
Action string `json:"action"`
Reason string `json:"reason"`
ChangedMS int64 `json:"changed_ms"`
}
func auditTaskWork(ctx context.Context, tx *sql.Tx, domain, actor, action, reason string, v TaskWork, now time.Time) error {
raw, err := json.Marshal(v)
if err != nil {
return err
}
// Legacy sessions acquire a baseline lazily. Never rewrite an earlier audit.
query := `INSERT INTO organizer_work_revisions(work_uuid,version,domain_uuid,owner_uuid,action,reason,changed_ms,data) VALUES(?,?,?,?,?,?,?,?)`
if action == "baseline" {
query += ` ON CONFLICT(work_uuid,version) DO NOTHING`
}
_, err = tx.ExecContext(ctx, query, v.UUID, v.Version, domain, actor, action, reason, now.UnixMilli(), string(raw))
return err
}
func correctTaskWork(ctx context.Context, tx *sql.Tx, domain, actor, action, reason string, v, old TaskWork, now time.Time) (TaskWork, error) {
reason = strings.TrimSpace(reason)
if reason == "" || len(reason) > 500 || !utf8.ValidString(reason) || strings.ContainsAny(reason, "\x00\r\n") {
return v, ErrTaskInvalid
}
if v.Version < 1 || old.EndMS == 0 || old.CancelledMS != 0 {
return v, ErrTaskConflict
}
if old.Version != v.Version {
return v, ErrTaskConflict
}
if v.TaskUUID != "" && v.TaskUUID != old.TaskUUID {
return v, ErrTaskInvalid
}
result := old
if action == "correct" {
if v.StartMS <= 0 || v.EndMS <= v.StartMS || v.EndMS > now.UnixMilli() || v.EndMS-v.StartMS > int64(31*24*time.Hour/time.Millisecond) {
return v, ErrTaskInvalid
}
// Adjacent spans are valid; double-counted elapsed time is not. Scope across
// Domains like the existing one-active-session constraint, without disclosing them.
var overlap bool
err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_work_sessions WHERE owner_uuid=? AND uuid<>? AND cancelled_ms=0 AND start_ms<? AND (end_ms=0 OR end_ms>?))`, actor, old.UUID, v.EndMS, v.StartMS).Scan(&overlap)
if err != nil {
return v, err
}
if overlap {
return v, ErrTaskConflict
}
result.StartMS = v.StartMS
result.EndMS = v.EndMS
} else {
result.CancelledMS = now.UnixMilli()
}
if err := auditTaskWork(ctx, tx, domain, actor, "baseline", "", old, now); err != nil {
return v, err
}
result.Version++
updated, err := tx.ExecContext(ctx, `UPDATE organizer_work_sessions SET start_ms=?,end_ms=?,cancelled_ms=?,version=? WHERE uuid=? AND domain_uuid=? AND owner_uuid=? AND version=?`, result.StartMS, result.EndMS, result.CancelledMS, result.Version, old.UUID, domain, actor, old.Version)
if err != nil {
return v, err
}
if count, err := updated.RowsAffected(); err != nil {
return v, err
} else if count != 1 {
return v, ErrTaskConflict
}
if err := auditTaskWork(ctx, tx, domain, actor, action, reason, result, now); err != nil {
return v, err
}
return result, tx.Commit()
}
type TaskWorkEntry struct {
TaskWork
Title string `json:"title"`
ProjectUUID string `json:"project_uuid"`
}
type TaskWorkPage struct {
Items []TaskWorkEntry `json:"items"`
Next string `json:"next"`
}
type workCursor struct {
Start int64 `json:"start"`
UUID string `json:"uuid"`
}
func (s *Store) TaskWorkHistory(ctx context.Context, domain, actor string, start, end time.Time, after string, limit int) (TaskWorkPage, error) {
out := TaskWorkPage{Items: []TaskWorkEntry{}}
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
return out, err
}
if !end.After(start) || end.Sub(start) > 367*24*time.Hour || limit < 1 || limit > 100 {
return out, ErrTaskInvalid
}
query := `SELECT w.uuid,w.task_uuid,w.start_ms,w.end_ms,w.version,w.cancelled_ms,COALESCE(i.title,''),COALESCE(i.project_uuid,'') FROM organizer_work_sessions w LEFT JOIN organizer_items i ON i.uuid=w.task_uuid AND i.domain_uuid=w.domain_uuid AND i.owner_uuid=w.owner_uuid WHERE w.domain_uuid=? AND w.owner_uuid=? AND w.start_ms<? AND (w.end_ms=0 OR w.end_ms>?)`
args := []any{domain, actor, end.UnixMilli(), start.UnixMilli()}
if after != "" {
if len(after) > 256 {
return out, ErrTaskInvalid
}
raw, err := base64.RawURLEncoding.DecodeString(after)
var cursor workCursor
if err != nil || json.Unmarshal(raw, &cursor) != nil || cursor.Start <= 0 {
return out, ErrTaskInvalid
}
if id, err := uuid.Parse(cursor.UUID); err != nil || id.String() != cursor.UUID {
return out, ErrTaskInvalid
}
query += ` AND (w.start_ms,w.uuid)<(?,?)`
args = append(args, cursor.Start, cursor.UUID)
}
query += ` ORDER BY w.start_ms DESC,w.uuid DESC LIMIT ?`
args = append(args, limit+1)
rows, err := s.DB.QueryContext(ctx, query, args...)
if err != nil {
return out, err
}
defer rows.Close()
for rows.Next() {
var v TaskWorkEntry
if err = rows.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &v.CancelledMS, &v.Title, &v.ProjectUUID); err != nil {
return out, err
}
out.Items = append(out.Items, v)
}
if len(out.Items) > limit {
out.Items = out.Items[:limit]
last := out.Items[limit-1]
raw, _ := json.Marshal(workCursor{Start: last.StartMS, UUID: last.UUID})
out.Next = base64.RawURLEncoding.EncodeToString(raw)
}
return out, rows.Err()
}
func (s *Store) TaskWorkRevisions(ctx context.Context, domain, actor, id string, before int64) ([]TaskWorkRevision, error) {
out := []TaskWorkRevision{}
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
return out, err
}
if before < 0 {
return out, ErrTaskInvalid
}
current, err := scanTaskWork(s.DB.QueryRowContext(ctx, `SELECT `+taskWorkColumns+` FROM organizer_work_sessions WHERE uuid=? AND domain_uuid=? AND owner_uuid=?`, id, domain, actor))
if err != nil {
return out, err
}
if before == 0 {
before = current.Version + 1
}
rows, err := s.DB.QueryContext(ctx, `SELECT action,reason,changed_ms,data FROM organizer_work_revisions WHERE work_uuid=? AND domain_uuid=? AND owner_uuid=? AND version<? ORDER BY version DESC LIMIT 100`, id, domain, actor, before)
if err != nil {
return out, err
}
defer rows.Close()
for rows.Next() {
var v TaskWorkRevision
var raw string
if err = rows.Scan(&v.Action, &v.Reason, &v.ChangedMS, &raw); err != nil {
return out, err
}
if err = json.Unmarshal([]byte(raw), &v.Item); err != nil {
return out, err
}
out = append(out, v)
}
if len(out) == 0 && before > current.Version {
out = append(out, TaskWorkRevision{Item: current, Action: "baseline"})
}
return out, rows.Err()
}
+32
View File
@@ -0,0 +1,32 @@
package store
import (
"strings"
"testing"
"time"
)
func TestExpandWikiPrefix(t *testing.T) {
now := time.Date(2026, 12, 31, 18, 0, 0, 0, time.UTC).In(time.FixedZone("JST", 9*3600))
for _, tt := range []struct{ template, subject, want string }{
{"/notes/{YYYY}/{MM}/{DD}/{Subject}", "日本語 / ../ x?y#z", "/notes/2027/01/01/日本語---..--x-y-z"},
{"/notes/{Subject}", "../", "/notes/untitled"},
{"/notes/{Subject}", "{YYYY}", "/notes/YYYY"},
{"/notes", "unused", "/notes"},
{"", "", ""},
} {
got, err := ExpandWikiPrefix(tt.template, tt.subject, now)
if err != nil || got != tt.want {
t.Fatalf("%q: %q, %v; want %q", tt.template, got, err, tt.want)
}
}
for _, bad := range []string{"/notes/{YY}", "/notes/{{Subject}}", "/notes/{Subject", "/a/../b/{YYYY}", "/_/api/{Subject}", "relative/{YYYY}"} {
if _, err := ExpandWikiPrefix(bad, "subject", now); err == nil {
t.Fatal("accepted", bad)
}
}
got, err := ExpandWikiPrefix("/notes/{Subject}", strings.Repeat("日本語", 200), now)
if err != nil || len(got) > len("/notes/")+180 || !ValidTaskWikiPrefix(got) {
t.Fatal(got, err)
}
}
+1
View File
@@ -0,0 +1 @@
:root{color-scheme:light dark}html,body{margin:0;width:100%;height:100%;overflow:hidden;background:#fff;color:#222;font:14px system-ui}canvas{display:block;width:100%;height:100%;outline:none}#status{position:absolute;inset:1rem auto auto 1rem;max-width:calc(100% - 4rem);margin:0;padding:.75rem 1rem;border-radius:.4rem;background:#222;color:#fff;z-index:1}#status[hidden]{display:none}
+16
View File
@@ -0,0 +1,16 @@
.feeds-page{height:100dvh;overflow:hidden;display:flex;flex-direction:column;background:var(--bg);color:var(--fg)}
.feed-external{width:calc(100vw - 1.5rem);height:calc(100dvh - 1.5rem);max-width:none;max-height:none;box-sizing:border-box;margin:auto;padding:0;background:var(--bg,#111);color:var(--fg);border:1px solid var(--border,#8885);border-radius:.5rem;overflow:hidden}.feed-external[open]{display:flex;flex-direction:column}.feed-external::backdrop{background:#0009}.feed-external-bar{display:flex;align-items:center;gap:.5rem;padding:.3rem .5rem;flex-shrink:0}.feed-external-bar span{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.feed-external-bar a{padding:.3rem;text-decoration:none;font-size:1.2rem}.feed-external iframe{flex:1;width:100%;min-height:0;border:0;background:#fff}@media(max-width:600px){.feed-external{width:100vw;height:100dvh;border:0;border-radius:0}}
.feed-help{display:flex;align-items:center;gap:.5rem;padding:.25rem 0;font-size:.78rem;flex-shrink:0}.feed-help[hidden]{display:none}.feed-help span{flex:1;min-width:0;overflow-wrap:anywhere}.feed-help button{flex-shrink:0}
.feed-capture{width:min(42rem,calc(100vw - 2rem));max-height:calc(100dvh - 2rem);box-sizing:border-box;overflow:auto;background:var(--bg,#111);color:var(--fg);border:1px solid var(--border,#8885);border-radius:.6rem;padding:1rem;box-shadow:0 1rem 4rem #0007}.feed-capture::backdrop{background:#0008}.feed-capture-heading{display:flex;align-items:center;justify-content:space-between;gap:.5rem}.feed-capture h2{font-size:1.05rem;margin:0}.feed-capture-heading,.feed-capture-field,.feed-capture-tokens,.feed-capture-destination,.feed-capture-actions,.feed-capture .feed-warning{grid-column:1/-1}#feeds-app .feed-capture form{grid-template-columns:1fr 1fr;gap:.65rem}#feeds-app .feed-capture .feed-capture-check{display:flex;align-items:center;gap:.4rem}#feeds-app .feed-capture input[type=checkbox]{width:1rem;min-height:1rem;flex-shrink:0}.feed-capture-check:has([name=confirm]){grid-column:1/-1}.feed-capture-tokens,.feed-capture-actions{display:flex;gap:.35rem;flex-wrap:wrap}.feed-capture-actions{justify-content:flex-end}.feed-capture-destination{font-size:.75rem;overflow-wrap:anywhere;opacity:.8}.feed-capture .feed-warning:empty{display:none}.feed-capture-field:has([name=prefix]){position:relative}.feed-capture-field:has([name=prefix]) a{position:absolute;right:.1rem;top:0;text-decoration:none}@media(max-width:600px){#feeds-app .feed-capture form{grid-template-columns:1fr}.feed-capture{padding:.75rem}}
.feed-folder-row{display:flex;align-items:center;min-width:0}#feeds-app .feed-folder-toggle{flex:0 0 1.7rem;border:0;padding:.15rem;min-width:1.7rem}#feeds-app .feed-folder-name{display:flex;align-items:center;gap:.3rem;flex:1;min-width:0;text-align:left;border:0;font-size:.78rem;padding:.2rem}.feed-folder-name span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.feed-folder-name svg{width:1rem;height:1rem;flex-shrink:0;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linejoin:round}.feed-folder>.feed-subscriptions{padding-left:1.1rem;border-left:1px solid var(--border,#8885);margin-left:.8rem}.feed-catalog .feed-subscriptions[hidden]{display:none}.feed-folder small{font-size:.7rem;opacity:.7;padding:.3rem}.feed-catalog button[aria-current=true]{background:color-mix(in srgb,var(--accent,#659de4) 22%,transparent)!important}.feed-tree-all{width:100%;text-align:left}.feed-folders{min-width:0}
.feed-layout.action-history{grid-template-columns:12rem minmax(0,1fr)}.feed-layout.action-history .feed-index{display:none}.feed-layout.action-history #feed-reader{display:block}.feed-history-controls{display:flex;gap:.3rem;flex-wrap:wrap;margin:.4rem 0}.feed-history-controls select{max-width:100%}.feed-history table{width:100%;table-layout:fixed;border-collapse:collapse;font-size:.8rem}.feed-history th,.feed-history td{padding:.3rem .4rem;text-align:left;border-bottom:1px solid var(--border,#8885);vertical-align:middle;overflow-wrap:anywhere}.feed-history th:first-child{width:7.5rem}.feed-history th:nth-child(3){width:6rem}.feed-history th:last-child{width:7rem}.feed-history td:nth-child(2)>span,.feed-history small{display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.feed-history small{font-size:.72rem;opacity:.7}.feed-history-buttons{display:flex;gap:.2rem;justify-content:flex-end}.feed-history-buttons a{display:inline-flex;align-items:center;justify-content:center;min-width:1.8rem;min-height:1.8rem;border:1px solid var(--border,#6666);border-radius:.2rem;text-decoration:none}.feed-history [data-state=unknown],.feed-history [data-state=conflict],.feed-history [data-state=failed]{color:var(--danger,#e57a7a)}.feed-history .feed-warning:empty{display:none}
@media(max-width:1100px){.feed-layout.action-history{grid-template-columns:10rem minmax(0,1fr)}}
@media(max-width:600px){.feed-history thead{display:none}.feed-history tbody,.feed-history tr{display:block}.feed-history tr{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:.15rem .3rem;padding:.5rem 0;border-bottom:1px solid var(--border,#8885)}.feed-history td{display:block;border:0;padding:0 .2rem;min-width:0}.feed-history td:first-child{grid-column:1;grid-row:1;font-size:.7rem;opacity:.7}.feed-history td:nth-child(2){grid-column:1;grid-row:2}.feed-history td:nth-child(3){grid-column:1;grid-row:3;font-size:.75rem}.feed-history td:last-child{grid-column:2;grid-row:1/4;align-self:center}.feed-history-buttons a{min-width:2.3rem;min-height:2.3rem}}
.feed-catalog{padding:.3rem 0 .4rem .5rem;display:grid;gap:.3rem;border-left:2px solid var(--border,#8885)}#feeds-app .feed-catalog select,#feeds-app .feed-catalog input{width:100%;min-width:0;box-sizing:border-box;font-size:.75rem}.feed-subscriptions{display:grid;gap:0}.feed-subscriptions button{text-align:left;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;font-size:.75rem!important;border:0!important;content-visibility:auto;contain-intrinsic-size:auto 29px}.feed-subscriptions button[aria-current=true]{background:color-mix(in srgb,var(--accent,#659de4) 22%,transparent)!important}
.feed-opml{display:grid;gap:.6rem;min-width:0}.feed-opml>label{display:grid;gap:.25rem}.feed-opml input[type=file]{width:100%;box-sizing:border-box}.feed-opml table{width:100%;table-layout:fixed;border-collapse:collapse;font-size:.8rem}.feed-opml th,.feed-opml td{text-align:left;vertical-align:top;padding:.3rem;border-bottom:1px solid var(--border,#8885);overflow-wrap:anywhere}.feed-opml th:first-child{width:1.6rem}.feed-opml th:nth-child(3){width:20%}.feed-opml th:last-child{width:20%}.feed-opml small{display:block;font-size:.7rem;opacity:.7;overflow-wrap:anywhere}#feeds-app .feed-opml input[type=checkbox]{width:auto;min-height:1.2rem}.feed-import-options{display:flex;gap:.75rem;align-items:center;flex-wrap:wrap}#feeds-app .feed-import-options label:has(input[type=checkbox]){display:flex;align-items:center}.feed-opml input:disabled{opacity:.4}
#feed-status:empty{display:none}#feed-reader .feed-body-html{white-space:normal}.feed-original{display:inline-flex;align-items:center;justify-content:center;min-width:2rem;min-height:2rem;border-radius:.2rem}.feed-original:hover{background:var(--border,#8883)}.feed-original svg{width:1.1rem;height:1.1rem;fill:none;stroke:currentColor;stroke-width:1.7;stroke-linecap:round;stroke-linejoin:round}
.feeds-page .topbar,.feeds-page .organizer-nav{flex-shrink:0}.feeds-page #feeds-app{flex:1;min-height:0;display:flex;flex-direction:column;width:100%;max-width:none;margin:0;padding:0 .5rem .25rem;box-sizing:border-box}
#feed-toolbar{display:flex;align-items:center;gap:.35rem;min-height:2.5rem;flex-wrap:wrap}#feed-toolbar input{flex:1;min-width:8rem;max-width:32rem}#feeds-app button,#feeds-app input,#feeds-app select{font:inherit;color:inherit;background:var(--panel,var(--bg));border:1px solid var(--border,#6666);border-radius:.2rem;padding:.2rem .45rem;min-height:1.8rem}#feeds-app button{cursor:pointer}#feeds-app button:disabled{opacity:.45;cursor:default}#feeds-app a{color:var(--accent,#659de4)}#feed-status{margin:0;min-height:1.25rem;font-size:.78rem;color:var(--muted,var(--fg));overflow-wrap:anywhere}
.feed-layout{flex:1;min-height:0;display:grid;grid-template-columns:12rem minmax(18rem,1fr) minmax(18rem,42%);border:1px solid var(--border,#6666)}#feed-sources,.feed-index,#feed-reader{min-width:0;min-height:0;overflow:auto;overscroll-behavior:contain}#feed-sources{border-right:1px solid var(--border,#6666);padding:.3rem}.feed-source{display:flex;align-items:center}.feed-source>button:first-child{flex:1;text-align:left;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;border:0!important}.feed-source small{font-size:.68rem}.feed-source[aria-current=true]{background:color-mix(in srgb,var(--accent,#659de4) 20%,transparent)}#feed-sources h3{font-size:.75rem;margin:.5rem 0 .15rem}.feed-index{display:flex;flex-direction:column}#feed-rows{flex:1;min-height:0;overflow:auto;outline-offset:-2px}#feed-columns,.feed-row{display:grid;grid-template-columns:1.8rem 5.4rem 8rem minmax(0,1fr);align-items:center;gap:.3rem;font-size:.8rem;padding:.25rem .4rem;box-sizing:border-box}#feed-columns{font-size:.7rem;border-bottom:1px solid var(--border,#6666);opacity:.7}.feed-row{cursor:pointer;min-height:1.85rem;content-visibility:auto;contain-intrinsic-size:auto 30px}.feed-row>span{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.feed-row.unread{font-weight:650}.feed-row[aria-selected=true]{background:color-mix(in srgb,var(--accent,#659de4) 24%,var(--bg,#16191f));box-shadow:inset 3px 0 var(--accent,#659de4)}.feed-row:hover{background:color-mix(in srgb,var(--accent,#659de4) 12%,transparent)}#feed-reader{border-left:1px solid var(--border,#6666);padding:.75rem}#feed-reader h1{font-size:1.1rem;margin:0 0 .4rem;overflow-wrap:anywhere}#feed-reader .feed-body{white-space:pre-wrap;overflow-wrap:anywhere;font-family:inherit;font-size:.9rem;line-height:1.6}.feed-actions{display:flex;flex-wrap:wrap;gap:.3rem;align-items:center}.feed-meta{font-size:.75rem;opacity:.7;margin:.4rem 0}#feed-keys{font-size:.72rem;padding:.3rem;opacity:.75}#feeds-app form{display:grid;gap:.5rem}#feeds-app form label{display:grid;gap:.2rem;font-size:.85rem}#feeds-app form input,#feeds-app form select{width:100%;box-sizing:border-box}#feeds-app form .feed-actions{display:flex}#feeds-app .feed-warning{font-size:.8rem;line-height:1.5;opacity:.8}.feed-source-error{color:var(--danger,#e57a7a)}
@media(max-width:1100px){.feed-layout{grid-template-columns:10rem minmax(0,1fr)}#feed-reader{display:none}.feed-layout.reading .feed-index{display:none}.feed-layout.reading #feed-reader{display:block}#feed-columns,.feed-row{grid-template-columns:1.5rem 4.7rem 7rem minmax(0,1fr)}}
@media(max-width:600px){.feed-layout{display:flex;flex-direction:column}#feed-sources{display:none;max-height:35%;flex-shrink:0;border-bottom:1px solid var(--border,#6666)}.feed-layout.sources-open #feed-sources{display:block}.feed-index,#feed-reader{flex:1;width:100%;box-sizing:border-box}#feed-columns,.feed-row{grid-template-columns:1.5rem 3.5rem minmax(0,1fr);font-size:.85rem}.feed-row{min-height:2.75rem}#feed-columns>span:nth-child(3),.feed-row>span:nth-child(3){display:none}#feeds-app button{min-height:2.3rem}#feed-keys{display:none}#feed-reader{border-left:0}.feeds-page .organizer-nav{font-size:.8rem}}
+1
View File
@@ -2,6 +2,7 @@
.organizer-nav a{display:flex;align-items:center;gap:.4rem;padding:.4rem .7rem;border-radius:.4rem;color:var(--link,var(--accent));text-decoration:none;white-space:nowrap;font-size:.9rem} .organizer-nav a{display:flex;align-items:center;gap:.4rem;padding:.4rem .7rem;border-radius:.4rem;color:var(--link,var(--accent));text-decoration:none;white-space:nowrap;font-size:.9rem}
.organizer-nav a[aria-current=page]{background:color-mix(in srgb,var(--accent,#478ddd) 15%,transparent);box-shadow:inset 0 -2px var(--accent,#478ddd);color:var(--fg)} .organizer-nav a[aria-current=page]{background:color-mix(in srgb,var(--accent,#478ddd) 15%,transparent);box-shadow:inset 0 -2px var(--accent,#478ddd);color:var(--fg)}
.organizer-nav a:hover{background:var(--row-hover,var(--border-soft,#8882))} .organizer-nav a:hover{background:var(--row-hover,var(--border-soft,#8882))}
@media(max-width:700px){.organizer-nav a span{display:none}.organizer-nav a{justify-content:center;min-width:2.75rem;min-height:2.75rem;box-sizing:border-box}}
.organizer-nav-icons{display:flex;align-items:center;gap:.15rem}.organizer-launch{display:inline-flex;align-items:center;justify-content:center;color:var(--fg);width:2.1rem;height:2.1rem;box-sizing:border-box;padding:.35rem;border-radius:50%} .organizer-nav-icons{display:flex;align-items:center;gap:.15rem}.organizer-launch{display:inline-flex;align-items:center;justify-content:center;color:var(--fg);width:2.1rem;height:2.1rem;box-sizing:border-box;padding:.35rem;border-radius:50%}
.organizer-launch svg{width:100%;height:100%;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linecap:round;stroke-linejoin:round}.organizer-launch:hover{color:var(--accent)} .organizer-launch svg{width:100%;height:100%;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linecap:round;stroke-linejoin:round}.organizer-launch:hover{color:var(--accent)}
.organizer-page .topbar{flex-wrap:wrap;gap:.2rem}.organizer-settings-content{min-width:0;padding:.75rem 1rem}.organizer-settings-content #task-productivity{margin:0;padding:0}.organizer-calendar-options{display:grid;gap:.4rem}.organizer-calendar-options label{display:flex;align-items:center;gap:.5rem;min-height:2rem} .organizer-page .topbar{flex-wrap:wrap;gap:.2rem}.organizer-settings-content{min-width:0;padding:.75rem 1rem}.organizer-settings-content #task-productivity{margin:0;padding:0}.organizer-calendar-options{display:grid;gap:.4rem}.organizer-calendar-options label{display:flex;align-items:center;gap:.5rem;min-height:2rem}
+1
View File
@@ -1,4 +1,5 @@
#task-productivity{max-width:1200px;margin:1rem auto;padding:0 1rem;color:var(--fg,var(--text-color));box-sizing:border-box} #task-productivity{max-width:1200px;margin:1rem auto;padding:0 1rem;color:var(--fg,var(--text-color));box-sizing:border-box}
#task-digest-history{min-width:0;margin:1rem 0}#task-digest-history summary{cursor:pointer;font-weight:600}#task-digest-history p{font-size:.9rem}#task-digest-history .digest-retry form{display:flex;gap:.6rem;align-items:center;flex-wrap:wrap}#task-digest-history .digest-retry label{display:flex;gap:.5rem;align-items:center}#task-digest-history button{font:inherit;color:inherit;background:var(--input-bg,var(--bg));border:1px solid var(--border);border-radius:.3rem;padding:.4rem .6rem;cursor:pointer}#task-digest-history th,#task-digest-history td{white-space:nowrap}#task-digest-history td:last-child{white-space:normal;min-width:10rem}@media(max-width:600px){#task-digest-history button,#task-digest-history .digest-retry label{min-height:44px}#task-digest-history .digest-retry input{min-width:24px;min-height:24px}}
body:has(#task-productivity) .topbar,body:has(#contacts-app) .topbar{flex-wrap:wrap;gap:.5rem} body:has(#task-productivity) .topbar,body:has(#contacts-app) .topbar{flex-wrap:wrap;gap:.5rem}
#task-productivity,#contacts-app{min-width:0;width:100%;overflow-wrap:anywhere} #task-productivity,#contacts-app{min-width:0;width:100%;overflow-wrap:anywhere}
@media(max-width:600px){body:has(#task-productivity) .topbar,body:has(#contacts-app) .topbar{padding:.5rem;gap:.3rem}body:has(#task-productivity) .topbar>.brand,body:has(#contacts-app) .topbar>.brand{flex:1}body:has(#task-productivity) .topbar>nav{display:none}} @media(max-width:600px){body:has(#task-productivity) .topbar,body:has(#contacts-app) .topbar{padding:.5rem;gap:.3rem}body:has(#task-productivity) .topbar>.brand,body:has(#contacts-app) .topbar>.brand{flex:1}body:has(#task-productivity) .topbar>nav{display:none}}
+1
View File
@@ -0,0 +1 @@
#task-work-history{max-width:1320px;margin:1rem auto;padding:0 1rem;color:var(--fg);box-sizing:border-box}#task-work-history>header,.work-history-controls{display:flex;align-items:center;flex-wrap:wrap;gap:.75rem}#task-work-history>header{justify-content:space-between}#task-work-history a{color:var(--link,var(--accent))}.work-history-controls label,.work-history-edit label{display:grid;gap:.25rem}.work-history-controls{margin-block:1rem}#task-work-history input,#task-work-history button{font:inherit;color:var(--fg);background:var(--input-bg,var(--bg));border:1px solid var(--border);border-radius:.3rem;padding:.35rem .5rem;box-sizing:border-box}#task-work-history button{cursor:pointer}#work-history-list{overflow:auto}.work-history-table{width:100%;border-collapse:collapse}.work-history-table td,.work-history-table th{padding:.3rem .4rem;text-align:left;vertical-align:top;border-bottom:1px solid var(--border)}.work-history-table td:not(:first-child){white-space:nowrap}.work-history-table td:first-child{overflow-wrap:anywhere}.work-history-table td button+button{margin-left:.4rem}.work-history-extra td{white-space:normal!important}.work-history-extra summary{color:var(--muted);font-size:.85rem;cursor:pointer}.work-history-extra ol{max-height:20rem;overflow:auto}.work-history-extra p{margin:.25rem 0;overflow-wrap:anywhere}.work-history-edit{display:flex;align-items:end;flex-wrap:wrap;gap:.6rem;padding:.7rem 0}.work-history-edit label:last-of-type{flex:1;min-width:12rem}.work-history-edit>p{width:100%}.work-history-edit input{max-width:100%}#work-history-paging{display:flex;justify-content:flex-end;gap:.6rem;margin-block:1rem}@media(max-width:760px){.work-history-table thead{display:none}.work-history-table,.work-history-table tbody,.work-history-table tr,.work-history-table td{display:block}.work-history-table tr{border-bottom:1px solid var(--border);padding:.4rem 0}.work-history-table td{border:0;white-space:normal!important}.work-history-table td[data-label]::before{content:attr(data-label);display:inline-block;min-width:4rem;color:var(--muted);margin-right:.5rem}#task-work-history input,#task-work-history button{min-height:44px}.work-history-edit{align-items:stretch}.work-history-edit label{width:100%;min-width:0!important}.work-history-extra summary{padding:.6rem 0}.work-history-controls label{max-width:100%}}
+20
View File
@@ -0,0 +1,20 @@
// Per-viewer volatile profile. Never use the SDK's default persistent OPFS
// profile: article cookies/history must disappear when the iframe is removed.
export function memoryProfile(limit = 32 * 1024 * 1024) {
const entries = new Map([['', null]]); let bytes = 0;
const path = value => {
if(typeof value !== 'string' || value.length > 2048 || value.startsWith('/') || value.includes('\\') || value.includes('\0') || value.split('/').some(v => v === '..' || v === '.')) throw Error('Invalid profile path');
return value.replace(/\/$/, '');
};
const parent = p => p.slice(0, Math.max(0, p.lastIndexOf('/')));
const requireParent = p => { if(entries.get(parent(p)) !== null) throw Error('Missing profile directory'); };
return {
async stat(value) { const p = path(value); return !entries.has(p) ? null : {size: entries.get(p)?.length || 0, isDir: entries.get(p) === null}; },
async readdir(value) { const p = path(value); if(entries.get(p) !== null) throw Error('Not a directory'); const prefix = p ? p+'/' : ''; return [...entries].filter(([k])=>k!==p && k.startsWith(prefix) && !k.slice(prefix.length).includes('/')).map(([k,v])=>k.slice(prefix.length)+(v===null?'/':'')); },
async readFile(value) { const data = entries.get(path(value)); if(!data) throw Error('Profile file not found'); return data.slice(); },
async writeFile(value,data) { const p = path(value); requireParent(p); if(entries.get(p)===null || !(data instanceof Uint8Array)) throw Error('Invalid profile file'); const next = bytes-(entries.get(p)?.length||0)+data.length; if(next>limit || (!entries.has(p)&&entries.size>=2048)) throw Error('Profile limit reached'); entries.set(p,data.slice()); bytes=next; },
async mkdir(value) { const p = path(value); if(entries.get(p)===null)return; requireParent(p); if(entries.has(p)||entries.size>=2048)throw Error('Profile limit reached'); entries.set(p,null); },
async unlink(value) { const p=path(value); if(!p)throw Error('Invalid profile path'); for(const [k,v] of entries)if(k===p||k.startsWith(p+'/')){bytes-=v?.length||0;entries.delete(k);} },
async rename(from,to) { const a=path(from),b=path(to); if(!a||!b||!entries.has(a)||b.startsWith(a+'/'))throw Error('Invalid profile rename'); if(a===b)return; requireParent(b); const moved=[...entries].filter(([k])=>k===a||k.startsWith(a+'/')); await this.unlink(b); for(const [k]of moved)entries.delete(k); for(const [k,v]of moved)entries.set(b+k.slice(a.length),v); }
};
}
+54
View File
@@ -0,0 +1,54 @@
import {memoryProfile} from './feed-gecko-profile.mjs';
const api = '/_/api/v1/extension/organizer.feeds/browser/';
const status = document.getElementById('status'), canvas = document.getElementById('screen');
let engine, started = false, ready = false, stopped = false, navigating = false, poll, resizeTimer;
let history = [], position = 0;
const ja = parent.document.documentElement.lang.startsWith('ja');
const text = (en,jp) => ja ? jp : en;
function report(message) { status.textContent=message; status.hidden=!message; }
function url(value) { try { const u=new URL(value); return ['https:','http:'].includes(u.protocol)&&!u.username&&!u.password&&u.href.length<=8192 ? u.href : ''; } catch {return '';} }
function send(data) { parent.postMessage({type:'pageswiki-gecko',...data},location.origin); }
function navigationState() { send({href:history[position],back:position>0,forward:position<history.length-1}); }
async function navigate(command) {
if(!ready||stopped||navigating)return;
const next=position+(command==='back'?-1:command==='forward'?1:0);
if(next<0||next>=history.length)return;
navigating=true;position=next;
try {
// Minimal upstream GRE omits SessionStoreFunctions: native history.back()
// fails. Keep a bounded viewer-local URL history and re-load the chosen URL.
await engine.load(history[position]);
const current=url(await engine.evalChrome('String(location.href)'));
if(current)history[position]=current;
navigationState();
} catch { report(text('Navigation failed. Use ↗ to open the original.','移動できませんでした。↗から原文を開いてください。')); }
finally {navigating=false;}
}
function stop() { stopped=true;clearTimeout(poll);clearTimeout(resizeTimer);engine?.destroy(); }
async function start(href) {
started=true;
if(!crossOriginIsolated || typeof SharedArrayBuffer==='undefined') { report(text('This browser cannot run the isolated viewer. Use ↗ to open the original.','このブラウザでは分離ビューアを実行できません。↗から原文を開いてください。'));return; }
report(text('Starting Gecko…','Geckoを起動中…'));
const timer=setTimeout(()=>{if(!ready&&!stopped)report(text('Startup is taking longer than expected. Close the viewer to stop, or use ↗.','起動に時間がかかっています。閉じると停止します。↗から原文も開けます。'));},45000);
try {
const {Gecko}=await import(api+'assets/v0.0.1/gecko.js'); if(stopped)return;
const socket=new URL(api+'wisp/',location.href);socket.protocol=location.protocol==='https:'?'wss:':'ws:';
engine=new Gecko({canvas,wasm:{url:api+'assets/v0.0.1/gecko.wasm.zst',compressed:true},wispUrl:socket.href,profile:memoryProfile(),width:Math.min(innerWidth,1920),height:Math.min(innerHeight,1080),env:{GECKO_NOWASMJIT:'1'},print:()=>{},printErr:()=>{}});
await engine.init();if(stopped)return;
await engine.load(href);if(stopped)return;
history=[url(await engine.evalChrome('String(location.href)'))||href];position=0;
ready=true;report('');canvas.focus();send({ready:true});
navigationState();
const update=async()=>{if(stopped)return;try{if(!navigating){const href=url(await engine.evalChrome('String(location.href)'));if(href&&!navigating&&href!==history[position]){history=history.slice(0,position+1);history.push(href);if(history.length>64)history.shift();position=history.length-1;navigationState();}}}catch{/* Keep the last validated URL. */}if(!stopped)poll=setTimeout(update,2000);};update();
} catch { report(text('Viewer failed. Close it and retry, or use ↗ to open the original.','ビューアを開始できませんでした。閉じて再試行するか、↗から原文を開いてください。')); }
finally {clearTimeout(timer);}
}
addEventListener('message',event=>{
if(event.source!==parent||event.origin!==location.origin||event.data?.type!=='pageswiki-gecko')return;
if(!started){const href=url(event.data.href);if(href)start(href);return;}
if(['back','forward','reload'].includes(event.data.command))navigate(event.data.command);
});
addEventListener('resize',()=>{clearTimeout(resizeTimer);resizeTimer=setTimeout(()=>{if(ready&&!stopped)engine.resize(Math.min(innerWidth,1920),Math.min(innerHeight,1080)).catch(()=>{});},150);});
addEventListener('keydown',e=>{if(e.key==='Escape'){e.preventDefault();send({close:true});}},true);
addEventListener('pagehide',stop);
send({listening:true});
+247
View File
@@ -0,0 +1,247 @@
(() => {
'use strict';
const root=document.getElementById('feeds-app');if(!root)return;
const ja=document.documentElement.lang.startsWith('ja'),t=(en,jp)=>ja?jp:en;
const api='/_/api/v1/extension/organizer.feeds/',writable=root.dataset.writable==='true';
const toolbar=document.getElementById('feed-toolbar'),status=document.getElementById('feed-status'),sourcesView=document.getElementById('feed-sources'),rows=document.getElementById('feed-rows'),reader=document.getElementById('feed-reader'),layout=root.querySelector('.feed-layout'),more=document.getElementById('feed-more');
const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(cls)n.className=cls;return n;};
const report=e=>{if(e.name!=='AbortError')status.textContent=e.message;};
function button(text,label,fn){const b=el('button',text);b.type='button';b.title=b.ariaLabel=label;b.addEventListener('click',()=>Promise.resolve().then(fn).catch(report));return b;}
async function request(path,options={}){const response=await fetch(api+path,{credentials:'same-origin',cache:'no-store',...options,headers:{'Content-Type':'application/json',...options.headers}});if(response.redirected||response.status===401)throw Error(t('Please sign in again.','再度ログインしてください。'));const data=await response.json().catch(()=>({}));if(!response.ok)throw Error(response.status===412?t('Changed elsewhere or refresh is cooling down. Reload before retrying.','別の操作と競合したか、更新の待機時間中です。再読込して確認してください。'):t('Request failed: ','操作に失敗しました: ')+(data.error||response.status));return data;}
const post=(path,body)=>request(path,{method:'POST',body:JSON.stringify(body)});
let sources=[],items=[],selected=-1,currentSource='',filter='',search='',next=null,loading=false,seq=0,detailSeq=0,editing=false,controller,detailController;
let remoteFeed='',remoteCategory='',categoryMode='',catalog=[],catalogFolders=[],catalogLoaded=false,catalogSearch='',catalogSeq=0,catalogController;
const folderStates=new Map();
function folderState(source){if(folderStates.has(source))return folderStates.get(source);let state=new Set();try{const raw=localStorage.getItem('pageswiki.feed-folders.v1:'+source);if(raw&&raw.length<=1<<20){const saved=JSON.parse(raw);if(Array.isArray(saved)&&saved.length<=1001)state=new Set(saved.filter(x=>typeof x==='string'&&x.length<=2100));}}catch{}folderStates.set(source,state);return state;}
let actionTimer;
const pendingAction=a=>a&&['queued','preparing','sending','unknown','check_queued','checking'].includes(a.status);
async function updateAction(item){const version=detailSeq;const data=await request('action?'+new URLSearchParams({source:item.source,id:item.id}));if(version!==detailSeq||items[selected]!==item||editing)return;item.action=data.action;item.actionLoading=false;
if(data.action&&['confirmed','conflict'].includes(data.action.status)){const fresh=(await request('item?'+new URLSearchParams({source:item.source,id:item.id}))).item;if(version!==detailSeq||items[selected]!==item||editing)return;Object.assign(item,fresh);drawRows();}renderItem(item);
}
function pollAction(item){clearTimeout(actionTimer);if(document.hidden||!item.action||!['queued','preparing','sending','check_queued','checking'].includes(item.action.status))return;actionTimer=setTimeout(()=>{if(items[selected]===item&&!editing)updateAction(item).catch(report);},2000);}
document.addEventListener('visibilitychange',()=>{if(document.hidden)clearTimeout(actionTimer);else{const item=items[selected];if(item?.provider==='freshrss'&&!editing)updateAction(item).catch(report);}});
const sourceLabel=el('strong','RSS'),searchbox=el('input');searchbox.type='search';searchbox.placeholder=t('Search titles / feeds… /','件名・配信元を検索… /');searchbox.ariaLabel=searchbox.placeholder;
const filters=el('select');for(const [v,en,jp]of [['','All','すべて'],['unread','Unread','未読'],['starred','Starred','star付き']]){const o=el('option',t(en,jp));o.value=v;filters.append(o);}filters.ariaLabel=t('Filter','絞り込み');filters.onchange=()=>{filter=filters.value;load(false).catch(report);};
const refresh=button('↻',t('Queue source refresh (r)','背景更新を予約 (r)'),queueRefresh);refresh.disabled=!writable;
toolbar.append(button('☰',t('Sources','購読元'),()=>layout.classList.toggle('sources-open')),sourceLabel,searchbox,filters,refresh);
if(writable)toolbar.append(button('+',t('Add source','購読元を追加'),()=>sourceForm()));
const transfer=button('⇄','OPML',opmlForm);transfer.id='feed-opml';toolbar.append(transfer);
const history=button('◷',t('Operation history','操作履歴'),actionHistory);history.id='feed-history';toolbar.append(history);
const helpButton=button('?',t('Keyboard shortcuts','キー操作'),showHelp),help=el('div',undefined,'feed-help');help.id='feed-help';help.hidden=true;helpButton.setAttribute('aria-controls',help.id);helpButton.setAttribute('aria-expanded','false');
help.append(el('span',t('j/k articles · Shift+j/k folders · Enter read · Esc index · / search · n unread · m read/unread · * star · r refresh · ? / Esc close help','j/k 記事移動 · Shift+j/k フォルダ移動 · Enter 本文 · Esc 一覧 · / 検索 · n 次の未読 · m 既読/未読 · * star · r 更新 · ? / Esc ヘルプを閉じる')),button('×',t('Close help','ヘルプを閉じる'),hideHelp));status.before(help);toolbar.append(helpButton);
let debounce;searchbox.addEventListener('input',()=>{clearTimeout(debounce);debounce=setTimeout(()=>{search=searchbox.value.trim();load(false).catch(report);},180);});
const columns=document.getElementById('feed-columns');for(const s of ['',t('Date','日付'),t('Feed','配信元'),t('Subject','件名')])columns.append(el('span',s));
function hideHelp(){if(help.contains(document.activeElement))helpButton.focus();help.hidden=true;helpButton.setAttribute('aria-expanded','false');}
function showHelp(){if(!help.hidden){hideHelp();return;}help.hidden=false;helpButton.setAttribute('aria-expanded','true');}
function date(ms){return ms?new Intl.DateTimeFormat(ja?'ja-JP':'en-GB',{month:'2-digit',day:'2-digit'}).format(new Date(ms)):'—';}
function closeReader(){editing=false;detailSeq++;detailController?.abort();layout.classList.remove('reading','action-history');rows.focus();}
function drawSources(){sourcesView.replaceChildren();const all=button(t('All sources','すべての購読元'),'',()=>chooseSource(''));sourcesView.append(all);let folder=null;
for(const source of sources){if(folder!==source.folder){folder=source.folder;sourcesView.append(el('h3',folder||t('Sources','購読元')));}const row=el('div',undefined,'feed-source');row.setAttribute('aria-current',String(currentSource===source.uuid));
const b=button((source.enabled?'':'⏸ ')+source.title,source.title,()=>chooseSource(source.uuid));if(source.error){b.classList.add('feed-source-error');b.title+=' · '+source.error;}row.append(b);if(writable)row.append(button('⚙',t('Source settings','購読元設定'),()=>sourceForm(source)));sourcesView.append(row);
if(source.uuid===currentSource&&source.provider==='freshrss')drawCatalog(sourcesView);
}
}
function catalogGroups(){
const groups=new Map();
for(const c of catalogFolders)groups.set(c.id,{id:c.id,label:c.label,mode:'',subs:[]});
const uncategorized={id:'',label:t('Uncategorized','未分類'),mode:'uncategorized',subs:[]};
for(const sub of catalog){if(!sub.categories.length)uncategorized.subs.push(sub);for(const c of sub.categories){if(!groups.has(c.id))groups.set(c.id,{id:c.id,label:c.label,mode:'',subs:[]});groups.get(c.id).subs.push(sub);}}
const ordered=[...groups.values()].sort((a,b)=>a.label.localeCompare(b.label)||a.id.localeCompare(b.id));if(uncategorized.subs.length)ordered.push(uncategorized);
return ordered;
}
function markCatalog(){for(const b of sourcesView.querySelectorAll('.feed-catalog button[data-category]'))b.setAttribute('aria-current',String((b.dataset.feed||'')===remoteFeed&&b.dataset.category===remoteCategory&&b.dataset.mode===categoryMode));}
async function chooseCatalog(id,mode,feed=''){
remoteCategory=id;categoryMode=mode;remoteFeed=feed;markCatalog();layout.classList.remove('sources-open','reading','action-history');
detailSeq++;detailController?.abort();clearTimeout(actionTimer);selected=-1;editing=false;reader.replaceChildren();await load(false);
}
function moveFolder(delta){
if(!catalogLoaded||sources.find(s=>s.uuid===currentSource)?.provider!=='freshrss')return;
const groups=catalogGroups();if(!groups.length)return;
const current=groups.findIndex(g=>g.id===remoteCategory&&g.mode===categoryMode);
const index=current<0?(delta>0?0:groups.length-1):current+delta;if(index<0||index>=groups.length)return;
// Folder search must not leave the newly selected folder invisible.
if(catalogSearch){catalogSearch='';drawSources();}
const group=groups[index],result=chooseCatalog(group.id,group.mode);
const selectedFolder=sourcesView.querySelector('.feed-folder-name[aria-current="true"]');
selectedFolder?.scrollIntoView({block:'nearest'});rows.focus({preventScroll:true});return result;
}
function drawCatalog(parent){
if(!catalogLoaded)return;
const nav=el('nav',undefined,'feed-catalog');nav.ariaLabel=t('FreshRSS folders','FreshRSSのフォルダ');const source=currentSource,state=folderState(source),ordered=catalogGroups();
const key=g=>JSON.stringify([g.mode,g.id]),valid=new Set(ordered.map(key));for(const k of state)if(!valid.has(k))state.delete(k);
const persist=()=>{try{localStorage.setItem('pageswiki.feed-folders.v1:'+source,JSON.stringify([...state]));}catch{}};
const select=(label,g,feed='')=>{const b=button(label,label,()=>chooseCatalog(g.id,g.mode,feed));b.dataset.category=g.id;b.dataset.mode=g.mode;if(feed)b.dataset.feed=feed;b.setAttribute('aria-current',String(feed===remoteFeed&&g.id===remoteCategory&&g.mode===categoryMode));return b;};
const all=select(t('All subscriptions','すべての購読'),{id:'',mode:''});all.className='feed-tree-all';nav.append(all);
const find=el('input');find.type='search';find.placeholder=find.ariaLabel=t('Find subscription / folder','購読・フォルダを検索');find.value=catalogSearch;nav.append(find);
const list=el('div',undefined,'feed-folders');nav.append(list);parent.append(nav);
function subscriptions(target,subs,g){let count=0;const sorted=[...subs].sort((a,b)=>a.title.localeCompare(b.title)||a.id.localeCompare(b.id));const more=button('↓',t('More subscriptions','購読元をさらに表示'),append);
function append(){more.remove();for(const sub of sorted.slice(count,count+50)){const group=g||{id:'',mode:''},b=select(sub.title||sub.id,group,sub.id);b.title=(sub.title||sub.id)+(sub.categories.length?' · '+sub.categories.map(c=>c.label).join(' / '):'');target.append(b);}count+=50;if(count<sorted.length)target.append(more);}append();
}
const draw=()=>{list.replaceChildren();const query=find.value.trim().toLocaleLowerCase();catalogSearch=find.value;
if(query){const matches=catalog.filter(s=>s.title.toLocaleLowerCase().includes(query)||s.categories.some(c=>c.label.toLocaleLowerCase().includes(query)));const results=el('div',undefined,'feed-subscriptions');list.append(results);subscriptions(results,matches,null);if(!matches.length)results.append(el('small',t('No subscriptions','購読なし')));return;}
for(const g of ordered){const group=el('div',undefined,'feed-folder'),line=el('div',undefined,'feed-folder-row'),children=el('div',undefined,'feed-subscriptions');group.dataset.category=g.id;group.dataset.mode=g.mode;
const name=select(g.label||g.id,g);name.className='feed-folder-name';name.title=(g.label||g.id)+(g.id?' · '+g.id:'');const label=el('span',g.label||g.id);name.replaceChildren();
const svg=document.createElementNS('http://www.w3.org/2000/svg','svg'),path=document.createElementNS(svg.namespaceURI,'path');svg.setAttribute('viewBox','0 0 24 24');svg.setAttribute('aria-hidden','true');path.setAttribute('d','M3 6h7l2 3h9v11H3z');svg.append(path);name.append(svg,label);
let open=state.has(key(g));const toggle=button('',t('Expand folder','フォルダを展開')+' · '+g.label,()=>{open=!open;if(open)state.add(key(g));else state.delete(key(g));persist();paint();});toggle.className='feed-folder-toggle';
const paint=()=>{toggle.textContent=open?'▾':'▸';toggle.setAttribute('aria-expanded',String(open));toggle.title=toggle.ariaLabel=(open?t('Collapse folder','フォルダを折りたたむ'):t('Expand folder','フォルダを展開'))+' · '+g.label;children.hidden=!open;children.replaceChildren();if(open){subscriptions(children,g.subs,g);if(!g.subs.length)children.append(el('small',t('Empty folder','空のフォルダ')));}};
line.append(toggle,name);group.append(line,children);list.append(group);paint();
}markCatalog();
};find.oninput=draw;draw();
}
async function loadCatalog(){catalogController?.abort();catalogController=new AbortController();const version=++catalogSeq;catalogLoaded=false;catalog=[];catalogFolders=[];if(sources.find(s=>s.uuid===currentSource)?.provider!=='freshrss')return;
const data=await request('catalog?'+new URLSearchParams({source:currentSource}),{signal:catalogController.signal});if(version!==catalogSeq)return;catalog=data.subscriptions;catalogFolders=data.folders||[];catalogLoaded=true;drawSources();
}
async function chooseSource(id){currentSource=id;remoteFeed=remoteCategory=categoryMode=catalogSearch='';catalog=[];catalogFolders=[];catalogLoaded=false;drawSources();layout.classList.remove('reading');editing=false;await Promise.all([loadCatalog(),load(false)]);if(sources.find(s=>s.uuid===id)?.provider!=='freshrss')layout.classList.remove('sources-open');}
function rowNode(item,index){const row=el('div',undefined,'feed-row'+(item.read?'':' unread'));row.id='feed-row-'+index;row.setAttribute('role','option');row.setAttribute('aria-selected',String(index===selected));for(const text of [(item.starred?'★':item.read?' ':'●'),date(item.published_ms),item.feed||sources.find(s=>s.uuid===item.source)?.title||'',item.title||t('(Untitled)','(無題)')])row.append(el('span',text));row.title=item.title;row.onclick=()=>open(index).catch(report);return row;}
function drawRows(){rows.replaceChildren(...items.map(rowNode));more.hidden=!next;more.textContent='↓';more.title=more.ariaLabel=t('More articles','続きを表示');if(!items.length)rows.append(el('p',t('No articles','記事なし'),'feed-warning'));}
async function load(append){if(append&&(!next||loading))return;controller?.abort();controller=new AbortController();const version=++seq;loading=true;more.disabled=true;
try{const q=new URLSearchParams({source:currentSource,filter,q:search,feed:remoteFeed,category:remoteCategory,category_mode:categoryMode});if(append){q.set('before',next.ms);q.set('key',next.key);}const data=await request('items?'+q,{signal:controller.signal});if(version!==seq)return;
if(!append){detailSeq++;items=[];selected=-1;reader.replaceChildren();layout.classList.remove('reading','action-history');editing=false;}items.push(...data.items);const last=data.items.at(-1);next=data.more&&last?{ms:last.published_ms,key:last.source+':'+last.id}:null;drawRows();status.textContent='';
}finally{if(version===seq){loading=false;more.disabled=false;}}
}
more.onclick=()=>load(true).catch(report);
async function reload(){sources=(await request('sources')).sources;if(!sources.some(s=>s.uuid===currentSource)){currentSource=remoteFeed=remoteCategory=categoryMode='';}drawSources();await loadCatalog();if(remoteCategory&&!catalogFolders.some(c=>c.id===remoteCategory))remoteCategory='';if(remoteFeed&&!catalog.some(s=>s.id===remoteFeed))remoteFeed='';drawSources();await load(false);}
async function queueRefresh(){const targets=sources.filter(s=>s.enabled&&(!currentSource||currentSource===s.uuid));for(const s of targets)await post('sources/'+s.uuid+'/refresh',{version:s.version});status.textContent=t('Refresh queued','更新を予約しました');}
toolbar.append(button('⟳',t('Reload cached list','取得済み一覧を再読込'),reload));
async function open(index){if(index<0||index>=items.length)return;editing=false;selected=index;detailController?.abort();detailController=new AbortController();const version=++detailSeq;for(const [i,row]of [...rows.querySelectorAll('.feed-row')].entries())row.setAttribute('aria-selected',String(i===index));rows.setAttribute('aria-activedescendant','feed-row-'+index);document.getElementById('feed-row-'+index)?.scrollIntoView({block:'nearest'});
layout.classList.remove('action-history');layout.classList.add('reading');reader.replaceChildren(el('p',t('Loading…','読み込み中…')));const item=(await request('item?'+new URLSearchParams({source:items[index].source,id:items[index].id}),{signal:detailController.signal})).item;if(version!==detailSeq)return;items[index]=item;if(item.provider==='freshrss'){item.actionLoading=true;renderItem(item);await updateAction(item);}else renderItem(item);
}
function openArticleLink(event,href){
if(!['embedded','gecko'].includes(root.dataset.linkMode)||event.button!==0||event.ctrlKey||event.metaKey||event.altKey||event.shiftKey)return;
let url;try{url=new URL(href,location.href);}catch{return;}if(!['https:','http:'].includes(url.protocol)||url.origin===location.origin||url.username||url.password)return;
event.preventDefault();const wasEditing=editing;editing=true;detailSeq++;clearTimeout(actionTimer);
const dialog=el('dialog',undefined,'feed-external'),bar=el('div',undefined,'feed-external-bar'),name=el('span',url.hostname),external=el('a','↗'),frame=el('iframe');dialog.ariaLabel=t('Original article preview','原文プレビュー');name.title=url.href;
external.href=url.href;external.target='_blank';external.rel='noopener noreferrer';external.title=external.ariaLabel=t('Open in a new tab if embedding is blocked','埋め込みできない場合は新しいタブで開く');
const gecko=root.dataset.linkMode==='gecko', controller=new AbortController();
const close=button('×',t('Close preview','プレビューを閉じる'),()=>dialog.close());bar.append(name,external,close);frame.title=dialog.ariaLabel;frame.referrerPolicy='no-referrer';
if(gecko){
frame.src=api+'browser/';frame.allow='cross-origin-isolated';
const send=command=>frame.contentWindow?.postMessage({type:'pageswiki-gecko',command},location.origin);
const nav={};for(const [icon,label,command]of [['←',t('Back','戻る'),'back'],['→',t('Forward','進む'),'forward'],['⟳',t('Reload','再読込'),'reload']]){const b=button(icon,label,()=>send(command));b.disabled=true;nav[command]=b;bar.insertBefore(b,name);}
addEventListener('message',e=>{if(e.origin!==location.origin||e.source!==frame.contentWindow||e.data?.type!=='pageswiki-gecko')return;if(e.data.listening)frame.contentWindow.postMessage({type:'pageswiki-gecko',href:url.href},location.origin);if(e.data.close)dialog.close();if(e.data.ready)nav.reload.disabled=false;if(typeof e.data.back==='boolean')nav.back.disabled=!e.data.back;if(typeof e.data.forward==='boolean')nav.forward.disabled=!e.data.forward;if(e.data.href){try{const u=new URL(e.data.href);if(['http:','https:'].includes(u.protocol)&&!u.username&&!u.password){name.textContent=u.hostname;name.title=external.href=u.href;}}catch{}}},{signal:controller.signal});
let hiddenTimer;document.addEventListener('visibilitychange',()=>{clearTimeout(hiddenTimer);if(document.hidden)hiddenTimer=setTimeout(()=>dialog.close(),60000);},{signal:controller.signal});dialog.addEventListener('close',()=>clearTimeout(hiddenTimer),{once:true});
}else{frame.setAttribute('sandbox','allow-scripts');frame.src=url.href;}
dialog.append(bar,frame);root.append(dialog);
dialog.addEventListener('keydown',e=>e.stopPropagation());dialog.addEventListener('close',()=>{controller.abort();frame.src='about:blank';dialog.remove();editing=wasEditing;const item=items[selected];if(item&&!editing)pollAction(item);});dialog.showModal();close.focus();
}
function renderItem(item){reader.replaceChildren();const actions=el('div',undefined,'feed-actions');actions.append(button('←',t('Back to list','一覧へ戻る'),closeReader));
const canWrite=item.provider==='local'||sources.some(s=>s.uuid===item.source&&s.enabled&&s.write_enabled);
const read=button(item.read?'●':'○',t('Toggle read state (m)','既読/未読 (m)'),()=>setState(!item.read,item.starred));const star=button(item.starred?'★':'☆',t('Toggle star (*)','star切替 (*)'),()=>setState(item.read,!item.starred));read.disabled=star.disabled=!writable||!canWrite||item.actionLoading||pendingAction(item.action);actions.append(read,star);
if(item.action){const a=item.action,labels={queued:t('Queued','送信待ち'),preparing:t('Checking','確認中'),sending:t('Sending','送信中'),checking:t('Rechecking','再確認中'),check_queued:t('Recheck queued','再確認待ち'),confirmed:t('Confirmed','確認済み'),unknown:t('Result unknown — recheck after one minute','結果不明 — 1分後に再確認'),conflict:t('Changed elsewhere — review before retrying','状態が一致しません。確認してから再操作してください'),failed:t('Not sent','未送信'),cancelled:t('Cancelled','中止')};const label=labels[a.status]||a.status;
const state=button(a.status==='unknown'?'↻':pendingAction(a)?'◷':a.status==='confirmed'?'✓':'!',label,async()=>{if(a.status!=='unknown')return;await post('action/check',{request_id:a.request_id});await updateAction(item);});state.className='feed-action-status';state.disabled=a.status!=='unknown'||!writable;state.title+=(a.error?' · '+a.error:'');actions.append(state);}
if(item.url){const a=el('a',undefined,'feed-original');a.title=a.ariaLabel=t('Open original','原文を開く');a.href=item.url;a.target='_blank';a.rel='noopener noreferrer';const svg=document.createElementNS('http://www.w3.org/2000/svg','svg'),path=document.createElementNS(svg.namespaceURI,'path');svg.setAttribute('viewBox','0 0 24 24');svg.setAttribute('aria-hidden','true');path.setAttribute('d','M14 3h7v7M21 3l-11 11M10 5H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-5');svg.append(path);a.append(svg);actions.append(a);}
const original=actions.querySelector('.feed-original');if(original)original.addEventListener('click',e=>openArticleLink(e,original.href));
if(item.url&&root.dataset.shareX==='true'){const share=el('a',undefined,'feed-share-x feed-original');share.title=share.ariaLabel=t('Share original article on X','原文の記事をXでシェア');share.href='https://twitter.com/intent/tweet?'+new URLSearchParams({text:item.title||'',url:item.url});share.target='_blank';share.rel='noopener noreferrer';const svg=document.createElementNS('http://www.w3.org/2000/svg','svg'),path=document.createElementNS(svg.namespaceURI,'path');svg.setAttribute('viewBox','0 0 24 24');svg.setAttribute('aria-hidden','true');path.setAttribute('d','M18 3h3l-7 8 8 10h-6l-5-7-6 7H2l7-9L2 3h6l5 6z');svg.append(path);share.append(svg);actions.append(share);}
if(writable)actions.append(button(t('+ Wiki / Task','+ Wiki・タスク'),t('Capture with attribution','出典付きで取り込む'),()=>captureForm(item)));
reader.append(actions,el('h1',item.title||t('(Untitled)','(無題)')),el('p',(item.feed||'')+' · '+(item.published_ms?new Date(item.published_ms).toLocaleString():t('Date unknown','日付不明')),'feed-meta'));
if(item.provider==='freshrss'&&!canWrite)read.title=star.title=t('FreshRSS · read only','FreshRSS · 読取専用');
const body=el('div',undefined,'feed-body');
if(item.body_html){body.classList.add('feed-body-html');const shadow=body.attachShadow({mode:'open'}),style=el('style',':host{display:block;overflow-wrap:anywhere;line-height:1.6}p{margin:.6em 0}h1,h2,h3{line-height:1.3}a{color:var(--link,var(--accent,#659de4))}pre{white-space:pre-wrap;overflow-wrap:anywhere;padding:.5em;background:color-mix(in srgb,currentColor 5%,transparent)}blockquote{margin:.5em 0;padding-left:1em;border-left:3px solid var(--border,#8885)}table{border-collapse:collapse;max-width:100%;display:block;overflow:auto}th,td{padding:.3em .6em;border:1px solid var(--border,#8885)}');const content=el('div');content.innerHTML=item.body_html;shadow.append(style,content);}
else body.textContent=item.body||t('(No article text)','(本文なし)');
if(body.shadowRoot)body.shadowRoot.addEventListener('click',e=>{const link=e.target.closest?.('a[href]');if(link)openArticleLink(e,link.href);});
reader.append(body);
pollAction(item);
}
async function setState(read,starred){const item=items[selected];if(!item||!writable||item.actionLoading||pendingAction(item.action))return;const source=item.source,id=item.id;
if(item.provider==='freshrss'){const config=sources.find(s=>s.uuid===source);if(!config?.enabled||!config?.write_enabled)return;const field=read!==item.read?'read':'starred',desired=field==='read'?read:starred;if(item[field]===desired)return;item.actionLoading=true;renderItem(item);try{await post('remote-state',{request_id:crypto.randomUUID(),source,id,field,desired,source_version:config.version});}finally{await updateAction(item);}return;}
const data=await post('state',{source,id,read,starred,version:item.version});Object.assign(item,{read,starred,version:data.version});drawRows();if(items[selected]===item&&!editing)renderItem(item);}
function field(form,label,value='',type='text'){const wrap=el('label',label),input=el('input');input.type=type;input.value=value;wrap.append(input);form.append(wrap);return input;}
function actionHistory(){
editing=true;const version=++detailSeq;detailController?.abort();controller?.abort();seq++;loading=false;more.disabled=false;clearTimeout(actionTimer);layout.classList.add('reading','action-history');status.textContent='';reader.replaceChildren();
const section=el('section',undefined,'feed-history'),heading=el('div',undefined,'feed-actions');section.id='feed-history-panel';reader.append(section);
heading.append(button('←',t('Back to list','一覧へ戻る'),closeReader),el('h1',t('Operation history','操作履歴')));section.append(heading);
const controls=el('div',undefined,'feed-history-controls'),states=el('select'),sourceFilter=el('select');states.id='feed-history-state';states.ariaLabel=t('Operation status','操作状態');sourceFilter.ariaLabel=t('Source','購読元');
for(const [value,en,jp]of [['unresolved','Needs attention','未解決'],['pending','In progress','処理中'],['unknown','Result unknown','結果不明'],['all','All','すべて'],['confirmed','Confirmed','確認済み'],['failed','Not sent','未送信'],['conflict','Conflict','不一致'],['cancelled','Cancelled','中止']]){const o=el('option',t(en,jp));o.value=value;states.append(o);}
for(const [value,label]of [['',t('All sources','すべての購読元')],...sources.filter(s=>s.provider==='freshrss').map(s=>[s.uuid,s.title])]){const o=el('option',label);o.value=value;sourceFilter.append(o);}
const reloadHistory=button('↻',t('Reload history','履歴を再読込'),()=>fetchHistory(false));reloadHistory.id='feed-history-reload';controls.append(states,sourceFilter,reloadHistory);section.append(controls);
const table=el('table'),head=el('thead'),headRow=el('tr'),body=el('tbody');for(const label of [t('Date','日時'),t('Article / operation','記事・操作'),t('Status','状態'),t('Actions','操作')]){const th=el('th',label);th.scope='col';headRow.append(th);}head.append(headRow);table.append(head,body);section.append(table);
const empty=el('p',t('No operations','操作なし'),'feed-warning'),notice=el('p',undefined,'feed-warning'),nextPage=button('↓',t('More operations','続きを表示'),()=>fetchHistory(true));notice.role='status';empty.hidden=true;nextPage.hidden=true;section.append(empty,notice,nextPage);
let cursor=null,generation=0,busy=false,abort;
const labels={queued:t('Queued','送信待ち'),preparing:t('Checking','確認中'),sending:t('Sending','送信中'),check_queued:t('Recheck queued','再確認待ち'),checking:t('Rechecking','再確認中'),unknown:t('Result unknown','結果不明'),confirmed:t('Confirmed','確認済み'),failed:t('Not sent','未送信'),conflict:t('Conflict','不一致'),cancelled:t('Cancelled','中止')};
const errors={network:t('Network error','通信エラー'),credentials:t('Authentication failed','認証失敗'),remote_error:t('Remote error','接続先エラー'),invalid_feed:t('Invalid response','応答不正'),limit:t('Limit exceeded','上限超過'),expired:t('Expired','期限切れ'),source_changed:t('Source settings changed','購読元設定が変更されました'),source_deleted:t('Source deleted','購読元削除済み'),conflict:t('Remote state differs','外部の状態が一致しません')};
function row(a){const tr=el('tr');tr.dataset.requestId=a.request_id;const when=el('td',new Date(a.created_ms).toLocaleString(ja?'ja-JP':'en-GB',{month:'2-digit',day:'2-digit',hour:'2-digit',minute:'2-digit'})),subject=el('td'),state=el('td',labels[a.status]||a.status),actions=el('td');when.title=new Date(a.created_ms).toLocaleString();state.dataset.state=a.status;state.title=errors[a.error]||a.error||'';
const title=a.item_title||a.id,name=a.source_title||a.source,operation=a.field==='read'?(a.desired?t('Read','既読'):t('Unread','未読')):(a.desired?t('Star','star追加'):t('Unstar','star解除'));
const text=el('span',title);text.title=title;subject.append(text,el('small',name+' · '+operation));if(!a.source_present)subject.append(el('small',t('Source deleted','購読元削除済み')));else if(!a.item_present)subject.append(el('small',t('Outside article cache','記事キャッシュ外')));
const tools=el('div',undefined,'feed-history-buttons');actions.append(tools);
if(a.item_present){const view=button('↗',t('Read article','記事を読む'),async()=>{let index=items.findIndex(i=>i.source===a.source&&i.id===a.id);if(index<0){const data=await request('item?'+new URLSearchParams({source:a.source,id:a.id}));if(detailSeq!==version)return;items.unshift(data.item);index=0;drawRows();}await open(index);});tools.append(view);}
if(a.status==='unknown'){const check=button('↻',t('Recheck remote state without resending','再送せず外部状態を再確認'),async()=>{check.disabled=true;try{await post('action/check',{request_id:a.request_id});await fetchHistory(false);}catch(e){notice.textContent=e.message;check.disabled=false;}});check.className='feed-history-check';check.disabled=!writable||!a.can_check;if(!a.source_present)check.title=t('Deleted source cannot be rechecked','削除した購読元は再確認できません');else if(a.check_after_ms>Date.now())check.title=t('Recheck after ','再確認可能時刻: ')+new Date(a.check_after_ms).toLocaleTimeString();else if(!a.can_check)check.title=t('Requires an enabled source and write permission','購読元の有効化と操作権限が必要です');tools.append(check);}
const save=el('a','↓');save.className='feed-history-export';save.href=api+'action/export?'+new URLSearchParams({request_id:a.request_id});save.download='pageswiki-feed-action.json';save.title=save.ariaLabel=t('Save operation receipt (JSON)','操作記録を保存(JSON)');tools.append(save);tr.append(when,subject,state,actions);return tr;}
async function fetchHistory(append){if(version!==detailSeq||!section.isConnected||append&&(busy||!cursor))return;abort?.abort();abort=new AbortController();const n=++generation;busy=true;nextPage.disabled=true;notice.textContent='';if(!append){cursor=null;nextPage.hidden=true;empty.hidden=true;body.replaceChildren();}
try{const q=new URLSearchParams({status:states.value,source:sourceFilter.value});if(append){q.set('before',cursor.created_ms);q.set('key',cursor.request_id);}const data=await request('actions?'+q,{signal:abort.signal});if(n!==generation||version!==detailSeq||!section.isConnected)return;if(!append)body.replaceChildren();body.append(...data.actions.map(row));cursor=data.more?data.actions.at(-1):null;nextPage.hidden=!cursor;empty.hidden=body.children.length!==0;
}catch(e){if(e.name!=='AbortError'&&n===generation&&section.isConnected)notice.textContent=e.message;}finally{if(n===generation){busy=false;nextPage.disabled=false;}}
}
states.onchange=sourceFilter.onchange=()=>fetchHistory(false);fetchHistory(false);
}
function opmlForm(){
editing=true;detailSeq++;detailController?.abort();layout.classList.add('reading');reader.replaceChildren();
const section=el('section',undefined,'feed-opml'),heading=el('div',undefined,'feed-actions');heading.append(el('h1','OPML'),button('←',t('Back','戻る'),closeReader));section.append(heading);reader.append(section);
const exportLink=el('a',t('Export local subscriptions','内蔵購読を書き出す'));exportLink.href=api+'opml/export';exportLink.download='pageswiki-feeds.opml';section.append(exportLink);if(!writable)return;
const fileLabel=el('label',t('Import local subscriptions','内蔵購読を取り込む')),file=el('input');file.type='file';file.accept='.opml,.xml,text/x-opml,application/xml,text/xml';file.id='feed-opml-file';fileLabel.append(file);section.append(fileLabel);
const preview=el('div');section.append(preview);let generation=0;
const reasons={add:t('Add','追加'),existing:t('Subscribed','登録済み'),duplicate:t('Duplicate','重複'),unsupported_url:t('Unsupported URL','未対応URL'),unsupported_type:t('Unsupported type','未対応形式'),invalid_fields:t('Invalid name/folder','名前・フォルダ不正'),commented:t('Commented','コメント扱い')};
file.onchange=async()=>{
const version=++generation,chosen=file.files[0];preview.replaceChildren();status.textContent='';if(!chosen)return;
try{
if(chosen.size>512*1024)throw Error(t('OPML must be at most 512 KiB.','OPMLは512 KiB以内で指定してください。'));
const opml=await chosen.text(),data=await post('opml/preview',{opml});if(version!==generation||!section.isConnected)return;
const form=el('form');form.id='feed-opml-preview';preview.append(form);const table=el('table'),head=el('thead'),tr=el('tr');for(const label of ['',t('Subscription','購読'),t('Folder','フォルダ'),t('Status','状態')])tr.append(el('th',label));head.append(tr);table.append(head);const body=el('tbody');table.append(body);form.append(table);
const choices=[];let remaining=data.available;
for(const row of data.rows){const tr=el('tr'),cell=el('td'),check=el('input');check.type='checkbox';check.value=row.index;check.ariaLabel=row.title;check.disabled=row.status!=='add';check.checked=!check.disabled&&remaining-->0;cell.append(check);const name=el('td');name.append(el('strong',row.title||'—'),el('small',row.url));tr.append(cell,name,el('td',row.folder||'—'),el('td',reasons[row.status]||row.status));body.append(tr);if(!check.disabled)choices.push(check);}
const options=el('div',undefined,'feed-import-options'),enabled=field(options,t('Enable','有効'),'','checkbox');enabled.checked=true;const interval=field(options,t('Interval (min)','更新間隔(分)'),'30','number');interval.min='5';interval.max='1440';interval.required=true;form.append(options);
const actions=el('div',undefined,'feed-actions'),save=el('button');save.type='submit';save.id='feed-opml-apply';actions.append(save);form.append(actions);
let busy=false,pending=null;
const update=()=>{const n=choices.filter(x=>x.checked).length;save.textContent=t('Add ','追加 ')+n;save.disabled=busy||n===0||n>data.available;save.title=n>data.available?t('Subscription limit exceeded','購読数の上限を超えています'):'';choices.forEach(check=>check.disabled=busy);enabled.disabled=interval.disabled=busy;};choices.forEach(check=>check.onchange=update);update();
form.onsubmit=async event=>{
event.preventDefault();if(busy)return;const selected=choices.filter(x=>x.checked).map(x=>Number(x.value));if(!selected.length||selected.length>data.available)return;
const input={opml,selected,expected:data.expected,enabled:enabled.checked,interval_seconds:Number(interval.value)*60},fingerprint=JSON.stringify(input);if(!pending||pending.fingerprint!==fingerprint)pending={fingerprint,request_id:crypto.randomUUID()};input.request_id=pending.request_id;busy=true;file.disabled=true;update();
try{await post('opml/import',input);if(!section.isConnected)return;await reload();status.textContent=t('Imported','取り込みました');}catch(e){report(e);}finally{busy=false;file.disabled=false;update();}
};
}catch(e){if(version===generation&&section.isConnected)report(e);}
};
}
function sourceForm(source){editing=true;detailSeq++;layout.classList.add('reading');reader.replaceChildren();const form=el('form'),buttons=el('div',undefined,'feed-actions');reader.append(form);form.append(el('h1',t('Source settings','購読元設定')));
const title=field(form,t('Name','表示名'),source?.title||''),folder=field(form,t('Folder','フォルダ'),source?.folder||'');title.required=true;title.maxLength=folder.maxLength=200;
const label=el('label',t('Provider','正本')),provider=el('select');for(const [id,name]of [['local',t('Local · RSS / Atom','内蔵 · RSS / Atom')],['freshrss','FreshRSS']]){const o=el('option',name);o.value=id;provider.append(o);}provider.value=source?.provider||'local';provider.disabled=!!source;label.append(provider);form.append(label);
const url=field(form,t('Public HTTPS feed / greader.php API URL','公開HTTPSのfeed / greader.php API URL'),source?.url||'','url');url.required=true;url.readOnly=!!source;
const user=field(form,t('FreshRSS user','FreshRSSユーザー')),password=field(form,t('FreshRSS API password','FreshRSS APIパスワード'),'','password');password.autocomplete='new-password';
const interval=field(form,t('Refresh interval (minutes, 5–1440)','更新間隔(分、5〜1440)'),String((source?.interval_seconds||1800)/60),'number');interval.min='5';interval.max='1440';interval.required=true;
const enabled=field(form,t('Enabled','有効'),'','checkbox');enabled.checked=source?source.enabled:true;
const write=field(form,t('Allow FreshRSS read/star changes','FreshRSSの既読・star変更を許可'),'','checkbox');write.checked=!!source?.write_enabled;
const toggle=()=>{write.parentElement.hidden=provider.value!=='freshrss';user.parentElement.hidden=password.parentElement.hidden=!!source||provider.value!=='freshrss';user.required=password.required=!source&&provider.value==='freshrss';};provider.onchange=toggle;toggle();
if(source)form.append(el('p',t('Last success: ','最終成功: ')+(source.last_ms?new Date(source.last_ms).toLocaleString():'—')+' · '+(source.error||'OK'),'feed-meta'));
const sourceID=source?.uuid||crypto.randomUUID();
const save=el('button',t('Save','保存'));save.type='submit';buttons.append(save,button(t('Cancel','取消'),'',closeReader));if(source)buttons.append(button(t('Delete source','購読元を削除'),'',async()=>{if(!confirm(t('Delete this source, cached articles and local read/star state?','購読元・取得記事・内蔵の既読/star状態を削除しますか?')))return;await post('sources/'+source.uuid+'/delete',{version:source.version});currentSource='';await reload();}));form.append(buttons);
form.onsubmit=async event=>{event.preventDefault();save.disabled=true;try{await post('sources',{uuid:sourceID,version:source?.version||0,provider:provider.value,title:title.value,folder:folder.value,url:url.value,interval_seconds:Number(interval.value)*60,enabled:enabled.checked,write_enabled:provider.value==='freshrss'&&write.checked,username:user.value,password:password.value});password.value='';await reload();status.textContent=t('Saved','保存しました');}catch(e){report(e);}finally{save.disabled=false;}};title.focus();
}
function captureForm(item){
editing=true;detailSeq++;clearTimeout(actionTimer);const dialog=el('dialog',undefined,'feed-capture'),form=el('form');dialog.ariaLabel=t('Capture Wiki / task','Wiki・タスク追加');reader.append(dialog);dialog.append(form);
const close=()=>{if(!busy)dialog.close();},heading=el('div',undefined,'feed-capture-heading');heading.append(el('h2',t('Capture Wiki / task','Wiki・タスク追加')),button('×',t('Close','閉じる'),close));form.append(heading);
dialog.addEventListener('close',()=>{clearTimeout(previewTimer);generation++;dialog.remove();editing=false;pollAction(item);});dialog.addEventListener('cancel',e=>{if(busy)e.preventDefault();});dialog.addEventListener('keydown',e=>e.stopPropagation());
const title=field(form,t('Title','件名'),item.title.slice(0,250));title.required=true;title.maxLength=300;
const task=field(form,t('Also create a task','タスクを作成する'),'','checkbox');task.disabled=root.dataset.tasks!=='true';
const prefix=field(form,t('Wiki parent Path (empty = task only)','Wikiの親Path(空ならタスクのみ)'),root.dataset.wikiPrefix||'');prefix.placeholder='/notes/feeds';prefix.required=true;prefix.maxLength=500;
if(root.dataset.tasks==='true'){const settings=el('a','⚙');settings.href='/-/sys/config?panel=organizer';settings.target='_blank';settings.rel='noopener';settings.title=settings.ariaLabel=t('Configure default Wiki parent path in personal settings','個人設定で既定のWiki親Pathを設定');prefix.parentElement.append(settings);}
const tokens=el('div',undefined,'feed-capture-tokens');for(const token of ['{YYYY}','{MM}','{DD}','{Subject}'])tokens.append(button(token,t('Insert ','挿入: ')+token,()=>{prefix.setRangeText(token,prefix.selectionStart??prefix.value.length,prefix.selectionEnd??prefix.value.length,'end');prefix.dispatchEvent(new Event('input',{bubbles:true}));prefix.focus();}));form.append(tokens);
const destination=el('output',undefined,'feed-capture-destination');destination.ariaLabel=t('Destination','保存先');form.append(destination);
const notes=field(form,t('Your note','メモ'));const copy=field(form,t('Include cached text (explicit copy)','取得済み本文も保存する(明示コピー)'),'','checkbox');
const confirm=field(form,t('I checked the destination publication policy and the material to copy.','保存先の公開範囲と、コピーする内容を確認しました。'),'','checkbox');confirm.required=true;
for(const [name,input]of Object.entries({title,task,prefix,notes,copy,confirm})){input.name=name;if(input.type==='checkbox')input.parentElement.classList.add('feed-capture-check');else input.parentElement.classList.add('feed-capture-field');}
const notice=el('p',undefined,'feed-warning');notice.role='status';const actions=el('div',undefined,'feed-capture-actions'),save=el('button',t('Create','作成'));save.type='submit';actions.append(button(t('Cancel','取消'),'',close),save);form.append(notice,actions);
let pending=null,generation=0,previewTimer,resolved=null,busy=false;const requestID=crypto.randomUUID();
const preview=()=>{clearTimeout(previewTimer);const n=++generation;resolved=null;confirm.checked=false;prefix.required=!task.checked;confirm.required=!!prefix.value.trim()||!task.checked;save.disabled=true;notice.textContent='';destination.textContent='';
if(!prefix.value.trim()){resolved='';destination.textContent=task.checked?t('Task only','タスクのみ'):'';save.disabled=!task.checked;return;}
previewTimer=setTimeout(async()=>{try{const data=await post('capture-preview',{prefix:prefix.value.trim(),title:title.value});if(n!==generation||!dialog.isConnected)return;resolved=data.prefix;destination.textContent=resolved.replace(/\/$/,'')+'/'+(task.checked?'mail-':'rss-')+requestID;save.disabled=false;}catch(e){if(n===generation&&dialog.isConnected){notice.textContent=t('Check the parent Path and placeholders.','親Pathとプレースホルダを確認してください。');}}},180);
};task.onchange=prefix.oninput=title.oninput=preview;copy.onchange=()=>{confirm.checked=false;};dialog.showModal();preview();title.focus();
form.onsubmit=async event=>{event.preventDefault();if(busy||resolved===null||!form.reportValidity())return;const body='Source: '+item.url+'\nFeed: '+item.feed+'\n\n'+notes.value+(copy.checked?'\n\n'+item.body:'');const spec={title:title.value,notes:notes.value,prefix:resolved,body,task:task.checked};const fingerprint=JSON.stringify(spec);if(!pending||pending.fingerprint!==fingerprint)pending={fingerprint,request_id:requestID};busy=true;save.disabled=true;
try{let data;if(task.checked){const input={request_id:pending.request_id,title:title.value,notes:(notes.value+'\nSource: '+item.url).trim()};if(spec.prefix){input.wiki_prefix=spec.prefix;input.wiki_body=body;}const res=await fetch('/_/api/v1/extension/organizer.tasks/capture',{method:'POST',credentials:'same-origin',headers:{'Content-Type':'application/json'},body:JSON.stringify(input)});data=await res.json().catch(()=>({}));if(!res.ok||res.redirected)throw Error(t('Capture failed: ','作成に失敗: ')+(data.error||res.status));}
else data=await post('note',{request_id:pending.request_id,source:item.source,id:item.id,path:spec.prefix.replace(/\/$/,'')+'/rss-'+pending.request_id,title:title.value,body,confirm_publication:confirm.checked});
dialog.replaceChildren(el('h2',t('Created','作成しました')));if(data.item){const a=el('a',t('Open task','タスクを開く'));a.href='/v/plan/task/'+encodeURIComponent(data.item.uuid);dialog.append(a);}const page=data.page_uuid||data.item?.page_uuid;if(page){const a=el('a',t('Open Wiki','Wikiを開く'));a.href='/v/uuid:'+encodeURIComponent(page);dialog.append(el('p'),a);}const done=button(t('Close','閉じる'),'',close);dialog.append(el('p'),done);done.focus();
}catch(e){notice.textContent=e.message;}finally{busy=false;save.disabled=resolved===null;}
};
}
document.addEventListener('keydown',event=>{if(event.defaultPrevented||event.isComposing||event.ctrlKey||event.altKey||event.metaKey||document.querySelector('dialog[open]'))return;if(event.key==='Escape'&&!help.hidden){hideHelp();event.preventDefault();return;}const input=event.target.closest?.('input,textarea,select,[contenteditable]:not([contenteditable=false]),[role=textbox]');if(input||event.target.isContentEditable){if(event.key==='Escape'){event.target.blur();event.preventDefault();}return;}if(event.key==='?'&&!help.hidden){hideHelp();event.preventDefault();return;}if(event.key==='Enter'&&event.target.closest?.('button,a'))return;if(editing){if(event.key==='Escape'){closeReader();event.preventDefault();}return;}
if(event.shiftKey&&['j','k'].includes(event.key.toLowerCase())){event.preventDefault();Promise.resolve().then(()=>moveFolder(event.key.toLowerCase()==='j'?1:-1)).catch(report);return;}
let action;switch(event.key){case 'j':case 'ArrowDown':action=()=>open(Math.min(items.length-1,selected+1));break;case 'k':case 'ArrowUp':action=()=>open(Math.max(0,selected-1));break;case 'Enter':case 'l':action=()=>open(Math.max(0,selected));break;case 'Escape':case 'h':action=closeReader;break;case '/':action=()=>searchbox.focus();break;case 'r':if(writable)action=queueRefresh;break;case 'm':action=()=>setState(!items[selected]?.read,!!items[selected]?.starred);break;case '*':action=()=>setState(!!items[selected]?.read,!items[selected]?.starred);break;case 'n':action=()=>{const i=items.findIndex((x,i)=>i>selected&&!x.read);if(i>=0)return open(i);};break;case '?':action=showHelp;break;}if(action){event.preventDefault();Promise.resolve().then(action).catch(report);}
});
(async()=>{const q=new URLSearchParams(location.search);currentSource=q.get('source')||'';await reload();if(q.has('settings')&&writable){sourceForm();return;}if(q.has('id')&&currentSource){let index=items.findIndex(x=>x.source===currentSource&&x.id===q.get('id'));if(index<0){const data=await request('item?'+new URLSearchParams({source:currentSource,id:q.get('id')}));items.unshift(data.item);index=0;drawRows();}await open(index);}})().catch(report);
})();
+9 -1
View File
@@ -7,6 +7,8 @@
const writable=root.dataset.writable==='true',today=root.dataset.today,zone=root.dataset.timezone; const writable=root.dataset.writable==='true',today=root.dataset.today,zone=root.dataset.timezone;
const names={progress:t('Today’s completion','本日の達成率'),today:t('Today’s tasks','本日のタスク'),tomorrow:t('Tomorrow’s tasks','明日のタスク'),tasks:t('Tasks','タスク'),schedule:t('Today’s schedule','本日のスケジュール'),activity:t('Activity report','達成度・作業レポート')}; const names={progress:t('Today’s completion','本日の達成率'),today:t('Today’s tasks','本日のタスク'),tomorrow:t('Tomorrow’s tasks','明日のタスク'),tasks:t('Tasks','タスク'),schedule:t('Today’s schedule','本日のスケジュール'),activity:t('Activity report','達成度・作業レポート')};
const defaults=[['progress',4,3],['today',4,3],['tomorrow',4,3],['tasks',8,4],['schedule',4,4],['activity',12,5]].map(([id,width,height])=>({id,width,height,...(id==='tasks'?{tab:'inbox'}:{})})); const defaults=[['progress',4,3],['today',4,3],['tomorrow',4,3],['tasks',8,4],['schedule',4,4],['activity',12,5]].map(([id,width,height])=>({id,width,height,...(id==='tasks'?{tab:'inbox'}:{})}));
names.feeds=t('RSS · latest cached articles','RSS · 最近の取得記事');
const available=[...defaults,{id:'feeds',width:6,height:4}];
const tabs={inbox:'Inbox',backlog:'Backlog',someday:'Someday',review:'Review',all:'Incomplete',done:'Done'}; const tabs={inbox:'Inbox',backlog:'Backlog',someday:'Someday',review:'Review',all:'Incomplete',done:'Done'};
const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(cls)n.className=cls;return n;}; const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(cls)n.className=cls;return n;};
function button(text,label,fn){const b=el('button',text);b.type='button';b.title=b.ariaLabel=label;b.onclick=fn;return b;} function button(text,label,fn){const b=el('button',text);b.type='button';b.title=b.ariaLabel=label;b.onclick=fn;return b;}
@@ -31,7 +33,7 @@
}); });
const edit=button('⚙',t('Arrange widgets','ウィジェットを配置'),()=>{editing=!editing;root.classList.toggle('is-arranging',editing);edit.setAttribute('aria-pressed',String(editing));});edit.setAttribute('aria-pressed','false'); const edit=button('⚙',t('Arrange widgets','ウィジェットを配置'),()=>{editing=!editing;root.classList.toggle('is-arranging',editing);edit.setAttribute('aria-pressed',String(editing));});edit.setAttribute('aria-pressed','false');
const refresh=button('↻',t('Refresh visible widgets','ウィジェットを更新'),()=>{for(const item of widgets){const card=cards.get(item.id);if(card)loadWidget(item,card);}}); const refresh=button('↻',t('Refresh visible widgets','ウィジェットを更新'),()=>{for(const item of widgets){const card=cards.get(item.id);if(card)loadWidget(item,card);}});
const add=el('select');add.ariaLabel=t('Add widget','ウィジェットを追加');add.onchange=()=>{const item=defaults.find(w=>w.id===add.value);if(item){widgets.push({...item});render();changed();}}; const add=el('select');add.ariaLabel=t('Add widget','ウィジェットを追加');add.onchange=()=>{const item=available.find(w=>w.id===add.value);if(item){widgets.push({...item});render();changed();}};
const reset=button(t('Reset layout','初期配置'),t('Reset layout','初期配置'),()=>{if(confirm(t('Replace this layout with the default layout? Save to apply.','初期配置に戻しますか? 保存するまでは確定しません。'))){widgets=defaults.map(x=>({...x}));render();changed();}}); const reset=button(t('Reset layout','初期配置'),t('Reset layout','初期配置'),()=>{if(confirm(t('Replace this layout with the default layout? Save to apply.','初期配置に戻しますか? 保存するまでは確定しません。'))){widgets=defaults.map(x=>({...x}));render();changed();}});
controls.append(refresh);if(writable)controls.append(edit,add,reset,save); controls.append(refresh);if(writable)controls.append(edit,add,reset,save);
function updateControls(){save.disabled=!dirty||saving||!loaded;edit.disabled=add.disabled=reset.disabled=saving||!loaded;refresh.disabled=!loaded;root.classList.toggle('dashboard-saving',saving);for(const card of cards.values())card.querySelectorAll('.widget-arrange button,.widget-arrange select,.widget-drag,.widget-resize').forEach(n=>n.disabled=saving);add.replaceChildren();const option=el('option',t('+ Add widget','+ ウィジェットを追加'));option.value='';add.append(option);for(const [id,name] of Object.entries(names)){if(widgets.some(w=>w.id===id)||id==='schedule'&&root.dataset.calendar!=='true')continue;const o=el('option',name);o.value=id;add.append(o);}} function updateControls(){save.disabled=!dirty||saving||!loaded;edit.disabled=add.disabled=reset.disabled=saving||!loaded;refresh.disabled=!loaded;root.classList.toggle('dashboard-saving',saving);for(const card of cards.values())card.querySelectorAll('.widget-arrange button,.widget-arrange select,.widget-drag,.widget-resize').forEach(n=>n.disabled=saving);add.replaceChildren();const option=el('option',t('+ Add widget','+ ウィジェットを追加'));option.value='';add.append(option);for(const [id,name] of Object.entries(names)){if(widgets.some(w=>w.id===id)||id==='schedule'&&root.dataset.calendar!=='true')continue;const o=el('option',name);o.value=id;add.append(o);}}
@@ -45,6 +47,7 @@
const card=el('section',undefined,'dashboard-widget');card.id=item.id;card.dataset.widget=item.id;applySize(item,card); const card=el('section',undefined,'dashboard-widget');card.id=item.id;card.dataset.widget=item.id;applySize(item,card);
const header=el('header',undefined,'widget-header'),heading=el('h2',names[item.id]);heading.id='widget-title-'+item.id;card.setAttribute('aria-labelledby',heading.id); const header=el('header',undefined,'widget-header'),heading=el('h2',names[item.id]);heading.id='widget-title-'+item.id;card.setAttribute('aria-labelledby',heading.id);
header.append(heading);const destinations={progress:'/v/plan/today',today:'/v/plan/today',tomorrow:'/v/plan/tasks/tomorrow',tasks:'/v/plan/tasks',schedule:'/v/plan',activity:'/v/plan/dashboard#activity'}; header.append(heading);const destinations={progress:'/v/plan/today',today:'/v/plan/today',tomorrow:'/v/plan/tasks/tomorrow',tasks:'/v/plan/tasks',schedule:'/v/plan',activity:'/v/plan/dashboard#activity'};
destinations.feeds='/v/feeds';
const open=link('↗',destinations[item.id]);open.title=open.ariaLabel=t('Open ','開く: ')+names[item.id];header.append(open); const open=link('↗',destinations[item.id]);open.title=open.ariaLabel=t('Open ','開く: ')+names[item.id];header.append(open);
const arrange=el('div',undefined,'widget-arrange'); const arrange=el('div',undefined,'widget-arrange');
if(writable){ if(writable){
@@ -78,6 +81,11 @@
if(controller.signal.aborted||!card.isConnected)return; if(controller.signal.aborted||!card.isConnected)return;
const signal=AbortSignal.any([controller.signal,AbortSignal.timeout(15000)]); const signal=AbortSignal.any([controller.signal,AbortSignal.timeout(15000)]);
try{ try{
if(item.id==='feeds'){
const data=await request('/_/api/v1/extension/organizer.feeds/items',{signal});if(controller.signal.aborted)return;
const list=el('ul',undefined,'dashboard-list');for(const item of data.items.slice(0,12)){const row=el('li');row.append(el('span',item.read?'○':'●'),link(item.title||t('(Untitled)','(無題)'),'/v/feeds?'+new URLSearchParams({source:item.source,id:item.id})));list.append(row);}
body.replaceChildren(list);if(!data.items.length)body.append(el('p',t('No cached articles.','取得済みの記事はありません。')));return;
}
if(item.id==='activity'){ if(item.id==='activity'){
body.replaceChildren();const report=el('section');report.id='task-productivity';report.dataset.view='report';const h=el('h3');h.className='visually-hidden';const message=el('p');message.id='productivity-status';message.role='status';const content=el('div');content.id='productivity-content';report.append(h,message,content);body.append(report);await window.PagesWikiProductivityMount(report,controller.signal);return; body.replaceChildren();const report=el('section');report.id='task-productivity';report.dataset.view='report';const h=el('h3');h.className='visually-hidden';const message=el('p');message.id='productivity-status';message.role='status';const content=el('div');content.id='productivity-content';report.append(h,message,content);body.append(report);await window.PagesWikiProductivityMount(report,controller.signal);return;
} }
+54
View File
@@ -0,0 +1,54 @@
(() => {
'use strict';
const root=document.getElementById('task-digest-history');if(!root)return;
const ja=document.documentElement.lang.startsWith('ja'),t=(en,jp)=>ja?jp:en;
const api='/_/api/v1/extension/organizer.tasks/digest-history';
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
const states={queued:t('Queued','待機中'),sending:t('Sending / pending result','送信中・結果待ち'),sent:t('Accepted by mail server','メールサーバー受付済み'),uncertain:t('Delivery uncertain','結果不明'),cancelled:t('Not sent','送信中止')};
const reasons={legacy:t('Migrated record; attempt details unknown','移行前の記録(詳細不明)'),smtp_unavailable:t('SMTP unavailable','SMTPを利用できません'),tasks_unavailable:t('Tasks unavailable','タスクを取得できません'),consent_changed:t('Settings or permission changed','設定・権限が変わりました'),transport_uncertain:t('SMTP result uncertain','SMTPの結果が不明です'),extension_disabled:t('Extension disabled','拡張機能が無効です'),day_expired:t('Requested date has passed','対象日を過ぎました'),worker_interrupted:t('Worker interrupted; check for delivery','処理中断の可能性・受信を確認してください')};
const details=el('details'),summary=el('summary',t('Email delivery history and retry','メール配信履歴・再送'));details.append(summary);root.append(details);
const status=el('p');status.role='status';status.setAttribute('aria-live','polite');
const controls=el('div'),refresh=el('button','↻ '+t('Refresh','更新'));refresh.type='button';controls.className='productivity-controls';controls.append(refresh);
const content=el('div'),pager=el('div');pager.className='productivity-controls';details.append(status,controls,content,pager);
let loaded=false,busy=false,cursor='',stack=[],pending=null,generation=0;
async function request(suffix='',options={}){
const response=await fetch(api+suffix,{credentials:'same-origin',cache:'no-store',signal:AbortSignal.timeout(15000),...options,headers:{'Content-Type':'application/json'}});
const data=await response.json().catch(()=>({}));
if(!response.ok||response.redirected){const errors={conflict:t('Changed, already queued, or the daily retry limit was reached. Refresh to check.','状態の変更・処理中・再送回数上限のいずれかです。更新して確認してください。'),invalid:t('Check the date and duplicate-delivery confirmation.','対象日と重複送信の確認を見直してください。'),forbidden:t('Enable delivery and check your permissions.','配信設定と権限を確認してください。'),email_required:t('Set your account email first.','先にプロフィールのメールアドレスを設定してください。'),smtp_unavailable:t('SMTP is not configured or enabled.','SMTPが未設定または無効です。')};const error=Error(errors[data.error]||t('Unable to confirm the result. Retry uses the same request.','結果を確認できません。同じ要求で再試行できます。'));error.status=response.status;throw error;}
return data;
}
function retryForm(data){
const box=el('div');box.className='digest-retry';const current=data.current;
box.append(el('p',t('Retries use current tasks and your current account email, not the original message. Saved settings apply. Up to 5 retries per day.','再送は元の本文ではなく、現在のタスクとアカウントのメールアドレスを使います。保存済み設定に従い、1日5回までです。')));
if(pending&&current?.uuid===pending.request_id){pending=null;status.textContent=t('Request is recorded. Check its status below.','要求は記録されています。以下の状態を確認してください。');}
if(root.dataset.writable!=='true')return box;
if(!pending&&(!data.enabled||!current||['queued','sending'].includes(current.state)||data.retries>=5)){
box.append(el('p',!data.enabled?t('Enable daily email in the settings above.','上の設定で日次メールを有効にしてください。'):!current?t('No delivery attempt for today yet.','本日の配信記録はまだありません。'):data.retries>=5?t('Daily retry limit reached.','本日の再送回数上限に達しました。'):t('Request is queued or running. Refresh later.','待機中または実行中です。しばらくして更新してください。')));return box;
}
const form=el('form'),ack=el('input'),label=el('label');ack.type='checkbox';ack.checked=!!pending?.duplicate_acknowledged;
const duplicate=pending?pending.duplicate_acknowledged:['sent','uncertain'].includes(current.state);
ack.required=duplicate;label.append(ack,document.createTextNode(t('I checked my inbox and accept possible duplicate delivery.','受信状況を確認し、重複して届く可能性を了承します。')));if(duplicate)form.append(label);
const send=el('button',pending?t('Retry the same request','同じ要求で再試行'):t('Queue today’s email again','本日分の再送を予約'));send.type='submit';form.append(send);box.append(form);
form.onsubmit=async event=>{event.preventDefault();if(busy||!form.reportValidity())return;
if(!pending)pending={request_id:crypto.randomUUID(),day:current.day,expected_attempt:current.uuid,duplicate_acknowledged:duplicate&&ack.checked};
busy=true;send.disabled=refresh.disabled=ack.disabled=true;pager.inert=true;
try{await request('/retry',{method:'POST',body:JSON.stringify(pending)});pending=null;cursor='';stack=[];const refreshed=await load();if(refreshed)status.textContent=t('Queued. The scheduler will process this request; queue delays may apply.','予約しました。スケジューラが処理するまでお待ちください。');else status.textContent=t('Request accepted. ','予約済みです。')+status.textContent;}
catch(error){if(error.status>=400&&error.status<500)pending=null;status.textContent=error.message;send.textContent=pending?t('Retry the same request','同じ要求で再試行'):t('Queue today’s email again','本日分の再送を予約');}
finally{busy=false;send.disabled=refresh.disabled=ack.disabled=false;pager.inert=false;}
};return box;
}
async function load(){
const ticket=++generation;refresh.disabled=true;content.inert=true;pager.inert=true;
try{const data=await request('?'+new URLSearchParams({limit:'25',...(cursor?{after:cursor}:{})}));if(ticket!==generation)return;
const table=el('table');table.className='productivity-table';const head=el('tr');for(const title of [t('Date','対象日'),t('Method','区分'),t('Status','状態'),t('Updated','更新'),t('Detail','詳細')])head.append(el('th',title));const thead=el('thead');thead.append(head);table.append(thead);const body=el('tbody');
for(const item of data.items){const row=el('tr');const date=new Date(item.updated_ms).toLocaleString(ja?'ja-JP':'en-GB',{timeZone:item.timezone||undefined});for(const value of [item.day,item.kind==='manual'?t('Manual','手動'):item.kind==='legacy'?t('Legacy','移行前'):t('Scheduled','定期'),states[item.state]||item.state,date+(item.timezone?' · '+item.timezone:''),reasons[item.reason]||''])row.append(el('td',value));body.append(row);}table.append(body);
const wrap=el('div');wrap.className='productivity-table-wrap';wrap.append(table);if(!data.items.length)wrap.append(el('p',t('No delivery history.','配信履歴はありません。')));
status.textContent='';content.replaceChildren(retryForm(data),wrap,el('p',t('Detailed attempts: '+data.retention_days+' days. Duplicate-prevention receipts are retained. An accepted message is not a guarantee of inbox delivery.','詳細履歴は'+data.retention_days+'日保持します。重複防止記録は残します。メールサーバーの受付は受信箱への到達保証ではありません。')));
pager.replaceChildren();const button=(title,fn)=>{const b=el('button',title);b.type='button';b.onclick=fn;pager.append(b);};
if(stack.length)button(t('Previous','前へ'),()=>{if(!busy){cursor=stack.pop();load();}});
if(data.next)button(t('Next','次へ'),()=>{if(!busy){stack.push(cursor);cursor=data.next;load();}});loaded=true;return true;
}catch(error){status.textContent=error.message;}finally{if(ticket===generation){refresh.disabled=false;content.inert=false;pager.inert=false;}}
}
refresh.onclick=()=>{if(!busy){cursor='';stack=[];load();}};
details.ontoggle=()=>{if(details.open&&!loaded)load();};
})();
+14 -8
View File
@@ -5,7 +5,8 @@
const word={tasks:['Tasks','タスク'],report:['Activity report','達成度・作業レポート'],settings:['Task defaults and email','タスクの既定値・メール'],contacts:['Contacts','連絡先'],completed:['Completed','完了件数'],points:['Points','ポイント'],work:['Work hours','作業時間'],focus:['Pomodoro hours','集中時間'],save:['Save','保存'],saved:['Saved','保存しました'],refresh:['Refresh','更新'],project_uuid:['Default project','既定のプロジェクト'],assignee_uuid:['Assignee UUID (annotation only)','担当者UUID(管理用メモ)'],due_days:['Due in days','期限までの日数'],start_days:['Start in days','着手までの日数'],planned_days:['Plan in days','取り組む日までの日数'],review_days:['Review in days','見直しまでの日数'],estimate_minutes:['Estimated minutes','見込み工数(分)'],complexity:['Complexity (1–5)','複雑度(1〜5)'],digest_enabled:['Email daily task list to my account email','毎日のやることリストを自分のメールに送る'],digest_time:['Delivery time','配信時刻'],timezone:['IANA timezone','タイムゾーン(IANA)'],none:['None','なし'],empty:['No records in this period.','この期間の記録はありません。'],partial:['Partial results: narrow the period.','件数上限に達しました。期間を狭めてください。'],error:['Request failed. Input is retained.','処理に失敗しました。入力は保持しています。'],email_required:['Set your account email first.','先にプロフィールにメールアドレスを設定してください。'],smtp_unavailable:['Ask an administrator to configure SMTP.','管理者によるSMTP設定が必要です。']}; const word={tasks:['Tasks','タスク'],report:['Activity report','達成度・作業レポート'],settings:['Task defaults and email','タスクの既定値・メール'],contacts:['Contacts','連絡先'],completed:['Completed','完了件数'],points:['Points','ポイント'],work:['Work hours','作業時間'],focus:['Pomodoro hours','集中時間'],save:['Save','保存'],saved:['Saved','保存しました'],refresh:['Refresh','更新'],project_uuid:['Default project','既定のプロジェクト'],assignee_uuid:['Assignee UUID (annotation only)','担当者UUID(管理用メモ)'],due_days:['Due in days','期限までの日数'],start_days:['Start in days','着手までの日数'],planned_days:['Plan in days','取り組む日までの日数'],review_days:['Review in days','見直しまでの日数'],estimate_minutes:['Estimated minutes','見込み工数(分)'],complexity:['Complexity (1–5)','複雑度(1〜5)'],digest_enabled:['Email daily task list to my account email','毎日のやることリストを自分のメールに送る'],digest_time:['Delivery time','配信時刻'],timezone:['IANA timezone','タイムゾーン(IANA)'],none:['None','なし'],empty:['No records in this period.','この期間の記録はありません。'],partial:['Partial results: narrow the period.','件数上限に達しました。期間を狭めてください。'],error:['Request failed. Input is retained.','処理に失敗しました。入力は保持しています。'],email_required:['Set your account email first.','先にプロフィールにメールアドレスを設定してください。'],smtp_unavailable:['Ask an administrator to configure SMTP.','管理者によるSMTP設定が必要です。']};
const t=k=>(word[k]||[k,k])[ja?1:0],el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;}; const t=k=>(word[k]||[k,k])[ja?1:0],el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
Object.assign(word,{notify_due:['Notify on due dates','期限日に通知する'],notify_planned:['Notify on planned dates','取り組む日に通知する'],reminder_time:['Default notification time','通知の既定時刻']}); Object.assign(word,{notify_due:['Notify on due dates','期限日に通知する'],notify_planned:['Notify on planned dates','取り組む日に通知する'],reminder_time:['Default notification time','通知の既定時刻']});
Object.assign(word,{wiki_prefix:['Default Wiki parent path for mail','メール本文の既定Wiki親Path']}); Object.assign(word,{wiki_prefix:['Default Wiki parent path for mail / RSS','メール・RSS取り込みの既定Wiki親Path']});
Object.assign(word,{rss_link_mode:['Open RSS article links','RSSの記事リンクの開き方']});
const status=root.querySelector('#productivity-status'),content=root.querySelector('#productivity-content'); const status=root.querySelector('#productivity-status'),content=root.querySelector('#productivity-content');
root.querySelectorAll('[data-word]').forEach(n=>n.textContent=t(n.dataset.word));root.querySelector('h1,h2,h3').textContent=t(root.dataset.view==='settings'?'settings':'report'); root.querySelectorAll('[data-word]').forEach(n=>n.textContent=t(n.dataset.word));root.querySelector('h1,h2,h3').textContent=t(root.dataset.view==='settings'?'settings':'report');
async function request(path,options={}){const timeout=AbortSignal.timeout(15000);const r=await fetch(api+path,{credentials:'same-origin',cache:'no-store',signal:signal?AbortSignal.any([signal,timeout]):timeout,...options,headers:{'Content-Type':'application/json',...options.headers}});const d=await r.json().catch(()=>({}));if(!r.ok||r.redirected)throw Error(t(d.error||'error'));return d;} async function request(path,options={}){const timeout=AbortSignal.timeout(15000);const r=await fetch(api+path,{credentials:'same-origin',cache:'no-store',signal:signal?AbortSignal.any([signal,timeout]):timeout,...options,headers:{'Content-Type':'application/json',...options.headers}});const d=await r.json().catch(()=>({}));if(!r.ok||r.redirected)throw Error(t(d.error||'error'));return d;}
@@ -14,11 +15,12 @@
const data=await request('preferences'),p=data.preferences;const form=el('form');form.className='productivity-settings';const fields={}; const data=await request('preferences'),p=data.preferences;const form=el('form');form.className='productivity-settings';const fields={};
const projects=await request('items?kind=project&view=all&limit=100'); const projects=await request('items?kind=project&view=all&limit=100');
if(p.project_uuid&&!projects.items.some(x=>x.uuid===p.project_uuid)){try{const current=await request('items/'+encodeURIComponent(p.project_uuid));if(current.item.kind==='project')projects.items.unshift(current.item);}catch{ /* Keep the saved reference visible without exposing inaccessible data. */ }} if(p.project_uuid&&!projects.items.some(x=>x.uuid===p.project_uuid)){try{const current=await request('items/'+encodeURIComponent(p.project_uuid));if(current.item.kind==='project')projects.items.unshift(current.item);}catch{ /* Keep the saved reference visible without exposing inaccessible data. */ }}
for(const key of ['project_uuid','wiki_prefix','assignee_uuid','due_days','start_days','planned_days','review_days','estimate_minutes','complexity','notify_due','notify_planned','reminder_time','digest_enabled','digest_time','timezone']){ for(const key of ['project_uuid','wiki_prefix','rss_link_mode','assignee_uuid','due_days','start_days','planned_days','review_days','estimate_minutes','complexity','notify_due','notify_planned','reminder_time','digest_enabled','digest_time','timezone']){
if(key==='notify_due')form.append(el('h3',ja?'新規タスクのInbox通知':'Inbox reminders for new tasks')); if(key==='notify_due')form.append(el('h3',ja?'新規タスクのInbox通知':'Inbox reminders for new tasks'));
if(key==='digest_enabled')form.append(el('h3',ja?'日次メール':'Daily email')); if(key==='digest_enabled')form.append(el('h3',ja?'日次メール':'Daily email'));
const label=el('label',t(key));label.className='productivity-setting';const input=el(key==='project_uuid'?'select':'input');input.name=key; const label=el('label',t(key));label.className='productivity-setting';const input=el(['project_uuid','rss_link_mode'].includes(key)?'select':'input');input.name=key;
if(key==='project_uuid'){const empty=el('option',t('none'));empty.value='';input.append(empty);for(const item of projects.items){const option=el('option',item.title);option.value=item.uuid;input.append(option);}if(p[key]&&!projects.items.some(x=>x.uuid===p[key])){const current=el('option',p[key]);current.value=p[key];input.append(current);}} if(key==='project_uuid'){const empty=el('option',t('none'));empty.value='';input.append(empty);for(const item of projects.items){const option=el('option',item.title);option.value=item.uuid;input.append(option);}if(p[key]&&!projects.items.some(x=>x.uuid===p[key])){const current=el('option',p[key]);current.value=p[key];input.append(current);}}
else if(key==='rss_link_mode'){const modes=[['tab',ja?'新しいタブ':'New tab'],['embedded',ja?'本文上のポップアップ':'Popup over article']];if(data.gecko_available||p[key]==='gecko')modes.push(['gecko',ja?'Gecko(実験的・中継通信)':'Gecko (experimental, relayed)']);for(const [value,label]of modes){const option=el('option',label);option.value=value;if(value==='gecko'&&!data.gecko_available){option.disabled=true;option.textContent+=ja?' — 利用不可':' — unavailable';}input.append(option);}p[key] ||= 'tab';}
else if(['digest_enabled','notify_due','notify_planned'].includes(key)){input.type='checkbox';input.checked=!!p[key];input.setAttribute('role','switch');} else if(['digest_enabled','notify_due','notify_planned'].includes(key)){input.type='checkbox';input.checked=!!p[key];input.setAttribute('role','switch');}
else if(key.endsWith('_days')||['estimate_minutes','complexity'].includes(key)){input.type='number';input.min=key==='complexity'?1:0;input.max=key.endsWith('_days')?3650:key==='complexity'?5:10080;input.step=1;} else if(key.endsWith('_days')||['estimate_minutes','complexity'].includes(key)){input.type='number';input.min=key==='complexity'?1:0;input.max=key.endsWith('_days')?3650:key==='complexity'?5:10080;input.step=1;}
else input.type=['digest_time','reminder_time'].includes(key)?'time':'text'; else input.type=['digest_time','reminder_time'].includes(key)?'time':'text';
@@ -26,26 +28,30 @@
if(key==='project_uuid')for(const option of input.options)option.defaultSelected=option.selected=option.value===(p.project_uuid||''); if(key==='project_uuid')for(const option of input.options)option.defaultSelected=option.selected=option.value===(p.project_uuid||'');
else if(input.type==='checkbox')input.defaultChecked=input.checked; else if(input.type==='checkbox')input.defaultChecked=input.checked;
else input.defaultValue=input.value; else input.defaultValue=input.value;
if(key==='wiki_prefix'){input.placeholder='/private/mail';input.maxLength=500;const help=el('p',ja?'メール本文をWikiに保存する親Pathです。空欄ならプロジェクトのWiki配下を使います。保存した本文はそのPathの公開範囲・閲覧権限に従います。':'Parent path for mail Wiki pages. If blank, use the project Wiki subtree. Saved mail inherits the destination path’s visibility and read permissions.');help.className='productivity-note';form.append(help);} if(key==='wiki_prefix'){input.placeholder='/notes/{YYYY}/{MM}/{Subject}';input.maxLength=500;const help=el('p',ja?'メール・RSS取り込みの保存先。{YYYY}・{MM}・{DD}は作成日(ドメインのタイムゾーン)、{Subject}は件名です。RSSでは入力欄を空にすればタスクのみ作成できます。設定が空欄の場合、メールはプロジェクトのWiki配下を使い、RSSは未入力になります。保存内容はそのPathの公開範囲・閲覧権限に従います。':'Mail / RSS capture destination. {YYYY}, {MM}, {DD} use the creation date in the Domain timezone; {Subject} uses the title. Clear the RSS field for task-only capture. If unset, mail uses the project Wiki subtree and RSS leaves the field blank. Content inherits destination visibility and permissions.');help.className='productivity-note';form.append(help);}
if(key==='reminder_time'){const help=el('p',ja?'新規タスクの対応する日付が設定されている場合だけ通知します。既存タスクは変更しません。通知時刻は保存時のドメインのタイムゾーン('+data.timezone+')を使用します。':'Reminders apply only to new tasks with the corresponding date. Existing tasks are unchanged. Times use the Domain timezone on save ('+data.timezone+').');help.className='productivity-note';form.append(help);} if(key==='reminder_time'){const help=el('p',ja?'新規タスクの対応する日付が設定されている場合だけ通知します。既存タスクは変更しません。通知時刻は保存時のドメインのタイムゾーン('+data.timezone+')を使用します。':'Reminders apply only to new tasks with the corresponding date. Existing tasks are unchanged. Times use the Domain timezone on save ('+data.timezone+').');help.className='productivity-note';form.append(help);}
} }
const note=el('p',ja?'日数は作成日からの暦日です。空欄は未設定、0は当日。担当者の指定では閲覧権限は付与されません。メールは今日の計画・期限到来分を最大100件送ります。SMTPは管理者設定を使用し、配信時刻にはキュー待ちの遅延があります。':'Offsets are calendar days from creation; blank means unset, 0 means today. Assignment does not grant access. Email includes up to 100 tasks planned or due today, using administrator SMTP settings. Queue delays may apply.');note.className='productivity-note';form.append(note); const note=el('p',ja?'日数は作成日からの暦日です。空欄は未設定、0は当日。担当者の指定では閲覧権限は付与されません。メールは今日の計画・期限到来分を最大100件送ります。SMTPは管理者設定を使用し、配信時刻にはキュー待ちの遅延があります。':'Offsets are calendar days from creation; blank means unset, 0 means today. Assignment does not grant access. Email includes up to 100 tasks planned or due today, using administrator SMTP settings. Queue delays may apply.');note.className='productivity-note';form.append(note);
if(data.last_digest){const states={sent:['Sent','送信済み'],sending:['Sending / result pending','送信中・結果未確定'],uncertain:['Delivery uncertain (no automatic retry)','結果不明(自動再送なし)'],cancelled:['Cancelled','送信取消']};form.append(el('p',(ja?'直近の配信: ':'Latest delivery: ')+data.last_digest.day+' · '+(states[data.last_digest.state]||['Unknown','不明'])[ja?1:0]));}
const save=el('button',t('save'));save.type='submit';save.disabled=root.dataset.writable!=='true';form.append(save);content.replaceChildren(form); const save=el('button',t('save'));save.type='submit';save.disabled=root.dataset.writable!=='true';form.append(save);content.replaceChildren(form);
fields.project_uuid.value=p.project_uuid||''; fields.project_uuid.value=p.project_uuid||'';
form.onsubmit=async event=>{event.preventDefault();if(!form.reportValidity())return;save.disabled=true;const value={...p};for(const [key,input] of Object.entries(fields)){value[key]=input.type==='checkbox'?input.checked:input.type==='number'?(input.value===''?null:Number(input.value)):input.value;}try{const result=await request('preferences',{method:'PUT',body:JSON.stringify(value)});Object.assign(p,result.preferences);status.textContent=t('saved');}catch(e){error(e);}finally{save.disabled=false;}}; form.onsubmit=async event=>{event.preventDefault();if(!form.reportValidity())return;save.disabled=true;const value={...p};for(const [key,input] of Object.entries(fields)){value[key]=input.type==='checkbox'?input.checked:input.type==='number'?(input.value===''?null:Number(input.value)):input.value;}try{const result=await request('preferences',{method:'PUT',body:JSON.stringify(value)});Object.assign(p,result.preferences);status.textContent=t('saved');}catch(e){error(e);}finally{save.disabled=false;}};
} }
function taskTable(data){const wrap=el('div');wrap.className='productivity-table-wrap';const table=el('table');table.className='productivity-table';const head=el('tr');for(const name of [ja?'タスク':'Task',t('points'),t('work')])head.append(el('th',name));const thead=el('thead');thead.append(head);table.append(thead);const body=el('tbody');for(const task of data.tasks){const row=el('tr'),name=el('td'),link=el('a',(task.completed?'✓ ':'')+(task.title||task.uuid));link.href='/v/plan/task/'+encodeURIComponent(task.uuid);name.append(link);row.append(name,el('td',String(task.points||0)),el('td',(task.work_ms/3600000).toFixed(2)));body.append(row);}table.append(body);wrap.append(table);if(!data.tasks.length)wrap.append(el('p',t('empty')));return wrap;} function taskTable(data){const wrap=el('div');wrap.className='productivity-table-wrap';const table=el('table');table.className='productivity-table';const head=el('tr');for(const name of [ja?'タスク':'Task',t('points'),t('work')])head.append(el('th',name));const thead=el('thead');thead.append(head);table.append(thead);const body=el('tbody');for(const task of data.tasks){const row=el('tr'),name=el('td'),link=el('a',(task.completed?'✓ ':'')+(task.title||task.uuid));link.href='/v/plan/task/'+encodeURIComponent(task.uuid);name.append(link);row.append(name,el('td',String(task.points||0)),el('td',(task.work_ms/3600000).toFixed(2)));body.append(row);}table.append(body);wrap.append(table);if(!data.tasks.length)wrap.append(el('p',t('empty')));return wrap;}
function projectTable(data){
const wrap=el('section');wrap.className='productivity-table-wrap';wrap.append(el('h3',ja?'プロジェクト別':'By project'));
const table=el('table');table.className='productivity-table';const head=el('tr');for(const title of [ja?'プロジェクト':'Project',t('completed'),t('points'),t('work')])head.append(el('th',title));const thead=el('thead');thead.append(head);table.append(thead);const body=el('tbody');
for(const p of data.projects||[]){const row=el('tr'),name=el('td');if(p.uuid){const link=el('a',p.title||p.uuid);link.href='/v/plan/project/'+encodeURIComponent(p.uuid);name.append(link);}else name.textContent=ja?'プロジェクトなし':'No project';row.append(name,el('td',String(p.completed)),el('td',String(p.points)),el('td',(p.work_ms/3600000).toFixed(2)));body.append(row);}table.append(body);wrap.append(table,el('small',ja?'現在の所属で集計します。集中時間(Pomodoro)は別集計です。':'Grouped by current project membership. Pomodoro time is reported separately.'));return wrap;
}
async function report(){ async function report(){
const controls=el('form');controls.className='productivity-controls';const from=el('input'),to=el('input');from.type=to.type='date';from.required=to.required=true;from.ariaLabel=ja?'開始日':'From';to.ariaLabel=ja?'終了日(含まない)':'To (exclusive)';const refresh=el('button','↻ '+t('refresh'));controls.append(from,to,refresh); const controls=el('form');controls.className='productivity-controls';const from=el('input'),to=el('input');from.type=to.type='date';from.required=to.required=true;from.ariaLabel=ja?'開始日':'From';to.ariaLabel=ja?'終了日(含まない)':'To (exclusive)';const refresh=el('button','↻ '+t('refresh'));const history=el('a',ja?'作業記録・訂正':'Work history & corrections');history.href='/v/plan/tasks/work';controls.append(from,to,refresh,history);
const results=el('section');content.replaceChildren(controls,results);let generation=0; const results=el('section');content.replaceChildren(controls,results);let generation=0;
async function load(){const ticket=++generation;refresh.disabled=true;try{const query=new URLSearchParams();if(from.value)query.set('from',from.value);if(to.value)query.set('to',to.value);const d=await request('report?'+query);if(ticket!==generation)return;status.textContent=d.partial?t('partial'):d.timezone; async function load(){const ticket=++generation;refresh.disabled=true;try{const query=new URLSearchParams();if(from.value)query.set('from',from.value);if(to.value)query.set('to',to.value);const d=await request('report?'+query);if(ticket!==generation)return;status.textContent=d.partial?t('partial'):d.timezone;
if(!from.value&&d.days.length){from.value=d.days[0].date;const end=new Date(d.days.at(-1).date+'T12:00:00Z');end.setUTCDate(end.getUTCDate()+1);to.value=end.toISOString().slice(0,10);} if(!from.value&&d.days.length){from.value=d.days[0].date;const end=new Date(d.days.at(-1).date+'T12:00:00Z');end.setUTCDate(end.getUTCDate()+1);to.value=end.toISOString().slice(0,10);}
const stats=el('div');stats.className='productivity-stats';for(const [label,key,divisor] of [['completed','completed',1],['points','points',1],['work','work_ms',3600000],['focus','focus_ms',3600000]]){const card=el('div',t(label));card.className='productivity-stat';const sum=d.days.reduce((total,day)=>total+day[key],0)/divisor;card.append(el('strong',divisor===1?String(sum):sum.toFixed(2)));stats.append(card);} const stats=el('div');stats.className='productivity-stats';for(const [label,key,divisor] of [['completed','completed',1],['points','points',1],['work','work_ms',3600000],['focus','focus_ms',3600000]]){const card=el('div',t(label));card.className='productivity-stat';const sum=d.days.reduce((total,day)=>total+day[key],0)/divisor;card.append(el('strong',divisor===1?String(sum):sum.toFixed(2)));stats.append(card);}
const scroll=el('div');scroll.className='productivity-heatmap-scroll';const grid=el('div');grid.className='productivity-heatmap';grid.ariaLabel=t('report');const detail=el('section');detail.append(taskTable(d));let detailSequence=0; const scroll=el('div');scroll.className='productivity-heatmap-scroll';const grid=el('div');grid.className='productivity-heatmap';grid.ariaLabel=t('report');const detail=el('section');detail.append(projectTable(d),taskTable(d));let detailSequence=0;
if(d.days.length){const weekday=new Date(d.days[0].date+'T12:00:00Z').getUTCDay();for(let i=0;i<weekday;i++)grid.append(el('span'));} if(d.days.length){const weekday=new Date(d.days[0].date+'T12:00:00Z').getUTCDay();for(let i=0;i<weekday;i++)grid.append(el('span'));}
for(const day of d.days){const cell=el('button');cell.type='button';cell.className='productivity-day';cell.dataset.level=String(day.points===0?0:day.points<=3?1:day.points<=8?2:day.points<=20?3:4);cell.title=cell.ariaLabel=`${day.date}: ${day.completed} ${t('completed')}, ${day.points} ${t('points')}, ${(day.work_ms/3600000).toFixed(2)}h`; for(const day of d.days){const cell=el('button');cell.type='button';cell.className='productivity-day';cell.dataset.level=String(day.points===0?0:day.points<=3?1:day.points<=8?2:day.points<=20?3:4);cell.title=cell.ariaLabel=`${day.date}: ${day.completed} ${t('completed')}, ${day.points} ${t('points')}, ${(day.work_ms/3600000).toFixed(2)}h`;
cell.onclick=async()=>{const sequence=++detailSequence;try{const next=new Date(day.date+'T12:00:00Z');next.setUTCDate(next.getUTCDate()+1);const single=await request('report?'+new URLSearchParams({from:day.date,to:next.toISOString().slice(0,10)}));if(sequence!==detailSequence)return;detail.replaceChildren(el('h2',day.date),taskTable(single));}catch(e){error(e);}};grid.append(cell);} cell.onclick=async()=>{const sequence=++detailSequence;try{const next=new Date(day.date+'T12:00:00Z');next.setUTCDate(next.getUTCDate()+1);const single=await request('report?'+new URLSearchParams({from:day.date,to:next.toISOString().slice(0,10)}));if(sequence!==detailSequence)return;detail.replaceChildren(el('h2',day.date),projectTable(single),taskTable(single));}catch(e){error(e);}};grid.append(cell);}
scroll.append(grid);const note=el('p',ja?'1タスクのポイント = 1 + ⌈見積分数 / 30⌉ × 複雑度(1〜5)。完了取消は集計から除外。作業時間とPomodoroは別集計で、重複する場合は加算しません。':'Points per task = 1 + ceil(estimated minutes / 30) × complexity (1–5). Reopened tasks are excluded. Work and Pomodoro are separate; overlapping time is not added together.');note.className='productivity-note';results.replaceChildren(stats,scroll,note,detail); scroll.append(grid);const note=el('p',ja?'1タスクのポイント = 1 + ⌈見積分数 / 30⌉ × 複雑度(1〜5)。完了取消は集計から除外。作業時間とPomodoroは別集計で、重複する場合は加算しません。':'Points per task = 1 + ceil(estimated minutes / 30) × complexity (1–5). Reopened tasks are excluded. Work and Pomodoro are separate; overlapping time is not added together.');note.className='productivity-note';results.replaceChildren(stats,scroll,note,detail);
}catch(e){error(e);}finally{refresh.disabled=false;}} }catch(e){error(e);}finally{refresh.disabled=false;}}
controls.onsubmit=e=>{e.preventDefault();load();};await load(); controls.onsubmit=e=>{e.preventDefault();load();};await load();
+52
View File
@@ -0,0 +1,52 @@
(() => {
'use strict';
const root=document.getElementById('task-work-history');if(!root)return;
const ja=document.documentElement.lang.startsWith('ja'),t=(en,jp)=>ja?jp:en;
const api='/_/api/v1/extension/organizer.tasks/',writable=root.dataset.writable==='true',zone=root.dataset.timezone;
const status=document.getElementById('work-history-status'),list=document.getElementById('work-history-list'),paging=document.getElementById('work-history-paging');
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
const button=(text,fn)=>{const b=el('button',text);b.type='button';b.onclick=fn;return b;};
const messages={conflict:t('The record changed or overlaps another recording. Reload and check before retrying.','他の更新または作業時間の重複があります。再読み込みして確認してください。'),forbidden:t('You cannot change this record.','この記録は変更できません。'),invalid:t('Check the time range and reason. End must not be in the future; corrections are limited to 31 days. Ambiguous daylight-saving times require an explicit offset via the API.','日時と理由を確認してください。終了は現在以前、期間は31日以内です。夏時間の曖昧な時刻はAPIでオフセット指定が必要です。')};
let controller=null,sequence=0,cursor='',stack=[],editing=null,busy=false;
const form=el('form'),from=el('input'),to=el('input');form.className='work-history-controls';
for(const [input,label] of [[from,t('From','開始日')],[to,t('To (exclusive)','終了日(含まない)')]]){input.type='date';input.required=true;const l=el('label',label);l.append(input);form.append(l);}
const refresh=el('button','↻ '+t('Refresh','更新'));refresh.type='submit';form.append(refresh,el('span',zone));document.getElementById('work-history-controls').append(form);
async function request(path,options={}){const r=await fetch(api+path,{credentials:'same-origin',cache:'no-store',signal:AbortSignal.any([AbortSignal.timeout(15000),controller.signal]),...options,headers:{'Content-Type':'application/json'}});const d=await r.json().catch(()=>({}));if(!r.ok||r.redirected)throw Error(messages[d.error]||t('Request failed. Input is retained.','処理に失敗しました。入力は保持しています。'));return d;}
const parts=new Intl.DateTimeFormat('en-CA',{timeZone:zone,year:'numeric',month:'2-digit',day:'2-digit',hour:'2-digit',minute:'2-digit',second:'2-digit',hourCycle:'h23'});
const wall=ms=>{const p=Object.fromEntries(parts.formatToParts(new Date(ms)).map(x=>[x.type,x.value]));return `${p.year}-${p.month}-${p.day}T${p.hour}:${p.minute}:${p.second}`;};
const stamp=ms=>ms?wall(ms).replace('T',' '):t('Running','記録中');
const dirty=()=>editing&&[...editing.querySelectorAll('input,textarea')].some(n=>n.value!==n.defaultValue);
const canLeave=()=>!busy&&(!dirty()||confirm(t('Discard unsaved changes?','未保存の変更を破棄しますか?')));
window.addEventListener('beforeunload',e=>{if(dirty()||busy){e.preventDefault();e.returnValue='';}});
function editor(entry,slot,action){
if(!canLeave())return;if(editing)editing.remove();
const f=el('form');f.className='work-history-edit';editing=f;
const start=el('input'),end=el('input');
if(action==='correct')for(const [input,value,label] of [[start,entry.start_ms,t('Start','開始')],[end,entry.end_ms,t('End','終了')]]){input.type='datetime-local';input.step='1';input.value=input.defaultValue=wall(value);input.required=true;const l=el('label',label+' · '+zone);l.append(input);f.append(l);}
else f.append(el('p',t('Exclude this recording from reports. Its values and audit history are retained.','この記録を集計対象から除外します。元の時刻と変更履歴は残ります。')));
const reason=el('input');reason.type='text';reason.required=true;reason.maxLength=160;const label=el('label',t('Reason','変更理由'));label.append(reason);f.append(label);
const message=el('p');message.role='status';const save=el('button',action==='correct'?t('Save correction','訂正を保存'):t('Cancel recording','記録を取り消す'));save.type='submit';
const dismiss=button(t('Close','閉じる'),()=>{if(canLeave()){f.remove();editing=null;}});f.append(message,save,dismiss);slot.append(f);reason.focus();
f.onsubmit=async e=>{e.preventDefault();if(busy||!f.reportValidity())return;busy=true;const controls=[...f.querySelectorAll('input,button')];controls.forEach(n=>n.disabled=true);
const data={uuid:entry.uuid,task_uuid:entry.task_uuid,version:entry.version,action,reason:reason.value};
if(action==='correct'){data.start=start.value===start.defaultValue?new Date(entry.start_ms).toISOString():start.value;data.end=end.value===end.defaultValue?new Date(entry.end_ms).toISOString():end.value;}
try{await request('work',{method:'POST',body:JSON.stringify(data)});editing=null;f.remove();cursor='';stack=[];if(await load())status.textContent=t('Saved. Reports now use the corrected values.','保存しました。集計にも反映されます。');}
catch(error){message.textContent=error.message;}finally{busy=false;controls.forEach(n=>n.disabled=false);}
};
}
function audit(entry,slot){
const details=el('details'),summary=el('summary',t('Change history','変更履歴')),body=el('div');details.append(summary,body);slot.append(details);let loaded=false;
const names={start:t('Started','開始'),stop:t('Stopped','終了'),correct:t('Corrected','訂正'),cancel:t('Cancelled','取消'),baseline:t('Previous value','以前の値')};
async function more(before=0){body.replaceChildren(el('p',t('Loading…','読み込み中…')));try{const data=await request('work-history/'+encodeURIComponent(entry.uuid)+(before?'?before='+before:''));const rows=el('ol');for(const revision of data.items){const li=el('li');li.append(el('strong',`v${revision.item.version} · ${names[revision.action]||revision.action}`),el('p',`${stamp(revision.item.start_ms)} → ${stamp(revision.item.end_ms)}`));if(revision.reason)li.append(el('p',revision.reason));if(revision.changed_ms)li.append(el('small',stamp(revision.changed_ms)));rows.append(li);}body.replaceChildren(rows);if(data.next)body.append(button(t('Older changes','以前の変更'),()=>more(data.next)));loaded=true;}catch(e){if(e.name!=='AbortError')body.replaceChildren(el('p',e.message),button(t('Retry','再試行'),()=>more(before)));}}
details.ontoggle=()=>{if(details.open&&!loaded)more();};
}
function render(entries){
const table=el('table');table.className='work-history-table';const thead=el('thead'),head=el('tr');for(const title of [t('Task','タスク'),t('Start','開始'),t('End','終了'),t('Duration','時間'),t('Actions','操作')])head.append(el('th',title));thead.append(head);table.append(thead);const tbody=el('tbody');
for(const entry of entries){const row=el('tr'),name=el('td'),link=el('a',entry.title||entry.task_uuid);link.href='/v/plan/task/'+encodeURIComponent(entry.task_uuid);name.append(link);if(entry.cancelled_ms)name.append(el('span',t(' · Cancelled',' · 取消済み')));const controls=el('td'),slot=el('td'),extra=el('tr');slot.colSpan=5;extra.append(slot);extra.className='work-history-extra';
if(writable&&entry.end_ms&&!entry.cancelled_ms){controls.append(button(t('Edit','訂正'),()=>editor(entry,slot,'correct')),button(t('Cancel','取消'),()=>editor(entry,slot,'cancel')));}
audit(entry,slot);const values=[name,el('td',stamp(entry.start_ms)),el('td',stamp(entry.end_ms)),el('td',entry.end_ms?((entry.end_ms-entry.start_ms)/3600000).toFixed(2)+'h':'—'),controls];values.forEach((cell,i)=>{cell.dataset.label=head.children[i].textContent;row.append(cell);});tbody.append(row,extra);
}table.append(tbody);list.replaceChildren(table);if(!entries.length)list.append(el('p',t('No recordings in this period.','この期間の記録はありません。')));
}
async function load(){const ticket=++sequence;controller?.abort();controller=new AbortController();refresh.disabled=true;list.inert=true;status.textContent=t('Loading…','読み込み中…');try{const query=new URLSearchParams({limit:'50'});if(from.value)query.set('from',from.value);if(to.value)query.set('to',to.value);if(cursor)query.set('after',cursor);const data=await request('work-history?'+query);if(ticket!==sequence)return;from.value=data.from;to.value=data.to;editing=null;render(data.items);status.textContent='';paging.replaceChildren();if(stack.length)paging.append(button(t('Previous','前のページ'),()=>{if(canLeave()){cursor=stack.pop();load();}}));if(data.next)paging.append(button(t('Next','次のページ'),()=>{if(canLeave()){stack.push(cursor);cursor=data.next;load();}}));return true;}catch(error){if(error.name!=='AbortError')status.textContent=error.message;}finally{if(ticket===sequence){refresh.disabled=false;list.inert=false;}}}
form.onsubmit=e=>{e.preventDefault();if(canLeave()){cursor='';stack=[];load();}};load();
})();
+1 -1
View File
@@ -33,7 +33,7 @@
const routes = {today:'/v/plan/today',all:'/v/plan/tasks',projects:'/v/plan/projects'}; const routes = {today:'/v/plan/today',all:'/v/plan/tasks',projects:'/v/plan/projects'};
const icons = {carryover:'↶',today:'☀',tomorrow:'⇢',inbox:'↓',backlog:'≡',someday:'◇',review:'↻',all:'☷',projects:'▱',done:'✓',archived:'▣'}; const icons = {carryover:'↶',today:'☀',tomorrow:'⇢',inbox:'↓',backlog:'≡',someday:'◇',review:'↻',all:'☷',projects:'▱',done:'✓',archived:'▣'};
Object.keys(icons).forEach(key => { const a = el('a'); a.href = routes[key] || '/v/plan/tasks/' + key; a.append(el('span',icons[key]),el('span',t(key))); if(view===key||(key==='tomorrow'&&view==='day'&&selectedDate===nextDay(today)))a.setAttribute('aria-current','page'); $('nav').append(a); }); Object.keys(icons).forEach(key => { const a = el('a'); a.href = routes[key] || '/v/plan/tasks/' + key; a.append(el('span',icons[key]),el('span',t(key))); if(view===key||(key==='tomorrow'&&view==='day'&&selectedDate===nextDay(today)))a.setAttribute('aria-current','page'); $('nav').append(a); });
const schedule=el('a','▦ '+t('schedule'));schedule.href='/v/plan/tasks/schedule'+(projectID?'?project='+projectID:'');$('nav').append(schedule); const schedule=el('a','▦ '+t('schedule'));schedule.href='/v/plan/tasks/schedule'+(projectID?'?project='+projectID:'');$('nav').append(schedule);const workHistory=el('a',ja?'◷ 作業記録':'◷ Work history');workHistory.href='/v/plan/tasks/work';$('nav').append(workHistory);
$('date').textContent = view==='day'?selectedDate:today; $('heading').textContent = t(view === 'project' ? 'project' : view === 'detail' || view === 'new' ? 'all' : view); $('date').textContent = view==='day'?selectedDate:today; $('heading').textContent = t(view === 'project' ? 'project' : view === 'detail' || view === 'new' ? 'all' : view);
$('help').textContent = t(view==='today'?'todayHelp':view==='review'?'reviewHelp':'private'); $('help').textContent = t(view==='today'?'todayHelp':view==='review'?'reviewHelp':'private');
$('search-label').textContent=t('search');$('quick-title').placeholder=t('quick');$('quick-title').setAttribute('aria-label',t('title')); $('search-label').textContent=t('search');$('quick-title').placeholder=t('quick');$('quick-title').setAttribute('aria-label',t('title'));
+9
View File
@@ -0,0 +1,9 @@
{{ define "feeds.gohtml" }}<!doctype html>
<html lang="{{ .Locale }}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>{{ T "feeds.title" }} · {{ .WikiName }}</title>
<link rel="stylesheet" href="/static/css/main.css"><link rel="stylesheet" href="/static/css/theme/base.css"><link rel="stylesheet" href="{{ .ThemeCSS }}"><link rel="stylesheet" href="/static/css/feeds.css?v=8"></head>
<body class="mode-view organizer-page feeds-page"><header class="topbar"><div class="brand"><a href="/">{{ .WikiName }}</a></div>{{ template "auth-menu" . }}</header>{{ template "organizer-nav" . }}
<main id="feeds-app" data-writable="{{ .FeedWritable }}" data-tasks="{{ .TasksEnabled }}" data-wiki-prefix="{{ .FeedWikiPrefix }}" data-share-x="{{ .FeedShareX }}" data-link-mode="{{ .FeedLinkMode }}">
<div id="feed-toolbar"></div><p id="feed-status" role="status" aria-live="polite"></p>
<div class="feed-layout"><aside id="feed-sources" aria-label="{{ T "feeds.title" }}"></aside><section class="feed-index"><div id="feed-columns" aria-hidden="true"></div><div id="feed-rows" tabindex="0" role="listbox" aria-label="{{ T "feeds.title" }}"></div><button type="button" id="feed-more" hidden>↓</button></section><article id="feed-reader" tabindex="0"></article></div>
</main><script src="/static/js/feeds.js?v=10" defer></script></body></html>{{ end }}
Loaded 100 of 105 files, more files were not shown because too many files have changed in this diff. Show more