Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Username suggestion logic #12779

Merged
merged 3 commits into from
Dec 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the while-loop with a for-loop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

index++;
if (usernameIsAvaliable(`${ usernames[0] }-${ index }`)) {
username = `${ usernames[0] }-${ index }`;
return `${ usernames[0] }-${ index }`;
}
}

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

Expand Down