fetch method
Retrieve all matching messages based on Channel.history description.
- if to is
null
and from innull
, then it will work on all messages. - if to is
null
and from is defined, then it will work on all messages since from. - if to is defined and from is
null
, then it will work on all messages up to to. - if both to and from are defined, then it will work on messages that were sent between from and to.
Implementation
Future<void> fetch() async {
var _cursor = from;
_messages = [];
do {
var result = await defaultFlow(
keyset: _keyset,
core: _core,
params: FetchHistoryParams(_keyset, _channel.name,
reverse: true,
count: 100,
start: _cursor,
end: to,
includeToken: true),
serialize: (object, [_]) => FetchHistoryResult.fromJson(object));
_cursor = result.endTimetoken;
_messages.addAll(await Future.wait(result.messages.map((message) async {
PubNubException? error;
if (_keyset.cipherKey != null || _core.crypto is CryptoModule) {
try {
if (!(message['message'] is String)) {
throw FormatException('not a base64 string.');
}
message['message'] = _keyset.cipherKey ==
_core.keysets.defaultKeyset.cipherKey
? await _core.parser.decode(utf8.decode(_core.crypto.decrypt(
base64.decode(message['message'] as String).toList())))
: await _core.parser.decode(utf8.decode(_core.crypto
.decryptWithKey(_keyset.cipherKey!,
base64.decode(message['message'] as String).toList())));
} on CryptoException catch (e) {
error = e;
} on FormatException catch (e) {
error = PubNubException(
'Can not decrypt the message payload. Please check keyset or crypto configuration. ${e.message}');
}
}
return BaseMessage(
publishedAt: Timetoken(BigInt.from(message['timetoken'])),
content: message['message'],
originalMessage: message,
error: error);
})));
} while (_cursor.value != BigInt.from(0));
}