history function

Future<List<Message>?> history({
  1. required String threadhash,
  2. String? accountAddress,
  3. int limit = FetchLimit.DEFAULT,
  4. String? pgpPrivateKey,
  5. bool toDecrypt = false,
})

Implementation

Future<List<Message>?> history({
  required String threadhash,
  String? accountAddress,
  int limit = FetchLimit.DEFAULT,
  String? pgpPrivateKey,
  bool toDecrypt = false,
}) async {
  accountAddress ??= getCachedWallet()?.address;
  if (accountAddress == null) {
    throw Exception('Account address is required.');
  }

  if (!isValidETHAddress(accountAddress)) {
    throw Exception('Invalid address $accountAddress');
  }

  pgpPrivateKey ??= getCachedWallet()?.pgpPrivateKey;
  if (pgpPrivateKey == null) {
    throw Exception('Private Key is required.');
  }

  try {
    if (limit < FetchLimit.MIN || limit > FetchLimit.MAX) {
      if (limit < FetchLimit.MIN) {
        throw Exception('Limit must be more than equal to ${FetchLimit.MIN}');
      } else {
        throw Exception('Limit must be less than equal to ${FetchLimit.MAX}');
      }
    }

    final List<Message>? messages =
        await getMessagesService(threadhash: threadhash, limit: limit);

    if (messages == null) {
      return null;
    }

    // final updatedMessages = addDeprecatedInfoToMessages(messages);
    final connectedUser =
        await getUser(address: pCAIP10ToWallet(accountAddress));

    if (toDecrypt) {
      return await decryptConversation(
        messages: messages,
        connectedUser: connectedUser,
        pgpPrivateKey: pgpPrivateKey,
      );
    }
    return messages;
  } catch (err) {
    log('[Push SDK] - API history - : $err');
    throw Exception('[Push SDK] - API history - : $err');
  }
}