X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FUser.js;h=c2e78837e439f827b4de23aebdbfd24a116dcd46;hb=d431028c73d41a22636130bd6aff562762eaf2bb;hp=a36ab683487fd7d9d6bc8761ee105536e0c9ca42;hpb=625022fdcf750f0aff8fcd699f7e9b89730e1d10;p=vchess.git diff --git a/server/models/User.js b/server/models/User.js index a36ab683..c2e78837 100644 --- a/server/models/User.js +++ b/server/models/User.js @@ -1,7 +1,7 @@ var db = require("../utils/database"); -var maild = require("../utils/mailer.js"); var genToken = require("../utils/tokenGenerator"); var params = require("../config/parameters"); +var sendEmail = require('../utils/mailer'); /* * Structure: @@ -16,6 +16,24 @@ var params = require("../config/parameters"); const UserModel = { + checkNameEmail: function(o) + { + if (typeof o.name === "string") + { + if (o.name.length == 0) + return "Empty name"; + if (!o.name.match(/^[\w]+$/)) + return "Bad characters in name"; + } + if (typeof o.email === "string") + { + if (o.email.length == 0) + return "Empty email"; + if (!o.email.match(/^[\w.+-]+@[\w.+-]+$/)) + return "Bad characters in email"; + } + }, + // NOTE: parameters are already cleaned (in controller), thus no sanitization here create: function(name, email, notify, callback) { @@ -45,6 +63,16 @@ const UserModel = }); }, + getByIds: function(ids, cb) { + db.serialize(function() { + const query = + "SELECT id, name " + + "FROM Users " + + "WHERE id IN (" + ids + ")"; + db.all(query, cb); + }); + }, + ///////// // MODIFY @@ -96,6 +124,41 @@ const UserModel = db.run(query, cb); }); }, + + ///////////////// + // NOTIFICATIONS + + tryNotify: function(oppId, message) + { + UserModel.getOne("id", oppId, (err,opp) => { + if (!err || !opp.notify) + return; //error is ignored here (TODO: should be logged) + const subject = "vchess.club - notification"; + const body = "Hello " + opp.name + "!\n" + message; + sendEmail(params.mail.noreply, opp.email, subject, body, err => { + res.json(err || {}); + }); + }); + } } +// 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}); +// } +// }); +// }); +//} + module.exports = UserModel;