deleteMessages method

Future<bool> deleteMessages()

deletes self owned messages only

Implementation

Future<bool> deleteMessages() async {
  var key = AtKey()
    ..key = storageKey + (isGroupChat ? groupChatId! : '') + (chatWithAtSign ?? ' ').substring(1)
    ..sharedBy = currentAtSign!
    ..sharedWith = chatWithAtSign
    ..metadata = Metadata();
  key.metadata.ccd = true;
  try {
    for (var i = 0; i < chatHistoryMessages.length; i++) {
      var message = Message.fromJson(chatHistoryMessages[i]);
      if (message.contentType == MessageContentType.image) {
        // removing 'AtKey{' and ending '}'
        var savedKey = message.message?.substring(6, (message.message?.length ?? 1) - 1);

        var key = constructKey(savedKey ?? '');
        await atClientManager.atClient.delete(key);
      }
    }
    chatHistoryMessages = [];
    var result = await atClientManager.atClient.put(key, json.encode(chatHistoryMessages));
    await getChatHistory();
    return result;
  } catch (e) {
    return false;
  }
}