Commit | Line | Data |
---|---|---|
8d7e2786 | 1 | var db = require("../utils/database"); |
58e7b94e | 2 | const UserModel = require("./User"); |
8d7e2786 BA |
3 | |
4 | /* | |
bf20f404 | 5 | * Structure: |
00f2759e BA |
6 | * id: integer |
7 | * added: datetime | |
8 | * uid: user id (int) | |
bf20f404 | 9 | * target: recipient id (optional) |
00f2759e | 10 | * vid: variant id (int) |
ab4f4bf2 | 11 | * fen: varchar (optional) |
052d17ea | 12 | * timeControl: string (3m+2s, 7d+1d ...) |
8d7e2786 BA |
13 | */ |
14 | ||
ab4f4bf2 | 15 | const ChallengeModel = |
8d7e2786 | 16 | { |
b1aa927b BA |
17 | checkChallenge: function(c) |
18 | { | |
bebcc8d4 | 19 | if (!c.vid.toString().match(/^[0-9]+$/)) |
b1aa927b | 20 | return "Wrong variant ID"; |
052d17ea BA |
21 | if (!c.timeControl.match(/^[0-9dhms +]+$/)) |
22 | return "Wrong characters in time control"; | |
bebcc8d4 | 23 | if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/)) |
b1aa927b | 24 | return "Bad FEN string"; |
58e7b94e BA |
25 | if (!!c.to) |
26 | return UserModel.checkNameEmail({name: c.to}); | |
bebcc8d4 | 27 | return ""; |
b1aa927b | 28 | }, |
98db2082 | 29 | |
b1aa927b BA |
30 | // fen cannot be undefined |
31 | create: function(c, cb) | |
32 | { | |
33 | db.serialize(function() { | |
bebcc8d4 | 34 | const query = |
b1aa927b BA |
35 | "INSERT INTO Challenges " + |
36 | "(added, uid, " + (!!c.to ? "target, " : "") + | |
bf20f404 | 37 | "vid, fen, timeControl) VALUES " + |
b1aa927b | 38 | "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") + |
bf20f404 | 39 | c.vid + ",'" + c.fen + "','" + c.timeControl + "')"; |
8c564f46 | 40 | db.run(query, function(err) { |
b4de2e73 | 41 | return cb(err, {cid: this.lastID}); |
bf20f404 | 42 | }); |
b1aa927b BA |
43 | }); |
44 | }, | |
ab4f4bf2 | 45 | |
b1aa927b BA |
46 | getOne: function(id, cb) |
47 | { | |
48 | db.serialize(function() { | |
bebcc8d4 | 49 | const query = |
b1aa927b BA |
50 | "SELECT * " + |
51 | "FROM Challenges " + | |
52 | "WHERE id = " + id; | |
53 | db.get(query, (err,challenge) => { | |
54 | return cb(err, challenge); | |
55 | }); | |
56 | }); | |
57 | }, | |
8d7e2786 | 58 | |
fe4c7e67 BA |
59 | // All challenges except where target is defined and not me, |
60 | // and I'm not the sender. | |
b1aa927b BA |
61 | getByUser: function(uid, cb) |
62 | { | |
63 | db.serialize(function() { | |
64 | const query = | |
65 | "SELECT * " + | |
66 | "FROM Challenges " + | |
fe4c7e67 BA |
67 | "WHERE target IS NULL" + |
68 | " OR uid = " + uid + | |
69 | " OR target = " + uid; | |
bebcc8d4 | 70 | db.all(query, (err,challenges) => { |
b1aa927b BA |
71 | return cb(err, challenges); |
72 | }); | |
73 | }); | |
74 | }, | |
8d7e2786 | 75 | |
2be5d614 | 76 | remove: function(id) |
b1aa927b BA |
77 | { |
78 | db.serialize(function() { | |
2be5d614 BA |
79 | const query = |
80 | "DELETE FROM Challenges " + | |
81 | "WHERE id = " + id; | |
82 | db.run(query); | |
83 | }); | |
84 | }, | |
85 | ||
86 | safeRemove: function(id, uid, cb) | |
87 | { | |
88 | db.serialize(function() { | |
89 | const query = | |
4edfed6c BA |
90 | "SELECT 1 " + |
91 | "FROM Challenges " + | |
92 | "WHERE id = " + id + " AND uid = " + uid; | |
bebcc8d4 BA |
93 | db.get(query, (err,chall) => { |
94 | if (!chall) | |
95 | return cb({errmsg: "Not your challenge"}); | |
2be5d614 | 96 | ChallengeModel.remove(id); |
bebcc8d4 | 97 | cb(null); |
36093eba | 98 | }); |
b1aa927b BA |
99 | }); |
100 | }, | |
ab4f4bf2 | 101 | |
83494c7f BA |
102 | // Remove challenges older than 1 month, and 1to1 older than 2 days |
103 | removeOld: function() | |
104 | { | |
105 | const tsNow = Date.now(); | |
106 | // 86400000 = 24 hours in milliseconds | |
107 | const day = 86400000; | |
108 | db.serialize(function() { | |
109 | const query = | |
cf7fac89 | 110 | "SELECT id, target, added " + |
83494c7f BA |
111 | "FROM Challenges"; |
112 | db.all(query, (err, challenges) => { | |
113 | challenges.forEach(c => { | |
114 | if ((!c.target && tsNow - c.added > 30*day) || | |
115 | (!!c.target && tsNow - c.added > 2*day)) | |
116 | { | |
cf7fac89 | 117 | db.run("DELETE FROM Challenges WHERE id = " + c.id); |
83494c7f BA |
118 | } |
119 | }); | |
120 | }); | |
121 | }); | |
122 | }, | |
123 | } | |
d431028c | 124 | |
ab4f4bf2 | 125 | module.exports = ChallengeModel; |