getChatHistory method

Future<void> getChatHistory({
  1. String? atsign,
})

Implementation

Future<void> getChatHistory({String? atsign}) async {
  try {
    chatHistory = [];
    var key = AtKey()
      ..key = storageKey + (isGroupChat ? groupChatId! : '') + (chatWithAtSign ?? ' ').substring(1)
      ..sharedBy = currentAtSign!
      ..sharedWith = chatWithAtSign
      ..metadata = Metadata();
    key.metadata.ccd = true;
    var keyValue = await atClientManager.atClient.get(key).catchError((e) {
      return AtValue();
    });
    // ignore: unnecessary_null_comparison
    if (keyValue != null && keyValue.value != null) {
      chatHistoryMessages = json.decode((keyValue.value) as String) as List;
    } else {
      chatHistoryMessages = [];
    }
    // get received messages
    key.key =
        storageKey + (isGroupChat ? groupChatId! : '') + (chatWithAtSign != null ? currentAtSign! : ' ').substring(1);
    key.sharedBy = chatWithAtSign;
    key.sharedWith = currentAtSign!;
    keyValue = await atClientManager.atClient.get(key).catchError((e) {
      return AtValue();
    });
    if (keyValue.value != null) {
      chatHistoryMessagesOther = json.decode((keyValue.value) as String) as List;
    } else {
      chatHistoryMessagesOther = [];
    }
    chatHistory = await interleave(chatHistoryMessages, chatHistoryMessagesOther);
    chatSink.add(chatHistory);
  } catch (error) {
    chatSink.add(chatHistory);
  }
}