<commit_before>slug = (text) ->
	text = slugify text, '.'
	return text.replace(/[^0-9a-z-_.]/g, '')

usernameIsAvaliable = (username) ->
	if username.length < 1
		return false

	if username is 'all'
		return false

	return not RocketChat.models.Users.findOneByUsername({$regex : new RegExp(username, "i") })

@generateSuggestion = (user) ->
	usernames = []
	username = undefined

	if RocketChat.settings.get 'UTF8_Names_slugify'
		usernames.push slug user.name
	else
		usernames.push user.name

	nameParts = user?.name?.split()
	if nameParts.length > 1
		first = nameParts[0]
		last = nameParts[nameParts.length - 1]

	if RocketChat.settings.get 'UTF8_Names_slugify'
		usernames.push slug first[0] + last
		usernames.push slug first + last[0]
	else
		usernames.push first[0] + last
		usernames.push first + last[0]

	if user.profile?.name?
		if RocketChat.settings.get 'UTF8_Names_slugify'
			usernames.push slug user.profile.name
		else
			usernames.push user.profile.name

	if user.services?
		for serviceName, service of user.services
			if service.name?
				if RocketChat.settings.get 'UTF8_Names_slugify'
					usernames.push slug service.name
				else
					usernames.push service.name
			else if service.username?
				if RocketChat.settings.get 'UTF8_Names_slugify'
					usernames.push slug service.username
				else
					usernames.push service.username

	if user.emails?.length > 0
		for email in user.emails when email.address? and email.verified is true
			usernames.push slug email.address.replace(/@.+$/, '')

		for email in user.emails when email.address? and email.verified is true
			usernames.push slug email.address.replace(/(.+)@(\w+).+/, '$1.$2')

	for item in usernames
		if usernameIsAvaliable item
			username = item
			break

	if usernames[0]? and usernames[0].length > 0
		index = 0
		while not username?
			index++
			if usernameIsAvaliable usernames[0] + '-' + index
				username = usernames[0] + '-' + index

	if usernameIsAvaliable username
		return username

	return undefined
RocketChat.generateUsernameSuggestion = generateSuggestion

Meteor.methods
	getUsernameSuggestion: ->
		if not Meteor.userId()
			throw new Meteor.Error 203, '[methods] getUsernameSuggestion -> Usuário não logado'

		user = Meteor.user()
		return generateSuggestion(user)
<commit_msg>Fix error in method getUsernameSuggestion<commit_after>slug = (text) ->
	text = slugify text, '.'
	return text.replace(/[^0-9a-z-_.]/g, '')

usernameIsAvaliable = (username) ->
	if username.length < 1
		return false

	if username is 'all'
		return false

	return not RocketChat.models.Users.findOneByUsername({$regex : new RegExp(username, "i") })

@generateSuggestion = (user) ->
	usernames = []
	username = undefined

	if RocketChat.settings.get 'UTF8_Names_slugify'
		usernames.push slug user.name
	else
		usernames.push user.name

	nameParts = user?.name?.split()
	if nameParts.length > 1
		first = nameParts[0]
		last = nameParts[nameParts.length - 1]

		if RocketChat.settings.get 'UTF8_Names_slugify'
			usernames.push slug first[0] + last
			usernames.push slug first + last[0]
		else
			usernames.push first[0] + last
			usernames.push first + last[0]

	if user.profile?.name?
		if RocketChat.settings.get 'UTF8_Names_slugify'
			usernames.push slug user.profile.name
		else
			usernames.push user.profile.name

	if user.services?
		for serviceName, service of user.services
			if service.name?
				if RocketChat.settings.get 'UTF8_Names_slugify'
					usernames.push slug service.name
				else
					usernames.push service.name
			else if service.username?
				if RocketChat.settings.get 'UTF8_Names_slugify'
					usernames.push slug service.username
				else
					usernames.push service.username

	if user.emails?.length > 0
		for email in user.emails when email.address? and email.verified is true
			usernames.push slug email.address.replace(/@.+$/, '')

		for email in user.emails when email.address? and email.verified is true
			usernames.push slug email.address.replace(/(.+)@(\w+).+/, '$1.$2')

	for item in usernames
		if usernameIsAvaliable item
			username = item
			break

	if usernames[0]? and usernames[0].length > 0
		index = 0
		while not username?
			index++
			if usernameIsAvaliable usernames[0] + '-' + index
				username = usernames[0] + '-' + index

	if usernameIsAvaliable username
		return username

	return undefined
RocketChat.generateUsernameSuggestion = generateSuggestion

Meteor.methods
	getUsernameSuggestion: ->
		if not Meteor.userId()
			throw new Meteor.Error 203, '[methods] getUsernameSuggestion -> Usuário não logado'

		user = Meteor.user()
		return generateSuggestion(user)

{'fraction_non_alphanumeric': 0.10585683297180043, 'fraction_numerical': 0.011713665943600867, 'mean_word_length': 3.748712667353244, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3215099', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1545, 'n_words': 392}