Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 23 additions & 1 deletion public/src/client/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ define('forum/register', [
$('#username').focus();
};

// CHATGPT ASSISTED FUNCTION
async function suggestAvailableUsername(userslug) {
let attempt = 0;
const maxAttempts = 20;
while (attempt < maxAttempts) {
const suffix = attempt === 0 ? '' : _.random(1, 999);
const username = `${userslug}${suffix}`;
try {
await User.checkUsername(username);
return username;
} catch (err) {
if (err.message.includes('[[error:username-taken]]')) {
attempt += 1;
continue;
}
throw err;
}
}
throw new Error('Could not find an available username after multiple attempts.');
}

function validateUsername(username, callback) {
callback = callback || function () {};

Expand All @@ -131,7 +152,8 @@ define('forum/register', [
if (results.every(obj => obj.status === 'rejected')) {
showSuccess(username_notify, successIcon);
} else {
showError(username_notify, '[[error:username-taken]]');
const suggestion = await suggestAvailableUsername(userslug);
showError(username_notify, `[[error:username-taken]] Suggested username: ${suggestion}`);
}

callback();
Expand Down
24 changes: 23 additions & 1 deletion src/topics/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,36 @@
}
}

// CHATGPT ASSISTED FUNCTION
async function suggestAvailableUsername(userslug) {
let attempt = 0;
const maxAttempts = 20;
while (attempt < maxAttempts) {
const suffix = attempt === 0 ? '' : _.random(1, 999);
const username = `${userslug}${suffix}`;
try {
await User.checkUsername(username);

Check failure on line 277 in src/topics/create.js

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest, 16)

'User' is not defined

Check failure on line 277 in src/topics/create.js

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest, 16)

Unexpected `await` inside a loop
return username;
} catch (err) {
if (err.message.includes('[[error:username-taken]]')) {
attempt += 1;
continue;

Check failure on line 282 in src/topics/create.js

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest, 16)

Unexpected use of continue statement
}
throw err;
}
}
throw new Error('Could not find an available username after multiple attempts.');
}

async function guestHandleValid(data) {
if (meta.config.allowGuestHandles && parseInt(data.uid, 10) === 0 && data.handle) {
if (data.handle.length > meta.config.maximumUsernameLength) {
throw new Error('[[error:guest-handle-invalid]]');
}
const exists = await user.existsBySlug(slugify(data.handle));
if (exists) {
throw new Error('[[error:username-taken]]');
const suggestion = await suggestAvailableUsername(user.slugify(data.handle));
throw new Error(`[[error:username-taken]] Suggested username: ${suggestion}`);
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion src/user/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
}
const exists = await User.existsBySlug(userslug);
if (exists) {
throw new Error('[[error:username-taken]]');
const suggestion = await suggestAvailableUsername(userslug);
throw new Error(`[[error:username-taken]] Suggested username: ${suggestion}`);
}

const { error } = await plugins.hooks.fire('filter:username.check', {
Expand All @@ -138,6 +139,27 @@
throw error;
}
}
// CHATGPT ASSISTED FUNCTION
async function suggestAvailableUsername(userslug) {
let attempt = 0;
const maxAttempts = 20;
while (attempt < maxAttempts) {
const suffix = attempt === 0 ? '' : _.random(1, 999);
const username = `${userslug}${suffix}`;
try {
await User.checkUsername(username);

Check failure on line 150 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest, 16)

Unexpected `await` inside a loop
return username;
} catch (err) {
if (err.message.includes('[[error:username-taken]]')) {
attempt += 1;
continue;

Check failure on line 155 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest, 16)

Unexpected use of continue statement
}
throw err;
}
}
throw new Error('Could not find an available username after multiple attempts.');
}

User.checkUsername = async username => isUsernameAvailable({ username });

async function isWebsiteValid(callerUid, data) {
Expand Down
Loading