package pipeline import ( "context" "database/sql" "errors" "time" ) // ChatNotification is one chat phone push (ntfy) in the durable outbox. It // follows the run notification outbox (run_notifications.go): a row is leased // for a bounded attempt, a crash during a lease is recovered when the lease // expires, and attempts are bounded. ID is the chat's app notification id, so // an enqueue is idempotent and a notification the operator already cleared in // the app is never published late. type ChatNotification struct { ID, SessionID, Title, Body, Click string // SequenceID is the ntfy sequence id the push is published with (so a later // clear dismisses it on the phone); "" publishes without one. SequenceID string Priority int Attempts int } // ChatNotificationAttempts bounds publication attempts per chat push. const ChatNotificationAttempts = 5 // chatNotificationLease is how long one claimed attempt owns its row. const chatNotificationLease = 60 // chatNotificationRetention bounds how long settled rows are kept. const chatNotificationRetention = 7 * 24 * time.Hour func (s *Store) initChatNotifications() error { _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_notifications ( id TEXT PRIMARY KEY, session_id TEXT NOT NULL, title TEXT NOT NULL, body TEXT NOT NULL, click TEXT NOT NULL, sequence_id TEXT NOT NULL DEFAULT '', priority INTEGER NOT NULL DEFAULT 0, status TEXT NOT NULL DEFAULT 'pending', attempts INTEGER NOT NULL DEFAULT 0, next_attempt INTEGER NOT NULL DEFAULT 0, diagnostic TEXT NOT NULL DEFAULT '', message_id TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL); CREATE INDEX IF NOT EXISTS chat_notifications_due ON chat_notifications(status,next_attempt);`) return err } // EnqueueChatNotification records one chat push once (by id) and prunes // settled rows past retention. func (s *Store) EnqueueChatNotification(ctx context.Context, n ChatNotification, now time.Time) error { if _, err := s.db.ExecContext(ctx, `DELETE FROM chat_notifications WHERE created_at0)`, now); err != nil { return nil, err } if _, err := s.db.ExecContext(ctx, `UPDATE chat_notifications SET status='failed',diagnostic='publisher acknowledgement unknown after final lease expired' WHERE status='sending' AND attempts>=? AND next_attempt<=?`, ChatNotificationAttempts, now); err != nil { return nil, err } n := new(ChatNotification) err := s.db.QueryRowContext(ctx, `UPDATE chat_notifications SET status='sending',attempts=attempts+1,next_attempt=? WHERE rowid=(SELECT rowid FROM chat_notifications WHERE status IN ('pending','sending') AND attempts= ChatNotificationAttempts { status = "failed" } } _, err := s.db.ExecContext(ctx, `UPDATE chat_notifications SET status=?,diagnostic=?,message_id=?,next_attempt=? WHERE id=? AND status='sending' AND attempts=?`, status, diagnostic, messageID, next, n.ID, n.Attempts) return err } // ChatNotificationStatus reports one push's outbox status ("" when absent) // and attempts. func (s *Store) ChatNotificationStatus(ctx context.Context, id string) (string, int, error) { var status string var attempts int err := s.db.QueryRowContext(ctx, `SELECT status,attempts FROM chat_notifications WHERE id=?`, id).Scan(&status, &attempts) if errors.Is(err, sql.ErrNoRows) { return "", 0, nil } return status, attempts, err } // ChatNotificationCleared reports whether id's app notification has been // cleared; a publication that raced a clear then clears the phone too. func (s *Store) ChatNotificationCleared(ctx context.Context, id string) (bool, error) { var cleared bool err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM app_notifications WHERE id=? AND cleared_at<>0)`, id).Scan(&cleared) return cleared, err }