deleteMyReaction method
Deletes all the current user's reactions from the message.
Optionally, you can specify a reactionType
to delete only that specific
current user's reaction.
Implementation
Message deleteMyReaction({
String? reactionType,
}) {
final reactionsToDelete = this.ownReactions?.where((it) {
if (reactionType != null) return it.type == reactionType;
return true;
});
// If there are no reactions to delete, we can return the message as is.
if (reactionsToDelete == null || reactionsToDelete.isEmpty) return this;
final ownReactions = <Reaction>[...?this.ownReactions];
final latestReactions = <Reaction>[...?this.latestReactions];
final reactionGroups = <String, ReactionGroup>{...?this.reactionGroups};
for (final reaction in reactionsToDelete) {
final type = reaction.type;
bool match(Reaction r) => r.type == type && r.userId == reaction.userId;
// Remove from own reactions and latest reactions.
ownReactions.removeWhere(match);
latestReactions.removeWhere(match);
final group = reactionGroups.remove(type);
if (group == null) continue;
// Update the reaction group.
final updatedCount = group.count - 1;
final updatedSumScores = group.sumScores - reaction.score;
if (updatedCount > 0 && updatedSumScores > 0) {
reactionGroups[type] = group.copyWith(
count: updatedCount,
sumScores: updatedSumScores,
);
}
}
return copyWith(
ownReactions: ownReactions,
latestReactions: latestReactions,
reactionGroups: reactionGroups,
);
}