var GameModel = require("../models/Game");
cron.schedule('0 0 0 * * *', function() {
// Remove some old users, challenges and games every 24h
- UserModel.cleanUsersDb(); //TODO: write this
- ChallengeModel.removeOld(); //TODO: this too
+ UserModel.cleanUsersDb();
+ ChallengeModel.removeOld();
GameModel.cleanGamesDb();
});
});
});
},
-}
-// TODO: adapt
-// Remove challenges older than 1 month, and 1to1 older than 36h
-//exports.removeOld = function()
-//{
-// var tsNow = new Date().getTime();
-// // 86400000 = 24 hours in milliseconds
-// var day = 86400000;
-// db.challenges.find({}, (err,challengeArray) => {
-// challengeArray.forEach( c => {
-// if (c._id.getTimestamp() + 30*day < tsNow //automatch
-// || (!!c.to && c._id.getTimestamp() + 1.5*day < tsNow)) //1 to 1
-// {
-// db.challenges.remove({"_id": c._id});
-// }
-// });
-// });
-//}
+ // 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);
+ }
+ });
+ });
+ });
+ },
+}
module.exports = ChallengeModel;
* loginTime: datetime (validity)
* sessionToken: token in cookies for authentication
* notify: boolean (send email notifications for corr games)
+ * created: datetime
*/
const UserModel =
db.serialize(function() {
const insertQuery =
"INSERT INTO Users " +
- "(name, email, notify) VALUES " +
- "('" + name + "', '" + email + "', " + notify + ")";
+ "(name, email, notify, created) VALUES " +
+ "('" + name + "', '" + email + "', " + notify + "," + Date.now() + ")";
db.run(insertQuery, err => {
if (!!err)
return callback(err);
res.json(err || {});
});
});
- }
-}
+ },
+
+ ////////////
+ // CLEANING
-// TODO: adapt
-//exports.cleanUsersDb = function()
-//{
-// var tsNow = new Date().getTime();
-// // 86400000 = 24 hours in milliseconds
-// var day = 86400000;
-//
-// db.users.find({}, (err,userArray) => {
-// userArray.forEach( u => {
-// if ((u.sessionTokens.length==0 &&
-// u._id.getTimestamp().getTime() + day < tsNow) //unlogged
-// || u.updated + 365*day < tsNow) //inactive for one year
-// {
-// db.users.remove({"_id": u._id});
-// }
-// });
-// });
-//}
+ cleanUsersDb: function()
+ {
+ const tsNow = Date.now();
+ // 86400000 = 24 hours in milliseconds
+ const day = 86400000;
+ db.serialize(function() {
+ const query =
+ "SELECT id, sessionToken, created " +
+ "FROM Users";
+ db.all(query, (err, users) => {
+ users.forEach(u => {
+ // Remove unlogged users for >1 day
+ if (!u.sessionToken && tsNow - u.created > day)
+ db.run("DELETE FROM Users WHERE id = " + u.id);
+ });
+ });
+ });
+ },
+}
module.exports = UserModel;