- getOne: function(id, cb)
- {
- db.serialize(function() {
- let query =
- "SELECT * " +
- "FROM Challenges c " +
- "JOIN Variants v " +
- " ON c.vid = v.id "
- "WHERE id = " + id;
- db.get(query, (err,challengeInfo) => {
- if (!!err)
- return cb(err);
- query =
- "SELECT w.uid AS id, u.name " +
- "FROM WillPlay w " +
- "JOIN Users u " +
- " ON w.uid = u.id " +
- "WHERE w.cid = " + id;
- db.run(query, (err2,players) => {
- if (!!err2)
- return cb(err2);
- const challenge = {
- id: id,
- uid: challengeInfo.uid,
- vname: challengeInfo.name,
- added: challengeInfo.added,
- nbPlayers: challengeInfo.nbPlayers,
- players: players, //currently in
- fen: challengeInfo.fen,
- mainTime: challengeInfo.mainTime,
- increment: challengeInfo.addTime,
- };
- return cb(null, challenge);
- });
- });
- });
- },
-
- getByUser: function(uid, cb)
- {
- db.serialize(function() {
- const query =
- "SELECT cid " +
- "FROM WillPlay " +
- "WHERE uid = " + uid;
- db.run(query, (err,challIds) => {
- if (!!err)
- return cb(err);
- let challenges = [];
- challIds.forEach(cidRow => {
- ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
- if (!!err2)
- return cb(err2);
- challenges.push(chall);
- });
- });
- return cb(null, challenges);
- });
- });
- },
-
- remove: function(id)
- {
- db.parallelize(function() {
- let query =
- "DELETE FROM Challenges " +
- "WHERE id = " + id;
- db.run(query);
- query =
- "DELETE FROM WillPlay " +
- "WHERE cid = " + id;
- db.run(query);
- });
- },
+ // Remove challenges older than 1 month, and 1to1 older than 2 days
+ removeOld: function()
+ {
+ const tsNow = Date.now();
+ // 86400000 = 24 hours in milliseconds
+ const day = 86400000;
+ db.serialize(function() {
+ const query =
+ "SELECT id, target " +
+ "FROM Challenges";
+ db.all(query, (err, challenges) => {
+ challenges.forEach(c => {
+ if ((!c.target && tsNow - c.added > 30*day) ||
+ (!!c.target && tsNow - c.added > 2*day))
+ {
+ db.run("DELETE FROM CHallenges WHERE id = " + c.id);
+ }
+ });
+ });
+ });
+ },