From 5ebc53726dbd8f5393aad82eebd1ba203dc69d60 Mon Sep 17 00:00:00 2001 From: Dcano-png Date: Mon, 15 Sep 2025 15:49:52 -0400 Subject: [PATCH 1/2] src profile and create suggestAvailableUsername helper function --- src/topics/create.js | 24 +++++++++++++++++++++++- src/user/profile.js | 24 +++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/topics/create.js b/src/topics/create.js index c366c21..8815ca5 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -266,6 +266,27 @@ module.exports = function (Topics) { } } + // 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.'); + } + async function guestHandleValid(data) { if (meta.config.allowGuestHandles && parseInt(data.uid, 10) === 0 && data.handle) { if (data.handle.length > meta.config.maximumUsernameLength) { @@ -273,7 +294,8 @@ module.exports = function (Topics) { } 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}`); } } } diff --git a/src/user/profile.js b/src/user/profile.js index 5274a6d..98d5185 100644 --- a/src/user/profile.js +++ b/src/user/profile.js @@ -127,7 +127,8 @@ module.exports = function (User) { } 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', { @@ -138,6 +139,27 @@ module.exports = function (User) { 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); + 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.'); + } + User.checkUsername = async username => isUsernameAvailable({ username }); async function isWebsiteValid(callerUid, data) { From 14e2641d281ab9f1d4d86b72f5cebe36a08e89c5 Mon Sep 17 00:00:00 2001 From: Dcano-png Date: Mon, 15 Sep 2025 15:53:45 -0400 Subject: [PATCH 2/2] src register helper suggestion --- public/src/client/register.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/public/src/client/register.js b/public/src/client/register.js index 62dbc41..c5a9fb2 100644 --- a/public/src/client/register.js +++ b/public/src/client/register.js @@ -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 () {}; @@ -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();