MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / create

Method create

packages/hydrooj/src/model/user.ts:321–378  ·  view source on GitHub ↗
(
        mail: string, uname: string, password: string,
        uid?: number, regip: string = '127.0.0.1', priv: number = system.get('default.priv'),
    )

Source from the content-addressed store, hash-verified

319
320 @ArgMethod
321 static async create(
322 mail: string, uname: string, password: string,
323 uid?: number, regip: string = '127.0.0.1', priv: number = system.get('default.priv'),
324 ) {
325 let autoAlloc = false;
326 if (typeof uid !== 'number') {
327 const [udoc] = await coll.find({}).sort({ _id: -1 }).limit(1).toArray();
328 uid = Math.max((udoc?._id || 0) + 1, 2);
329 autoAlloc = true;
330 }
331 const salt = randomstring();
332 while (true) {
333 try {
334 // eslint-disable-next-line no-await-in-loop
335 await coll.insertOne({
336 _id: uid,
337 mail,
338 mailLower: handleMailLower(mail),
339 uname,
340 unameLower: uname.trim().toLowerCase(),
341 // eslint-disable-next-line no-await-in-loop
342 hash: await pwhash(password.toString(), salt),
343 salt,
344 hashType: 'hydro',
345 regat: new Date(),
346 ip: [regip],
347 loginat: new Date(),
348 loginip: regip,
349 priv,
350 avatar: `gravatar:${mail}`,
351 });
352 // eslint-disable-next-line no-await-in-loop
353 await domain.collUser.updateOne(
354 { uid, domainId: 'system' },
355 { $set: { join: true } },
356 { upsert: true },
357 );
358 // make sure user is immediately available after creation
359 // give some time for database to sync in replicas
360 for (let i = 1; i <= 10; i++) {
361 const udoc = await UserModel.getById('system', uid); // eslint-disable-line no-await-in-loop
362 if (udoc) break;
363 await sleep(500); // eslint-disable-line no-await-in-loop
364 }
365 return uid;
366 } catch (e) {
367 if (e?.code === 11000) {
368 // Duplicate Key Error
369 if (autoAlloc && JSON.stringify(e.keyPattern) === '{"_id":1}') {
370 uid++;
371 continue;
372 }
373 throw new UserAlreadyExistError(Object.values(e?.keyValue || {}));
374 }
375 throw e;
376 }
377 }
378 }

Callers 15

applyFunction · 0.80
MonacoContainerFunction · 0.80
initMonacoMethod · 0.80
componentDidMountMethod · 0.80
runFunction · 0.80
runFunction · 0.80
runFunction · 0.80
runFunction · 0.80
LoaderClass · 0.80
buildProjectionFunction · 0.80
upgrade.tsFile · 0.80
getUrlMethod · 0.80

Calls 6

randomstringFunction · 0.90
sleepFunction · 0.90
findMethod · 0.80
getByIdMethod · 0.80
handleMailLowerFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected