deleteChat method

  1. @override
Future<void> deleteChat(
  1. ChatModel chat
)

Deletes the given chat.

chat: The chat to be deleted.

Implementation

@override
Future<void> deleteChat(ChatModel chat) async {
  var chatCollection = await _db
      .collection(_options.chatsMetaDataCollectionName)
      .doc(chat.id)
      .withConverter(
        fromFirestore: (snapshot, _) =>
            FirebaseChatDocument.fromJson(snapshot.data()!, snapshot.id),
        toFirestore: (chat, _) => chat.toJson(),
      )
      .get();

  var chatData = chatCollection.data();

  if (chatData != null) {
    for (var userId in chatData.users) {
      await _db
          .collection(_options.usersCollectionName)
          .doc(userId)
          .collection(_options.userChatsCollectionName)
          .doc(chat.id)
          .delete();
    }

    if (chat.id != null) {
      await _db
          .collection(_options.chatsCollectionName)
          .doc(chat.id)
          .delete();
      await _storage
          .ref(_options.chatsCollectionName)
          .child(chat.id!)
          .listAll()
          .then((value) {
        for (var element in value.items) {
          element.delete();
        }
      });
    }
  }
}