addReaction method
Future<void>
addReaction({
- required PeamanReaction reaction,
- bool updateParentReactionsCount = true,
- bool updateUserReactionsCount = true,
- dynamic onSuccess()?,
- dynamic onError(
- dynamic
Implementation
Future<void> addReaction({
required final PeamanReaction reaction,
final bool updateParentReactionsCount = true,
final bool updateUserReactionsCount = true,
final Function(PeamanReaction)? onSuccess,
final Function(dynamic)? onError,
}) async {
try {
if (reaction.feedId == null) throw Exception("feedId can't be null");
if (reaction.ownerId == null) throw Exception("feedId can't be null");
if (reaction.parentId == null) throw Exception("parentId can't be null");
if (reaction.parentOwnerId == null)
throw Exception("parentOwnerId can't be null");
final _currentMillis = DateTime.now().millisecondsSinceEpoch;
final _reactionsRef =
PeamanReferenceHelper.reactionsCol(feedId: reaction.feedId!)
.doc(reaction.id);
final _reactedFeedRef =
PeamanReferenceHelper.reactedFeedsCol(uid: reaction.ownerId!)
.doc(reaction.feedId);
final _feedReactorRef =
PeamanReferenceHelper.feedReactorsCol(feedId: reaction.feedId!)
.doc(reaction.ownerId);
final _reaction = reaction.copyWith(
id: _reactionsRef.id,
createdAt: reaction.createdAt ?? _currentMillis,
updatedAt: reaction.updatedAt ?? _currentMillis,
);
final _reactedFeed = PeamanReactedFeed(
id: reaction.feedId,
createdAt: _currentMillis,
updatedAt: _currentMillis,
);
final _feedReactor = PeamanFeedReactor(
uid: reaction.ownerId,
createdAt: _currentMillis,
updatedAt: _currentMillis,
);
final _futures = <Future>[];
_futures.add(_reactionsRef.set(_reaction.toJson()));
if (_reaction.parent == PeamanReactionParent.feed) {
if (updateParentReactionsCount) {
final _feedPropertyFuture = updateFeedPropertiesCount(
feedId: _reaction.feedId!,
reactionsCount: 1,
);
_futures.add(_feedPropertyFuture);
}
if (updateUserReactionsCount) {
final _userPropertyFuture = PUserProvider.updateUserPropertiesCount(
uid: _reaction.parentOwnerId!,
reactionsReceivedFromFeeds: 1,
);
_futures.add(_userPropertyFuture);
}
final _reactedFeedFuture = _reactedFeedRef.set(_reactedFeed.toJson());
final _feedReactorFuture = _feedReactorRef.set(_feedReactor.toJson());
_futures.add(_reactedFeedFuture);
_futures.add(_feedReactorFuture);
print('Success: Adding reaction to feed ${reaction.feedId}');
} else {
if (updateParentReactionsCount) {
final _commentPropertyFuture = updateCommentPropertiesCount(
feedId: _reaction.feedId!,
commentId: _reaction.parentId!,
reactionsCount: 1,
);
_futures.add(_commentPropertyFuture);
}
if (updateUserReactionsCount) {
final _userPropertyFuture = PUserProvider.updateUserPropertiesCount(
uid: _reaction.parentOwnerId!,
reactionsReceivedFromFeeds: 1,
);
_futures.add(_userPropertyFuture);
}
print('Success: Adding reaction to comment ${_reaction.parentId}');
}
await Future.wait(_futures);
onSuccess?.call(_reaction);
} catch (e) {
print(e);
print('Error!!!: Adding reaction');
onError?.call(e);
}
}