core: allow cross-domain problem search

This commit is contained in:
undefined committed 2021-12-01 22:28:22 +08:00
1 parent 71a5f6dd13
commit a0734a1030
11 files changed
+120 -53

No files matched your search

+35 -11
View File
@@ -1,26 +1,50 @@
import DomainModel from 'hydrooj/src/model/domain';
import * as system from 'hydrooj/src/model/system';
import * as bus from 'hydrooj/src/service/bus';
import sonic from './service';
bus.on('problem/add', async (doc, docId) => {
await sonic.push('problem', `${doc.domainId}@title`, docId.toString(), doc.title);
await sonic.push('problem', `${doc.domainId}@content`, docId.toString(), doc.content.toString());
const union = await DomainModel.searchUnion({ union: doc.domainId, problem: true });
const tasks = [];
for (const domainId of [doc.domainId, ...union.map((i) => i._id)]) {
tasks.push(
sonic.push('problem', `${domainId}@title`, `${doc.domainId}/${docId}`, doc.title),
sonic.push('problem', `${domainId}@content`, `${doc.domainId}/${docId}`, doc.content.toString()),
);
}
await Promise.all(tasks);
});
bus.on('problem/edit', async (pdoc) => {
await sonic.flusho('problem', `${pdoc.domainId}@title`, pdoc.docId.toString());
await sonic.flusho('problem', `${pdoc.domainId}@content`, pdoc.docId.toString());
await sonic.push('problem', `${pdoc.domainId}@title`, pdoc.docId.toString(), pdoc.title);
await sonic.push('problem', `${pdoc.domainId}@content`, pdoc.docId.toString(), pdoc.content.toString());
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
const tasks = [];
const id = `${pdoc.domainId}/${pdoc.docId}`;
for (const domainId of [pdoc.domainId, ...union.map((i) => i._id)]) {
tasks.push(
sonic.flusho('problem', `${domainId}@title`, id)
.then(() => sonic.push('problem', `${domainId}@title`, id, pdoc.title)),
sonic.flusho('problem', `${domainId}@content`, id)
.then(() => sonic.push('problem', `${domainId}@content`, id, pdoc.content.toString())),
);
}
await Promise.all(tasks);
});
bus.on('problem/del', async (domainId, docId) => {
await sonic.flusho('problem', `${domainId}@title`, docId.toString());
await sonic.flusho('problem', `${domainId}@content`, docId.toString());
const union = await DomainModel.searchUnion({ union: domainId, problem: true });
const tasks = [];
const id = `${domainId}/${docId}`;
for (const domain of [domainId, ...union.map((i) => i._id)]) {
tasks.push(
sonic.flusho('problem', `${domain}@title`, id),
sonic.flusho('problem', `${domain}@content`, id),
);
}
await Promise.all(tasks);
});
global.Hydro.lib.problemSearch = async (domainId: string, query: string, limit = system.get('pagination.problem')) => {
const docIds = await sonic.query('problem', `${domainId}@title`, query, { limit });
if (limit - docIds.length > 0) docIds.push(...await sonic.query('problem', `${domainId}@content`, query, { limit: limit - docIds.length }));
return docIds.map(Number);
const ids = await sonic.query('problem', `${domainId}@title`, query, { limit });
if (limit - ids.length > 0) ids.push(...await sonic.query('problem', `${domainId}@content`, query, { limit: limit - ids.length }));
return ids;
};