mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
testdata.convert
This commit is contained in:
1 parent
26f780ddee
commit
0d7d6ea12a
15 files changed
+199
-127
No files matched your search
+16
-7
@@ -90,7 +90,7 @@ function getMultiStatus(domainId, query) {
|
||||
* @returns {Pdoc}
|
||||
*/
|
||||
function edit(domainId, _id, $set) {
|
||||
return document.set(domainId, document.TYPE_CONTEST, _id, $set);
|
||||
return document.set(domainId, document.TYPE_PROBLEM, _id, $set);
|
||||
}
|
||||
|
||||
function inc(domainId, _id, field, n) {
|
||||
@@ -111,13 +111,21 @@ async function random(domainId, query) {
|
||||
}
|
||||
|
||||
async function getList(domainId, pids, doThrow = true) {
|
||||
pids = new Set(pids);
|
||||
const r = {};
|
||||
if (doThrow) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
for (const pid of pids) r[pid] = await get(domainId, pid);
|
||||
} else {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
for (const pid of pids) r[pid] = await get(domainId, pid).catch((e) => e);
|
||||
const pdocs = await document.getMulti(
|
||||
domainId, document.TYPE_PROBLEM,
|
||||
{ $or: [{ docId: { $in: pids } }, { pid: { $in: pids } }] },
|
||||
).toArray();
|
||||
for (const pdoc of pdocs) {
|
||||
r[pdoc.docId] = r[pdoc.pid] = pdoc;
|
||||
}
|
||||
if (pdocs.length !== pids.size) {
|
||||
if (doThrow) {
|
||||
for (const pid of pids) {
|
||||
if (!r[pid]) throw new ProblemNotFoundError(domainId, pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
@@ -141,6 +149,7 @@ async function updateStatus(domainId, pid, uid, rid, status) {
|
||||
}
|
||||
|
||||
async function setTestdata(domainId, _id, readStream) {
|
||||
// TODO read config in zipfile
|
||||
const pdoc = await get(domainId, _id);
|
||||
const id = await file.add(readStream, 'data.zip');
|
||||
if (pdoc.data && typeof pdoc.data === 'object') file.dec(this.pdoc.data);
|
||||
|
||||
@@ -110,6 +110,7 @@ async function judge(domainId, rid) {
|
||||
type: 'judge',
|
||||
rid,
|
||||
domainId,
|
||||
config: pdoc.config || '',
|
||||
pid: rdoc.pid,
|
||||
data: pdoc.data,
|
||||
lang: rdoc.lang,
|
||||
|
||||
+89
-67
@@ -3,85 +3,107 @@ const { ValidationError } = require('../error');
|
||||
|
||||
const coll = db.collection('token');
|
||||
|
||||
function index() {
|
||||
function ensureIndexes() {
|
||||
return Promise.all([
|
||||
coll.createIndex([{ uid: 1 }, { tokenType: 1 }, { updateAt: -1 }], { sparse: true }),
|
||||
coll.createIndex('expireAt', { expireAfterSeconds: 0 }),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a token.
|
||||
* @param {number} tokenType type of the token.
|
||||
* @param {number} expireSeconds expire time, in seconds.
|
||||
* @param {object} data extra data.
|
||||
* @returns {Promise<Array>} token ID, token data
|
||||
*/
|
||||
async function add(tokenType, expireSeconds, data) {
|
||||
const now = new Date();
|
||||
const str = String.random(32);
|
||||
const res = await coll.insertOne({
|
||||
...data,
|
||||
_id: str,
|
||||
tokenType,
|
||||
createAt: now,
|
||||
updateAt: now,
|
||||
expireAt: new Date(now.getTime() + expireSeconds * 1000),
|
||||
});
|
||||
return [str, res.ops];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a token.
|
||||
* @param {string} tokenId token ID.
|
||||
* @param {number} tokenType type of the token.
|
||||
* @returns {Promise<object>} The token document, or null.
|
||||
*/
|
||||
async function get(tokenId, tokenType) {
|
||||
const res = await coll.findOne({ _id: tokenId, tokenType });
|
||||
if (!res) throw new ValidationError('token');
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a token.
|
||||
* @param {string} tokenId token ID.
|
||||
* @param {number} tokenType type of the token.
|
||||
* @param {number} expireSeconds expire time, in seconds.
|
||||
* @param {object} data extra data.
|
||||
* @returns {Promise<object>} The token document, or null.
|
||||
*/
|
||||
async function update(tokenId, tokenType, expireSeconds, data) {
|
||||
const now = new Date();
|
||||
const res = await coll.findOneAndUpdate({ _id: tokenId, tokenType }, {
|
||||
$set: {
|
||||
...data,
|
||||
updateAt: now,
|
||||
expireAt: new Date(now.getTime() + expireSeconds * 1000),
|
||||
tokenType,
|
||||
},
|
||||
});
|
||||
return res.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a token.
|
||||
* @param {string} tokenId token ID.
|
||||
* @param {int} tokenType type of the token.
|
||||
* @returns {Promise<boolean>} true if deleted, or false.
|
||||
*/
|
||||
async function del(tokenId, tokenType) {
|
||||
const result = await coll.deleteOne({ _id: tokenId, tokenType });
|
||||
return !!result.deletedCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a token(or create one)
|
||||
* @param {number} tokenType type of the token.
|
||||
* @param {number} expireSeconds expire time, in seconds.
|
||||
* @param {object} data extra data.
|
||||
* @returns {Array} token ID, token data
|
||||
*/
|
||||
async function createOrUpdate(tokenType, expireSeconds, data) {
|
||||
const d = await coll.findOne({ tokenType, ...data });
|
||||
if (!d) {
|
||||
const res = await add(tokenType, expireSeconds, data);
|
||||
return res[0];
|
||||
}
|
||||
await update(d._id, tokenType, expireSeconds, data);
|
||||
return d._id;
|
||||
}
|
||||
|
||||
global.Hydro.model.token = module.exports = {
|
||||
TYPE_SESSION: 0,
|
||||
TYPE_CSRF_TOKEN: 1,
|
||||
TYPE_REGISTER: 2,
|
||||
TYPE_CHANGEMAIL: 3,
|
||||
|
||||
index,
|
||||
|
||||
/**
|
||||
* Add a token.
|
||||
* @param {number} tokenType type of the token.
|
||||
* @param {number} expireSeconds expire time, in seconds.
|
||||
* @param {object} data extra data.
|
||||
* @returns {Array} token ID, token data
|
||||
*/
|
||||
async add(tokenType, expireSeconds, data) {
|
||||
const now = new Date();
|
||||
const str = String.random(32);
|
||||
const res = await coll.insertOne({
|
||||
...data,
|
||||
_id: str,
|
||||
tokenType,
|
||||
createAt: now,
|
||||
updateAt: now,
|
||||
expireAt: new Date(now.getTime() + expireSeconds * 1000),
|
||||
});
|
||||
return [str, res.ops];
|
||||
},
|
||||
|
||||
/**
|
||||
* Get a token.
|
||||
* @param {string} tokenId token ID.
|
||||
* @param {number} tokenType type of the token.
|
||||
* @returns {object} The token document, or null.
|
||||
*/
|
||||
async get(tokenId, tokenType) {
|
||||
const res = await coll.findOne({ _id: tokenId, tokenType });
|
||||
if (!res) throw new ValidationError('token');
|
||||
return res;
|
||||
},
|
||||
|
||||
/**
|
||||
* Update a token.
|
||||
* @param {string} tokenId token ID.
|
||||
* @param {number} tokenType type of the token.
|
||||
* @param {number} expireSeconds expire time, in seconds.
|
||||
* @param {object} data extra data.
|
||||
* @returns {object} The token document, or null.
|
||||
*/
|
||||
async update(tokenId, tokenType, expireSeconds, data) {
|
||||
const now = new Date();
|
||||
const res = await coll.findOneAndUpdate({ _id: tokenId, tokenType }, {
|
||||
$set: {
|
||||
...data,
|
||||
updateAt: now,
|
||||
expireAt: new Date(now.getTime() + expireSeconds * 1000),
|
||||
tokenType,
|
||||
},
|
||||
});
|
||||
return res.value;
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete a token.
|
||||
* @param {string} tokenId token ID.
|
||||
* @param {int} tokenType type of the token.
|
||||
* @returns {boolean} true if deleted, or false.
|
||||
*/
|
||||
async delete(tokenId, tokenType) {
|
||||
const result = await coll.deleteOne({ _id: tokenId, tokenType });
|
||||
return !!result.deletedCount;
|
||||
},
|
||||
ensureIndexes,
|
||||
add,
|
||||
createOrUpdate,
|
||||
get,
|
||||
update,
|
||||
del,
|
||||
deleteByUid(uid) {
|
||||
return coll.deleteMany({ uid });
|
||||
},
|
||||
|
||||
+1
-1
@@ -224,7 +224,7 @@ function deleteRoles(domainId, roles) {
|
||||
function ban(uid) {
|
||||
return Promise.all([
|
||||
coll.updateOne({ _id: uid }, { $set: { ban: true } }),
|
||||
token.deleteByUid(uid),
|
||||
token.delByUid(uid),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user