messagePreview function

String messagePreview(
  1. Message? message, {
  2. String? currentUserId,
})

A one-line preview of message, as shown in the channel list.

Implementation

String messagePreview(Message? message, {String? currentUserId}) {
  if (message == null) return 'No messages yet';
  if (message.isDeleted) return 'This message was deleted';

  final body = switch (message) {
    _ when (message.text ?? '').isNotEmpty => message.text!,
    _ when message.attachments.isNotEmpty =>
      _attachmentPreview(message.attachments),
    _ => 'Message',
  };

  final flattened = body.replaceAll(RegExp(r'\s+'), ' ').trim();
  final author = message.user;
  if (author == null) return flattened;
  if (author.id == currentUserId) return 'You: $flattened';
  return '${author.name}: $flattened';
}