mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: allow cross-domain problem search
This commit is contained in:
1 parent
71a5f6dd13
commit
a0734a1030
11 files changed
+120
-53
No files matched your search
@@ -116,6 +116,7 @@ if (argv.args[0] === 'restore') {
|
||||
fs.copySync(`${dir}/env`, `${os.homedir()}/.hydro/env`, { overwrite: true });
|
||||
}
|
||||
fs.removeSync(dir);
|
||||
console.log('Successfully restored.');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hydrooj",
|
||||
"version": "3.1.47",
|
||||
"version": "3.2.0",
|
||||
"bin": "bin/hydrooj.js",
|
||||
"main": "src/loader",
|
||||
"module": "src/loader",
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -367,6 +367,7 @@ export interface DomainDoc extends Record<string, any> {
|
||||
export interface DomainUnion {
|
||||
_id: string;
|
||||
union: string[];
|
||||
problem: boolean;
|
||||
}
|
||||
|
||||
// Message
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+35
-11
@@ -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;
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hydrooj/sonic",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.0",
|
||||
"description": "Sonic search service",
|
||||
"main": "service.js",
|
||||
"typings": "service.d.ts",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import DomainModel from 'hydrooj/src/model/domain';
|
||||
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/src/pipelineUtils';
|
||||
import sonic from './service';
|
||||
|
||||
@@ -10,10 +11,15 @@ export async function run({ domainId }, report) {
|
||||
const cb = async (pdoc) => {
|
||||
i++;
|
||||
if (!(i % 1000)) report({ message: `${i} problems indexed` });
|
||||
await Promise.all([
|
||||
sonic.push('problem', `${pdoc.domainId}@title`, pdoc.docId.toString(), pdoc.title),
|
||||
sonic.push('problem', `${pdoc.domainId}@content`, pdoc.docId.toString(), pdoc.content.toString()),
|
||||
]).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
|
||||
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
|
||||
const tasks = [];
|
||||
for (const did of [domainId, ...union.map((j) => j._id)]) {
|
||||
tasks.push(
|
||||
pdoc.title && sonic.push('problem', `${did}@title`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.title),
|
||||
pdoc.content.toString() && sonic.push('problem', `${did}@content`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.content.toString()),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
|
||||
};
|
||||
if (domainId) await iterateAllProblemInDomain(domainId, ['title', 'content'], cb);
|
||||
else await iterateAllProblem(['title', 'content'], cb);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hydrooj/ui-default",
|
||||
"version": "4.26.26",
|
||||
"version": "4.27.0",
|
||||
"author": "undefined <i@undefined.moe>",
|
||||
"license": "AGPL-3.0",
|
||||
"main": "hydro.js",
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
{% macro render_problem_title(pdoc, tdoc=none, show_tags=true, show_invisible_flag=true, invalid=false, inline=false) %}
|
||||
{%- if not invalid %}
|
||||
<a
|
||||
{% if tdoc -%}
|
||||
href="{{ url('problem_detail', pid=pdoc.pid|default(pdoc.docId), query={tid:tdoc.docId}) }}"
|
||||
{%- else -%}
|
||||
href="{{ url('problem_detail', pid=pdoc.pid|default(pdoc.docId)) }}"
|
||||
{%- endif %}
|
||||
>
|
||||
{%- endif %}
|
||||
{{ pdoc.pid if pdoc.pid else 'P'+pdoc.docId }}. {{ pdoc.title }}
|
||||
{%- if not invalid %}
|
||||
{%- if not invalid -%}
|
||||
{% set _linkArgs = { pid:pdoc.pid|default(pdoc.docId) } %}
|
||||
{% if tdoc %}{{ set(_linkArgs, 'query', {tid:tdoc.docId}) }}{% endif %}
|
||||
{% if pdoc.domainId !== handler.domainId %}{{ set(_linkArgs, 'domainId', pdoc.domainId) }}{% endif %}
|
||||
<a href="{{ url('problem_detail', _linkArgs) }}">
|
||||
{%- endif -%}
|
||||
{%- if pdoc.domainId !== handler.domainId -%}
|
||||
{{ pdoc.domainId }}#
|
||||
{%- endif -%}
|
||||
{%- if pdoc.pid -%}
|
||||
{{ pdoc.pid }}
|
||||
{%- else -%}
|
||||
{{ 'P'+pdoc.docId if pdoc.domainId === handler.domainId else pdoc.docId }}
|
||||
{%- endif -%}
|
||||
. {{ pdoc.title }}
|
||||
{%- if not invalid -%}
|
||||
</a>
|
||||
{%- endif %}
|
||||
{%- if pdoc.hidden and show_invisible_flag %}
|
||||
{%- endif -%}
|
||||
{%- if pdoc.hidden and show_invisible_flag -%}
|
||||
<span class="text-orange">({{ _('Hidden') }})</span>
|
||||
{%- endif %}
|
||||
{%- if show_tags %}
|
||||
{%- endif -%}
|
||||
{%- if show_tags -%}
|
||||
{{ render_problem_tags(pdoc, inline=inline) }}
|
||||
{%- endif %}
|
||||
{%- endif -%}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro render_problem_tags(pdoc, show_none_label=false, inline=false) %}
|
||||
|
||||
Reference in new issue
Block a user