Commit | Line | Data |
---|---|---|
c5c47010 | 1 | const 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) |
7ba4a5bc | 11 | * randomness: integer in 0..2 |
ab4f4bf2 | 12 | * fen: varchar (optional) |
7ba4a5bc | 13 | * cadence: string (3m+2s, 7d ...) |
8d7e2786 BA |
14 | */ |
15 | ||
ab4f4bf2 | 16 | const ChallengeModel = |
8d7e2786 | 17 | { |
b1aa927b BA |
18 | checkChallenge: function(c) |
19 | { | |
866842c3 BA |
20 | return ( |
21 | c.vid.toString().match(/^[0-9]+$/) && | |
22 | c.cadence.match(/^[0-9dhms +]+$/) && | |
7ba4a5bc | 23 | c.randomness.toString().match(/^[0-2]$/) && |
866842c3 BA |
24 | c.fen.match(/^[a-zA-Z0-9, /-]*$/) && |
25 | (!c.to || UserModel.checkNameEmail({name: c.to})) | |
26 | ); | |
b1aa927b | 27 | }, |
98db2082 | 28 | |
b1aa927b BA |
29 | create: function(c, cb) |
30 | { | |
31 | db.serialize(function() { | |
bebcc8d4 | 32 | const query = |
b1aa927b | 33 | "INSERT INTO Challenges " + |
51145f8c | 34 | "(added, uid, " + (c.to ? "target, " : "") + |
7ba4a5bc BA |
35 | "vid, randomness, fen, cadence) " + |
36 | "VALUES " + | |
51145f8c | 37 | "(" + Date.now() + "," + c.uid + "," + (c.to ? c.to + "," : "") + |
7ba4a5bc | 38 | c.vid + "," + c.randomness + ",'" + c.fen + "','" + c.cadence + "')"; |
8c564f46 | 39 | db.run(query, function(err) { |
866842c3 | 40 | cb(err, {cid: this.lastID}); |
bf20f404 | 41 | }); |
b1aa927b BA |
42 | }); |
43 | }, | |
ab4f4bf2 | 44 | |
866842c3 | 45 | // All challenges related to user with ID uid |
b1aa927b BA |
46 | getByUser: function(uid, cb) |
47 | { | |
48 | db.serialize(function() { | |
49 | const query = | |
50 | "SELECT * " + | |
51 | "FROM Challenges " + | |
fe4c7e67 BA |
52 | "WHERE target IS NULL" + |
53 | " OR uid = " + uid + | |
54 | " OR target = " + uid; | |
bebcc8d4 | 55 | db.all(query, (err,challenges) => { |
866842c3 | 56 | cb(err, challenges); |
b1aa927b BA |
57 | }); |
58 | }); | |
59 | }, | |
8d7e2786 | 60 | |
2be5d614 | 61 | remove: function(id) |
b1aa927b BA |
62 | { |
63 | db.serialize(function() { | |
2be5d614 BA |
64 | const query = |
65 | "DELETE FROM Challenges " + | |
66 | "WHERE id = " + id; | |
67 | db.run(query); | |
68 | }); | |
69 | }, | |
70 | ||
866842c3 | 71 | safeRemove: function(id, uid) |
2be5d614 BA |
72 | { |
73 | db.serialize(function() { | |
74 | const query = | |
4edfed6c BA |
75 | "SELECT 1 " + |
76 | "FROM Challenges " + | |
1511c610 BA |
77 | "WHERE id = " + id + " " + |
78 | // Condition: I'm the sender or the target | |
51145f8c | 79 | "AND (uid = " + uid + " OR target = " + uid + ")"; |
bebcc8d4 | 80 | db.get(query, (err,chall) => { |
866842c3 BA |
81 | if (!err && chall) |
82 | ChallengeModel.remove(id); | |
36093eba | 83 | }); |
b1aa927b BA |
84 | }); |
85 | }, | |
83494c7f | 86 | } |
d431028c | 87 | |
ab4f4bf2 | 88 | module.exports = ChallengeModel; |