Critical: - Fix MXC URI resolution: all avatars/images now resolve mxc:// to HTTP - Sync persistence: only write changed rooms, batch message upserts - lastActivityAt uses room.lastEvent.originServerTs, not creation time High: - Shared MatrixAvatar widget replaces 6 duplicate implementations - CallScreen decodes roomId before LiveKit JWT fetch - Decline button actually dismisses incoming call overlay - EventTypes constants replace raw string literals - LiveKitService uses lazy auth reads, onDispose disconnects Medium: - CallController is keepAlive with timer/room cleanup - authRepository is keepAlive (used from keepAlive notifier) - StreamController not closed in stopListening (crash fix) - Index on messages.roomId for query performance - 400ms debounce on user search - Static DateFormat in MessageBubble - Hardcoded strings replaced with AppConfig refs - Duplicate isDirectMessage field removed from RoomModel - E2EE profile claim corrected to Phase 3 Shared utilities: - lib/shared/widgets/matrix_avatar.dart - lib/shared/utils/mxc_url.dart - lib/shared/utils/room_preview.dart - lib/shared/utils/matrix_id.dart rawJson column removed (unused, caused main-thread jsonEncode) Schema migrated to v2 with roomId index. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
127 lines
4.0 KiB
Dart
127 lines
4.0 KiB
Dart
// Version: 1.2.0 | Created: 2026-04-01 | Updated: 2026-04-02
|
|
// MatrixRTC repository — handles outgoing call invites and detects incoming
|
|
// calls via m.call.invite events per MSC4143 (MatrixRTC spec).
|
|
//
|
|
// Incoming calls are surfaced via the incomingCallStream so the
|
|
// IncomingCallOverlay can react to them.
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../../../core/auth/auth_notifier.dart';
|
|
import '../../../core/auth/auth_state.dart';
|
|
import '../../../core/network/matrix_client.dart';
|
|
import '../../../shared/utils/matrix_id.dart';
|
|
import '../../../shared/utils/mxc_url.dart';
|
|
import '../domain/incoming_call.dart';
|
|
|
|
part 'matrixrtc_repository.g.dart';
|
|
|
|
/// Repository for sending and receiving MatrixRTC call signalling events.
|
|
class MatrixRtcRepository {
|
|
MatrixRtcRepository({required Client client, required String? myUserId})
|
|
: _client = client,
|
|
_myUserId = myUserId;
|
|
|
|
final Client _client;
|
|
final String? _myUserId;
|
|
|
|
final _incomingCallController = StreamController<IncomingCall>.broadcast();
|
|
|
|
/// Emits whenever an incoming call invite arrives for the local user.
|
|
Stream<IncomingCall> get incomingCallStream => _incomingCallController.stream;
|
|
|
|
StreamSubscription<EventUpdate>? _eventSubscription;
|
|
|
|
/// Begin listening for incoming m.call.invite events.
|
|
void startListening() {
|
|
_eventSubscription?.cancel();
|
|
_eventSubscription = _client.onEvent.stream.listen(_onEvent);
|
|
}
|
|
|
|
void stopListening() {
|
|
_eventSubscription?.cancel();
|
|
_eventSubscription = null;
|
|
}
|
|
|
|
/// Close the stream controller. Called from ref.onDispose only.
|
|
void dispose() {
|
|
stopListening();
|
|
_incomingCallController.close();
|
|
}
|
|
|
|
/// Send a call invite to [roomId] to start a voice or video call.
|
|
///
|
|
/// Uses the standard m.call.invite event type. The room's other participants
|
|
/// will receive this via their sync stream.
|
|
Future<void> sendCallInvite({
|
|
required String roomId,
|
|
required bool isVideo,
|
|
}) async {
|
|
final room = _client.getRoomById(roomId);
|
|
if (room == null) return;
|
|
|
|
final callId = 'call_${DateTime.now().millisecondsSinceEpoch}';
|
|
|
|
await room.sendEvent({
|
|
'msgtype': EventTypes.CallInvite,
|
|
'call_id': callId,
|
|
'lifetime': 60000, // 60 seconds before invite expires
|
|
'offer': {
|
|
'type': 'offer',
|
|
'sdp': '', // SDP is populated by LiveKit once connected
|
|
},
|
|
'version': '1',
|
|
'invitee': null, // null = invite entire room
|
|
'm.intentional_mentions': {'user_ids': [], 'room': false},
|
|
}, type: EventTypes.CallInvite);
|
|
}
|
|
|
|
void _onEvent(EventUpdate update) {
|
|
if (update.type != EventUpdateType.timeline) return;
|
|
if (update.content['type'] != EventTypes.CallInvite) return;
|
|
|
|
final senderId = update.content['sender'] as String?;
|
|
// Ignore our own invites.
|
|
if (senderId == _myUserId) return;
|
|
|
|
final roomId = update.roomID;
|
|
final content = update.content['content'] as Map<String, dynamic>?;
|
|
if (content == null) return;
|
|
|
|
final callId = content['call_id'] as String?;
|
|
if (callId == null) return;
|
|
|
|
final room = _client.getRoomById(roomId);
|
|
final senderProfile = room?.unsafeGetUserFromMemoryOrFallback(
|
|
senderId ?? '',
|
|
);
|
|
|
|
_incomingCallController.add(
|
|
IncomingCall(
|
|
callId: callId,
|
|
roomId: roomId,
|
|
callerId: senderId ?? '',
|
|
callerDisplayName:
|
|
senderProfile?.displayName ?? senderId?.matrixLocalpart ?? '',
|
|
callerAvatarUrl: resolveMxcUrl(_client, senderProfile?.avatarUrl),
|
|
isVideo: (content['offer'] != null),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@Riverpod(keepAlive: true)
|
|
MatrixRtcRepository matrixRtcRepository(Ref ref) {
|
|
final client = ref.watch(matrixClientProvider);
|
|
final authState = ref.watch(authProvider);
|
|
final myUserId = authState is AuthAuthenticated ? authState.userId : null;
|
|
|
|
final repo = MatrixRtcRepository(client: client, myUserId: myUserId);
|
|
repo.startListening();
|
|
ref.onDispose(repo.dispose);
|
|
return repo;
|
|
}
|