Repository navigation
feat: optional device push notifications (FCM + web push) - #185
Merged
Merged
Conversation
Storage layer for optional device push notifications (issue iamxvbaba#98), off by default: - domain.PushToken and store.PushTokenStore with memory + postgres backends - device_push_tokens migration, sqlc queries and generated code - account.registerDevice/unregisterDevice persist tokens when a store is wired and stay no-ops otherwise; keyed by (token_type, token), reassigned across accounts, empty token -> TOKEN_EMPTY - TELESRV_PUSH_ENABLED master switch gates token storage - rpc.Deps.PushTokens and main.go wiring; store/rpc tests Enabling push only records tokens at this point; no sender yet.
Sender layer for issue iamxvbaba#98, gated by TELESRV_PUSH_ENABLED: - internal/push: MTProto-v2 push cipher (byte-compatible with the official clients), FCM HTTP v1 sender with service-account OAuth2, RFC 8291 web push with RFC 8292 VAPID, and a Service that dispatches by token type - offline trigger: the outbox dispatcher hands an update that reached no live session to OfflinePushBridge, which builds a loc_key payload from the viewer's updates, coalesces per user, and sends from a worker pool - mute awareness: a muted peer yields a throttled MESSAGE_MUTED badge refresh, and a device whose token is no_muted is skipped for it - invalidation: an UNREGISTERED/410 rejection deletes the token - config: TELESRV_PUSH_FCM_* and TELESRV_PUSH_WEBPUSH_VAPID_* plus debounce and send timeout, wired in cmd/telesrv; .env.example and configuration docs updated - tests: FCM httptest, the RFC 8291 test vector, an end-to-end VAPID+decrypt check, and the rpc trigger/mute cases
- docs/push-notifications.md: scoped design (storage, cipher, senders, offline trigger, mute/badge, APNs deferred) kept in sync with the implementation - docs/telegram-differences.md: the push row now describes the opt-in FCM/web push behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optional device push notifications (FCM + web push)
Implements the scoped design the owner asked for in #98. Off by default: a
deployment that does not opt in behaves exactly as before (
account.registerDevicestays a no-op returning
true, no token is stored, no push is sent).Today
account.registerDeviceis accepted and discarded, and a backgroundedclient receives nothing until it reconnects. This adds optional server-side push
delivery for Android FCM and web push, and honours per-chat mute.
How it works
account.registerDevice/unregisterDevicenow persist thedevice token (and the client
secret) when a token store is wired. A token isglobally unique per
(token_type, token)and reassigned to the newest account;an empty token returns
TOKEN_EMPTY.live sessions an update reached. When that count is zero, an
OfflinePushBridgebuilds aloc_keynotification from the viewer's updates,coalesces per user, and sends from a small worker pool (never blocking the
outbox). Online delivery is completely unchanged.
mute_until(per-peer scope plus theusers/chats/broadcasts category default). A muted peer yields a throttled
MESSAGE_MUTEDbadge refresh instead of a visible notification; a device whosetoken is
no_mutedis skipped for that refresh.UNREGISTERED/404/410rejection deletes the token.Encryption
FCM (
token_type 2) — the notification JSON is sealed with the clientsecretusing the MTProto-v2 scheme the official clients use, and sent as thepfield of a data-only FCM message:This was matched against the reference implementations (DrKLO Android
PushListenerController+MessageKeyData, and Telegram Web K'spushSingleManager), and the cipher is round-tripped by an independent clientdecryptor in tests.
Web push (
token_type 10) — the subscription is a JSON token(
endpoint+p256dh/auth). The notification JSON is encrypted withRFC 8291 (
aes128gcm, RFC 8188) and delivered with an RFC 8292VAPID ES256
Authorizationheader. Validated against the RFC 8291 §5 test vectorand an end-to-end test that verifies the VAPID JWT and decrypts the body.
Configuration (all optional)
TELESRV_PUSH_ENABLEDfalseTELESRV_PUSH_FCM_SERVICE_ACCOUNT_FILETELESRV_PUSH_FCM_PROJECT_ID/_TOKEN_URL/_SCOPETELESRV_PUSH_WEBPUSH_VAPID_PUBLIC_KEY/_PRIVATE_KEYTELESRV_PUSH_WEBPUSH_SUBJECT/_TTL86400sub, TTL.TELESRV_PUSH_DEBOUNCE/_SEND_TIMEOUT30s/5sNo new dependency: the OAuth2 service-account JWT and VAPID use only the standard
library (
crypto/ecdh,crypto/ecdsa,crypto/aes, HKDF viacrypto/hmac).Client requirement (unavoidable)
(
google-services.jsonswap) so FCM delivers to the server's service account.holds (
VITE_PUSH_SERVER_KEY). The push service rejects a mismatched key.Testing
internal/push: FCM sender against anhttptestserver (token caching,unregistered invalidation), the MTProto cipher round-trip, the RFC 8291 test
vector, and an end-to-end VAPID+decrypt check.
internal/rpc: registration/storage, outbox "notify only whensent == 0",mute →
MESSAGE_MUTED, andno_mutedskipping.internal/store: memory + postgres (*_integration_test.go, DSN-gated).Not included
Feedback welcome, especially on the payload/
loc_keychoices and whether pushshould stay in-tree (as here) or be an optional sidecar.