Sha256: 502cda757b9065a9a836ead5840d7bb3b25cfa949577d06b54a68dda81cfeef5

Contents?: true

Size: 1.68 KB

Versions: 25

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Decidim
  module ContentParsers
    # 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
    class UserParser < BaseParser
      # Class used as a container for metadata
      #
      # @!attribute users
      #   @return [Array] an array of Decidim::User mentioned in content
      Metadata = Struct.new(:users)

      # Matches a nickname if contains letters, numbers or underscores.
      MENTION_REGEX = /\B@(\w*)\b/.freeze

      # 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
      def rewrite
        content.gsub(MENTION_REGEX) do |match|
          users[match[1..-1]]&.to_global_id&.to_s || match
        end
      end

      # (see BaseParser#metadata)
      def metadata
        Metadata.new(existing_users)
      end

      private

      def users
        @users ||=
          existing_users.index_by(&:nickname)
      end

      def existing_users
        @existing_users ||= Decidim::User.where(organization: current_organization, nickname: content_nicknames)
      end

      def content_nicknames
        @content_nicknames ||= content.scan(MENTION_REGEX).flatten.uniq
      end

      def current_organization
        @current_organization ||= context[:current_organization]
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.26.10 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.9 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.8 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.7 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.5 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.4 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.3 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.2 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.1 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.0 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.0.rc2 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.26.0.rc1 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.25.2 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.25.1 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.25.0 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.25.0.rc4 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.25.0.rc3 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.25.0.rc2 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.25.0.rc1 lib/decidim/content_parsers/user_parser.rb
decidim-core-0.24.3 lib/decidim/content_parsers/user_parser.rb