send function

Implementation

Future<MessageWithCID?> send(ChatSendOptions options) async {
  final computedOptions = computeOptions(options);

  computedOptions.account ??= getCachedWallet()?.address;
  computedOptions.signer ??= getCachedWallet()?.signer;
  computedOptions.pgpPrivateKey ??= getCachedWallet()?.pgpPrivateKey;

  /**
   * Validate Input Options
   */
  await validateSendOptions(computedOptions);

  final wallet = getWallet(
      address: computedOptions.account, signer: computedOptions.signer);
  final connectedUser = await getConnectedUserV2(
    wallet: wallet,
    privateKey: options.pgpPrivateKey,
  );

  final isGroup = !isValidETHAddress(computedOptions.to);
  final group = isGroup ? await getGroupInfo(chatId: computedOptions.to) : null;

  final conversationHashResponse = await conversationHash(
    conversationId: computedOptions.to,
    accountAddress: computedOptions.account!,
  );

  bool isIntent = !isGroup && conversationHashResponse == null;

  try {
    final senderAcount = await getUser(address: computedOptions.account!);

    if (senderAcount == null) {
      throw Exception('Cannot get sender account.');
    }

    String messageContent;
    if (computedOptions.messageType == MessageType.REPLY ||
        computedOptions.messageType == MessageType.COMPOSITE) {
      messageContent =
          'MessageType Not Supported by this sdk version. Plz upgrade !!!';
    } else {
      messageContent = computedOptions.messageObj?.content;
    }

    final sendMessagePayload = await sendMessagePayloadCore(
      senderConnectedUser: connectedUser,
      receiverAddress: computedOptions.to,
      senderPgpPrivateKey: computedOptions.pgpPrivateKey,
      senderAddress: computedOptions.account,
      messageType: computedOptions.messageType,
      messageContent: messageContent,
      messageObj: computedOptions.messageObj,
      group: group,
      isGroup: isGroup,
    );

    return sendMessageService(sendMessagePayload, isIntent);
  } catch (e) {
    log('[Push SDK] - API  - Error - API $e');
    rethrow;
  }
}