applyReactionEvent method Null safety

bool applyReactionEvent(
  1. ReactionEvent event
)

Applies ReactionEvent to this message.

Returns true if the given ReactionEvent applied to this message successfully, otherwise false.

Implementation

bool applyReactionEvent(ReactionEvent event) {
  if (event.messageId != messageId) {
    logger.i('message id is mismatched');
    return false;
  }

  final keys = reactions?.map((e) => e.key).toList();
  final existIndex = keys?.indexWhere((e) => e == event.key) ?? -1;
  if (existIndex != -1) {
    final reaction = reactions?[existIndex];
    if (reaction != null && reaction.merge(event)) {
      if (event.operation == ReactionEventAction.delete &&
          reaction.userIds.isEmpty) {
        reactions?.removeWhere((e) => e.key == event.key);
      }
      return true;
    } else {
      return false;
    }
  } else if (event.operation == ReactionEventAction.add) {
    final reaction = Reaction(
      key: event.key,
      userIds: [event.userId],
      updatedAt: event.updatedAt,
    );
    reactions?.add(reaction);
    return true;
  } else {
    return false;
  }
}