From 7d7b12392f4abb46decf0d56d1bcd7800833cb8e Mon Sep 17 00:00:00 2001 From: undefined Date: Tue, 30 Jun 2020 23:03:18 +0800 Subject: [PATCH] v2.5.0 --- hydro/entry/common.js | 2 +- hydro/model/record.js | 1 - hydro/model/token.js | 4 +-- hydro/script/blacklist.js | 4 ++- hydro/script/deleteUser.js | 4 ++- hydro/script/install.js | 4 ++- hydro/script/problemStat.js | 47 ++++++++++++++++++++++++++++ hydro/script/rating.js | 4 ++- hydro/script/recalcRating.js | 4 ++- hydro/script/register.js | 4 ++- hydro/script/setSuperadmin.js | 4 ++- hydro/script/uninstall.js | 8 ----- hydro/service/server.js | 9 ++---- module/migrate-vijos/hydro.json | 2 +- module/migrate-vijos/script.js | 7 +++-- package.json | 4 +-- templates/partials/login_dialog.html | 1 + yarn.lock | 24 ++++++-------- 18 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 hydro/script/problemStat.js delete mode 100644 hydro/script/uninstall.js diff --git a/hydro/entry/common.js b/hydro/entry/common.js index 1095747fd..d1ae4eb71 100644 --- a/hydro/entry/common.js +++ b/hydro/entry/common.js @@ -25,7 +25,7 @@ const builtinHandler = [ ]; const builtinScript = [ - 'install', 'uninstall', 'rating', 'recalcRating', 'register', + 'install', 'rating', 'recalcRating', 'register', 'problemStat', 'blacklist', 'setSuperadmin', 'deleteUser', ]; diff --git a/hydro/model/record.js b/hydro/model/record.js index 31283ce30..1fb59d7ae 100644 --- a/hydro/model/record.js +++ b/hydro/model/record.js @@ -20,7 +20,6 @@ async function add(domainId, data, addTask = true) { time: 0, memory: 0, hidden: false, - rejudged: false, judgeTexts: [], compilerTexts: [], testCases: [], diff --git a/hydro/model/token.js b/hydro/model/token.js index 2d79d1495..9a9ae8af4 100644 --- a/hydro/model/token.js +++ b/hydro/model/token.js @@ -37,9 +37,9 @@ async function add(tokenType, expireSeconds, data) { * @param {number} tokenType type of the token. * @returns {Promise} The token document, or null. */ -async function get(tokenId, tokenType) { +async function get(tokenId, tokenType, doThrow = true) { const res = await coll.findOne({ _id: tokenId, tokenType }); - if (!res) throw new ValidationError('token'); + if (!res && doThrow) throw new ValidationError('token'); return res; } diff --git a/hydro/script/blacklist.js b/hydro/script/blacklist.js index 956ea4f70..9ac1bef85 100644 --- a/hydro/script/blacklist.js +++ b/hydro/script/blacklist.js @@ -1,3 +1,5 @@ +const description = 'Add blacklist by ip, uid'; + // This code format is just for fun. DO NOT DO THIS IN YOUR CODE ! /* eslint-disable */ const { ObjectID } = require('bson'); @@ -61,4 +63,4 @@ async function run({address = null, discuss = null, user = null, dryrun = true}, if (user) await _user(user, new Set(), new Set(), new Set(), dryrun, report) } -global.Hydro.script.blacklist = module.exports = { run }; +global.Hydro.script.blacklist = module.exports = { run, description }; diff --git a/hydro/script/deleteUser.js b/hydro/script/deleteUser.js index 8e3c8d8bd..bf96f090d 100644 --- a/hydro/script/deleteUser.js +++ b/hydro/script/deleteUser.js @@ -1,3 +1,5 @@ +const description = 'Delete a user'; + /* eslint-disable no-await-in-loop */ const user = require('../model/user'); const document = require('../model/document'); @@ -85,4 +87,4 @@ async function run({ return uid; } -global.Hydro.script.deleteUser = module.exports = { run }; +global.Hydro.script.deleteUser = module.exports = { run, description }; diff --git a/hydro/script/install.js b/hydro/script/install.js index f9e8dfeac..6d1eab284 100644 --- a/hydro/script/install.js +++ b/hydro/script/install.js @@ -1,3 +1,5 @@ +const description = 'Install'; + const { defaults } = require('lodash'); const db = require('../service/db'); const { PRIV_ALL } = require('../model/builtin').PRIV; @@ -66,4 +68,4 @@ async function run({ username, password } = {}) { await Promise.all(tasks); } -global.Hydro.script.install = module.exports = { run }; +global.Hydro.script.install = module.exports = { run, description }; diff --git a/hydro/script/problemStat.js b/hydro/script/problemStat.js new file mode 100644 index 000000000..f3b308292 --- /dev/null +++ b/hydro/script/problemStat.js @@ -0,0 +1,47 @@ +const description = 'Recalcuates nSubmit and nAccept in problem status.'; + +const db = require('../service/db'); +const { STATUS_ACCEPTED } = require('../model/builtin').STATUS; +const document = require('../model/document'); + +async function run() { + const pipeline = [ + { + $match: { hidden: false, type: { $ne: 'run' } }, + }, + { + $group: { + _id: { domainId: '$domainId', pid: '$pid', uid: '$uid' }, + nSubmit: { $sum: 1 }, + nAccept: { + $sum: { + $cond: [{ $eq: ['$status', STATUS_ACCEPTED] }, 1, 0], + }, + }, + }, + }, + { + $group: { + _id: { domainId: '$_id.domainId', pid: '$_id.pid' }, + nSubmit: { $sum: '$nSubmit' }, + nAccept: { $sum: { $min: ['$nAccept', 1] } }, + }, + }, + ]; + const bulk = db.collection('document').initializeUnorderedBulkOp(); + db.collection('record').aggregate(pipeline).forEach( + (adoc) => bulk.find({ + domainId: adoc._id.domainId, + docType: document.TYPE_PROBLEM, + docId: adoc._id.pid, + }).updateOne({ + $set: { + nSubmit: adoc.nSubmit, + nAccept: adoc.nAccept, + }, + }), + ); + await bulk.execute(); +} + +global.Hydro.script.problemStat = module.exports = { run, description }; diff --git a/hydro/script/rating.js b/hydro/script/rating.js index a65770a15..770e69dae 100644 --- a/hydro/script/rating.js +++ b/hydro/script/rating.js @@ -1,3 +1,5 @@ +const description = 'Calculate the rating of a contest'; + const { ObjectID } = require('bson'); const contest = require('../model/contest'); const user = require('../model/user'); @@ -18,4 +20,4 @@ async function run({ domainId, contestId }) { return rated; } -global.Hydro.script.rating = module.exports = { run }; +global.Hydro.script.rating = module.exports = { run, description }; diff --git a/hydro/script/recalcRating.js b/hydro/script/recalcRating.js index d0d8be789..9095a690a 100644 --- a/hydro/script/recalcRating.js +++ b/hydro/script/recalcRating.js @@ -1,3 +1,5 @@ +const description = 'Calculate rating of a domain, or all domains'; + /* eslint-disable no-await-in-loop */ const domain = require('../model/domain'); const contest = require('../model/contest'); @@ -64,4 +66,4 @@ async function run({ domainId, isSub = false }, report) { return true; } -global.Hydro.script.recalcRating = module.exports = { run }; +global.Hydro.script.recalcRating = module.exports = { run, description }; diff --git a/hydro/script/register.js b/hydro/script/register.js index f1263d8ff..4e6fcb074 100644 --- a/hydro/script/register.js +++ b/hydro/script/register.js @@ -1,3 +1,5 @@ +const description = 'Create a new user'; + const user = require('../model/user'); const system = require('../model/system'); @@ -13,4 +15,4 @@ async function run({ return uid; } -global.Hydro.script.register = module.exports = { run }; +global.Hydro.script.register = module.exports = { run, description }; diff --git a/hydro/script/setSuperadmin.js b/hydro/script/setSuperadmin.js index fc591efd1..28c163c48 100644 --- a/hydro/script/setSuperadmin.js +++ b/hydro/script/setSuperadmin.js @@ -1,3 +1,5 @@ +const description = 'Set a user as superadmin.'; + const user = require('../model/user'); const { PRIV_ALL } = require('../model/builtin').PRIV; @@ -10,4 +12,4 @@ async function run({ return uid; } -global.Hydro.script.setSuperadmin = module.exports = { run }; +global.Hydro.script.setSuperadmin = module.exports = { run, description }; diff --git a/hydro/script/uninstall.js b/hydro/script/uninstall.js deleted file mode 100644 index f4d80335e..000000000 --- a/hydro/script/uninstall.js +++ /dev/null @@ -1,8 +0,0 @@ -const db = require('../service/db.js'); - -async function run() { - await db.dropDatabase(); - console.log('Dropped'); -} - -global.Hydro.script.uninstall = module.exports = { run }; diff --git a/hydro/service/server.js b/hydro/service/server.js index 3d7c7ed95..0e0e6d7e6 100644 --- a/hydro/service/server.js +++ b/hydro/service/server.js @@ -284,7 +284,7 @@ class Handler { } async checkCsrfToken(csrfToken) { - const sdoc = await token.get(csrfToken, token.TYPE_CSRF_TOKEN); + const sdoc = await token.get(csrfToken, token.TYPE_CSRF_TOKEN, false); if (!sdoc || sdoc.uid !== this.user._id) throw new CsrfTokenError(csrfToken); } @@ -429,12 +429,7 @@ async function handle(ctx, HandlerClass, checker) { if (h[`__${method}`]) await h[`__${method}`](args); if (h[`_${method}`]) await h[`_${method}`](args); if (h[method]) await h[method](args); - - if (method === 'post' && ctx.request.body.operation) { - const operation = `_${ctx.request.body.operation}` - .replace(/_([a-z])/gm, (s) => s[1].toUpperCase()); - if (h[`${method}${operation}`]) await h[`${method}${operation}`](args); - } + if (operation) await h[`post${operation}`](args); if (h.cleanup) await h.cleanup(args); if (h._cleanup) await h._cleanup(args); diff --git a/module/migrate-vijos/hydro.json b/module/migrate-vijos/hydro.json index 434c30fb2..1694e1288 100644 --- a/module/migrate-vijos/hydro.json +++ b/module/migrate-vijos/hydro.json @@ -1,5 +1,5 @@ { "id": "migrate-vijos", - "version": "1.0.3", + "version": "1.0.4", "description": "Migrate From Vijos" } \ No newline at end of file diff --git a/module/migrate-vijos/script.js b/module/migrate-vijos/script.js index e15268f58..d68329408 100644 --- a/module/migrate-vijos/script.js +++ b/module/migrate-vijos/script.js @@ -170,7 +170,7 @@ const tasks = { judgeText: (c.judge_text || '') + (c.message || ''), }); } - return { + const rdoc = { _id: doc._id, status: doc.status, score: doc.score, @@ -180,16 +180,17 @@ const tasks = { lang: doc.lang, uid: doc.uid, pid: pid(doc.pid), - tid: doc.tid, domainId: doc.domain_id, judger: doc.judge_uid, judgeAt: doc.judge_at, judgeTexts: doc.judge_texts, compilerTexts: doc.compiler_texts, - rejudged: doc.rejudged, testCases, hidden: doc.hidden, }; + if (doc.rejudged) rdoc.rejudged = true; + if (doc.tid) rdoc.tid = doc.tid; + return rdoc; }, domain: { _id: '_id', diff --git a/package.json b/package.json index 4c026f50d..6448b3edb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.4.3", + "version": "2.5.0", "main": "hydro/loader.js", "repository": "https://github.com/hydro-dev/Hydro.git", "author": "masnn", @@ -31,7 +31,7 @@ "nodemailer": "^6.4.10", "nunjucks": "^3.2.1", "prismjs": "^1.20.0", - "snyk": "^1.348.1", + "snyk": "^1.354.0", "sockjs": "^0.3.20", "superagent": "^5.2.2", "superagent-proxy": "^2.0.0", diff --git a/templates/partials/login_dialog.html b/templates/partials/login_dialog.html index ceb104cc3..c80de9716 100644 --- a/templates/partials/login_dialog.html +++ b/templates/partials/login_dialog.html @@ -31,6 +31,7 @@ {{ _('Remember me') }} + diff --git a/yarn.lock b/yarn.lock index bc5a7afc2..8be3e9abf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2773,11 +2773,6 @@ ip@1.1.5, ip@^1.1.5: resolved "https://registry.yarnpkg.com/ip/-/ip-1.1.5.tgz#bdded70114290828c0a039e72ef25f5aaec4354a" integrity sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo= -ipaddr.js@^1.9.1: - version "1.9.1" - resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3" - integrity sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g== - is-accessor-descriptor@^0.1.6: version "0.1.6" resolved "https://registry.yarnpkg.com/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz#a9e12cb3ae8d876727eeef3843f8a0897b5c98d6" @@ -5040,10 +5035,10 @@ snyk-go-plugin@1.14.2: tmp "0.1.0" tslib "^1.10.0" -snyk-gradle-plugin@3.5.0: - version "3.5.0" - resolved "https://registry.yarnpkg.com/snyk-gradle-plugin/-/snyk-gradle-plugin-3.5.0.tgz#6e3280ef8b4965978fa7cd4664480b23234e1049" - integrity sha512-zOHr3802bDKCcSAFfoEvt/wGSaOSGRm6yQjikdo1RTz7jTeQnSMJxCFYTOhfpu9Tka4d5q0TsdI/2VLFAvPKhA== +snyk-gradle-plugin@3.5.1: + version "3.5.1" + resolved "https://registry.yarnpkg.com/snyk-gradle-plugin/-/snyk-gradle-plugin-3.5.1.tgz#a424a7b8136471296e971e7b25f4776d1a08b9ff" + integrity sha512-8tZwQCqRbjp1azvc+bBRXSbn2AjbUpFAM6qoSpM/IZpfGl1RaOtz4/JTkGFxj+iBhTFoAkGxEunT66eO0pHZZw== dependencies: "@snyk/cli-interface" "2.8.0" "@snyk/dep-graph" "^1.17.0" @@ -5217,10 +5212,10 @@ snyk-try-require@1.3.1, snyk-try-require@^1.1.1, snyk-try-require@^1.3.1: lru-cache "^4.0.0" then-fs "^2.0.0" -snyk@^1.348.1: - version "1.349.0" - resolved "https://registry.yarnpkg.com/snyk/-/snyk-1.349.0.tgz#b29ea17037f7bc5ff29ac4a4c3227fe412fe23cb" - integrity sha512-2gE31HJ42H2gYDci9WB+f1GOTNLQXIcO5jm7i8sw+Xpfce3FYYH3s0oCecnrDHV3qOXbr6Qa5MtL0bwWmOA3fg== +snyk@^1.354.0: + version "1.354.0" + resolved "https://registry.yarnpkg.com/snyk/-/snyk-1.354.0.tgz#c23dd28177a4d57c4e02d43583ce435873e569d8" + integrity sha512-0X3BHsdBfpJPpAVB8e7vlXCi7DfjEkVwSZLjtA5pXWpRCKyuDrVvJl+dctr11eEubbo/bJBDxWsmOnzxsdkTIw== dependencies: "@snyk/cli-interface" "2.8.0" "@snyk/dep-graph" "1.18.3" @@ -5238,7 +5233,6 @@ snyk@^1.348.1: debug "^4.1.1" diff "^4.0.1" glob "^7.1.3" - ipaddr.js "^1.9.1" needle "^2.5.0" open "^7.0.3" os-name "^3.0.0" @@ -5248,7 +5242,7 @@ snyk@^1.348.1: snyk-config "3.1.0" snyk-docker-plugin "3.12.3" snyk-go-plugin "1.14.2" - snyk-gradle-plugin "3.5.0" + snyk-gradle-plugin "3.5.1" snyk-module "3.1.0" snyk-mvn-plugin "2.17.1" snyk-nodejs-lockfile-parser "1.22.0"