core: better pid sort method

This commit is contained in:
undefined committed 2021-11-25 19:35:37 +08:00
1 parent 06a247f819
commit 1a857ae40a
5 files changed
+27 -5

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "3.1.35",
"version": "3.1.36",
"bin": "bin/hydrooj.js",
"main": "src/loader",
"module": "src/loader",
+1 -1
View File
@@ -148,7 +148,7 @@ 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({ pid: 1, docId: 1 }),
problem.getMulti(domainId, query).sort({ sort: 1, docId: 1 }),
page,
system.get('pagination.problem'),
);
+1
View File
@@ -242,6 +242,7 @@ declare module './model/problem' {
html?: boolean;
stats?: any;
difficulty?: number;
sort?: string;
reference?: {
domainId: string;
pid: number;
+12 -3
View File
@@ -21,6 +21,10 @@ import storage from './storage';
export interface ProblemDoc extends Document { }
export type Field = keyof ProblemDoc;
function sortable(source: string) {
return source.replace(/(\d+)/g, (str) => '0'.repeat(6 - str.length) + str);
}
export class ProblemModel {
static PROJECTION_LIST: Field[] = [
'_id', 'domainId', 'docType', 'docId', 'pid',
@@ -92,7 +96,7 @@ export class ProblemModel {
content: string, owner: number, tag: string[] = [], hidden = false,
) {
const args: Partial<ProblemDoc> = {
title, tag, hidden, nSubmit: 0, nAccept: 0,
title, tag, hidden, nSubmit: 0, nAccept: 0, sort: sortable(pid || `P${docId}`),
};
if (pid) args.pid = pid;
await bus.serial('problem/before-add', domainId, content, owner, docId, args);
@@ -125,7 +129,7 @@ export class ProblemModel {
}
static getMulti(domainId: string, query: FilterQuery<ProblemDoc>, projection = ProblemModel.PROJECTION_LIST) {
return document.getMulti(domainId, document.TYPE_PROBLEM, query, projection);
return document.getMulti(domainId, document.TYPE_PROBLEM, query, projection).sort({ sort: 1 });
}
static getStatus(domainId: string, docId: number, uid: number) {
@@ -138,7 +142,12 @@ export class ProblemModel {
static async edit(domainId: string, _id: number, $set: Partial<ProblemDoc>): Promise<ProblemDoc> {
const delpid = $set.pid === '';
if (delpid) delete $set.pid;
if (delpid) {
delete $set.pid;
$set.sort = sortable(`P${_id}`);
} else if ($set.pid) {
$set.sort = sortable($set.pid);
}
await bus.serial('problem/before-edit', $set);
const result = await document.set(domainId, document.TYPE_PROBLEM, _id, $set, delpid ? { pid: '' } : undefined);
await bus.emit('problem/edit', result);
+12
View File
@@ -648,6 +648,18 @@ const scripts: UpgradeScript[] = [
}
return true;
},
async function _54_55() {
const _FRESH_INSTALL_IGNORE = 1;
const bulk = db.collection('document').initializeUnorderedBulkOp();
function sortable(source: string) {
return source.replace(/(\d+)/g, (str) => '0'.repeat(6 - str.length) + str);
}
await iterateAllProblem(['pid', '_id'], async (pdoc) => {
bulk.find({ _id: pdoc._id }).updateOne({ $set: { sort: sortable(pdoc.pid || `P${pdoc.docId}`) } });
});
if (bulk.length) await bulk.execute();
return true;
},
];
export default scripts;