hasMutedWord function

bool hasMutedWord({
  1. required List<MutedWord> mutedWords,
  2. required String text,
  3. List<Facet>? facets,
  4. List<String>? outlineTags,
  5. List<String>? languages,
})

Implementation

bool hasMutedWord({
  required List<MutedWord> mutedWords,
  required String text,
  List<Facet>? facets,
  List<String>? outlineTags,
  List<String>? languages,
}) {
  final hasExceptionLanguage = _hasExceptionLanguage(languages);
  final tags = <String>{
    if (outlineTags != null) ...outlineTags.map((e) => e.toLowerCase()),
    if (facets != null)
      ...facets
          .map((e) => e.features.whereType<UFacetFeatureFacetTag>())
          .where((e) => e.isNotEmpty)
          .map((e) => e.first.data.tag.toLowerCase())
  }.toList();

  for (final mute in mutedWords) {
    final mutedWord = mute.value.toLowerCase();
    final postText = text.toLowerCase();

    if (tags.contains(mutedWord)) return true;
    if (!mute.targets.contains(KnownMutedWordTarget.content.toUnion())) {
      continue;
    }
    if ((mutedWord.characters.length == 1 || hasExceptionLanguage) &&
        postText.contains(mutedWord)) {
      return true;
    }
    if (mutedWord.length > postText.length) continue;
    if (mutedWord == postText) return true;
    if (_whitespacePunctuationRegex.hasMatch(mutedWord) &&
        postText.contains(mutedWord)) {
      return true;
    }

    for (final word in postText.split(_wordBoundaryRegex)) {
      if (word == mutedWord) return true;

      final wordTrimmedPunctuation = word.replaceAll(
        _leadingTrailingPunctuationRegex,
        '',
      );

      if (mutedWord == wordTrimmedPunctuation) return true;
      if (mutedWord.length > wordTrimmedPunctuation.length) continue;

      if (_punctuationRegex.hasMatch(wordTrimmedPunctuation)) {
        final spacedWord = wordTrimmedPunctuation.replaceAll(
          _punctuationRegex,
          ' ',
        );
        if (spacedWord == mutedWord) return true;

        final contiguousWord = spacedWord.replaceAll(_spaceRegex, '');
        if (contiguousWord == mutedWord) return true;

        final wordParts = wordTrimmedPunctuation.split(_punctuationRegex);
        for (final wordPart in wordParts) {
          if (wordPart == mutedWord) return true;
        }
      }
    }
  }

  return false;
}