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

Class UserModel

packages/hydrooj/src/model/user.ts:201–510  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

199}
200
201class UserModel {
202 static coll = coll;
203 static collGroup = collGroup;
204 static User = User;
205 static cache = cache;
206 static defaultUser: Udoc = {
207 _id: 0,
208 uname: 'Unknown User',
209 unameLower: 'unknown user',
210 avatar: 'gravatar:unknown@hydro.local',
211 mail: 'unknown@hydro.local',
212 mailLower: 'unknown@hydro.local',
213 salt: '',
214 hash: '',
215 hashType: 'hydro',
216 priv: 0,
217 perm: 0n,
218 regat: new Date('2000-01-01'),
219 loginat: new Date('2000-01-01'),
220 ip: ['127.0.0.1'],
221 loginip: '127.0.0.1',
222 };
223
224 static _handleMailLower = handleMailLower;
225 static _deleteUserCache = deleteUserCache;
226
227 @ArgMethod
228 static async getById(domainId: string, _id: number, scope: bigint | string = PERM.PERM_ALL): Promise<User> {
229 if (cache.has(`id/${_id}/${domainId}`)) return cache.get(`id/${_id}/${domainId}`) || null;
230 const udoc = await (_id < -999 ? collV : coll).findOne({ _id });
231 if (!udoc) return null;
232 const [dudoc, groups] = await Promise.all([
233 domain.getDomainUser(domainId, udoc),
234 UserModel.listGroup(domainId, _id),
235 ]);
236 dudoc.group = groups.map((i) => i.name);
237 if (typeof scope === 'string') scope = BigInt(scope);
238 return initAndCache(udoc, dudoc, scope);
239 }
240
241 static async getList(domainId: string, uids: number[]): Promise<Udict> {
242 const r: Udict = {};
243 await Promise.all(uniq(uids).map(async (uid) => {
244 r[uid] = (await UserModel.getById(domainId, uid)) || new User(UserModel.defaultUser, {});
245 }));
246 return r;
247 }
248
249 @ArgMethod
250 static async getByUname(domainId: string, uname: string): Promise<User | null> {
251 const unameLower = uname.trim().toLowerCase();
252 if (cache.has(`name/${unameLower}/${domainId}`)) return cache.get(`name/${unameLower}/${domainId}`);
253 const udoc = (await coll.findOne({ unameLower })) || await collV.findOne({ unameLower });
254 if (!udoc) return null;
255 const dudoc = await domain.getDomainUser(domainId, udoc);
256 return initAndCache(udoc, dudoc);
257 }
258

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected