join method

Future<void> join(
  1. ChatId chatId, {
  2. bool withAudio = true,
  3. bool withVideo = false,
  4. bool withScreen = false,
})

Joins an OngoingCall identified by the given chatId.

Implementation

Future<void> join(
  ChatId chatId, {
  bool withAudio = true,
  bool withVideo = false,
  bool withScreen = false,
}) async {
  Log.debug(
    'join($chatId, $withAudio, $withVideo, $withScreen)',
    '$runtimeType',
  );

  final WebStoredCall? webStored = WebUtils.getCall(chatId);
  final ChatCallDeviceId? webDevice = webStored?.deviceId;

  if (webDevice != null && !WebUtils.isPopup) {
    // Call seems to already exist in the Web, thus try to leave and remove
    // the existing one.
    WebUtils.removeCall(chatId);
    await _callsRepo.leave(chatId, webDevice);
  }

  try {
    final FutureOr<RxChat?> chatOrFuture = _chatService.get(chatId);
    final RxChat? chat = chatOrFuture is RxChat?
        ? chatOrFuture
        : await chatOrFuture;

    final ChatCall? chatCall = chat?.chat.value.ongoingCall;

    Rx<OngoingCall>? call;

    try {
      call = await _callsRepo.join(
        chatId,
        chatCall,
        withAudio: withAudio,
        withVideo: withVideo,
        withScreen: withScreen,
      );
    } on CallAlreadyJoinedException catch (e) {
      await _callsRepo.leave(chatId, e.deviceId);
      call = await _callsRepo.join(
        chatId,
        chatCall,
        withAudio: withAudio,
        withVideo: withVideo,
        withScreen: withScreen,
      );
    }

    if (isClosed) {
      call?.value.dispose();
    } else {
      call?.value.connect(this);
    }
  } catch (e) {
    // If any other error occurs, it's guaranteed that the broken call will be
    // removed.
    _callsRepo.remove(chatId);
    rethrow;
  }
}