Skip to content

Commit

Permalink
Merge pull request #29915 from RocketChat/release-6.2.11
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Jul 26, 2023
2 parents 09a4d49 + 6437cdf commit 5f116d1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-lizards-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fix: Performance issue when using api to create users
2 changes: 2 additions & 0 deletions .changeset/quick-spies-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { escapeRegExp } from '@rocket.chat/string-helpers';
import { Users } from '@rocket.chat/models';

export const checkEmailAvailability = async function (email: string): Promise<boolean> {
return !(await Users.findOne({
'emails.address': { $regex: new RegExp(`^${escapeRegExp(email).trim()}$`, 'i') },
}));
return !(await Users.findOneByEmailAddress(email));
};
26 changes: 22 additions & 4 deletions apps/meteor/server/models/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ export class UsersRaw extends BaseRaw {
{ key: { language: 1 }, sparse: true },
{ key: { 'active': 1, 'services.email2fa.enabled': 1 }, sparse: true }, // used by statistics
{ key: { 'active': 1, 'services.totp.enabled': 1 }, sparse: true }, // used by statistics
// Used for case insensitive queries
// @deprecated
// Should be converted to unique index later within a migration to prevent errors of duplicated
// records. Those errors does not helps to identify the duplicated value so we need to find a
// way to help the migration in case it happens.
{
key: { 'emails.address': 1 },
unique: false,
sparse: true,
name: 'emails.address_insensitive',
collation: { locale: 'en', strength: 2, caseLevel: false },
},
];
}

Expand Down Expand Up @@ -1885,17 +1897,23 @@ export class UsersRaw extends BaseRaw {

findOneByEmailAddressAndServiceNameIgnoringCase(emailAddress, userId, serviceName, options) {
const query = {
'emails.address': new RegExp(`^${escapeRegExp(String(emailAddress).trim())}$`, 'i'),
'emails.address': String(emailAddress).trim(),
[`services.${serviceName}.id`]: userId,
};

return this.findOne(query, options);
return this.findOne(query, {
collation: { locale: 'en', strength: 2 }, // Case insensitive
...options,
});
}

findOneByEmailAddress(emailAddress, options) {
const query = { 'emails.address': String(emailAddress).trim().toLowerCase() };
const query = { 'emails.address': String(emailAddress).trim() };

return this.findOne(query, options);
return this.findOne(query, {
collation: { locale: 'en', strength: 2 }, // Case insensitive
...options,
});
}

findOneAdmin(userId, options) {
Expand Down

0 comments on commit 5f116d1

Please sign in to comment.