attach method

Future<void> attach(
  1. UploadSource source, {
  2. String caption = '',
})

Sends a file, optionally with a caption. It shows at once as a pending bubble with upload progress; when the bytes are on the server the message goes out like text (same retry by client id). Files over 10 MB or of a type the gateway refuses fail immediately with a transient error.

Implementation

Future<void> attach(UploadSource source, {String caption = ''}) async {
  if (source.size > Limits.attachmentBytes) {
    _emit(_state.copyWith(transientError: const HatangException('file_too_large', 'Files must be 10 MB or smaller')));
    return;
  }
  if (!uploadMimeTypes.contains(source.mimeType)) {
    _emit(
      _state.copyWith(
        transientError: HatangException('unsupported_file_type', 'File type ${source.mimeType} is not allowed'),
      ),
    );
    return;
  }
  final p = PendingMessage(
    clientId: ids.clientId(),
    content: caption.trim(),
    createdAt: clock.now(),
    upload: PendingUpload(source: source),
  );
  _emit(_state.copyWith(pending: [..._state.pending, p]));
  await _uploadThenSend(p);
}