e32cd682065bb90e8bb5928bda3ed9ef9f9838be
1 const db
= require("../utils/database");
2 const UserModel
= require("./User");
9 * target: recipient id (optional)
10 * vid: variant id (int)
12 * fen: varchar (optional)
13 * cadence: string (3m+2s, 7d ...)
14 * options: string (js object)
17 const ChallengeModel
= {
19 checkChallenge: function(c
) {
21 c
.vid
.toString().match(/^[0-9]+$/) &&
22 c
.cadence
.match(/^[0-9dhms
+]+$/) &&
23 c
.fen
.match(/^[a-zA-Z0-9, /-]*$/) &&
24 (!c
.to
|| UserModel
.checkNameEmail({ name: c
.to
}))
28 create: function(c
, cb
) {
29 db
.serialize(function() {
31 "INSERT INTO Challenges " +
32 "(added, uid, " + (c
.to
? "target, " : "") +
33 "vid, options, fen, cadence) " +
34 "VALUES (" + Date
.now() + "," + c
.uid
+ "," + (c
.to
? c
.to
+ "," : "")
35 + c
.vid
+ ",?,'" + c
.fen
+ "','" + c
.cadence
+ "')";
36 db
.run(query
, c
.options
, function(err
) {
37 cb(err
, { id: this.lastID
});
42 // All challenges related to user with ID uid
43 getByUser: function(uid
, cb
) {
44 db
.serialize(function() {
48 "WHERE target IS NULL" +
50 " OR target = " + uid
;
51 db
.all(query
, (err
, challenges
) => {
57 remove: function(id
) {
58 db
.serialize(function() {
60 "DELETE FROM Challenges " +
66 safeRemove: function(id
, uid
) {
67 db
.serialize(function() {
71 "WHERE id = " + id
+ " " +
72 // Condition: I'm the sender or the target
73 "AND (uid = " + uid
+ " OR target = " + uid
+ ")";
74 db
.get(query
, (err
,chall
) => {
76 ChallengeModel
.remove(id
);
83 module
.exports
= ChallengeModel
;