Turn on push from onboarding step 3 and add a test-push endpoint (Feature 19)
Onboarding step 3's "Turn on notifications" now calls usePush().subscribePush
straight from that tap (the only place onboarding asks for permission) and
shows how it ended: on; blocked, with how to allow it in the browser's site
settings; not supported, with how to add MealQuest to the Home Screen on
iPhone; or a dismissed prompt that can be asked again. "Maybe later" still
finishes without asking. The interim notificationsUnavailable is gone.
POST /api/v1/settings/notifications/test-push queues "MealQuest test
notification" through the notification outbox as a plan_ready event with a
fresh plan_ready:test-<ULID> key, opening /plan, on the push channel only
(new notify.Service.EmitPush), never Google Chat. It answers 202
{event_key, subscriptions}, or 409 no_subscriptions / push_off rather than
reporting success when nothing would be sent.
Maestro task f89279b8-b92c-4d9f-a6c2-fd87fccda509.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+58
-4
@@ -14,6 +14,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/db"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/notify"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/store"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/vapid"
|
||||
)
|
||||
@@ -69,22 +70,75 @@ func (e *ValidationError) Error() string {
|
||||
return "invalid push subscription: " + strings.Join(parts, "; ")
|
||||
}
|
||||
|
||||
// Service stores push subscriptions.
|
||||
// Service stores push subscriptions and sends a test notification to them.
|
||||
type Service struct {
|
||||
st *store.Store
|
||||
publicKey string
|
||||
notifier *notify.Service
|
||||
}
|
||||
|
||||
// NewService returns a Service over st handing out publicKey, the VAPID
|
||||
// public key in vapid's encoding.
|
||||
func NewService(st *store.Store, publicKey string) (*Service, error) {
|
||||
// public key in vapid's encoding, that sends its test notification through
|
||||
// notifier.
|
||||
func NewService(st *store.Store, publicKey string, notifier *notify.Service) (*Service, error) {
|
||||
if st == nil {
|
||||
return nil, errors.New("push: nil store")
|
||||
}
|
||||
if _, err := vapid.ParsePublicKey(publicKey); err != nil {
|
||||
return nil, fmt.Errorf("push: VAPID public key: %w", err)
|
||||
}
|
||||
return &Service{st: st, publicKey: publicKey}, nil
|
||||
if notifier == nil {
|
||||
return nil, errors.New("push: nil notifier")
|
||||
}
|
||||
return &Service{st: st, publicKey: publicKey, notifier: notifier}, nil
|
||||
}
|
||||
|
||||
// The test notification SendTest sends.
|
||||
const (
|
||||
TestTitle = "MealQuest test notification"
|
||||
TestBody = "Push works on this device. Tap to open the plan."
|
||||
TestURL = "/plan"
|
||||
)
|
||||
|
||||
// ErrNoSubscriptions is returned by SendTest when no device has turned on
|
||||
// push: there is nothing to send the test to.
|
||||
var ErrNoSubscriptions = errors.New("push: no device has turned on notifications")
|
||||
|
||||
// TestSent is a test notification queued for delivery: its event key and
|
||||
// how many subscriptions were stored when it was queued.
|
||||
type TestSent struct {
|
||||
EventKey string `json:"event_key"`
|
||||
Subscriptions int `json:"subscriptions"`
|
||||
}
|
||||
|
||||
// SendTest queues TestTitle for every stored subscription, through the
|
||||
// notification outbox as a plan_ready event whose tap opens TestURL. Its
|
||||
// key, "plan_ready:test-" and a new id, is never used twice, so it is never
|
||||
// taken for an event already sent. It goes to push alone, never to Google
|
||||
// Chat. With no subscription it is ErrNoSubscriptions, and with push
|
||||
// switched off for plan_ready it is notify.ErrPushOff; neither queues
|
||||
// anything. Delivery happens after SendTest returns, with the outbox's
|
||||
// retries.
|
||||
func (s *Service) SendTest(ctx context.Context) (TestSent, error) {
|
||||
subs, err := db.New(s.st.Reader).ListPushSubscriptions(ctx)
|
||||
if err != nil {
|
||||
return TestSent{}, fmt.Errorf("push: list subscriptions: %w", err)
|
||||
}
|
||||
if len(subs) == 0 {
|
||||
return TestSent{}, ErrNoSubscriptions
|
||||
}
|
||||
key := string(notify.PlanReady) + ":test-" + store.NewID()
|
||||
err = s.notifier.EmitPush(ctx, notify.Event{
|
||||
Type: notify.PlanReady,
|
||||
Key: key,
|
||||
Title: TestTitle,
|
||||
Body: TestBody,
|
||||
URL: TestURL,
|
||||
})
|
||||
if err != nil {
|
||||
return TestSent{}, fmt.Errorf("push: send test notification: %w", err)
|
||||
}
|
||||
return TestSent{EventKey: key, Subscriptions: len(subs)}, nil
|
||||
}
|
||||
|
||||
// PublicKey returns the VAPID public key, for applicationServerKey.
|
||||
|
||||
Reference in New Issue
Block a user