add method

  1. @override
Future<Rx<OngoingCall>?> add(
  1. ChatCall call
)
override

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

Implementation

@override
Future<Rx<OngoingCall>?> add(ChatCall call) 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) {
    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;
}