localizedSystemMessageTextFromMetadata function

String? localizedSystemMessageTextFromMetadata(
  1. Map<String, dynamic>? metadata,
  2. ChatUiLocalizations l10n
)

localizedSystemMessageText over a raw metadata map — used by the adapter, which builds the map before it has a ChatMessage to put it on, so both the persisted text and every later repaint come out of this one switch.

Implementation

String? localizedSystemMessageTextFromMetadata(
  Map<String, dynamic>? metadata,
  ChatUiLocalizations l10n,
) {
  if (metadata == null) return null;
  final event = metadata[SystemMessageMetadataKeys.event];
  if (event is! String) return null;
  final userLabel = metadata[SystemMessageMetadataKeys.userLabel];
  if (userLabel is! String || userLabel.isEmpty) return null;

  final userId = metadata[SystemMessageMetadataKeys.userId];
  final actorUserId = metadata[SystemMessageMetadataKeys.actorUserId];
  final isKick = actorUserId is String && actorUserId != userId;
  final rawActorLabel = metadata[SystemMessageMetadataKeys.actorLabel];
  final actorLabel = rawActorLabel is String && rawActorLabel.isNotEmpty
      ? rawActorLabel
      : null;

  if (event == 'user_left' && isKick) {
    if (actorLabel == null) return null;
    if (metadata[SystemMessageMetadataKeys.userIsSelf] == true) {
      return l10n.youWereRemovedBy(actorLabel);
    }
    if (metadata[SystemMessageMetadataKeys.actorIsSelf] == true) {
      return l10n.youRemoved(userLabel);
    }
    return l10n.userRemovedBy(userLabel, actorLabel);
  }

  return switch (event) {
    'user_joined' => l10n.userJoined(userLabel),
    'user_left' => l10n.userLeft(userLabel),
    'user_role_changed' => l10n.userRoleChanged(userLabel),
    _ => null,
  };
}