core: add @subscribe decorator

This commit is contained in:
undefined committed 2023-01-30 07:27:45 +08:00
1 parent 88461a78cd
commit 3807d12168
7 files changed
+31 -35

No files matched your search

+2 -11
View File
@@ -27,9 +27,8 @@ import * as system from '../model/system';
import token from '../model/token';
import * as training from '../model/training';
import user from '../model/user';
import * as bus from '../service/bus';
import {
ConnectionHandler, Handler, param, query, requireSudo, Types,
ConnectionHandler, Handler, param, query, requireSudo, subscribe, Types,
} from '../service/server';
import { camelCase, md5 } from '../utils';
@@ -519,22 +518,14 @@ class HomeMessagesHandler extends Handler {
class HomeMessagesConnectionHandler extends ConnectionHandler {
category = '#message';
dispose: bus.Disposable;
async prepare() {
this.dispose = bus.on('user/message', this.onMessageReceived.bind(this));
}
@subscribe('user/message')
async onMessageReceived(uid: number, mdoc: MessageDoc) {
if (uid !== this.user._id) return;
const udoc = (await user.getById(this.args.domainId, mdoc.from))!;
udoc.avatarUrl = avatar(udoc.avatar, 64);
this.send({ udoc, mdoc });
}
async cleanup() {
if (this.dispose) this.dispose();
}
}
export async function apply(ctx: Context) {
+5 -6
View File
@@ -30,7 +30,6 @@ import solution from '../model/solution';
import storage from '../model/storage';
import * as system from '../model/system';
import user from '../model/user';
import * as bus from '../service/bus';
import {
Handler, param, post, query, route, Types,
} from '../service/server';
@@ -168,7 +167,7 @@ export class ProblemMainHandler extends Handler {
});
sort = result.hits;
}
await bus.parallel('problem/list', query, this);
await this.ctx.parallel('problem/list', query, this);
// eslint-disable-next-line prefer-const
let [pdocs, ppcount, pcount] = fail
? [[], 0, 0]
@@ -297,7 +296,7 @@ export class ProblemRandomHandler extends Handler {
.map((i) => i.split('category:')[1]?.split(',')));
const q = buildQuery(this.user);
if (category.length) q.$and = category.map((tag) => ({ tag }));
await bus.parallel('problem/list', q, this);
await this.ctx.parallel('problem/list', q, this);
const pid = await problem.random(domainId, q);
if (!pid) throw new NoProblemError();
this.response.body = { pid };
@@ -352,7 +351,7 @@ export class ProblemDetailHandler extends ContestDetailBaseHandler {
if (this.domain.langs) t.push(this.domain.langs.split(',').map((i) => i.trim()).filter((i) => i));
this.pdoc.config.langs = intersection(baseLangs, ...t);
}
await bus.parallel('problem/get', this.pdoc, this);
await this.ctx.parallel('problem/get', this.pdoc, this);
[this.psdoc, this.udoc] = await Promise.all([
problem.getStatus(domainId, this.pdoc.docId, this.user._id),
user.getById(domainId, this.pdoc.owner),
@@ -547,7 +546,7 @@ export class ProblemSubmitHandler extends ProblemDetailHandler {
tid && contest.updateStatus(domainId, tid, this.user._id, rid, this.pdoc.docId),
]);
}
bus.broadcast('record/change', rdoc);
this.ctx.broadcast('record/change', rdoc);
if (tid && !pretest && !contest.canShowSelfRecord.call(this, this.tdoc)) {
this.response.body = { tid };
this.response.redirect = this.url(this.tdoc.rule === 'homework' ? 'homework_detail' : 'contest_detail', { tid });
@@ -608,7 +607,7 @@ export class ProblemHackHandler extends ProblemDetailHandler {
);
const rdoc = await record.get(domainId, rid);
// TODO contest: update status;
bus.broadcast('record/change', rdoc);
this.ctx.broadcast('record/change', rdoc);
this.response.body = { rid };
this.response.redirect = this.url('record_detail', { rid });
}
+3 -4
View File
@@ -1,9 +1,9 @@
import { Context } from '../context';
import { PRIV } from '../model/builtin';
import * as DocumentModel from '../model/document';
import DomainModel from '../model/domain';
import RecordModel from '../model/record';
import UserModel from '../model/user';
import * as bus from '../service/bus';
import db from '../service/db';
import { Handler } from '../service/server';
@@ -70,10 +70,9 @@ class StatusUpdateHandler extends Handler {
}
}
bus.on('ready', () => coll.createIndex('updateAt', { expireAfterSeconds: 24 * 3600 }));
export async function apply(ctx) {
export async function apply(ctx: Context) {
ctx.Route('status', '/status', StatusHandler);
ctx.Route('status_admin', '/.status', AdminStatusHandler);
ctx.Route('status_update', '/status/update', StatusUpdateHandler);
await db.ensureIndexes(coll, { key: { updateAt: 1 }, expireAfterSeconds: 24 * 2600 });
}