mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +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.copySync(`${dir}/env`, `${os.homedir()}/.hydro/env`, { overwrite: true });
|
||||||
}
|
}
|
||||||
fs.removeSync(dir);
|
fs.removeSync(dir);
|
||||||
|
console.log('Successfully restored.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "3.1.47",
|
"version": "3.2.0",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -128,17 +128,22 @@ export class ProblemMainHandler extends ProblemHandler {
|
|||||||
const query: FilterQuery<ProblemDoc> = {};
|
const query: FilterQuery<ProblemDoc> = {};
|
||||||
let psdict = {};
|
let psdict = {};
|
||||||
const search = global.Hydro.lib.problemSearch;
|
const search = global.Hydro.lib.problemSearch;
|
||||||
let sort: number[];
|
let sort: string[];
|
||||||
if (category.length) {
|
let fail = false;
|
||||||
query.$and = [];
|
if (category.length) query.$and = category.map((tag) => ({ tag }));
|
||||||
for (const tag of category) query.$and.push({ tag });
|
|
||||||
}
|
|
||||||
if (q) category.push(q);
|
if (q) category.push(q);
|
||||||
if (category.length) this.extraTitleContent = category.join(',');
|
if (category.length) this.extraTitleContent = category.join(',');
|
||||||
if (q) {
|
if (q) {
|
||||||
if (search) {
|
if (search) {
|
||||||
const result = await search(domainId, q);
|
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;
|
sort = result;
|
||||||
} else query.$text = { $search: q };
|
} else query.$text = { $search: q };
|
||||||
}
|
}
|
||||||
@@ -147,26 +152,21 @@ export class ProblemMainHandler extends ProblemHandler {
|
|||||||
}
|
}
|
||||||
await bus.serial('problem/list', query, this);
|
await bus.serial('problem/list', query, this);
|
||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
let [pdocs, ppcount, pcount] = await paginate(
|
let [pdocs, ppcount, pcount] = fail
|
||||||
problem.getMulti(domainId, query).sort({ sort: 1, docId: 1 }),
|
? [[], 0, 0]
|
||||||
page,
|
: await problem.list(
|
||||||
system.get('pagination.problem'),
|
domainId, query, undefined,
|
||||||
);
|
page, system.get('pagination.problem'),
|
||||||
if (sort) pdocs = pdocs.sort((a, b) => sort.indexOf(a.docId) - sort.indexOf(b.docId));
|
);
|
||||||
|
if (sort) pdocs = pdocs.sort((a, b) => sort.indexOf(`${a.domainId}/${a.docId}`) - sort.indexOf(`${b.domainId}/${b.docId}`));
|
||||||
if (q) {
|
if (q) {
|
||||||
const pdoc = await problem.get(domainId, +q || q, problem.PROJECTION_LIST);
|
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;
|
const count = pdocs.length;
|
||||||
pdocs = pdocs.filter((doc) => doc.docId !== pdoc.docId);
|
pdocs = pdocs.filter((doc) => doc.docId !== pdoc.docId);
|
||||||
pdocs.unshift(pdoc);
|
pdocs.unshift(pdoc);
|
||||||
pcount = pcount - count + pdocs.length;
|
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)) {
|
if (this.user.hasPriv(PRIV.PRIV_USER_PROFILE)) {
|
||||||
psdict = await problem.getListStatus(
|
psdict = await problem.getListStatus(
|
||||||
|
|||||||
@@ -367,6 +367,7 @@ export interface DomainDoc extends Record<string, any> {
|
|||||||
export interface DomainUnion {
|
export interface DomainUnion {
|
||||||
_id: string;
|
_id: string;
|
||||||
union: string[];
|
union: string[];
|
||||||
|
problem: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message
|
// Message
|
||||||
|
|||||||
@@ -275,6 +275,11 @@ class DomainModel {
|
|||||||
static async getUnion(domainId: string) {
|
static async getUnion(domainId: string) {
|
||||||
return await collUnion.findOne({ _id: domainId });
|
return await collUnion.findOne({ _id: domainId });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ArgMethod
|
||||||
|
static async searchUnion(query) {
|
||||||
|
return await collUnion.find(query).toArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bus.once('app/started', async () => {
|
bus.once('app/started', async () => {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
import { buildProjection } from '../utils';
|
import { buildProjection } from '../utils';
|
||||||
import { STATUS } from './builtin';
|
import { STATUS } from './builtin';
|
||||||
import * as document from './document';
|
import * as document from './document';
|
||||||
|
import DomainModel from './domain';
|
||||||
import storage from './storage';
|
import storage from './storage';
|
||||||
|
|
||||||
export interface ProblemDoc extends Document { }
|
export interface ProblemDoc extends Document { }
|
||||||
@@ -132,6 +133,30 @@ export class ProblemModel {
|
|||||||
return document.getMulti(domainId, document.TYPE_PROBLEM, query, projection).sort({ sort: 1 });
|
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) {
|
static getStatus(domainId: string, docId: number, uid: number) {
|
||||||
return document.getStatus(domainId, document.TYPE_PROBLEM, docId, uid);
|
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 system from 'hydrooj/src/model/system';
|
||||||
import * as bus from 'hydrooj/src/service/bus';
|
import * as bus from 'hydrooj/src/service/bus';
|
||||||
import sonic from './service';
|
import sonic from './service';
|
||||||
|
|
||||||
bus.on('problem/add', async (doc, docId) => {
|
bus.on('problem/add', async (doc, docId) => {
|
||||||
await sonic.push('problem', `${doc.domainId}@title`, docId.toString(), doc.title);
|
const union = await DomainModel.searchUnion({ union: doc.domainId, problem: true });
|
||||||
await sonic.push('problem', `${doc.domainId}@content`, docId.toString(), doc.content.toString());
|
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) => {
|
bus.on('problem/edit', async (pdoc) => {
|
||||||
await sonic.flusho('problem', `${pdoc.domainId}@title`, pdoc.docId.toString());
|
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
|
||||||
await sonic.flusho('problem', `${pdoc.domainId}@content`, pdoc.docId.toString());
|
const tasks = [];
|
||||||
await sonic.push('problem', `${pdoc.domainId}@title`, pdoc.docId.toString(), pdoc.title);
|
const id = `${pdoc.domainId}/${pdoc.docId}`;
|
||||||
await sonic.push('problem', `${pdoc.domainId}@content`, pdoc.docId.toString(), pdoc.content.toString());
|
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) => {
|
bus.on('problem/del', async (domainId, docId) => {
|
||||||
await sonic.flusho('problem', `${domainId}@title`, docId.toString());
|
const union = await DomainModel.searchUnion({ union: domainId, problem: true });
|
||||||
await sonic.flusho('problem', `${domainId}@content`, docId.toString());
|
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')) => {
|
global.Hydro.lib.problemSearch = async (domainId: string, query: string, limit = system.get('pagination.problem')) => {
|
||||||
const docIds = await sonic.query('problem', `${domainId}@title`, query, { limit });
|
const ids = 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 }));
|
if (limit - ids.length > 0) ids.push(...await sonic.query('problem', `${domainId}@content`, query, { limit: limit - ids.length }));
|
||||||
return docIds.map(Number);
|
return ids;
|
||||||
};
|
};
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/sonic",
|
"name": "@hydrooj/sonic",
|
||||||
"version": "1.1.1",
|
"version": "1.2.0",
|
||||||
"description": "Sonic search service",
|
"description": "Sonic search service",
|
||||||
"main": "service.js",
|
"main": "service.js",
|
||||||
"typings": "service.d.ts",
|
"typings": "service.d.ts",
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import DomainModel from 'hydrooj/src/model/domain';
|
||||||
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/src/pipelineUtils';
|
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/src/pipelineUtils';
|
||||||
import sonic from './service';
|
import sonic from './service';
|
||||||
|
|
||||||
@@ -10,10 +11,15 @@ export async function run({ domainId }, report) {
|
|||||||
const cb = async (pdoc) => {
|
const cb = async (pdoc) => {
|
||||||
i++;
|
i++;
|
||||||
if (!(i % 1000)) report({ message: `${i} problems indexed` });
|
if (!(i % 1000)) report({ message: `${i} problems indexed` });
|
||||||
await Promise.all([
|
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
|
||||||
sonic.push('problem', `${pdoc.domainId}@title`, pdoc.docId.toString(), pdoc.title),
|
const tasks = [];
|
||||||
sonic.push('problem', `${pdoc.domainId}@content`, pdoc.docId.toString(), pdoc.content.toString()),
|
for (const did of [domainId, ...union.map((j) => j._id)]) {
|
||||||
]).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
|
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);
|
if (domainId) await iterateAllProblemInDomain(domainId, ['title', 'content'], cb);
|
||||||
else await iterateAllProblem(['title', 'content'], cb);
|
else await iterateAllProblem(['title', 'content'], cb);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/ui-default",
|
"name": "@hydrooj/ui-default",
|
||||||
"version": "4.26.26",
|
"version": "4.27.0",
|
||||||
"author": "undefined <i@undefined.moe>",
|
"author": "undefined <i@undefined.moe>",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"main": "hydro.js",
|
"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) %}
|
{% macro render_problem_title(pdoc, tdoc=none, show_tags=true, show_invisible_flag=true, invalid=false, inline=false) %}
|
||||||
{%- if not invalid %}
|
{%- if not invalid -%}
|
||||||
<a
|
{% set _linkArgs = { pid:pdoc.pid|default(pdoc.docId) } %}
|
||||||
{% if tdoc -%}
|
{% if tdoc %}{{ set(_linkArgs, 'query', {tid:tdoc.docId}) }}{% endif %}
|
||||||
href="{{ url('problem_detail', pid=pdoc.pid|default(pdoc.docId), query={tid:tdoc.docId}) }}"
|
{% if pdoc.domainId !== handler.domainId %}{{ set(_linkArgs, 'domainId', pdoc.domainId) }}{% endif %}
|
||||||
{%- else -%}
|
<a href="{{ url('problem_detail', _linkArgs) }}">
|
||||||
href="{{ url('problem_detail', pid=pdoc.pid|default(pdoc.docId)) }}"
|
{%- endif -%}
|
||||||
{%- endif %}
|
{%- if pdoc.domainId !== handler.domainId -%}
|
||||||
>
|
{{ pdoc.domainId }}#
|
||||||
{%- endif %}
|
{%- endif -%}
|
||||||
{{ pdoc.pid if pdoc.pid else 'P'+pdoc.docId }}. {{ pdoc.title }}
|
{%- if pdoc.pid -%}
|
||||||
{%- if not invalid %}
|
{{ pdoc.pid }}
|
||||||
|
{%- else -%}
|
||||||
|
{{ 'P'+pdoc.docId if pdoc.domainId === handler.domainId else pdoc.docId }}
|
||||||
|
{%- endif -%}
|
||||||
|
. {{ pdoc.title }}
|
||||||
|
{%- if not invalid -%}
|
||||||
</a>
|
</a>
|
||||||
{%- endif %}
|
{%- endif -%}
|
||||||
{%- if pdoc.hidden and show_invisible_flag %}
|
{%- if pdoc.hidden and show_invisible_flag -%}
|
||||||
<span class="text-orange">({{ _('Hidden') }})</span>
|
<span class="text-orange">({{ _('Hidden') }})</span>
|
||||||
{%- endif %}
|
{%- endif -%}
|
||||||
{%- if show_tags %}
|
{%- if show_tags -%}
|
||||||
{{ render_problem_tags(pdoc, inline=inline) }}
|
{{ render_problem_tags(pdoc, inline=inline) }}
|
||||||
{%- endif %}
|
{%- endif -%}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro render_problem_tags(pdoc, show_none_label=false, inline=false) %}
|
{% macro render_problem_tags(pdoc, show_none_label=false, inline=false) %}
|
||||||
|
|||||||
Reference in new issue
Block a user