Miki Tebeka avatar Miki Tebeka committed f0e9b7f

Deleting messages - the early days

Comments (0)

Files changed (1)

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))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.