package server import ( "context" "crypto/sha256" "encoding/hex" "log" "strings" "time" "git.a-wan.net/alan/pipelines/internal/stage" ) // chatAttentionClear dismisses one ntfy message by sequence id; tests may // replace it. var chatAttentionClear = stage.ClearNtfy // chatNtfySequence is the ntfy sequence id a chat notification (app id // "chat:::") is published with, so clearing the app // notification can also clear it in the ntfy app. It is derived from the app // id, so nothing extra is stored and a restart still clears it. func chatNtfySequence(id string) string { digest := sha256.Sum256([]byte(id)) return "pl-" + hex.EncodeToString(digest[:12]) } // clearChatNtfy asks ntfy to clear the chat notifications among ids (every // app notification id that was cleared), off the caller's goroutine. ntfy // emits message_clear and its clients dismiss the notification. Run // notifications are published without a sequence id and are left alone. func clearChatNtfy(ids []string) { if !chatAttentionConfigured() { return } var sequences []string for _, id := range ids { if strings.HasPrefix(id, "chat:") { sequences = append(sequences, chatNtfySequence(id)) } } if len(sequences) == 0 { return } clear := chatAttentionClear go func() { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() for _, sequence := range sequences { if err := clear(ctx, "system", sequence); err != nil { log.Printf("clear ntfy notification %s: %v", sequence, err) } } }() }