1 const db
= require("../utils/database");
2 const UserModel
= require("./User");
9 * target: recipient id (optional)
10 * vid: variant id (int)
11 * fen: varchar (optional)
12 * cadence: string (3m+2s, 7d+1d ...)
15 const ChallengeModel
=
17 checkChallenge: function(c
)
20 c
.vid
.toString().match(/^[0-9]+$/) &&
21 c
.cadence
.match(/^[0-9dhms
+]+$/) &&
22 c
.fen
.match(/^[a-zA-Z0-9, /-]*$/) &&
23 (!c
.to
|| UserModel
.checkNameEmail({name: c
.to
}))
27 create: function(c
, cb
)
29 db
.serialize(function() {
31 "INSERT INTO Challenges " +
32 "(added, uid, " + (!!c
.to
? "target, " : "") + "vid, fen, cadence) " +
34 "(" + Date
.now() + "," + c
.uid
+ "," + (!!c
.to
? c
.to
+ "," : "") +
35 c
.vid
+ ",'" + c
.fen
+ "','" + c
.cadence
+ "')";
36 db
.run(query
, function(err
) {
37 cb(err
, {cid: this.lastID
});
42 // All challenges related to user with ID uid
43 getByUser: function(uid
, cb
)
45 db
.serialize(function() {
49 "WHERE target IS NULL" +
51 " OR target = " + uid
;
52 db
.all(query
, (err
,challenges
) => {
60 db
.serialize(function() {
62 "DELETE FROM Challenges " +
68 safeRemove: function(id
, uid
)
70 db
.serialize(function() {
74 "WHERE id = " + id
+ " AND uid = " + uid
;
75 db
.get(query
, (err
,chall
) => {
77 ChallengeModel
.remove(id
);
83 module
.exports
= ChallengeModel
;