refactor: closes #12629, allow passing arrays to meta.userOrGroupExists

This commit is contained in:
Barış Soner Uşaklı
2024-06-10 16:59:55 -04:00
parent be86d8efc7
commit bad1564301
3 changed files with 32 additions and 28 deletions

View File

@@ -24,20 +24,26 @@ Meta.templates = require('./templates');
Meta.blacklist = require('./blacklist'); Meta.blacklist = require('./blacklist');
Meta.languages = require('./languages'); Meta.languages = require('./languages');
const user = require('../user');
const groups = require('../groups');
/* Assorted */ /* Assorted */
Meta.userOrGroupExists = async function (slug) { Meta.userOrGroupExists = async function (slug) {
if (!slug) { const isArray = Array.isArray(slug);
if ((isArray && slug.some(slug => !slug)) || (!isArray && !slug)) {
throw new Error('[[error:invalid-data]]'); throw new Error('[[error:invalid-data]]');
} }
const user = require('../user');
const groups = require('../groups'); slug = isArray ? slug.map(s => slugify(s, false)) : slugify(slug);
slug = slugify(slug);
const [userExists, groupExists] = await Promise.all([ const [userExists, groupExists] = await Promise.all([
user.existsBySlug(slug), user.existsBySlug(slug),
groups.existsBySlug(slug), groups.existsBySlug(slug),
]); ]);
return userExists || groupExists;
return isArray ?
slug.map((s, i) => userExists[i] || groupExists[i]):
(userExists || groupExists);
}; };
if (nconf.get('isPrimary')) { if (nconf.get('isPrimary')) {

View File

@@ -50,8 +50,12 @@ User.exists = async function (uids) {
}; };
User.existsBySlug = async function (userslug) { User.existsBySlug = async function (userslug) {
const exists = await User.getUidByUserslug(userslug); if (Array.isArray(userslug)) {
return !!exists; const uids = await User.getUidsByUserslugs(userslug);
return uids.map(uid => !!uid);
}
const uid = await User.getUidByUserslug(userslug);
return !!uid;
}; };
User.getUidsFromSet = async function (set, start, stop) { User.getUidsFromSet = async function (set, start, stop) {
@@ -112,6 +116,10 @@ User.getUidByUserslug = async function (userslug) {
return await db.sortedSetScore('userslug:uid', userslug); return await db.sortedSetScore('userslug:uid', userslug);
}; };
User.getUidsByUserslugs = async function (userslugs) {
return await db.sortedSetScores('userslug:uid', userslugs);
};
User.getUsernamesByUids = async function (uids) { User.getUsernamesByUids = async function (uids) {
const users = await User.getUsersFields(uids, ['username']); const users = await User.getUsersFields(uids, ['username']);
return users.map(user => user.username); return users.map(user => user.username);

View File

@@ -1492,28 +1492,18 @@ describe('User', () => {
}); });
}); });
it('should return true if user/group exists', (done) => { it('should return true/false if user/group exists or not', async () => {
meta.userOrGroupExists('registered-users', (err, exists) => { assert.strictEqual(await meta.userOrGroupExists('registered-users'), true);
assert.ifError(err); assert.strictEqual(await meta.userOrGroupExists('John Smith'), true);
assert(exists); assert.strictEqual(await meta.userOrGroupExists('doesnot exist'), false);
done(); assert.deepStrictEqual(await meta.userOrGroupExists(['doesnot exist', 'nope not here']), [false, false]);
}); assert.deepStrictEqual(await meta.userOrGroupExists(['doesnot exist', 'John Smith']), [false, true]);
}); assert.deepStrictEqual(await meta.userOrGroupExists(['administrators', 'John Smith']), [true, true]);
it('should return true if user/group exists', (done) => { await assert.rejects(
meta.userOrGroupExists('John Smith', (err, exists) => { meta.userOrGroupExists(['', undefined]),
assert.ifError(err); { message: '[[error:invalid-data]]' },
assert(exists); );
done();
});
});
it('should return false if user/group does not exists', (done) => {
meta.userOrGroupExists('doesnot exist', (err, exists) => {
assert.ifError(err);
assert(!exists);
done();
});
}); });
it('should delete user', async () => { it('should delete user', async () => {