Experimental news notification system + fix Eightpieces variant
[vchess.git] / server / models / User.js
index f3adb31..021cadc 100644 (file)
@@ -20,8 +20,8 @@ const UserModel =
   checkNameEmail: function(o)
   {
     return (
-      (!o.name || o.name.match(/^[\w]+$/)) &&
-      (!o.email || o.email.match(/^[\w.+-]+@[\w.+-]+$/))
+      (!o.name || !!(o.name.match(/^[\w-]+$/))) &&
+      (!o.email || !!(o.email.match(/^[\w.+-]+@[\w.+-]+$/)))
     );
   },
 
@@ -75,6 +75,17 @@ const UserModel =
     });
   },
 
+  setNewsRead: function(uid)
+  {
+    db.serialize(function() {
+      const query =
+        "UPDATE Users " +
+        "SET newsRead = " + Date.now() + " " +
+        "WHERE id = " + uid;
+      db.run(query);
+    });
+  },
+
   // Set session token only if empty (first login)
   // NOTE: weaker security (but avoid to re-login everywhere after each logout)
   // TODO: option would be to reset all tokens periodically, e.g. every 3 months
@@ -118,7 +129,7 @@ const UserModel =
   notify: function(user, message)
   {
     const subject = "vchess.club - notification";
-    const body = "Hello " + user.name + "!" + `
+    const body = "Hello " + user.name + " !" + `
 ` + message;
     sendEmail(params.mail.noreply, user.email, subject, body);
   },
@@ -141,13 +152,20 @@ const UserModel =
     const day = 86400000;
     db.serialize(function() {
       const query =
-        "SELECT id, sessionToken, created " +
+        "SELECT id, sessionToken, created, name, email " +
         "FROM Users";
       db.all(query, (err, users) => {
         users.forEach(u => {
-          // Remove unlogged users for >1 day
+          // Remove unlogged users for > 24h
           if (!u.sessionToken && tsNow - u.created > day)
+          {
+            notify(
+              u,
+              "Your account has been deleted because " +
+              "you didn't log in for 24h after registration"
+            );
             db.run("DELETE FROM Users WHERE id = " + u.id);
+          }
         });
       });
     });