react method

void react(
  1. String principalId,
  2. String messageId,
  3. String emoji, {
  4. required bool add,
})

Adds or removes the reaction emoji of principalId on messageId; a repeat changes nothing. Throws 404, 410 message_deleted when adding to a deleted message.

Implementation

void react(
  String principalId,
  String messageId,
  String emoji, {
  required bool add,
}) {
  final m = visibleMessage(principalId, messageId);
  if (add && m['deleted_at'] != null) {
    throw const FakeProblem(410, 'message_deleted');
  }
  final key = '$messageId|$emoji';
  final who = _reactions.putIfAbsent(key, () => {});
  if (add ? !who.add(principalId) : !who.remove(principalId)) return;
  m['reactions'] = [
    for (final e in _reactions.entries)
      if (e.key.startsWith('$messageId|') && e.value.isNotEmpty)
        {
          'emoji': e.key.substring(messageId.length + 1),
          'count': e.value.length,
        },
  ];
  // Reactions never raise the message version (04 9.3); the payload carries the count.
  append(
    add ? 'reaction.added' : 'reaction.removed',
    resource: {'kind': 'message', 'id': messageId},
    scope: _scopeOf(m['thread_id']! as String),
    payload: {
      'message_id': messageId,
      'thread_id': m['thread_id'],
      'emoji': emoji,
      'principal_id': principalId,
      'count': who.length,
    },
    actor: principalId,
  );
}