core: 修复strpid无法设置的问题

This commit is contained in:
undefined committed 2020-11-24 12:43:07 +08:00
1 parent 65094e53b6
commit 0195137c61
5 files changed
+27 -28

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.13.54", "version": "2.13.55",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+9 -14
View File
@@ -12,9 +12,7 @@ import {
Pdoc, User, Rdoc, PathComponent, Pdoc, User, Rdoc, PathComponent,
} from '../interface'; } from '../interface';
import paginate from '../lib/paginate'; import paginate from '../lib/paginate';
import { import { isTitle, isContent, isPid } from '../lib/validator';
checkPid, isTitle, isContent, isPid,
} from '../lib/validator';
import { ProblemAdd } from '../lib/ui'; import { ProblemAdd } from '../lib/ui';
import * as file from '../model/file'; import * as file from '../model/file';
import * as problem from '../model/problem'; import * as problem from '../model/problem';
@@ -25,7 +23,7 @@ import * as solution from '../model/solution';
import { PERM, PRIV, CONSTANT } from '../model/builtin'; import { PERM, PRIV, CONSTANT } from '../model/builtin';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import { import {
Route, Connection, Handler, ConnectionHandler, Types, param, Route, Connection, Handler, ConnectionHandler, Types, param, post, route,
} from '../service/server'; } from '../service/server';
export const parseCategory = (value: string) => flatten(value.split('+').map((e) => e.split(','))).map((e) => e.trim()); export const parseCategory = (value: string) => flatten(value.split('+').map((e) => e.split(','))).map((e) => e.trim());
@@ -170,7 +168,7 @@ export class ProblemDetailHandler extends ProblemHandler {
udoc: User; udoc: User;
@param('pid', Types.String, true, null, parsePid) @route('pid', Types.String, true, null, parsePid)
async _prepare(domainId: string, pid: number | string) { async _prepare(domainId: string, pid: number | string) {
this.response.template = 'problem_detail.html'; this.response.template = 'problem_detail.html';
this.pdoc = await problem.get(domainId, pid, this.user._id); this.pdoc = await problem.get(domainId, pid, this.user._id);
@@ -437,15 +435,12 @@ export class ProblemEditHandler extends ProblemManageHandler {
@param('title', Types.String, isTitle) @param('title', Types.String, isTitle)
@param('content', Types.String, isContent) @param('content', Types.String, isContent)
async post(domainId: string, title: string, content: string) { @post('pid', Types.String, isPid, true)
const $update: Partial<Pdoc> = { title, content }; async post(domainId: string, title: string, content: string, newPid: string = '') {
if (this.request.body.pid) { const $update: Partial<Pdoc> = { title, content, pid: newPid };
if (this.request.body.pid.toString() === this.pdoc.docId.toString()) $update.pid = this.request.body.pid; let pdoc = await problem.get(domainId, this.request.params.pid);
else $update.pid = checkPid(this.request.body.pid); pdoc = await problem.edit(domainId, pdoc.docId, $update);
} this.response.redirect = this.url('problem_detail', { pid: pdoc.pid || pdoc.docId });
const pdoc = await problem.get(domainId, this.request.params.pid);
await problem.edit(domainId, pdoc.docId, $update);
this.response.redirect = this.url('problem_detail', { pid: this.request.params.pid });
} }
} }
+7 -4
View File
@@ -1,7 +1,7 @@
/* eslint-disable object-curly-newline */ /* eslint-disable object-curly-newline */
import assert from 'assert'; import assert from 'assert';
import { import {
ObjectID, Cursor, FilterQuery, UpdateQuery, ObjectID, Cursor, FilterQuery, UpdateQuery, OnlyFieldsOfType,
} from 'mongodb'; } from 'mongodb';
import { import {
Pdoc, Ddoc, Drdoc, Tdoc, TrainingDoc, ProblemStatusDoc, Pdoc, Ddoc, Drdoc, Tdoc, TrainingDoc, ProblemStatusDoc,
@@ -92,12 +92,15 @@ export async function get<K extends keyof DocType>(domainId: string, docType: K,
} }
export async function set<K extends keyof DocType>( export async function set<K extends keyof DocType>(
domainId: string, docType: K, docId: DocType[K]['docId'], $set: Partial<DocType[K]>, domainId: string, docType: K, docId: DocType[K]['docId'], $set?: Partial<DocType[K]>, $unset?: OnlyFieldsOfType<DocType[K], any, true | '' | 1>,
): Promise<DocType[K]> { ): Promise<DocType[K]> {
await bus.parallel('document/set', domainId, docType, docId, $set); await bus.parallel('document/set', domainId, docType, docId, $set, $unset);
const update: UpdateQuery<DocType[K]> = {};
if ($set) update.$set = $set;
if ($unset) update.$unset = $unset;
const res = await coll.findOneAndUpdate( const res = await coll.findOneAndUpdate(
{ domainId, docType, docId }, { domainId, docType, docId },
{ $set }, update,
{ returnOriginal: false, upsert: true }, { returnOriginal: false, upsert: true },
); );
return res.value; return res.value;
+8 -7
View File
@@ -82,12 +82,11 @@ export async function add(
tag: string[] = [], category: string[] = [], data: ProblemDataSource = null, hidden = false, tag: string[] = [], category: string[] = [], data: ProblemDataSource = null, hidden = false,
) { ) {
const pidCounter = await domain.inc(domainId, 'pidCounter', 1); const pidCounter = await domain.inc(domainId, 'pidCounter', 1);
return await document.add( const args: Partial<Pdoc> = {
domainId, content, owner, document.TYPE_PROBLEM, pidCounter, null, null, title, data, category, tag, hidden, nSubmit: 0, nAccept: 0,
{ };
pid, title, data, category, tag, hidden, nSubmit: 0, nAccept: 0, if (pid) args.pid = pid;
}, return await document.add(domainId, content, owner, document.TYPE_PROBLEM, pidCounter, null, null, args);
) as number;
} }
export async function get( export async function get(
@@ -116,7 +115,9 @@ export function getMultiStatus(domainId: string, query: FilterQuery<Pdoc>) {
} }
export function edit(domainId: string, _id: number, $set: Partial<Pdoc>): Promise<Pdoc> { export function edit(domainId: string, _id: number, $set: Partial<Pdoc>): Promise<Pdoc> {
return document.set(domainId, document.TYPE_PROBLEM, _id, $set); const delpid = $set.pid === '';
if (delpid) delete $set.pid;
return document.set(domainId, document.TYPE_PROBLEM, _id, $set, delpid ? { pid: '' } : undefined);
} }
export function inc(domainId: string, _id: number, field: NumberKeys<Pdoc>, n: number): Promise<Pdoc> { export function inc(domainId: string, _id: number, field: NumberKeys<Pdoc>, n: number): Promise<Pdoc> {
+2 -2
View File
@@ -1,6 +1,6 @@
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
import cluster from 'cluster'; import cluster from 'cluster';
import { Db, FilterQuery } from 'mongodb'; import { Db, FilterQuery, OnlyFieldsOfType } from 'mongodb';
import { argv } from 'yargs'; import { argv } from 'yargs';
import { Logger } from '../logger'; import { Logger } from '../logger';
import { import {
@@ -48,7 +48,7 @@ export interface EventMap {
'document/add': (doc: any) => VoidReturn 'document/add': (doc: any) => VoidReturn
'document/set': <T extends keyof DocType> 'document/set': <T extends keyof DocType>
(domainId: string, docType: T, docId: DocType[T], $set: any) => VoidReturn (domainId: string, docType: T, docId: DocType[T], $set: any, $unset: OnlyFieldsOfType<DocType[T], any, true | '' | 1>) => VoidReturn
'problem/edit': (doc: Pdoc) => VoidReturn 'problem/edit': (doc: Pdoc) => VoidReturn
'problem/list': (query: FilterQuery<Pdoc>, handler: any) => VoidReturn 'problem/list': (query: FilterQuery<Pdoc>, handler: any) => VoidReturn