添加部分注释,修复一些功能

This commit is contained in:
masnn committed 2020-04-13 17:41:44 +08:00
1 parent f40e4e2c74
commit d3e36fcb6c
32 files changed
+320 -174

No files matched your search

+12 -8
View File
@@ -2,11 +2,15 @@ const
db = require('../service/db.js'),
coll = db.collection('blacklist');
module.exports = {
add(ip) {
let expireAt = new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000);
return coll.findOneAndUpdate({ _id: ip }, { $set: { expireAt } }, { upsert: true });
},
get: ip => coll.findOne({ _id: ip }),
delete: ip => coll.deleteOne({ _id: ip })
};
async function add(ip) {
let expireAt = new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000);
return coll.findOneAndUpdate({ _id: ip }, { $set: { expireAt } }, { upsert: true });
}
function get(ip) {
return coll.findOne({ _id: ip });
}
function del(ip) {
return coll.deleteOne({ _id: ip });
}
module.exports = { add, get, del };
+1 -34
View File
@@ -1,37 +1,4 @@
const perm = require('../permission');
const BUILTIN_USERS = [
{
_id: 0,
uname: 'Hydro',
unameLower: 'hydro',
email: '',
emailLower: '',
salt: '',
hash: '',
gender: 'other',
regat: new Date(),
regip: '127.0.0.1',
gravatar: '',
loginat: new Date(),
loginip: '127.0.0.1',
role: 'guest'
},
{
_id: 1,
email: 'guest@hydro',
emailLower: 'guest@hydro',
uname: 'Guest',
unameLower: 'guest',
hash: '',
salt: '',
regat: new Date(),
regip: '127.0.0.1',
loginat: new Date(),
loginip: '127.0.0.1',
gravatar: 'guest@hydro',
role: 'guest'
}
];
const BUILTIN_ROLES = [
{ _id: 'guest', perm: perm.PERM_BASIC },
{ _id: 'default', perm: perm.PERM_DEFAULT },
@@ -262,6 +229,6 @@ const STATUS_CODES = {
30: 'ignored'
};
module.exports = {
BUILTIN_USERS, BUILTIN_ROLES, CATEGORIES, VIEW_LANGS, FOOTER_EXTRA_HTMLS, LANGS,
BUILTIN_ROLES, CATEGORIES, VIEW_LANGS, FOOTER_EXTRA_HTMLS, LANGS,
LANG_TEXTS, LANG_HIGHLIGHT_ID, STATUS, STATUS_TEXTS, STATUS_CODES
};
+25 -1
View File
@@ -5,13 +5,28 @@ const
coll = db.collection('contest'),
coll_status = db.collection('contest.status');
const RULES = {
homework: require('../module/contest/homework'),
oi: require('../module/contest/oi'),
acm: require('../module/contest/acm')
};
/**
* @typedef {import('bson').ObjectID} ObjectID
* @typedef {import('../interface').Tdoc} Tdoc
*/
/**
* @param {string} title
* @param {string} content
* @param {number} owner
* @param {string} rule
* @param {Date} beginAt
* @param {Date} endAt
* @param {ObjectID[]} pids
* @param {object} data
* @returns {ObjectID} tid
*/
async function add(title, content, owner, rule,
beginAt = new Date(), endAt = new Date(), pids = [], data = {}) {
validator.checkTitle(title);
@@ -23,6 +38,11 @@ async function add(title, content, owner, rule,
let res = await coll.insertOne(data);
return res.insertedId;
}
/**
* @param {ObjectID} tid
* @param {object} $set
* @returns {Tdoc} tdoc after modification
*/
async function edit(tid, $set) {
if ($set.title) validator.checkTitle($set.title);
if ($set.content) validator.checkIntro($set.content);
@@ -36,6 +56,10 @@ async function edit(tid, $set) {
await coll.findOneAndUpdate({ tid }, { $set });
return tdoc;
}
/**
* @param {ObjectID} tid
* @returns {Tdoc}
*/
async function get(tid) {
let tdoc = await coll.findOne({ _id: tid });
if (!tdoc) throw new ContestNotFoundError(tid);
+37 -4
View File
@@ -6,6 +6,12 @@ const
coll = db.collection('problem'),
coll_status = db.collection('problem.status');
/**
* @typedef {import('../interface').Pdoc} Pdoc
* @typedef {import('bson').ObjectID} ObjectID
* @typedef {import('mongodb').Cursor} Cursor
*/
/**
* @param {string} title
* @param {string} content
@@ -42,7 +48,12 @@ async function add({
});
return pid;
}
async function get({ pid, uid }) {
/**
* @param {string|ObjectID} pid
* @param {number} uid
* @returns {Pdoc}
*/
async function get(pid, uid = null) {
let query = {};
if (pid.generationTime || pid.length == 24) query = { _id: new ObjectID(pid) };
else query = { pid: parseInt(pid) || pid };
@@ -54,18 +65,36 @@ async function get({ pid, uid }) {
}
return pdoc;
}
/**
* @param {ObjectID} pid
* @returns {Pdoc}
*/
async function getById(_id) {
_id = new ObjectID(_id);
let pdoc = await coll.findOne({ _id });
if (!pdoc) throw new ProblemNotFoundError(_id);
return pdoc;
}
async function getMany(query, sort, page, limit) {
return await coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit).toArray();
/**
* @param {string|ObjectID} pid
* @param {number} uid
* @returns {Pdoc[]}
*/
function getMany(query, sort, page, limit) {
return coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit).toArray();
}
/**
* @param {object} query
* @returns {Cursor}
*/
function getMulti(query) {
return coll.find(query);
}
/**
* @param {ObjectID} _id
* @param {object} query
* @returns {Pdoc}
*/
async function edit(_id, $set) {
if ($set.title) validator.checkTitle($set.title);
if ($set.content) validator.checkContent($set.content);
@@ -74,6 +103,10 @@ async function edit(_id, $set) {
if (!pdoc) throw new ProblemNotFoundError(_id);
return pdoc;
}
/**
* @param {object} query
* @returns {number}
*/
async function count(query) {
return await coll.find(query).count();
}
@@ -87,7 +120,7 @@ async function random(query) {
}
async function getList(pids) {
let r = {};
for (let pid of pids) r[pid] = await get({pid});
for (let pid of pids) r[pid] = await get({ pid });
return r;
}
+7 -2
View File
@@ -55,7 +55,8 @@ async function reset(rid) {
judgeTexts: [],
compilerTexts: [],
judgeAt: null,
judger: null
judger: null,
rejudged: true
});
}
async function count(query) {
@@ -66,6 +67,9 @@ async function getList(rids) {
for (let rid of rids) r[rid] = await get(rid);
return r;
}
function getUserInProblemMulti(uid, pid) {
return coll.find({ owner: uid, pid });
}
module.exports = {
add,
@@ -74,5 +78,6 @@ module.exports = {
update,
count,
reset,
getList
getList,
getUserInProblemMulti
};
+2 -6
View File
@@ -67,9 +67,5 @@ module.exports = {
},
async getMostRecentSessionByUid(uid) {
return await coll.findOne({ uid, token_type: this.TYPE_SESSION }, { sort: { updateAt: -1 } });
},
init: () => Promise.all([
coll.createIndex([{ uid: 1 }, { tokenType: 1 }, { updateAt: -1 }], { sparse: true }),
coll.createIndex('expireAt', { expireAfterSeconds: 0 })
])
};
}
};
+13 -13
View File
@@ -10,7 +10,7 @@ const
class USER {
constructor(user) {
this._id = user._id;
this.email = user.email;
this.mail = user.mail;
this.uname = user.uname;
this.salt = user.salt;
this.hash = user.hash;
@@ -52,12 +52,12 @@ async function getByUname(uname) {
udoc.perm = role.perm;
return new USER(udoc);
}
async function getByEmail(email, ignoreMissing = false) {
let emailLower = email.trim().toLowerCase();
let udoc = await coll.findOne({ emailLower });
async function getByEmail(mail, ignoreMissing = false) {
let mailLower = mail.trim().toLowerCase();
let udoc = await coll.findOne({ mailLower });
if (!udoc) {
if (ignoreMissing) return null;
else throw new UserNotFoundError(email);
else throw new UserNotFoundError(mail);
}
let role = await coll_role.findOne({ _id: udoc.role || 'default' });
udoc.perm = role.perm;
@@ -70,9 +70,9 @@ async function setPassword(uid, password) {
$set: { salt, hash: pwhash.hash(password, salt) }
});
}
async function setEmail(uid, email) {
validator.checkEmail(email);
return await setById(uid, { email, emailLower: email.trim().toLowerCase() });
async function setEmail(uid, mail) {
validator.checkEmail(mail);
return await setById(uid, { mail, mailLower: mail.trim().toLowerCase() });
}
function setById(uid, args) {
coll.findOneAndUpdate({ _id: uid }, { $set: args });
@@ -88,17 +88,17 @@ async function changePassword(uid, currentPassword, newPassword) {
$set: { salt, hash: pwhash.hash(newPassword, salt) }
});
}
async function create({ uid, email, uname, password, regip = '127.0.0.1', role = 'default' }) {
async function create({ uid, mail, uname, password, regip = '127.0.0.1', role = 'default' }) {
validator.checkUname(uname);
validator.checkPassword(password);
validator.checkEmail(email);
validator.checkEmail(mail);
let salt = pwhash.salt();
if (!uid) uid = system.incUserCounter();
try {
await coll.insertOne({
_id: uid,
email,
emailLower: email.trim().toLowerCase(),
mail,
mailLower: mail.trim().toLowerCase(),
uname,
unameLower: uname.trim().toLowerCase(),
password: pwhash.hash(password, salt),
@@ -108,7 +108,7 @@ async function create({ uid, email, uname, password, regip = '127.0.0.1', role =
loginat: new Date(),
loginip: regip,
role,
gravatar: email
gravatar: mail
});
} catch (e) {
throw new UserAlreadyExistError([uid, uname, email]);