core: fix user_search handler

This commit is contained in:
undefined committed 2021-05-04 13:24:03 +08:00
1 parent f3153993f7
commit ff6777a8c0
4 files changed
+14 -29

No files matched your search

-19
View File
@@ -1,19 +0,0 @@
import 'hydrooj';
import 'hydrooj/src/lib/i18n';
let lib;
try {
lib = require('@hydrooj/geoip/lib');
} catch (e) {
console.warn('Maxmind db load fail');
}
describe('geoip', () => {
test('lookup', () => {
if (lib) {
expect(lib.lookup('127.0.0.1', 'zh_CN').display).toStrictEqual('Unknown address');
expect(lib.lookup('1.1.1.1', 'zh-CN').display).toStrictEqual('大洋洲 澳大利亚');
expect(lib.provider.includes('MaxMind')).toBeTruthy();
}
});
});
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.24.8",
"version": "2.24.9",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",
+9 -5
View File
@@ -237,12 +237,16 @@ class UserSearchHandler extends Handler {
@param('q', Types.String)
@param('exectMatch', Types.Boolean)
async get(domainId: string, q: string, exactMatch = false) {
let udoc = await user.getById(domainId, parseInt(q, 10));
let udoc = await user.getById(domainId, +q);
const udocs: User[] = udoc ? [udoc] : [];
udoc = await user.getByUname(domainId, q);
if (udoc) udocs.push(udoc);
udoc = await user.getByEmail(domainId, q);
if (udoc) udocs.push(udoc);
if (!udocs.length) {
udoc = await user.getByUname(domainId, q);
if (udoc) udocs.push(udoc);
else {
udoc = await user.getByEmail(domainId, q);
if (udoc) udocs.push(udoc);
}
}
if (!exactMatch) udocs.push(...await user.getPrefixList(domainId, q, 20));
for (const i in udocs) {
udocs[i].avatarUrl = avatar(udocs[i].avatar);
+4 -4
View File
@@ -276,11 +276,11 @@ class UserModel {
@ArgMethod
static async getPrefixList(domainId: string, prefix: string, limit: number = 50) {
prefix = prefix.toLowerCase();
const $regex = new RegExp(`^${escapeRegExp(prefix)}`, 'gmi');
const uids = await coll.find({ unameLower: { $regex } })
.limit(limit).map((doc) => doc._id).toArray();
const $regex = new RegExp(`\\A${escapeRegExp(prefix)}`, 'gmi');
const udocs = await coll.find({ unameLower: { $regex } })
.limit(limit).project({ _id: 1 }).toArray();
const users = [];
for (const uid of uids) users.push(UserModel.getById(domainId, uid));
for (const { _id } of udocs) users.push(UserModel.getById(domainId, _id));
return await Promise.all(users);
}