followBackUser method

Future<void> followBackUser({
  1. required String uid,
  2. required String friendId,
})

Implementation

Future<void> followBackUser({
  required final String uid,
  required final String friendId,
}) async {
  try {
    final _currentMillis = DateTime.now().millisecondsSinceEpoch;

    final _friendRef = PeamanReferenceHelper.usersCol.doc(friendId);
    final _userRef = PeamanReferenceHelper.usersCol.doc(uid);
    final _receivedRef = PeamanReferenceHelper.receivedFollowRequestsCol(
      uid: uid,
    ).doc(friendId);
    final _sentRef = PeamanReferenceHelper.sentFollowRequestsCol(
      uid: friendId,
    ).doc(uid);

    final _friendFollowersRef = PeamanReferenceHelper.userFollowersCol(
      uid: friendId,
    ).doc(uid);
    final _userFollowingRef = PeamanReferenceHelper.userFollowingsCol(
      uid: uid,
    ).doc(friendId);

    final _futures = <Future>[];

    final _friendFollowersFuture = _friendFollowersRef.set({
      'uid': uid,
      'created_at': _currentMillis,
      'updated_at': _currentMillis,
    });
    _futures.add(_friendFollowersFuture);

    final _userFollowingFuture = _userFollowingRef.set({
      'uid': friendId,
      'created_at': _currentMillis,
      'updated_at': _currentMillis,
    });
    _futures.add(_userFollowingFuture);

    final _friendUpdateFuture = _friendRef.update({
      'followers': FieldValue.increment(1),
    });
    _futures.add(_friendUpdateFuture);

    final _userUpdateFuture = _userRef.update({
      'following': FieldValue.increment(1),
    });
    _futures.add(_userUpdateFuture);

    final _requestFuture = _receivedRef.delete();
    _futures.add(_requestFuture);

    final _sentFuture = _sentRef.delete();
    _futures.add(_sentFuture);

    await Future.wait(_futures);
    print('Success: Following back $friendId');
  } catch (e) {
    print(e);
    print('Error!!!: Following back');
  }
}