sendMessage method

Future<void> sendMessage()

Sends the current message

Implementation

Future<void> sendMessage() async {
  if (_timeOut > 0 ||
      (_effectiveController.text.trim().isEmpty &&
          _effectiveController.attachments.isEmpty)) {
    return;
  }

  final streamChannel = StreamChannel.of(context);
  final channel = streamChannel.channel;
  var message = _effectiveController.value;

  if (!channel.ownCapabilities.contains(PermissionType.sendLinks) &&
      _urlRegex.allMatches(message.text ?? '').any((element) =>
          element.group(0)?.split('.').last.isValidTLD() == true)) {
    showInfoBottomSheet(
      context,
      icon: StreamSvgIcon.error(
        color: StreamChatTheme.of(context).colorTheme.accentError,
        size: 24,
      ),
      title: 'Links are disabled',
      details: 'Sending links is not allowed in this conversation.',
      okText: context.translations.okLabel,
    );
    return;
  }

  final containsCommand = message.command != null;
  // If the message contains command we should append it to the text
  // before sending it.
  if (containsCommand) {
    message = message.copyWith(text: '/${message.command} ${message.text}');
  }

  var shouldKeepFocus = widget.shouldKeepFocusAfterMessage;
  shouldKeepFocus ??= !_commandEnabled;

  widget.onQuotedMessageCleared?.call();

  _effectiveController.reset();

  if (widget.preMessageSending != null) {
    message = await widget.preMessageSending!(message);
  }

  message = message.replaceMentionsWithId();

  // If the channel is not up to date, we should reload it before sending
  // the message.
  if (!channel.state!.isUpToDate) {
    await streamChannel.reloadChannel();

    // We need to wait for the frame to be rendered with the updated channel
    // state before sending the message.
    await WidgetsBinding.instance.endOfFrame;
  }

  await _sendOrUpdateMessage(message: message);

  if (mounted) {
    if (shouldKeepFocus) {
      FocusScope.of(context).requestFocus(_effectiveFocusNode);
    } else {
      FocusScope.of(context).unfocus();
    }
  }
}