reactToPost method

  1. @override
Future<TimelinePost> reactToPost(
  1. TimelinePost post,
  2. TimelinePostReaction reaction, {
  3. Uint8List? image,
})

Implementation

@override
Future<TimelinePost> reactToPost(
  TimelinePost post,
  TimelinePostReaction reaction, {
  Uint8List? image,
}) async {
  var reactionId = const Uuid().v4();
  // also fetch the user information and add it to the reaction
  var user = await _userService.getUser(reaction.creatorId);
  var updatedReaction = reaction.copyWith(id: reactionId, creator: user);
  if (image != null) {
    var imageRef = _storage
        .ref()
        .child('${_options.timelineCollectionName}/${post.id}/$reactionId}');
    var result = await imageRef.putData(image);
    var imageUrl = await result.ref.getDownloadURL();
    updatedReaction = updatedReaction.copyWith(imageUrl: imageUrl);
  }

  var updatedPost = post.copyWith(
    reaction: post.reaction + 1,
    reactions: post.reactions?..add(updatedReaction),
  );

  var postRef = _db.collection(_options.timelineCollectionName).doc(post.id);
  await postRef.update({
    'reaction': FieldValue.increment(1),
    'reactions': FieldValue.arrayUnion([updatedReaction.toJson()]),
  });
  posts = posts
      .map(
        (p) => p.id == post.id ? updatedPost : p,
      )
      .toList();
  notifyListeners();
  return updatedPost;
}