debian-mirror-gitlab/lib/gitlab/user_extractor.rb

56 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
# This class extracts all users found in a piece of text by the username or the
# email adress
module Gitlab
class UserExtractor
# Not using `Devise.email_regexp` to filter out any chars that an email
# does not end with and not pinning the email to a start of end of a string.
EMAIL_REGEXP = /(?<email>([^@\s]+@[^@\s]+(?<!\W)))/
USERNAME_REGEXP = User.reference_pattern
def initialize(text)
@text = text
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-20 20:47:30 +05:30
def users
return User.none unless @text.present?
2018-12-05 23:21:45 +05:30
@users ||= User.from_union(union_relations)
2018-11-20 20:47:30 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-20 20:47:30 +05:30
def usernames
matches[:usernames]
end
def emails
matches[:emails]
end
def references
@references ||= matches.values.flatten
end
def matches
@matches ||= {
emails: @text.scan(EMAIL_REGEXP).flatten.uniq,
usernames: @text.scan(USERNAME_REGEXP).flatten.uniq
}
end
private
2018-12-05 23:21:45 +05:30
def union_relations
2018-11-20 20:47:30 +05:30
relations = []
relations << User.by_any_email(emails) if emails.any?
relations << User.by_username(usernames) if usernames.any?
2018-12-05 23:21:45 +05:30
relations
2018-11-20 20:47:30 +05:30
end
end
end