core, ui, judge: rewrite http & ws layer (#352)

This commit is contained in:
undefined authored and GitHub committed 2022-05-03 20:22:12 +08:00
1 parent 3d2384f31a
commit 5af082eac8
37 files changed
+868 -889

No files matched your search

+2 -2
View File
@@ -40,8 +40,8 @@ registerResolver(
'Query', 'contest(id: ObjectID!)', 'Contest',
async (arg, ctx) => {
arg.id = new ObjectID(arg.id);
ctx.tdoc = await contest.get(ctx.domainId, new ObjectID(arg.id));
if (!ctx.tdoc) throw new ContestNotFoundError(ctx.domainId, arg.id);
ctx.tdoc = await contest.get(ctx.args.domainId, new ObjectID(arg.id));
if (!ctx.tdoc) throw new ContestNotFoundError(ctx.args.domainId, arg.id);
return ctx.tdoc;
},
'Get a contest by ID',
+1 -1
View File
@@ -422,7 +422,7 @@ class HomeMessagesConnectionHandler extends ConnectionHandler {
async onMessageReceived(uid: number, mdoc: MessageDoc) {
if (uid !== this.user._id) return;
const udoc = (await user.getById(this.domainId, mdoc.from))!;
const udoc = (await user.getById(this.args.domainId, mdoc.from))!;
udoc.avatarUrl = avatar(udoc.avatar, 64);
this.send({ udoc, mdoc });
}
+9 -9
View File
@@ -68,7 +68,7 @@ registerValue('Problem', [
registerResolver(
'Query', 'problem(id: Int, pid: String)', 'Problem',
async (arg, ctx) => {
const pdoc = await problem.get(ctx.domainId, arg.pid || arg.id);
const pdoc = await problem.get(ctx.args.domainId, arg.pid || arg.id);
if (!pdoc) return null;
if (pdoc.hidden) ctx.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
if (pdoc.assign.length && !ctx.user.own(pdoc)) {
@@ -90,14 +90,14 @@ registerResolver(
registerResolver(
'ProblemManage', 'delete', 'Boolean!',
async (arg, ctx) => {
const tdocs = await contest.getRelated(ctx.domainId, ctx.pdoc.docId);
const tdocs = await contest.getRelated(ctx.args.domainId, ctx.pdoc.docId);
if (tdocs.length) throw new BadRequestError('Problem already used by contest {0}', tdocs[0]._id);
return problem.del(ctx.pdoc.domainId, ctx.pdoc.docId);
},
);
registerResolver(
'ProblemManage', 'edit(title: String, content: String, tag: [String], hidden: Boolean, assign: [String])', 'Problem!',
(arg, ctx) => problem.edit(ctx.domainId, ctx.pdoc.docId, arg),
(arg, ctx) => problem.edit(ctx.args.domainId, ctx.pdoc.docId, arg),
);
function buildQuery(udoc: User) {
@@ -150,7 +150,7 @@ export class ProblemMainHandler extends ProblemHandler {
let pcountRelation = 'eq';
if (category.length) query.$and = category.map((tag) => ({ tag }));
if (q) category.push(q);
if (category.length) this.extraTitleContent = category.join(',');
if (category.length) this.UiContext.extraTitleContent = category.join(',');
let total = 0;
if (q) {
if (search) {
@@ -360,7 +360,7 @@ export class ProblemDetailHandler extends ProblemHandler {
tsdoc: this.tsdoc,
};
this.response.template = 'problem_detail.html';
this.extraTitleContent = this.pdoc.title;
this.UiContext.extraTitleContent = this.pdoc.title;
}
@query('tid', Types.ObjectID, true)
@@ -402,9 +402,9 @@ export class ProblemDetailHandler extends ProblemHandler {
}
if (!this.response.body.tdoc) {
if (this.psdoc?.rid) {
this.response.body.rdoc = await record.get(this.domainId, this.psdoc.rid);
this.response.body.rdoc = await record.get(this.args.domainId, this.psdoc.rid);
}
this.response.body.ctdocs = await contest.getRelated(this.domainId, this.pdoc.docId);
this.response.body.ctdocs = await contest.getRelated(this.args.domainId, this.pdoc.docId);
}
}
@@ -436,7 +436,7 @@ export class ProblemDetailHandler extends ProblemHandler {
async postDelete() {
if (!this.user.own(this.pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM);
const tdocs = await contest.getRelated(this.domainId, this.pdoc.docId);
const tdocs = await contest.getRelated(this.args.domainId, this.pdoc.docId);
if (tdocs.length) throw new BadRequestError('Problem already used by contest {0}', tdocs[0]._id);
await problem.del(this.pdoc.domainId, this.pdoc.docId);
this.response.redirect = this.url('problem_main');
@@ -500,7 +500,7 @@ export class ProblemSubmitHandler extends ProblemDetailHandler {
const rdoc = await record.get(domainId, rid);
if (!pretest) {
await Promise.all([
problem.inc(this.domainId, this.pdoc.docId, 'nSubmit', 1),
problem.inc(domainId, this.pdoc.docId, 'nSubmit', 1),
problem.incStatus(domainId, this.pdoc.docId, this.user._id, 'nSubmit', 1),
domain.incUserInDomain(domainId, this.user._id, 'nSubmit'),
tid && contest.updateStatus(domainId, tid, this.user._id, rid, this.pdoc.docId),
+3 -3
View File
@@ -250,14 +250,14 @@ class RecordMainConnectionHandler extends ConnectionHandler {
async message(msg: { rids: string[] }) {
if (!(msg.rids instanceof Array)) return;
const rids = msg.rids.map((id) => new ObjectID(id));
const rdocs = await record.getMulti(this.domainId, { _id: { $in: rids } }).project(buildProjection(record.PROJECTION_LIST)).toArray();
const rdocs = await record.getMulti(this.args.domainId, { _id: { $in: rids } }).project(buildProjection(record.PROJECTION_LIST)).toArray();
for (const rdoc of rdocs) this.onRecordChange(rdoc);
}
async onRecordChange(rdoc: RecordDoc) {
if (!this.all) {
if (!this.pretest && rdoc.input) return;
if (rdoc.domainId !== this.domainId) return;
if (rdoc.domainId !== this.args.domainId) return;
if (rdoc.contest && ![this.tid, '000000000000000000000000'].includes(rdoc.contest.toString())) return;
if (this.tid && contest.isLocked(this.tdoc)) return;
if (this.tid && !contest.canShowSelfRecord.call(this, this.tdoc, true)) return;
@@ -267,7 +267,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
user.getById(this.args.domainId, rdoc.uid),
problem.get(rdoc.domainId, rdoc.pid),
]);
const tdoc = this.tid ? this.tdoc || await contest.get(rdoc.domainId, new ObjectID(this.tid)) : null;
+9 -9
View File
@@ -71,25 +71,25 @@ registerResolver(
);
registerResolver('Query', 'user(id: Int, uname: String, mail: String)', 'User', (arg, ctx) => {
if (arg.id) return user.getById(ctx.domainId, arg.id);
if (arg.mail) return user.getByEmail(ctx.domainId, arg.mail);
if (arg.uname) return user.getByUname(ctx.domainId, arg.uname);
if (arg.id) return user.getById(ctx.args.domainId, arg.id);
if (arg.mail) return user.getByEmail(ctx.args.domainId, arg.mail);
if (arg.uname) return user.getByUname(ctx.args.domainId, arg.uname);
return ctx.user;
}, `Get a user by id, uname, or mail.
Returns current user if no argument is provided.`);
registerResolver('Query', 'users(ids: [Int], search: String, limit: Int, exact: Boolean)', '[User]', async (arg, ctx) => {
if (arg.ids?.length) {
const res = await user.getList(ctx.domainId, arg.ids);
const res = await user.getList(ctx.args.domainId, arg.ids);
return Object.keys(res).map((id) => res[+id]);
}
if (!arg.search) return [];
const udoc = await user.getById(ctx.domainId, +arg.search)
|| await user.getByUname(ctx.domainId, arg.search)
|| await user.getByEmail(ctx.domainId, arg.search);
const udoc = await user.getById(ctx.args.domainId, +arg.search)
|| await user.getByUname(ctx.args.domainId, arg.search)
|| await user.getByEmail(ctx.args.domainId, arg.search);
const udocs: User[] = arg.exact
? []
: await user.getPrefixList(ctx.domainId, arg.search, Math.min(arg.limit || 10, 10));
: await user.getPrefixList(ctx.args.domainId, arg.search, Math.min(arg.limit || 10, 10));
if (udoc && !udocs.find((i) => i._id === udoc._id)) {
udocs.pop();
udocs.unshift(udoc);
@@ -329,7 +329,7 @@ class UserDetailHandler extends Handler {
this.response.body = {
isSelfProfile, udoc, sdoc, pdocs, tags,
};
this.extraTitleContent = udoc.uname;
this.UiContext.extraTitleContent = udoc.uname;
}
}