Miki Tebeka committed f0e9b7f

Deleting messages - the early days

  • Participants
  • Parent commits 59a273c

Comments (0)

Files changed (1)

File src/duderemember.clj

   (if (zero? (count messages))
     "No messages for you!"
     (for [message messages]
-      [:div {:class "message"}
+      [:div {:class "message" :id (key->string (:key message))}
        "Time: " (format-time (:time message)) [:br]
        (if (nil? (:url message))
          ["Text:" [:br] (:text message)]
           via voice calls or SMS"]
     [:div "All you need to do is to " [:a {"href" (login-url "/")} "login"]
           " using your Google account and update your phone."]))
+(defn can-delete [user message]
+  (let [user-id (:user-id user)]
+    (and (not (nil? user-id)) (= user-id (:user-id message)))))
+(defn delete-message [id]
+  (let [message-key (string->key id)
+        messages (select "message" where (= :key message-key))]
+    (if (empty? messages)
+      (do (log/error (format "DELETE %s - not found" id))
+          {:status 404 :body (format "Eeek, can't find %s!" id)})
+      (let [message (first messages)]
+        (if (not (can-delete (current-user) message))
+          (do 
+            (log/error (format "Unauthorized: %s delete %s" (current-user) id))
+            {:status 401 :body "No DELETE for you!"})
+          (do (delete-entity message-key)
+              {:status 200 :body "OK"}))))))
 (defn welcome-page []
   (let [user (current-user)]
     (if (nil? user)
         (let [phone (params "phone")]
           (update-user (current-user) phone)
           (redirect "/")))
+  (DELETE "/message/:id" [id] (delete-message id))
   (POST "/twilio/sms" {params :params} (twilio-sms params))
   (POST "/twilio/voice" {params :params} (twilio-voice params))