2018-08-20 10:48:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This class extracts all users found in a piece of text by the username or the
|
2018-10-30 06:53:01 -04:00
|
|
|
# email address
|
2018-08-20 10:48:08 -04:00
|
|
|
|
|
|
|
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)
|
2019-03-15 12:34:37 -04:00
|
|
|
# EE passes an Array to `text` in a few places, so we want to support both
|
|
|
|
# here.
|
|
|
|
@text = Array(text).join(' ')
|
2018-08-20 10:48:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def users
|
|
|
|
return User.none unless @text.present?
|
2019-03-15 12:34:37 -04:00
|
|
|
return User.none if references.empty?
|
2018-08-20 10:48:08 -04:00
|
|
|
|
2018-09-11 11:31:34 -04:00
|
|
|
@users ||= User.from_union(union_relations)
|
2018-08-20 10:48:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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-09-11 11:31:34 -04:00
|
|
|
def union_relations
|
2018-08-20 10:48:08 -04:00
|
|
|
relations = []
|
|
|
|
|
|
|
|
relations << User.by_any_email(emails) if emails.any?
|
|
|
|
relations << User.by_username(usernames) if usernames.any?
|
|
|
|
|
2018-09-11 11:31:34 -04:00
|
|
|
relations
|
2018-08-20 10:48:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|