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

+19 -19
View File
@@ -128,17 +128,22 @@ export class ProblemMainHandler extends ProblemHandler {
const query: FilterQuery<ProblemDoc> = {};
let psdict = {};
const search = global.Hydro.lib.problemSearch;
let sort: number[];
if (category.length) {
query.$and = [];
for (const tag of category) query.$and.push({ tag });
}
let sort: string[];
let fail = false;
if (category.length) query.$and = category.map((tag) => ({ tag }));
if (q) category.push(q);
if (category.length) this.extraTitleContent = category.join(',');
if (q) {
if (search) {
const result = await search(domainId, q);
query.docId = { $in: result };
if (!result.length) fail = true;
if (!query.$and) query.$and = [];
query.$and.push({
$or: result.map((i) => {
const [did, docId] = i.split('/');
return { domainId: did, docId: +docId };
}),
});
sort = result;
} else query.$text = { $search: q };
}
@@ -147,26 +152,21 @@ export class ProblemMainHandler extends ProblemHandler {
}
await bus.serial('problem/list', query, this);
// eslint-disable-next-line prefer-const
let [pdocs, ppcount, pcount] = await paginate(
problem.getMulti(domainId, query).sort({ sort: 1, docId: 1 }),
page,
system.get('pagination.problem'),
);
if (sort) pdocs = pdocs.sort((a, b) => sort.indexOf(a.docId) - sort.indexOf(b.docId));
let [pdocs, ppcount, pcount] = fail
? [[], 0, 0]
: await problem.list(
domainId, query, undefined,
page, system.get('pagination.problem'),
);
if (sort) pdocs = pdocs.sort((a, b) => sort.indexOf(`${a.domainId}/${a.docId}`) - sort.indexOf(`${b.domainId}/${b.docId}`));
if (q) {
const pdoc = await problem.get(domainId, +q || q, problem.PROJECTION_LIST);
if (pdoc) {
if (pdoc && (!pdoc.hidden || this.user.own(pdoc) || this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN))) {
const count = pdocs.length;
pdocs = pdocs.filter((doc) => doc.docId !== pdoc.docId);
pdocs.unshift(pdoc);
pcount = pcount - count + pdocs.length;
}
const canViewHidden = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
if (!canViewHidden) {
const unshift = [];
for (const doc of pdocs) if (doc.hidden && !this.user.own(doc)) unshift.push(doc);
for (const task of unshift) pdocs.unshift(task);
}
}
if (this.user.hasPriv(PRIV.PRIV_USER_PROFILE)) {
psdict = await problem.getListStatus(
+1
View File
@@ -367,6 +367,7 @@ export interface DomainDoc extends Record<string, any> {
export interface DomainUnion {
_id: string;
union: string[];
problem: boolean;
}
// Message
+5
View File
@@ -275,6 +275,11 @@ class DomainModel {
static async getUnion(domainId: string) {
return await collUnion.findOne({ _id: domainId });
}
@ArgMethod
static async searchUnion(query) {
return await collUnion.find(query).toArray();
}
}
bus.once('app/started', async () => {
+25
View File
@@ -16,6 +16,7 @@ import {
import { buildProjection } from '../utils';
import { STATUS } from './builtin';
import * as document from './document';
import DomainModel from './domain';
import storage from './storage';
export interface ProblemDoc extends Document { }
@@ -132,6 +133,30 @@ export class ProblemModel {
return document.getMulti(domainId, document.TYPE_PROBLEM, query, projection).sort({ sort: 1 });
}
static async list(
domainId: string, query: FilterQuery<ProblemDoc>,
projection = ProblemModel.PROJECTION_LIST, page: number, pageSize: number,
): Promise<[ProblemDoc[], number, number]> {
const union = await DomainModel.getUnion(domainId);
const domainIds = [domainId];
if (union?.problem) domainIds.push(...union.union);
let count = 0;
const pdocs = [];
for (const id of domainIds) {
// TODO enhance performance
// eslint-disable-next-line no-await-in-loop
const ccount = await document.getMulti(id, document.TYPE_PROBLEM, query).count();
if (pdocs.length < pageSize && (page - 1) * pageSize - count <= ccount) {
// eslint-disable-next-line no-await-in-loop
pdocs.push(...await document.getMulti(id, document.TYPE_PROBLEM, query, projection)
.sort({ sort: 1, docId: 1 })
.skip(Math.max((page - 1) * pageSize - count, 0)).limit(pageSize - pdocs.length).toArray());
}
count += ccount;
}
return [pdocs, Math.ceil(count / pageSize), count];
}
static getStatus(domainId: string, docId: number, uid: number) {
return document.getStatus(domainId, document.TYPE_PROBLEM, docId, uid);
}