Skip to content

Commit

Permalink
[IMPROVE] Username suggestion logic (#12779)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored and sampaiodiego committed Dec 3, 2018
1 parent 4eb4d52 commit ca90c9c
Showing 1 changed file with 20 additions and 48 deletions.
68 changes: 20 additions & 48 deletions server/methods/getUsernameSuggestion.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/* global slugify */
import { Meteor } from 'meteor/meteor';
import { Match } from 'meteor/check';
import _ from 'underscore';

function slug(text) {
text = slugify(text, '.');
return text.replace(/[^0-9a-z-_.]/g, '');
return slugify(text, '.').replace(/[^0-9a-z-_.]/g, '');
}

function usernameIsAvaliable(username) {
if (username.length < 1) {
if (username.length === 0) {
return false;
}

Expand All @@ -20,57 +17,37 @@ function usernameIsAvaliable(username) {
return !RocketChat.models.Users.findOneByUsername(username);
}


const name = (username) => (RocketChat.settings.get('UTF8_Names_Slugify') ? slug(username) : username);

function generateSuggestion(user) {
let usernames = [];
let username = undefined;

if (Meteor.settings.public.sandstorm) {
usernames.push(user.services.sandstorm.preferredHandle);
}

if (Match.test(user && user.name, String)) {
if (RocketChat.settings.get('UTF8_Names_Slugify')) {
usernames.push(slug(user.name));
} else {
usernames.push(user.name);
}
if (user.name) {

usernames.push(name(user.name));

const nameParts = user.name.split(' ');

if (nameParts.length > 1) {
const first = nameParts[0];
const [first] = nameParts;
const 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]);
}
usernames.push(name(first[0] + last));
usernames.push(name(first + last[0]));
}
}

if (user.profile && user.profile.name) {
if (RocketChat.settings.get('UTF8_Names_Slugify')) {
usernames.push(slug(user.profile.name));
} else {
usernames.push(user.profile.name);
}
usernames.push(name(user.profile.name));
}

if (Array.isArray(user.services)) {
let services = user.services.map((service) => _.values(_.pick(service, 'name', 'username', 'firstName', 'lastName')));

services = _.uniq(_.flatten(services));

for (const service of services) {
if (RocketChat.settings.get('UTF8_Names_Slugify')) {
usernames.push(slug(service));
} else {
usernames.push(service);
}
}
const services = new Set(user.services.flatMap(({ name, username, firstName, lastName }) => [name, username, firstName, lastName]));
usernames.push(...services.map(name));
}

if (user.emails && user.emails.length > 0) {
Expand All @@ -82,28 +59,23 @@ function generateSuggestion(user) {
}
}

usernames = _.compact(usernames);
usernames = usernames.filter((e) => e);

for (const item of usernames) {
if (usernameIsAvaliable(item)) {
return item;
}
}

if (usernames.length === 0 || usernames[0].length === 0) {
usernames.push(RocketChat.settings.get('Accounts_DefaultUsernamePrefixSuggestion'));
}
usernames.push(RocketChat.settings.get('Accounts_DefaultUsernamePrefixSuggestion'));

let index = 0;
let index = RocketChat.models.Users.find({ username: new RegExp(`^${ usernames[0] }-[0-9]+`) }).count();
const username = '';
while (!username) {
index++;
if (usernameIsAvaliable(`${ usernames[0] }-${ index }`)) {
username = `${ usernames[0] }-${ index }`;
return `${ usernames[0] }-${ index }`;
}
}

if (usernameIsAvaliable(username)) {
return username;
index++;
}
}

Expand Down

0 comments on commit ca90c9c

Please sign in to comment.