sendReaction method

Future<SendReactionResponse> sendReaction (
  1. Message message,
  2. String type,
  3. {Map<String, dynamic> extraData: const {}}
)

Send a reaction to this channel

Implementation

Future<SendReactionResponse> sendReaction(
  Message message,
  String type, {
  Map<String, dynamic> extraData = const {},
}) async {
  final messageId = message.id;
  final data = Map<String, dynamic>.from(extraData)
    ..addAll({
      'type': type,
    });

  final currentScore = message.ownReactions
          ?.firstWhere((r) => r.type == type, orElse: () => null)
          ?.score ??
      0;

  final newReaction = Reaction(
    type: type,
    extraData: extraData,
    createdAt: DateTime.now(),
    user: _client.state.user,
    messageId: messageId,
    userId: _client.state.user?.id,
    score: currentScore + 1,
  );

  state?._addMessageReaction(message, newReaction);

  try {
    final res = await _client.post(
      '/messages/$messageId/reaction',
      data: {'reaction': data},
    );
    return _client.decode(res.data, SendReactionResponse.fromJson);
  } catch (_) {
    state?._removeMessageReaction(message, newReaction);
    rethrow;
  }
}