removeComment method

Future<void> removeComment({
  1. required String feedId,
  2. required String commentId,
  3. required String ownerId,
  4. required String parentId,
  5. required String parentOwnerId,
  6. dynamic onSuccess(
    1. String
    )?,
  7. dynamic onError(
    1. dynamic
    )?,
})

Implementation

Future<void> removeComment({
  required final String feedId,
  required final String commentId,
  required final String ownerId,
  required final String parentId,
  required final String parentOwnerId,
  final Function(String)? onSuccess,
  final Function(dynamic)? onError,
}) async {
  try {
    final _commentRef =
        PeamanReferenceHelper.commentsCol(feedId: feedId).doc(commentId);
    final _commentedFeedRef =
        PeamanReferenceHelper.commentedFeedsCol(uid: ownerId).doc(feedId);
    final _repliedFeedRef =
        PeamanReferenceHelper.repliedFeedsCol(uid: ownerId).doc(feedId);
    final _feedCommenterRef =
        PeamanReferenceHelper.feedCommentersCol(feedId: feedId).doc(ownerId);
    final _feedReplierRef =
        PeamanReferenceHelper.feedRepliersCol(feedId: feedId).doc(ownerId);

    final _futures = <Future>[];

    await _commentRef.delete();

    if (feedId == parentId) {
      final _feedPropertiesFuture = updateFeedPropertiesCount(
        feedId: feedId,
        commentsCount: -1,
      );

      final _userPropertyFuture = PUserProvider.updateUserPropertiesCount(
        uid: parentOwnerId,
        commentsReceivedFromFeeds: -1,
      );

      _futures.add(_feedPropertiesFuture);
      _futures.add(_userPropertyFuture);

      final _commentsSnap = await getCommentsByOwnerId(
        ownerId: ownerId,
        feedId: feedId,
        parent: PeamanCommentParent.feed,
        query: (ref) => ref.limit(1),
      );

      if (_commentsSnap.isEmpty) {
        final _commentedFeedFuture = _commentedFeedRef.delete();
        final _feedCommenterFuture = _feedCommenterRef.delete();
        _futures.add(_commentedFeedFuture);
        _futures.add(_feedCommenterFuture);
      }

      print('Success: Removing comment from feed $feedId');
    } else {
      final _commentPropertiesFuture = updateCommentPropertiesCount(
        feedId: feedId,
        commentId: parentId,
        repliesCount: -1,
      );

      final _feedPropertiesFuture = updateFeedPropertiesCount(
        feedId: feedId,
        repliesCount: -1,
      );

      final _userPropertyFuture = PUserProvider.updateUserPropertiesCount(
        uid: parentOwnerId,
        repliesReceivedFromFeeds: -1,
      );

      _futures.add(_commentPropertiesFuture);
      _futures.add(_userPropertyFuture);
      _futures.add(_feedPropertiesFuture);

      final _repliesSnap = await getCommentsByOwnerId(
        ownerId: ownerId,
        feedId: feedId,
        parent: PeamanCommentParent.comment,
        query: (ref) => ref.limit(1),
      );

      if (_repliesSnap.isEmpty) {
        final _repliedFeedFuture = _repliedFeedRef.delete();
        final _feedReplierFuture = _feedReplierRef.delete();
        _futures.add(_repliedFeedFuture);
        _futures.add(_feedReplierFuture);
      }

      print('Success: Removing comment from comment $parentId');
    }

    await Future.wait(_futures);
    onSuccess?.call(commentId);
  } catch (e) {
    print(e);
    print('Error!!!: Removing comment');
    onError?.call(e);
  }
}