add method

  1. @override
Future<Rx<OngoingCall>?> add(
  1. ChatCall call, {
  2. bool dontAddIfAccounted = false,
})
override

Adds the provided ChatCall to the calls, if not already.

If dontAddIfAccounted is true, then this call won't be added if it was added by anything earlier, even if it's already gone.

Implementation

@override
Future<Rx<OngoingCall>?> add(
  ChatCall call, {
  bool dontAddIfAccounted = false,
}) async {
  Log.debug('add($call)', '$runtimeType');

  Rx<OngoingCall>? ongoing = calls[call.chatId];

  // If we're already in this call or call already exists, then ignore it.
  if ((ongoing != null &&
          ongoing.value.state.value != OngoingCallState.ended) ||
      call.members.any((e) => e.user.id == me)) {
    return ongoing;
  }

  // If [ChatCall] is already finished, then there's no reason to add it.
  if (call.finishReason != null) {
    return null;
  }

  // If dialing has already ended, then notification shouldn't be displayed.
  if (call.dialed == null) {
    return null;
  } else if (call.dialed is ChatMembersDialedConcrete) {
    if ((call.dialed as ChatMembersDialedConcrete).members.none(
      (e) => e.user.id == me,
    )) {
      return null;
    }
  }

  if (ongoing == null) {
    final DateTime? accountedAt = _accountedCalls[call.id];
    if (accountedAt != null) {
      if (dontAddIfAccounted) {
        return null;
      }

      if (accountedAt.difference(DateTime.now()).abs() < _accountedTimeout) {
        // This call is already considered reported, thus don't add it again.
        return null;
      }
    }

    _accountedCalls[call.id] = DateTime.now();

    ongoing = Rx<OngoingCall>(
      OngoingCall(
        call.chatId,
        me,
        call: call,
        withAudio: false,
        withVideo: false,
        withScreen: false,
        mediaSettings: media.value,
        creds: await getCredentials(call.id),
      ),
    );
    calls[call.chatId] = ongoing;
  } else {
    ongoing.value.call.value = call;
  }

  return ongoing;
}