From 51145f8c0e0595f195e26827e932306eb0083c10 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 4 Mar 2020 23:18:37 +0100
Subject: [PATCH] Fix targetted challenge deletion on server when refused

---
 server/models/Challenge.js  | 6 +++---
 server/routes/challenges.js | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/server/models/Challenge.js b/server/models/Challenge.js
index 40d125c9..2be700bd 100644
--- a/server/models/Challenge.js
+++ b/server/models/Challenge.js
@@ -31,10 +31,10 @@ const ChallengeModel =
     db.serialize(function() {
       const query =
         "INSERT INTO Challenges " +
-          "(added, uid, " + (!!c.to ? "target, " : "") +
+          "(added, uid, " + (c.to ? "target, " : "") +
           "vid, randomness, fen, cadence) " +
         "VALUES " +
-          "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
+          "(" + Date.now() + "," + c.uid + "," + (c.to ? c.to + "," : "") +
           c.vid + "," + c.randomness + ",'" + c.fen + "','" + c.cadence + "')";
       db.run(query, function(err) {
         cb(err, {cid: this.lastID});
@@ -76,7 +76,7 @@ const ChallengeModel =
         "FROM Challenges " +
         "WHERE id = " + id + " " +
           // Condition: I'm the sender or the target
-          "AND (uid = " + uid + " OR to = " + uid + ")";
+          "AND (uid = " + uid + " OR target = " + uid + ")";
       db.get(query, (err,chall) => {
         if (!err && chall)
           ChallengeModel.remove(id);
diff --git a/server/routes/challenges.js b/server/routes/challenges.js
index aee6fd2a..680a69fd 100644
--- a/server/routes/challenges.js
+++ b/server/routes/challenges.js
@@ -24,7 +24,7 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => {
     if (req.body.chall.to)
     {
       UserModel.getOne("name", challenge.to, (err,user) => {
-        if (!!err || !user)
+        if (err || !user)
           res.json(err || {errmsg: "Typo in player name"});
         else
         {
-- 
2.44.0