rejectCall static method

void rejectCall(
  1. String sessionID,
  2. String status, {
  3. required dynamic onSuccess(
    1. Call call
    )?,
  4. required dynamic onError(
    1. CometChatException excep
    ),
})

Rejects an incoming call.

Migration Note: Migrated from platform channels to native Dart implementation. Uses CallRepository for call rejection. Behavior and signature remain identical for backward compatibility.

Android Reference: CometChat.rejectCall(String sessionID, String status, CallbackListener<Call>)

Implementation

static void rejectCall(
  String sessionID,
  String status, {
  required Function(Call call)? onSuccess,
  required Function(CometChatException excep) onError,
}) async {
  try {
    // Validate parameters
    if (sessionID.isEmpty) {
      onError(
        CometChatException(
          "Error",
          "Session ID is null",
          "Session ID is null",
        ),
      );
      return;
    }
    if (status.isEmpty) {
      onError(
        CometChatException(
          "Error",
          "Call status is null",
          "Call status is null",
        ),
      );
      return;
    }

    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call native Dart call repository - returns raw response map
    final responseData = await sdk.calls.updateCallStatusRaw(
      sessionID,
      status,
    );

    // Parse using Call.fromMap() to match Android SDK's Call.fromJson()
    final publicCall = Call.fromMap(responseData);

    // Android: CallManager.rejectCall() -> destroy() drops the cached call.
    if (_initialCall?.sessionId == sessionID) _initialCall = null;

    // Call success callback
    if (onSuccess != null) onSuccess(publicCall);
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    onError(cometChatEx);
  } catch (e) {
    onError(
      CometChatException(
        ErrorCode.errorUnhandledException,
        e.toString(),
        e.toString(),
      ),
    );
  }
}