send push notifications regardless of online status

This commit is contained in:
talksik
2026-05-18 12:24:42 -07:00
parent 6226d17254
commit 9ef4935241
2 changed files with 37 additions and 52 deletions
+29 -39
View File
@@ -6,17 +6,9 @@ import (
"fmt"
"log/slog"
pbpusher "github.com/flowy-live/llink/genproto/llink/pusher"
"github.com/flowy-live/llink/internal/network"
"google.golang.org/grpc"
)
// PusherClient is a narrow subset of the pusher gRPC service so tests can
// supply a fake without standing up a real server.
type PusherClient interface {
IsOnline(ctx context.Context, in *pbpusher.IsOnlineRequest, opts ...grpc.CallOption) (*pbpusher.IsOnlineResponse, error)
}
type NotifyInput struct {
NetworkID string
SenderHumanID string
@@ -39,27 +31,34 @@ type NotifyInput struct {
// Notifier fans out one particle to Expo:
// 1. Resolve recipients (visibility ∩ network members, minus sender).
// 2. Drop anyone currently connected via WebSocket.
// 3. Send a batched Expo request for the remainder's tokens.
// 4. Prune tokens Expo reports as DeviceNotRegistered.
// 2. Send a batched Expo request for every recipient's tokens.
// 3. Prune tokens Expo reports as DeviceNotRegistered.
//
// Online/offline presence is intentionally NOT consulted: a live WebSocket
// is a poor proxy for "user is actively consuming this particle right now"
// (backgrounded apps, idle desktops, etc. all look online), and the resulting
// false-negatives outweigh the duplicate-notification cost on a focused
// device, which the OS handles via Focus modes and per-app settings.
type Notifier struct {
networkR network.Reader
tokens Service
pusher PusherClient
expo *ExpoClient
}
func NewNotifier(networkR network.Reader, tokens Service, pusher PusherClient, expo *ExpoClient) *Notifier {
func NewNotifier(networkR network.Reader, tokens Service, expo *ExpoClient) *Notifier {
return &Notifier{
networkR: networkR,
tokens: tokens,
pusher: pusher,
expo: expo,
}
}
func (n *Notifier) NotifyParticleCreated(ctx context.Context, in NotifyInput) error {
if in.NetworkID == "" || in.ParticleID == "" {
slog.Info("pushnotify: skip — missing ids",
"networkID", in.NetworkID,
"particleID", in.ParticleID,
)
return nil
}
@@ -69,31 +68,31 @@ func (n *Notifier) NotifyParticleCreated(ctx context.Context, in NotifyInput) er
}
recipients := network.ResolveVisibility(in.StreamVisibleTo, members)
recipientsBeforeSenderFilter := len(recipients)
recipients = filterOut(recipients, in.SenderHumanID)
if len(recipients) == 0 {
slog.Info("pushnotify: skip — no recipients",
"networkID", in.NetworkID,
"particleID", in.ParticleID,
"senderHumanID", in.SenderHumanID,
"members", len(members),
"visibleTo", in.StreamVisibleTo,
"resolved", recipientsBeforeSenderFilter,
)
return nil
}
online, err := n.queryOnline(ctx, recipients)
if err != nil {
return fmt.Errorf("presence lookup: %w", err)
}
offline := make([]string, 0, len(recipients))
for _, id := range recipients {
if !online[id] {
offline = append(offline, id)
}
}
if len(offline) == 0 {
return nil
}
tokens, err := n.tokens.ListForHumans(ctx, offline)
tokens, err := n.tokens.ListForHumans(ctx, recipients)
if err != nil {
return fmt.Errorf("token lookup: %w", err)
}
if len(tokens) == 0 {
slog.Info("pushnotify: skip — no tokens for recipients",
"networkID", in.NetworkID,
"particleID", in.ParticleID,
"recipients", len(recipients),
"recipientIDs", recipients,
)
return nil
}
@@ -103,7 +102,6 @@ func (n *Notifier) NotifyParticleCreated(ctx context.Context, in NotifyInput) er
"networkID", in.NetworkID,
"particleID", in.ParticleID,
"recipients", len(recipients),
"offline", len(offline),
"tokens", len(tokens),
"sent", len(tickets),
)
@@ -116,14 +114,6 @@ func (n *Notifier) NotifyParticleCreated(ctx context.Context, in NotifyInput) er
return nil
}
func (n *Notifier) queryOnline(ctx context.Context, humanIDs []string) (map[string]bool, error) {
resp, err := n.pusher.IsOnline(ctx, &pbpusher.IsOnlineRequest{HumanIds: humanIDs})
if err != nil {
return nil, err
}
return resp.Online, nil
}
// DeviceNotRegistered is the one feedback signal we honor; other ticket
// errors (MessageTooBig, RateLimit, …) are logged and dropped.
func (n *Notifier) cleanupDeadTokens(ctx context.Context, msgs []Message, tickets []Ticket) {