import type { PresenceState } from '@chat-app/shared/supabase'; export const PRESENCE_HEARTBEAT_MS = 30_000; export const PRESENCE_DEVICE_STALE_MS = 90_000; export function createPeerPresenceChannelName(userId: string, subscriptionId: string): string { return 'peer-presence:' + userId + ':' + subscriptionId; } export function getEffectivePresenceState( profileState: PresenceState, latestDeviceSeenAt: string | null, nowMs = Date.now(), ): PresenceState { if (profileState === 'offline' || profileState === 'invisible') return profileState; if (!latestDeviceSeenAt) return 'offline'; const seenMs = Date.parse(latestDeviceSeenAt); if (!Number.isFinite(seenMs)) return 'offline'; return nowMs - seenMs <= PRESENCE_DEVICE_STALE_MS ? profileState : 'offline'; }