Closer/functions/src/games/onGameSessionUpdate.ts

333 lines
15 KiB
TypeScript
Raw Normal View History

import * as admin from 'firebase-admin'
import { onDocumentWritten, Change } from 'firebase-functions/v2/firestore'
import { recipientInQuietHours } from '../notifications/quietHours'
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
import { sendPushToUser } from '../notifications/push'
import { logger } from '../log'
/**
* Firestore trigger that notifies partners when a game session is created or completed.
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
*
* Path: couples/{coupleId}/sessions/{sessionId}
* Condition: onWrite (create, update, delete)
*/
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
export const onGameSessionUpdate = onDocumentWritten(
'couples/{coupleId}/sessions/{sessionId}',
async (event) => {
const { coupleId, sessionId } = event.params
// The per-couple active-session lock lives at sessions/_active — it is a pointer, not a
// game session, so it must never produce a partner notification.
if (sessionId === '_active') return
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
const change = event.data
if (!change) return
if (!change.after.exists) return // deletion — nothing to notify (skip the four reads below)
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
const db = admin.firestore()
const messaging = admin.messaging()
// Get the session document
const sessionDoc = await db.collection('couples').doc(coupleId).collection('sessions').doc(sessionId).get()
const session = sessionDoc.data()
if (!session) {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
logger.log(`[onGameSessionUpdate] session ${sessionId} not found, skipping`)
return
}
// Get couple info
const coupleDoc = await db.collection('couples').doc(coupleId).get()
if (!coupleDoc.exists) {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
logger.warn(`[onGameSessionUpdate] couple ${coupleId} not found`)
return
}
const coupleData = coupleDoc.data() ?? {}
const userIds = (coupleData.userIds ?? []) as string[]
if (userIds.length !== 2) {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
logger.warn(`[onGameSessionUpdate] invalid couple ${coupleId}: expected 2 users, got ${userIds.length}`)
return
}
const partnerA = userIds[0]
const partnerB = userIds[1]
const userA = await db.collection('users').doc(partnerA).get()
const userB = await db.collection('users').doc(partnerB).get()
// displayName is E2EE in users/{uid}, so the OS-rendered push uses a generic label; the app shows
// the real name in-app (resolved locally). Avatar (photoUrl) stays plaintext and is still sent.
const partnerAName = 'Your partner'
const partnerBName = 'Your partner'
const avatarA = userA.data()?.photoUrl as string | undefined
const avatarB = userB.data()?.photoUrl as string | undefined
// M-001: per-recipient quiet-hours lookup ("no notifications" promise). Fail-open.
const dataFor = (uid: string) => (uid === partnerA ? userA.data() : userB.data())
const currentData = change.after.data() ?? {}
const status = currentData.status as string | undefined
const sessionRef = db.collection('couples').doc(coupleId).collection('sessions').doc(sessionId)
// Detect start/finish from the session's CURRENT state + a one-time flag, NOT from the
// change.before→change.after status diff. The atomic session start (F-RACE-001) writes the
// session doc AND the sessions/_active pointer in a single transaction; transactional writes
// can be delivered to onWrite with change.before === change.after (the "Snapshot has no
// readTime" path), which made the inactive→active edge unreliable and intermittently dropped
// the partner_started_game push. Claiming a flag inside a transaction makes each notification
// fire exactly once no matter how the event is delivered (and prevents double-sends).
// ── New session started ──────────────────────────────────────────────
if (status === 'active' && !currentData.startNotifiedAt) {
const claimed = await db.runTransaction(async (tx) => {
const fresh = await tx.get(sessionRef)
const d = fresh.data()
if (!fresh.exists || !d || d.status !== 'active' || d.startNotifiedAt) return false
tx.update(sessionRef, { startNotifiedAt: admin.firestore.FieldValue.serverTimestamp() })
return true
})
if (claimed) {
const startedBy = currentData.startedByUserId
const gameType = currentData.gameType ?? 'wheel'
const recipientId = startedBy === partnerA ? partnerB : partnerA
const starterName = startedBy === partnerA ? partnerAName : partnerBName
const starterAvatar = startedBy === partnerA ? avatarA : avatarB
if (recipientInQuietHours(dataFor(recipientId))) {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
logger.log(`[onGameSessionUpdate] recipient ${recipientId} in quiet hours — suppressing start push`)
} else {
await notifyPartner(
db, messaging, recipientId, starterName, gameType,
'partner_started_game', `${starterName} has started a game. Tap to join!`, coupleId,
starterAvatar, sessionId
)
}
}
return
}
// ── Partner joined an active session ─────────────────────────────────
// The non-starter opening the session writes their uid into `joinedByUsers` (client, rule-gated).
// Notify the STARTER once, with the joiner's name + avatar. One-time via `joinNotifiedAt`
// (server-only flag, claimed in a transaction — same pattern as start/finishNotifiedAt).
if (status === 'active' && !currentData.joinNotifiedAt && Array.isArray(currentData.joinedByUsers)) {
const startedBy = currentData.startedByUserId
const joiner = (currentData.joinedByUsers as string[]).find((u) => u && u !== startedBy)
if (joiner) {
const claimed = await db.runTransaction(async (tx) => {
const fresh = await tx.get(sessionRef)
const d = fresh.data()
if (!fresh.exists || !d || d.status !== 'active' || d.joinNotifiedAt) return false
const j = Array.isArray(d.joinedByUsers) ? (d.joinedByUsers as string[]) : []
if (!j.some((u) => u && u !== d.startedByUserId)) return false
tx.update(sessionRef, { joinNotifiedAt: admin.firestore.FieldValue.serverTimestamp() })
return true
})
if (claimed) {
const gameType = currentData.gameType ?? 'wheel'
const joinerName = joiner === partnerA ? partnerAName : partnerBName
const joinerAvatar = joiner === partnerA ? avatarA : avatarB
if (recipientInQuietHours(dataFor(startedBy))) {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
logger.log(`[onGameSessionUpdate] starter ${startedBy} in quiet hours — suppressing join push`)
} else {
await notifyPartner(
db, messaging, startedBy, joinerName, gameType,
'partner_joined_game', `${joinerName} joined your game — tap to play together.`, coupleId,
joinerAvatar, sessionId
)
}
}
}
return
}
// ── Session completed (reveal ready for both) ────────────────────────
fix(ui,games): defect-polish batch C1 from P3/UX review - Challenges catalog: hide the 🔒 Premium badge once the couple has premium (A-003b; matches the Play hub's showPremiumBadge pattern). Verified live both directions under an authorized grant/revoke cycle. - Game banner lifecycle (BANNER-LIFE-001): entering a session's screen now consumes any banner pointing at it (GamePromptController.consumeForSession wired into ActiveGameSessionMonitor.enter), and activity from a DIFFERENT session may replace a stale persistent banner. Verified live: no banner on reveal; stale RESULTS banner replaced by a new session's prompt. - Waiting/join screen: says 'Your turn — {name} already played their part' for the non-starter once a first part landed (new partHasFinished mapped from partFinishNotifiedAt; completedByUsers only fills at reveal). Session observe mapping now also carries completedByUsers/joinedByUsers. - How Well results: matched-row colors are now a theme-aware container+content pair (dark mode was near-invisible: fixed pale-green container under onSurfaceVariant text). - Date Match: top card fully opaque — next card's text no longer bleeds through (was alpha 0.96). - functions: don't send 'X finished — see your results!' for abandoned/quit sessions (status flips to completed with empty completedByUsers; a real completion always has both uids). Found live when a quit triggered a false banner. Needs deploy (bundled with C4). Unit + functions suites green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 21:03:22 -05:00
// Only a REAL completion has results: the async flow marks each user complete at reveal, so
// status flips to 'completed' with both uids in completedByUsers. An abandoned/quit session
// also lands on 'completed' (the abandon write) but with an empty list — pushing
// "finished — see your results!" for those was a false notification into an empty reveal.
const completedBy = Array.isArray(currentData.completedByUsers) ? currentData.completedByUsers : []
if (status === 'completed' && !currentData.finishNotifiedAt && completedBy.length >= 2) {
const claimed = await db.runTransaction(async (tx) => {
const fresh = await tx.get(sessionRef)
const d = fresh.data()
if (!fresh.exists || !d || d.status !== 'completed' || d.finishNotifiedAt) return false
tx.update(sessionRef, { finishNotifiedAt: admin.firestore.FieldValue.serverTimestamp() })
return true
})
if (claimed) {
const gt = currentData.gameType ?? 'wheel'
// Notify BOTH partners, each naming the OTHER. M-001: skip a recipient in quiet hours.
if (recipientInQuietHours(dataFor(partnerA))) {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
logger.log(`[onGameSessionUpdate] ${partnerA} in quiet hours — suppressing finish push`)
} else {
await notifyPartner(
db, messaging, partnerA, partnerBName, gt,
'partner_finished_game', `${partnerBName} finished — tap to see your results!`, coupleId,
avatarB, sessionId
)
}
if (recipientInQuietHours(dataFor(partnerB))) {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
logger.log(`[onGameSessionUpdate] ${partnerB} in quiet hours — suppressing finish push`)
} else {
await notifyPartner(
db, messaging, partnerB, partnerAName, gt,
'partner_finished_game', `${partnerAName} finished — tap to see your results!`, coupleId,
avatarA, sessionId
)
}
}
return
}
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
}
)
/**
* Notify the WAITING partner the moment the FIRST player finishes their part of an async game.
*
* Async games (this_or_that / wheel / how_well / desire_sync) write each player's answers to
* couples/{coupleId}/{gameType}/{sessionId}.answers[uid]; the SESSION doc only flips to
* 'completed' once BOTH have answered (which onGameSessionUpdate turns into partner_finished_game).
* Between first-finish and both-finish the waiting partner got NOTHING they never learned it was
* their turn (the symptom: "X finished a game but the partner was never notified"). The
* PARTNER_COMPLETED_PART client route already exists; this is the trigger that finally emits it.
*
* Path: couples/{coupleId}/{gameType}/{sessionId} (answer doc; same id as the session doc).
*
* Deployed as four narrow, explicitly-pathed triggers (one per async-game collection) sharing the
* handler below, rather than one broad `{gameType}` wildcard the wildcard fired a (no-op)
* invocation on every write to ANY couple subcollection (messages, reactions, ); the narrow paths
* only fire for the four game collections.
*/
async function handleGamePartFinished(
coupleId: string,
gameType: string,
sessionId: string,
change: Change<admin.firestore.DocumentSnapshot> | undefined
): Promise<void> {
if (!change?.after.exists) return
const answers = (change.after.data()?.answers ?? {}) as Record<string, unknown>
const answerUids = Object.keys(answers)
// Only the FIRST finisher (exactly one answer present) nudges the partner. Zero = session just
// created; two = both done → the session flips to completed and onGameSessionUpdate sends
// partner_finished_game instead.
if (answerUids.length !== 1) return
const finisherUid = answerUids[0]
const db = admin.firestore()
const messaging = admin.messaging()
const sessionRef = db.collection('couples').doc(coupleId).collection('sessions').doc(sessionId)
// Claim a one-time flag on the SESSION doc (consistent with start/finishNotifiedAt; rule-safe;
// writing it re-fires onGameSessionUpdate but that no-ops on an active+already-started session).
const claimed = await db.runTransaction(async (tx) => {
const fresh = await tx.get(sessionRef)
const d = fresh.data()
if (!fresh.exists || !d) return false
if (d.status === 'completed' || d.partFinishNotifiedAt) return false
tx.update(sessionRef, { partFinishNotifiedAt: admin.firestore.FieldValue.serverTimestamp() })
return true
})
if (!claimed) return
const coupleDoc = await db.collection('couples').doc(coupleId).get()
const userIds = (coupleDoc.data()?.userIds ?? []) as string[]
const recipient = userIds.find((u) => u !== finisherUid)
if (!recipient) return
const finisher = await db.collection('users').doc(finisherUid).get()
const finisherName = 'Your partner' // displayName is E2EE; the app shows the real name in-app
const finisherAvatar = finisher.data()?.photoUrl as string | undefined
await notifyPartner(
db, messaging, recipient, finisherName, gameType,
'partner_completed_part', yourTurnBody(gameType), coupleId,
finisherAvatar, sessionId
)
}
export const onThisOrThatPartFinished = onDocumentWritten(
'couples/{coupleId}/this_or_that/{sessionId}',
(event) => handleGamePartFinished(event.params.coupleId, 'this_or_that', event.params.sessionId, event.data)
)
export const onWheelPartFinished = onDocumentWritten(
'couples/{coupleId}/wheel/{sessionId}',
(event) => handleGamePartFinished(event.params.coupleId, 'wheel', event.params.sessionId, event.data)
)
export const onHowWellPartFinished = onDocumentWritten(
'couples/{coupleId}/how_well/{sessionId}',
(event) => handleGamePartFinished(event.params.coupleId, 'how_well', event.params.sessionId, event.data)
)
export const onDesireSyncPartFinished = onDocumentWritten(
'couples/{coupleId}/desire_sync/{sessionId}',
(event) => handleGamePartFinished(event.params.coupleId, 'desire_sync', event.params.sessionId, event.data)
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
)
/**
* Per-game body for the "your turn" (partner_completed_part) push mirrors the client banner's
* YOUR_TURN copy in ui/components/GamePromptBanner.kt so foreground and background read the same.
*/
function yourTurnBody(gameType: string): string {
switch (gameType) {
case 'how_well': return 'Your turn — guess their answers'
case 'desire_sync': return 'Your turn — only mutual yeses ever show'
case 'this_or_that': return 'Your turn — see where you line up'
default: return 'Your turn — reveal how you line up'
}
}
/**
* Send notification to partner via FCM and write to notification_queue.
*/
async function notifyPartner(
db: admin.firestore.Firestore,
messaging: admin.messaging.Messaging,
partnerId: string,
partnerName: string,
gameType: string,
notificationType: string,
body: string,
coupleId: string,
senderAvatarUrl?: string,
sessionId?: string
): Promise<void> {
const title =
notificationType === 'partner_finished_game'
? `${partnerName} finished the game`
: notificationType === 'partner_completed_part'
? `${partnerName} finished their part`
: notificationType === 'partner_joined_game'
? `${partnerName} joined your game`
: `${partnerName} is playing`
// Write an in-app notification record for the partner
await db
.collection('users')
.doc(partnerId)
.collection('notification_queue')
.add({
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
type: notificationType,
title,
body,
read: false,
createdAt: admin.firestore.FieldValue.serverTimestamp(),
})
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
await sendPushToUser(db, messaging, partnerId, {
notification: { title, body },
// Put backgrounded notifications on the Games channel instead of the FCM fallback channel,
// so importance/sound and the per-category toggle apply. E-OBS.
android: { notification: { channelId: 'game_activity' } },
data: {
refactor(functions): B1 migrate 13 Firestore triggers to v2 + harden Migrate all Firestore triggers off the v1 API to firebase-functions/v2/firestore (onDocumentCreated/Updated/Written); context.params→event.params, snap→event.data, change.before/after→event.data.before/after. Region stays us-central1 (global option). Hardening folded in (all reuse in-repo patterns): - Adopt the shared sendPushToUser()/getUserTokens() helper in every trigger, removing ~7 copied token readers and the copied send/prune blocks. FCM tokens are no longer logged in plaintext anywhere here (redacted inside push.ts). - console.* → firebase-functions/logger (structured). - Idempotency: new claimOnce() (atomic create-if-absent marker under couples/{id}/notif_marks) dedupes at-least-once redelivery on the non-idempotent senders (onAnswerWritten/Revealed, onMessageWritten, onCoupleLeave, onEntitlementChanged, onDateReflectionWritten/Revealed, onDateHistoryCreated). Fail-open. onGameSessionUpdate/ onGamePartFinished/notifyOnDateMatch already had transactional claim-flags — preserved. - onRestoreRequested: the plan's "claim" is implemented as the existing 60s time-WINDOW (lastRestorePartnerAlertAt), not a permanent recipientUid marker — a permanent marker would wrongly block legitimate re-requests (restore docs are deleted+recreated by design). Faithful port of onGameSessionUpdate/onGamePartFinished (broad wildcard + allowlist kept); the trigger split and read reorder are deferred to B6 as separate commits. Build clean; 70 tests green (+ new idempotency.test.ts, push token/prune tests). Emulator discovery loads all triggers as v2 in us-central1. firebase-functions still v5.1.1 (v6 bump deferred to B6). dist rebuilt. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 23:46:17 -05:00
type: notificationType,
couple_id: coupleId,
game_type: gameType,
// The acting partner's display name (public; also in the title) so the in-app foreground
// banner can name them instead of a generic "Your partner".
sender_name: partnerName,
// Lets the client deep link a results-ready push to the per-session results/replay screen
// (a completed session isn't returned by getActiveSession). E-003 results-ready.
...(sessionId ? { game_session_id: sessionId } : {}),
...(senderAvatarUrl && senderAvatarUrl.length > 0
? { sender_avatar_url: senderAvatarUrl }
: {}),
},
})
}