Files
pages/internal/store/activity_subscriptions.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

128 lines
5.0 KiB
Go

package store
import (
"context"
"strings"
)
// Subscriptions are trusted worker configuration, never request parameters.
// Changing the type set requires a NEW consumer ID: its cursor may already
// have passed old events of a newly added type.
type ActivitySubscription struct {
ConsumerID string
EventTypes []string
}
func (sub ActivitySubscription) filter() (string, []any, error) {
if strings.TrimSpace(sub.ConsumerID) == "" || len(sub.ConsumerID) > 100 || len(sub.EventTypes) == 0 || len(sub.EventTypes) > 16 {
return "", nil, ErrActivityOutboxInput
}
args := make([]any, 0, len(sub.EventTypes))
marks := make([]string, 0, len(sub.EventTypes))
for _, typ := range sub.EventTypes {
if strings.TrimSpace(typ) == "" || len(typ) > 100 {
return "", nil, ErrActivityOutboxInput
}
args = append(args, typ)
marks = append(marks, "?")
}
return strings.Join(marks, ","), args, nil
}
// Only the initial upgrade scans the old journal. Thereafter the INSERT
// trigger updates a small per-Domain/type watermark in the source transaction.
func (s *Store) ensureActivitySubscriptions() error {
tx, err := s.DB.Begin()
if err != nil {
return err
}
defer tx.Rollback()
for _, q := range []string{
`CREATE TABLE IF NOT EXISTS activity_outbox_heads(domain_uuid TEXT NOT NULL,event_type TEXT NOT NULL,last_sequence INTEGER NOT NULL,PRIMARY KEY(domain_uuid,event_type))`,
`CREATE INDEX IF NOT EXISTS idx_activity_heads_type ON activity_outbox_heads(event_type,domain_uuid,last_sequence)`,
`CREATE INDEX IF NOT EXISTS idx_activity_outbox_subscription ON activity_outbox(domain_uuid,event_type,sequence)`,
`CREATE TABLE IF NOT EXISTS activity_outbox_migrations(name TEXT PRIMARY KEY)`,
`CREATE TRIGGER IF NOT EXISTS activity_outbox_advance_head AFTER INSERT ON activity_outbox BEGIN
INSERT INTO activity_outbox_heads(domain_uuid,event_type,last_sequence) VALUES(NEW.domain_uuid,NEW.event_type,NEW.sequence)
ON CONFLICT(domain_uuid,event_type) DO UPDATE SET last_sequence=MAX(last_sequence,excluded.last_sequence); END`,
} {
if _, err = tx.Exec(q); err != nil {
return err
}
}
var migrated bool
if err = tx.QueryRow(`SELECT EXISTS(SELECT 1 FROM activity_outbox_migrations WHERE name='typed-heads-v1')`).Scan(&migrated); err != nil {
return err
}
if !migrated {
if _, err = tx.Exec(`INSERT INTO activity_outbox_heads(domain_uuid,event_type,last_sequence) SELECT domain_uuid,event_type,MAX(sequence) FROM activity_outbox GROUP BY domain_uuid,event_type ON CONFLICT(domain_uuid,event_type) DO UPDATE SET last_sequence=MAX(last_sequence,excluded.last_sequence)`); err != nil {
return err
}
if _, err = tx.Exec(`INSERT INTO activity_outbox_migrations(name) VALUES('typed-heads-v1')`); err != nil {
return err
}
}
return tx.Commit()
}
// PendingActivityConsumerDomains does not scan event history or rely on the
// legacy shared status. Acknowledgment by another consumer cannot hide work.
func (s *Store) PendingActivityConsumerDomains(ctx context.Context, sub ActivitySubscription, limit int) ([]string, error) {
marks, args, err := sub.filter()
if err != nil {
return nil, err
}
if limit < 1 || limit > 100 {
limit = 100
}
args = append([]any{sub.ConsumerID}, args...)
args = append(args, limit)
rows, err := s.DB.QueryContext(ctx, `SELECT h.domain_uuid FROM activity_outbox_heads h LEFT JOIN activity_outbox_checkpoints c ON c.domain_uuid=h.domain_uuid AND c.consumer_id=? WHERE h.event_type IN (`+marks+`) AND h.last_sequence>COALESCE(c.last_sequence,0) GROUP BY h.domain_uuid ORDER BY COALESCE(c.updated_at_ms,0),h.domain_uuid LIMIT ?`, args...)
if err != nil {
return nil, err
}
defer rows.Close()
out := []string{}
for rows.Next() {
var domain string
if err = rows.Scan(&domain); err != nil {
return nil, err
}
out = append(out, domain)
}
return out, rows.Err()
}
// The SQL limit is applied per type first, so unrelated floods and SQLite's
// final multi-type ordering cannot materialize an unbounded pending backlog.
func (s *Store) ActivityOutboxForConsumer(ctx context.Context, sub ActivitySubscription, domain string, limit int) ([]ActivityOutboxRecord, error) {
_, _, err := sub.filter()
if err != nil || domain == "" {
return nil, ErrActivityOutboxInput
}
if limit < 1 || limit > 500 {
limit = 100
}
queries := []string{}
args := []any{}
for _, typ := range sub.EventTypes {
queries = append(queries, `SELECT * FROM (SELECT `+activityOutboxColumns+` FROM activity_outbox WHERE domain_uuid=? AND event_type=? AND sequence>COALESCE((SELECT last_sequence FROM activity_outbox_checkpoints WHERE consumer_id=? AND domain_uuid=?),0) ORDER BY sequence LIMIT ?)`)
args = append(args, domain, typ, sub.ConsumerID, domain, limit)
}
args = append(args, limit)
rows, err := s.DB.QueryContext(ctx, strings.Join(queries, " UNION ")+` ORDER BY sequence LIMIT ?`, args...)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ActivityOutboxRecord{}
for rows.Next() {
var item ActivityOutboxRecord
if err = scanActivityOutbox(rows, &item); err != nil {
return nil, err
}
items = append(items, item)
}
return items, rows.Err()
}