Interface UserMentionScanner


public interface UserMentionScanner
Handles rendering and parsing user mention strings in texts.
Since:
5.16
  • Method Details

    • getMentionedUsers

      @Nonnull Set<String> getMentionedUsers(@Nonnull CharSequence text, int maxMentions)
      Returns all mentioned user names in the text.

      Note that the case of the user name returned matches the case used in the text and might not be identical to the case of the ApplicationUser's name.

      Parameters:
      text - text to search for user mentions
      maxMentions - maximum size of the returned set
      Returns:
      set of user names mentioned
    • processMentions

      void processMentions(@Nonnull CharSequence text, @Nonnull Consumer<UserMentionScanner.UserMention> callback)
      Calls callback for each user mention found in text.
      Parameters:
      text - text to search for user mentions
      callback - callback that gets called for every user mention found
    • renderMention

      @Nonnull String renderMention(@Nonnull String username)
      Renders the full user mention for a user name.
      Parameters:
      username - user name to render
      Returns:
      rendered user mention
    • replaceMentions

      @Nonnull Optional<CharSequence> replaceMentions(@Nonnull CharSequence text, @Nonnull Function<UserMentionScanner.UserMention,Optional<String>> replacementFunction)
      Replaces all user mentions with the string returned from the passed replacementFunction.

      Return types for both the replacementFunction as well as this method are Optional so the calling code can perform more efficient operations when no replacements have taken place.

      Example usage:

      
       String replacedText = userMentionScanner.replaceMentions(text, mention ->
               Optional.of(userMentionScanner.renderMention(mention.getUsername().replace("_", "-"))))
           .orElse(text);
       
      Parameters:
      text - text to search and replace user mentions in
      replacementFunction - function that optionally returns a replacement for the full mention of the user
      Returns:
      the new text or Optional.empty() if no replacement took place