Add Knightrelay1. Some fixes. Move odd 'isAttackedBy_multiple_colors' to Checkered...
[vchess.git] / server / models / User.js
... / ...
CommitLineData
1const db = require("../utils/database");
2const genToken = require("../utils/tokenGenerator");
3const params = require("../config/parameters");
4const sendEmail = require('../utils/mailer');
5
6/*
7 * Structure:
8 * _id: integer
9 * name: varchar
10 * email: varchar
11 * loginToken: token on server only
12 * loginTime: datetime (validity)
13 * sessionToken: token in cookies for authentication
14 * notify: boolean (send email notifications for corr games)
15 * created: datetime
16 * newsRead: datetime
17 */
18
19const UserModel = {
20 checkNameEmail: function(o) {
21 return (
22 (!o.name || !!(o.name.match(/^[\w-]+$/))) &&
23 (!o.email || !!(o.email.match(/^[\w.+-]+@[\w.+-]+$/)))
24 );
25 },
26
27 create: function(name, email, notify, cb) {
28 db.serialize(function() {
29 const query =
30 "INSERT INTO Users " +
31 "(name, email, notify, created) VALUES " +
32 "('" + name + "','" + email + "'," + notify + "," + Date.now() + ")";
33 db.run(query, function(err) {
34 cb(err, { id: this.lastID });
35 });
36 });
37 },
38
39 // Find one user by id, name, email, or token
40 getOne: function(by, value, cb) {
41 const delimiter = (typeof value === "string" ? "'" : "");
42 db.serialize(function() {
43 const query =
44 "SELECT * " +
45 "FROM Users " +
46 "WHERE " + by + " = " + delimiter + value + delimiter;
47 db.get(query, cb);
48 });
49 },
50
51 getByIds: function(ids, cb) {
52 db.serialize(function() {
53 const query =
54 "SELECT id, name " +
55 "FROM Users " +
56 "WHERE id IN (" + ids + ")";
57 db.all(query, cb);
58 });
59 },
60
61 /////////
62 // MODIFY
63
64 setLoginToken: function(token, id) {
65 db.serialize(function() {
66 const query =
67 "UPDATE Users " +
68 "SET loginToken = '" + token + "',loginTime = " + Date.now() + " " +
69 "WHERE id = " + id;
70 db.run(query);
71 });
72 },
73
74 setNewsRead: function(id) {
75 db.serialize(function() {
76 const query =
77 "UPDATE Users " +
78 "SET newsRead = " + Date.now() + " " +
79 "WHERE id = " + id;
80 db.run(query);
81 });
82 },
83
84 // Set session token only if empty (first login)
85 // NOTE: weaker security (but avoid to re-login everywhere after each logout)
86 // TODO: option would be to reset all tokens periodically, e.g. every 3 months
87 trySetSessionToken: function(id, cb) {
88 db.serialize(function() {
89 let query =
90 "SELECT sessionToken " +
91 "FROM Users " +
92 "WHERE id = " + id;
93 db.get(query, (err,ret) => {
94 const token = ret.sessionToken || genToken(params.token.length);
95 query =
96 "UPDATE Users " +
97 // Also empty the login token to invalidate future attempts
98 "SET loginToken = NULL" +
99 (!ret.sessionToken ? (", sessionToken = '" + token + "'") : "") + " " +
100 "WHERE id = " + id;
101 db.run(query);
102 cb(token);
103 });
104 });
105 },
106
107 updateSettings: function(user) {
108 db.serialize(function() {
109 const query =
110 "UPDATE Users " +
111 "SET name = '" + user.name + "'" +
112 ", email = '" + user.email + "'" +
113 ", notify = " + user.notify + " " +
114 "WHERE id = " + user.id;
115 db.run(query);
116 });
117 },
118
119 /////////////////
120 // NOTIFICATIONS
121
122 notify: function(user, message) {
123 const subject = "vchess.club - notification";
124 const body = "Hello " + user.name + " !" + `
125` + message;
126 sendEmail(params.mail.noreply, user.email, subject, body);
127 },
128
129 tryNotify: function(id, message) {
130 UserModel.getOne("id", id, (err,user) => {
131 if (!err && user.notify) UserModel.notify(user, message);
132 });
133 },
134
135 ////////////
136 // CLEANING
137
138 cleanUsersDb: function() {
139 const tsNow = Date.now();
140 // 86400000 = 24 hours in milliseconds
141 const day = 86400000;
142 db.serialize(function() {
143 const query =
144 "SELECT id, sessionToken, created, name, email " +
145 "FROM Users";
146 db.all(query, (err, users) => {
147 let toRemove = [];
148 users.forEach(u => {
149 // Remove users unlogged for > 24h
150 if (!u.sessionToken && tsNow - u.created > day)
151 {
152 notify(
153 u,
154 "Your account has been deleted because " +
155 "you didn't log in for 24h after registration"
156 );
157 }
158 });
159 if (toRemove.length > 0) {
160 db.run(
161 "DELETE FROM Users " +
162 "WHERE id IN (" + toRemove.join(",") + ")"
163 );
164 }
165 });
166 });
167 },
168}
169
170module.exports = UserModel;