class Decidim::ContentParsers::UserParser

A parser that searches user mentions in content.

A word starting with `@` will be considered as a possible mention if they only contains letters, numbers or underscores.

@see BaseParser Examples of how to use a content parser

Constants

MENTION_REGEX

Matches a nickname if contains letters, numbers or underscores.

Metadata

Class used as a container for metadata

@!attribute users

@return [Array] an array of Decidim::User mentioned in content

Public Instance Methods

metadata() click to toggle source

(see BaseParser#metadata)

# File lib/decidim/content_parsers/user_parser.rb, line 34
def metadata
  Metadata.new(existing_users)
end
rewrite() click to toggle source

Replaces found mentions matching a nickname of an existing user in the current organization with a global id. Other mentions found that doesn't match an existing user are returned as is.

@return [String] the content with the valid mentions replaced by a global id

# File lib/decidim/content_parsers/user_parser.rb, line 27
def rewrite
  content.gsub(MENTION_REGEX) do |match|
    users[match[1..-1]]&.to_global_id&.to_s || match
  end
end

Private Instance Methods

content_nicknames() click to toggle source
# File lib/decidim/content_parsers/user_parser.rb, line 49
def content_nicknames
  @content_nicknames ||= content.scan(MENTION_REGEX).flatten.uniq
end
current_organization() click to toggle source
# File lib/decidim/content_parsers/user_parser.rb, line 53
def current_organization
  @current_organization ||= context[:current_organization]
end
existing_users() click to toggle source
# File lib/decidim/content_parsers/user_parser.rb, line 45
def existing_users
  @existing_users ||= Decidim::User.where(organization: current_organization, nickname: content_nicknames)
end
users() click to toggle source
# File lib/decidim/content_parsers/user_parser.rb, line 40
def users
  @users ||=
    existing_users.index_by(&:nickname)
end