A platform for community discussion. Free, open, simple.
DEV: refactor username validation helper class to take explicit args (#32136)
Kelv60d7a4f1205a
diff --git a/app/assets/javascripts/discourse/app/components/modal/create-account.gjs b/app/assets/javascripts/discourse/app/components/modal/create-account.gjsindex c46a3c1055e..0200beba851 100644--- a/app/assets/javascripts/discourse/app/components/modal/create-account.gjs+++ b/app/assets/javascripts/discourse/app/components/modal/create-account.gjs@@ -63,7 +63,20 @@ export default class CreateAccount extends Component {maskPassword = true;emailValidationVisible = false;nameValidationHelper = new NameValidationHelper(this);- usernameValidationHelper = new UsernameValidationHelper(this);+ usernameValidationHelper = new UsernameValidationHelper({+ getAccountEmail: () => this.accountEmail,+ getAccountUsername: () => this.accountUsername,+ getPrefilledUsername: () => this.prefilledUsername,+ getAuthOptionsUsername: () => this.authOptions?.username,+ getForceValidationReason: () => this.forceValidationReason,+ siteSettings: this.siteSettings,+ isInvalid: () => this.isDestroying || this.isDestroyed,+ updateIsDeveloper: (isDeveloper) => (this.isDeveloper = isDeveloper),+ updateUsernames: (username) => {+ this.accountUsername = username;+ this.prefilledUsername = username;+ },+ });passwordValidationHelper = new PasswordValidationHelper(this);userFieldsValidationHelper = new UserFieldsValidationHelper({getUserFields: () => this.site.get("user_fields"),@@ -377,7 +390,7 @@ export default class CreateAccount extends Component {// then look for a registered username that matches the email.discourseDebounce(this,- this.usernameValidationHelper.fetchExistingUsername,+ () => this.usernameValidationHelper.fetchExistingUsername(),500);}diff --git a/app/assets/javascripts/discourse/app/controllers/invites-show.js b/app/assets/javascripts/discourse/app/controllers/invites-show.jsindex 74ce794a84c..b9113b72e06 100644--- a/app/assets/javascripts/discourse/app/controllers/invites-show.js+++ b/app/assets/javascripts/discourse/app/controllers/invites-show.js@@ -23,7 +23,20 @@ export default class InvitesShowController extends Controller {@tracked isDeveloper;queryParams = ["t"];nameValidationHelper = new NameValidationHelper(this);- usernameValidationHelper = new UsernameValidationHelper(this);+ usernameValidationHelper = new UsernameValidationHelper({+ getAccountEmail: () => this.accountEmail,+ getAccountUsername: () => this.accountUsername,+ getPrefilledUsername: () => this.prefilledUsername,+ getAuthOptionsUsername: () => this.authOptions?.username,+ getForceValidationReason: () => this.forceValidationReason,+ siteSettings: this.siteSettings,+ isInvalid: () => this.isDestroying || this.isDestroyed,+ updateIsDeveloper: (isDeveloper) => (this.isDeveloper = isDeveloper),+ updateUsernames: (username) => {+ this.accountUsername = username;+ this.prefilledUsername = username;+ },+ });passwordValidationHelper = new PasswordValidationHelper(this);userFieldsValidationHelper = new UserFieldsValidationHelper({getUserFields: () => this.site.get("user_fields"),diff --git a/app/assets/javascripts/discourse/app/controllers/signup.js b/app/assets/javascripts/discourse/app/controllers/signup.jsindex f52b0ec5d18..74b33ff027c 100644--- a/app/assets/javascripts/discourse/app/controllers/signup.js+++ b/app/assets/javascripts/discourse/app/controllers/signup.js@@ -43,7 +43,20 @@ export default class SignupPageController extends Controller {maskPassword = true;emailValidationVisible = false;nameValidationHelper = new NameValidationHelper(this);- usernameValidationHelper = new UsernameValidationHelper(this);+ usernameValidationHelper = new UsernameValidationHelper({+ getAccountEmail: () => this.accountEmail,+ getAccountUsername: () => this.accountUsername,+ getPrefilledUsername: () => this.prefilledUsername,+ getAuthOptionsUsername: () => this.authOptions?.username,+ getForceValidationReason: () => this.forceValidationReason,+ siteSettings: this.siteSettings,+ isInvalid: () => this.isDestroying || this.isDestroyed,+ updateIsDeveloper: (isDeveloper) => (this.isDeveloper = isDeveloper),+ updateUsernames: (username) => {+ this.accountUsername = username;+ this.prefilledUsername = username;+ },+ });passwordValidationHelper = new PasswordValidationHelper(this);userFieldsValidationHelper = new UserFieldsValidationHelper({getUserFields: () => this.site.get("user_fields"),@@ -353,7 +366,7 @@ export default class SignupPageController extends Controller {// then look for a registered username that matches the email.discourseDebounce(this,- this.usernameValidationHelper.fetchExistingUsername,+ () => this.usernameValidationHelper.fetchExistingUsername(),500);}diff --git a/app/assets/javascripts/discourse/app/lib/username-validation-helper.js b/app/assets/javascripts/discourse/app/lib/username-validation-helper.jsindex 47a86b7657f..84a629bd4a9 100644--- a/app/assets/javascripts/discourse/app/lib/username-validation-helper.js+++ b/app/assets/javascripts/discourse/app/lib/username-validation-helper.js@@ -22,32 +22,51 @@ export default class UsernameValidationHelper {@tracked usernameValidationResult;checkedUsername = null;- constructor(owner) {- this.owner = owner;+ constructor({+ getAccountEmail,+ getAccountUsername,+ getPrefilledUsername,+ getAuthOptionsUsername,+ getForceValidationReason,+ siteSettings,+ isInvalid,+ updateIsDeveloper,+ updateUsernames,+ }) {+ this.getAccountEmail = getAccountEmail;+ this.getAccountUsername = getAccountUsername;+ this.getPrefilledUsername = getPrefilledUsername;+ this.getAuthOptionsUsername = getAuthOptionsUsername;+ this.getForceValidationReason = getForceValidationReason;+ this.siteSettings = siteSettings;+ this.isInvalid = isInvalid;+ this.updateIsDeveloper = updateIsDeveloper;+ this.updateUsernames = updateUsernames;}async fetchExistingUsername() {- const result = await User.checkUsername(null, this.owner?.accountEmail);+ const accountUsername = this.getAccountUsername();+ const result = await User.checkUsername(null, this.getAccountEmail());if (result.suggestion &&- (isEmpty(this.owner.accountUsername) ||- this.owner.accountUsername === this.owner.get("authOptions.username"))+ (isEmpty(accountUsername) ||+ accountUsername === this.getAuthOptionsUsername())) {- this.owner.accountUsername = result.suggestion;- this.owner.prefilledUsername = result.suggestion;+ this.updateUsernames(result.suggestion);}}get usernameValidation() {+ const accountUsername = this.getAccountUsername();if (this.usernameValidationResult &&- this.checkedUsername === this.owner.accountUsername+ this.checkedUsername === accountUsername) {return this.usernameValidationResult;}- const result = this.basicUsernameValidation(this.owner.accountUsername);+ const result = this.basicUsernameValidation(accountUsername);if (result.shouldCheck) {discourseDebounce(this, this.checkUsernameAvailability, 500);@@ -57,24 +76,24 @@ export default class UsernameValidationHelper {}basicUsernameValidation(username) {- if (username && username === this.owner.prefilledUsername) {+ if (username && username === this.getPrefilledUsername()) {return validResult({ reason: i18n("user.username.prefilled") });}if (isEmpty(username)) {return failedResult({message: i18n("user.username.required"),- reason: this.owner.forceValidationReason+ reason: this.getForceValidationReason()? i18n("user.username.required"): null,});}- if (username.length < this.owner.siteSettings.min_username_length) {+ if (username.length < this.siteSettings.min_username_length) {return failedResult({ reason: i18n("user.username.too_short") });}- if (username.length > this.owner.siteSettings.max_username_length) {+ if (username.length > this.siteSettings.max_username_length) {return failedResult({ reason: i18n("user.username.too_long") });}@@ -85,17 +104,18 @@ export default class UsernameValidationHelper {}async checkUsernameAvailability() {+ const accountUsername = this.getAccountUsername();const result = await User.checkUsername(- this.owner.accountUsername,- this.owner.accountEmail+ accountUsername,+ this.getAccountEmail());- if (this.owner.isDestroying || this.owner.isDestroyed) {+ if (this.isInvalid()) {return;}- this.checkedUsername = this.owner.accountUsername;- this.owner.isDeveloper = !!result.is_developer;+ this.checkedUsername = accountUsername;+ this.updateIsDeveloper(!!result.is_developer);if (result.available) {this.usernameValidationResult = validResult({