addDeprecatedInfoToMessages function

List<Message> addDeprecatedInfoToMessages(
  1. List<Message> chats
)

Implementation

List<Message> addDeprecatedInfoToMessages(List<Message> chats) {
  Map<String, String> latestDIDs = {};

  for (var chat in chats) {
    if (isValidCAIP10NFTAddress(chat.fromDID)) {
      String didWithoutTimestamp =
          chat.fromDID.split(':').sublist(0, 5).join(':');
      String timestamp = chat.fromDID.split(':')[5];

      if (!latestDIDs.containsKey(didWithoutTimestamp) ||
          timestamp.compareTo(
                  latestDIDs[didWithoutTimestamp].toString().split(':')[5]) >
              0) {
        latestDIDs[didWithoutTimestamp] = chat.fromDID;
      }
    }
  }

  for (var chat in chats) {
    if (isValidCAIP10NFTAddress(chat.fromDID)) {
      String didWithoutTimestamp =
          chat.fromDID.split(':').sublist(0, 5).join(':');

      if (latestDIDs[didWithoutTimestamp] != chat.fromDID) {
        chat.deprecated = true;
        chat.deprecatedCode = 'NFT Owner Changed';
      }
    }
  }

  return chats;
}