From 1511c610b30e524a95c45c8fa263275ec3013101 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 4 Mar 2020 23:04:24 +0100
Subject: [PATCH] Fix targetted challenge deletion after refusal

---
 client/src/views/Hall.vue  | 5 +++--
 server/models/Challenge.js | 4 +++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index 8d8b311a..5cbe0c86 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -812,6 +812,8 @@ export default {
         const oppsid = this.getOppsid(c);
         if (oppsid)
           this.send("refusechallenge", { data: c.id, target: oppsid });
+        if (c.type == "corr")
+          ajax("/challenges", "DELETE", { id: c.id });
       }
       this.send("deletechallenge", { data: c.id });
     },
@@ -850,9 +852,8 @@ export default {
       }
       else {
         // My challenge
-        if (c.type == "corr") {
+        if (c.type == "corr")
           ajax("/challenges", "DELETE", { id: c.id });
-        }
         this.send("deletechallenge", { data: c.id });
       }
       // In all cases, the challenge is consumed:
diff --git a/server/models/Challenge.js b/server/models/Challenge.js
index 243da709..40d125c9 100644
--- a/server/models/Challenge.js
+++ b/server/models/Challenge.js
@@ -74,7 +74,9 @@ const ChallengeModel =
       const query =
         "SELECT 1 " +
         "FROM Challenges " +
-        "WHERE id = " + id + " AND uid = " + uid;
+        "WHERE id = " + id + " " +
+          // Condition: I'm the sender or the target
+          "AND (uid = " + uid + " OR to = " + uid + ")";
       db.get(query, (err,chall) => {
         if (!err && chall)
           ChallengeModel.remove(id);
-- 
2.44.0