fetchOrCreateGroupCall method

Future<GroupCall?> fetchOrCreateGroupCall(
  1. String roomId
)

Implementation

Future<GroupCall?> fetchOrCreateGroupCall(String roomId) async {
  final groupCall = getGroupCallForRoom(roomId);
  final room = client.getRoomById(roomId);
  if (room == null) {
    Logs().w('Not found room id = $roomId');
    return null;
  }

  if (groupCall != null) {
    if (!room.canJoinGroupCall) {
      Logs().w('No permission to join group calls in room $roomId');
      return null;
    }
    return groupCall;
  }

  if (!room.groupCallsEnabled) {
    await room.enableGroupCalls();
  }

  if (room.canCreateGroupCall) {
    // The call doesn't exist, but we can create it

    final groupCall = await newGroupCall(
        roomId, GroupCallType.Video, GroupCallIntent.Prompt);
    if (groupCall != null) {
      await groupCall.sendMemberStateEvent();
    }
    return groupCall;
  }

  final completer = Completer<GroupCall?>();
  Timer? timer;
  final subscription = onIncomingGroupCall.stream.listen((GroupCall call) {
    if (call.room.id == roomId) {
      timer?.cancel();
      completer.complete(call);
    }
  });

  timer = Timer(Duration(seconds: 30), () {
    subscription.cancel();
    completer.completeError('timeout');
  });

  return completer.future;
}