chahinebrini 822053e11e feat(calls): CallKit/ConnectionService + VoIP-PushKit + EU-Ringback
Caller/Callee UX:
- lib/ringback.ts + assets/sounds/ringback_eu.mp3 (EU 425Hz Festnetz-Tone)
- stores/call.ts: stopRingback bei connected, hangup-reasons, logCallToChat fix
- locales: 'Wird angerufen…' statt 'Ruft an…'

CallKit (iOS) + ConnectionService (Android):
- lib/callkit.ts: setupCallKeep, displayIncomingCall, startOutgoingCall, reportConnected/Ended (appName 'ReBreak-Audio', includesCallsInRecents=false für DSGVO/DiGA)
- hooks/useCallKeepEvents.ts: native answer/end/mute → useCallStore-Actions
- stores/call.ts: CallKit-Aufrufe an allen lifecycle-Punkten
- app.config.ts: @config-plugins/react-native-callkeep + UIBackgroundModes voip/audio + Android-Telecom-Perms

VoIP-PushKit Backend:
- services/voip-push.ts: @parse/node-apn Provider mit .p12 (Topic org.rebreak.app.voip)
- services/push.ts sendCallRingPush: feuert beide Pfade (VoIP iOS + Expo Android/Fallback)
- prisma: push_tokens.voip_token Column + Migration 20260604
- api/users/me/push-token: optional voipToken im Body
- Env (Infisical): APNS_VOIP_P12_PATH/PASSWORD/TOPIC/PRODUCTION

Push-tap routing + cold-start handling:
- app/_layout.tsx: type:'call' Push → useCallStore.receiveIncoming + /call

Docs: ops/CALLKIT_SETUP.md (Apple-Portal-Steps für VoIP-Cert)
2026-06-04 09:27:13 +02:00

179 lines
6.3 KiB
TypeScript

import { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, Alert, StyleSheet } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
import { SafeAreaView } from 'react-native-safe-area-context';
import { Ionicons } from '@expo/vector-icons';
import { useRouter } from 'expo-router';
import { useTranslation } from 'react-i18next';
import { useCallStore } from '../stores/call';
import { UserAvatar } from '../components/UserAvatar';
function fmtDuration(ms: number) {
const s = Math.floor(ms / 1000);
const m = Math.floor(s / 60);
return `${m}:${String(s % 60).padStart(2, '0')}`;
}
export default function CallScreen() {
const { t } = useTranslation();
const router = useRouter();
const status = useCallStore((s) => s.status);
const peer = useCallStore((s) => s.peer);
const muted = useCallStore((s) => s.muted);
const speaker = useCallStore((s) => s.speaker);
const startedAt = useCallStore((s) => s.startedAt);
const endReason = useCallStore((s) => s.endReason);
const acceptCall = useCallStore((s) => s.acceptCall);
const declineCall = useCallStore((s) => s.declineCall);
const hangup = useCallStore((s) => s.hangup);
const toggleMute = useCallStore((s) => s.toggleMute);
const toggleSpeaker = useCallStore((s) => s.toggleSpeaker);
const clear = useCallStore((s) => s._clear);
const [elapsed, setElapsed] = useState(0);
// Kein aktiver Call → Screen schließen.
useEffect(() => {
if (status === 'idle') router.back();
}, [status, router]);
// Call beendet → kurz "beendet" zeigen, dann schließen + aufräumen.
useEffect(() => {
if (status !== 'ended') return;
const tm = setTimeout(() => {
clear();
router.back();
}, 1300);
return () => clearTimeout(tm);
}, [status, clear, router]);
// Gesprächsdauer-Timer.
useEffect(() => {
if (status !== 'connected' || !startedAt) return;
const id = setInterval(() => setElapsed(Date.now() - startedAt), 1000);
return () => clearInterval(id);
}, [status, startedAt]);
async function onAccept() {
try {
await acceptCall();
} catch (e: any) {
if (e?.message === 'webrtc_unavailable') {
Alert.alert(t('call.title'), t('call.needs_rebuild'));
}
hangup('failed');
}
}
const subtitle =
status === 'outgoing'
? t('call.calling')
: status === 'incoming'
? t('call.incoming')
: status === 'connecting'
? t('call.connecting')
: status === 'connected'
? fmtDuration(elapsed)
: status === 'ended'
? endReason === 'declined'
? t('call.declined')
: endReason === 'unanswered'
? t('call.no_answer')
: endReason === 'failed'
? t('call.failed')
: t('call.ended')
: '';
return (
<View style={{ flex: 1 }}>
<LinearGradient colors={['#0f172a', '#1e3a34', '#0f172a']} style={StyleSheet.absoluteFill} />
<SafeAreaView style={{ flex: 1, justifyContent: 'space-between', alignItems: 'center' }}>
{/* Oben: Avatar + Name + Status */}
<View style={{ alignItems: 'center', marginTop: 72, gap: 18 }}>
<UserAvatar
userId={peer?.id ?? null}
avatar={peer?.avatar ?? null}
nickname={peer?.nickname ?? '?'}
size="xl"
/>
<Text style={{ color: '#fff', fontSize: 26, fontFamily: 'Nunito_700Bold' }} numberOfLines={1}>
{peer?.nickname ?? '…'}
</Text>
<Text style={{ color: 'rgba(255,255,255,0.7)', fontSize: 16, fontFamily: 'Nunito_600SemiBold', fontVariant: ['tabular-nums'] }}>
{subtitle}
</Text>
</View>
{/* Unten: Aktions-Buttons */}
<View style={{ marginBottom: 56, width: '100%', alignItems: 'center' }}>
{status === 'incoming' ? (
<View style={{ flexDirection: 'row', justifyContent: 'space-evenly', width: '100%' }}>
<CircleBtn color="#ef4444" icon="call" rotate onPress={declineCall} label={t('call.decline')} />
<CircleBtn color="#22c55e" icon="call" onPress={onAccept} label={t('call.accept')} />
</View>
) : status === 'ended' ? null : (
<View style={{ flexDirection: 'row', justifyContent: 'center', alignItems: 'center', gap: 28 }}>
{(status === 'connected' || status === 'connecting') && (
<CircleBtn
color={muted ? '#fff' : 'rgba(255,255,255,0.18)'}
iconColor={muted ? '#0f172a' : '#fff'}
icon={muted ? 'mic-off' : 'mic'}
onPress={toggleMute}
label={muted ? t('call.unmute') : t('call.mute')}
/>
)}
{(status === 'connected' || status === 'connecting' || status === 'outgoing') && (
<CircleBtn
color={speaker ? '#fff' : 'rgba(255,255,255,0.18)'}
iconColor={speaker ? '#0f172a' : '#fff'}
icon={speaker ? 'volume-high' : 'volume-medium'}
onPress={toggleSpeaker}
label={speaker ? t('call.speaker_on') : t('call.speaker_off')}
/>
)}
<CircleBtn color="#ef4444" icon="call" rotate onPress={() => hangup('ended')} label={t('call.hang_up')} />
</View>
)}
</View>
</SafeAreaView>
</View>
);
}
function CircleBtn({
color,
iconColor = '#fff',
icon,
rotate,
onPress,
label,
}: {
color: string;
iconColor?: string;
icon: keyof typeof Ionicons.glyphMap;
rotate?: boolean;
onPress: () => void;
label: string;
}) {
return (
<View style={{ alignItems: 'center', gap: 8 }}>
<TouchableOpacity
activeOpacity={0.8}
onPress={onPress}
style={{
width: 68,
height: 68,
borderRadius: 34,
backgroundColor: color,
alignItems: 'center',
justifyContent: 'center',
}}
>
<Ionicons name={icon} size={28} color={iconColor} style={rotate ? { transform: [{ rotate: '135deg' }] } : undefined} />
</TouchableOpacity>
<Text style={{ color: 'rgba(255,255,255,0.8)', fontSize: 12, fontFamily: 'Nunito_600SemiBold' }}>{label}</Text>
</View>
);
}