Files
alan d1846441cc Stream ntfy notifications to app clients over /connect
Record every notification published to ntfy (run outbox, stage notify
hooks, chat question/approval/reply) in a persisted app_notifications
outbox and stream it to subscribe_notifications clients with an after
cursor; clear_notifications and ack_attention clear across devices, as do
runs leaving waiting, deleted runs and answered chat questions. Add the
notify.ntfy_enabled setting (default on) to stop only the ntfy publish.
2026-11-01 02:03:58 +00:00

99 lines
3.4 KiB
Go

package server
import (
"context"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// attentionAckStore persists the shared in-app acknowledgements.
// *pipeline.Store implements it.
type attentionAckStore interface {
AttentionAcks(context.Context) (protocol.AttentionAcks, error)
AckAttention(context.Context, *protocol.AttentionAcks, time.Time) (delta, all protocol.AttentionAcks, err error)
}
// attentionAckHub fans an accepted acknowledgement out to every other open
// connection, so a dismissal on one device disappears on the others live.
// It holds each connection's serialized write function, never the socket.
type attentionAckHub struct {
mu sync.Mutex
clients map[string]func(protocol.Message) error
}
func newAttentionAckHub() *attentionAckHub {
return &attentionAckHub{clients: map[string]func(protocol.Message) error{}}
}
func (h *attentionAckHub) attach(clientID string, write func(protocol.Message) error) {
h.mu.Lock()
defer h.mu.Unlock()
h.clients[clientID] = write
}
func (h *attentionAckHub) detach(clientID string) {
h.mu.Lock()
defer h.mu.Unlock()
delete(h.clients, clientID)
}
// broadcast pushes delta to every connection except the sender. Each write
// runs on its own goroutine (bounded by the connection's own write timeout), so
// one slow client cannot delay the sender's reply or the other clients.
func (h *attentionAckHub) broadcast(sender string, delta protocol.AttentionAcks) {
h.mu.Lock()
targets := make([]func(protocol.Message) error, 0, len(h.clients))
for id, write := range h.clients {
if id != sender {
targets = append(targets, write)
}
}
h.mu.Unlock()
for _, write := range targets {
frame := delta
go func() {
_ = write(protocol.Message{Type: protocol.AttentionAcksChanged, AttentionAcks: &frame})
}()
}
}
// handleAttentionAcks answers attention_acks and ack_attention. It reports
// whether request was one of them. An ack that changes nothing (already
// dismissed, or a watermark that does not advance) replies with the whole set
// but pushes nothing.
func handleAttentionAcks(ctx context.Context, request protocol.Message, store attentionAckStore, hub *attentionAckHub, notes *appNotifier, clientID string, write func(protocol.Message) error) bool {
if request.Type != protocol.GetAttentionAcks && request.Type != protocol.AckAttention {
return false
}
if store == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "attention acknowledgements are unavailable: no store"})
return true
}
var all, delta protocol.AttentionAcks
var err error
if request.Type == protocol.GetAttentionAcks {
all, err = store.AttentionAcks(ctx)
} else {
delta, all, err = store.AckAttention(ctx, request.AttentionAcks, time.Now())
}
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return true
}
_ = write(protocol.Message{Type: protocol.AttentionAcksReply, ID: request.ID, AttentionAcks: &all})
if hub != nil && !delta.Empty() {
hub.broadcast(clientID, delta)
}
// The acknowledged items' app notifications clear on every device. The
// request (not only the delta) decides, so an ack repeated after a new
// notification still clears it.
if request.Type == protocol.AckAttention && notes != nil {
if valid, err := protocol.ValidateAttentionAck(request.AttentionAcks); err == nil {
notes.clearForAcks(ctx, valid)
}
}
return true
}