calcUnlocalizedBody method

String calcUnlocalizedBody({
  1. bool hideReply = false,
  2. bool hideEdit = false,
  3. bool plaintextBody = false,
  4. bool removeMarkdown = false,
})

Calculating the body of an event regardless of localization.

Implementation

String calcUnlocalizedBody(
    {bool hideReply = false,
    bool hideEdit = false,
    bool plaintextBody = false,
    bool removeMarkdown = false}) {
  if (redacted) {
    return 'Removed by ${senderFromMemoryOrFallback.displayName ?? senderId}';
  }
  var body = plaintextBody ? this.plaintextBody : this.body;

  // we need to know if the message is an html message to be able to determine
  // if we need to strip the reply fallback.
  var htmlMessage = content['format'] != 'org.sdn.custom.html';
  // If we have an edit, we want to operate on the new content
  final newContent = content.tryGetMap<String, Object?>('m.new_content');
  if (hideEdit &&
      relationshipType == RelationshipTypes.edit &&
      newContent != null) {
    if (plaintextBody && newContent['format'] == 'org.sdn.custom.html') {
      htmlMessage = true;
      body = HtmlToText.convert(
          newContent.tryGet<String>('formatted_body') ?? formattedText);
    } else {
      htmlMessage = false;
      body = newContent.tryGet<String>('body') ?? body;
    }
  }
  // Hide reply fallback
  // Be sure that the plaintextBody already stripped teh reply fallback,
  // if the message is formatted
  if (hideReply && (!plaintextBody || htmlMessage)) {
    body = body.replaceFirst(
        RegExp(r'^>( \*)? <[^>]+>[^\n\r]+\r?\n(> [^\n]*\r?\n)*\r?\n'), '');
  }

  // return the html tags free body
  if (removeMarkdown == true) {
    final html = markdown(body);
    final document = parse(
      html,
    );
    body = document.documentElement?.text ?? body;
  }
  return body;
}