1. Steven! Ragnarök
  2. messageme-clj

Commits

Steven! Ragnarök  committed 2671b87

Add console code and revamp inbox to mark messages as read.

  • Participants
  • Parent commits ea967ec
  • Branches default

Comments (0)

Files changed (2)

File src/messageme/console.clj

View file
  • Ignore whitespace
+(ns messageme.console
+  (:require [messageme.core :as mm]))
+
+(def prompt-string "> ")
+
+(def commands
+  {"send" mm/send
+   "inbox" mm/inbox
+   "connect" mm/connect
+   "exit" mm/exit})
+
+(defn execute [[command & arg-str]]
+  (if (commands command)
+    (apply (commands command) arg-str)
+    (println "Command" command "does not exist.")))
+
+(defn -main []
+  (println (str "Available comands: " (keys commands)))
+  (loop []
+    (print prompt-string) (flush)
+    (let [input (vec (.split (read-line) " " 2))]
+      (execute input))
+  (recur)))

File src/messageme/core.clj

View file
  • Ignore whitespace
 (ns messageme.core)
 
-(def message-box (agent []))
+(def message-box (agent ()))
 
 (def connected-users (agent []))
 
 (defn do-connect-user [user]
-  (send-off connected-users register-connected-user user))
+  (send-off connected-users (fn [connected-users]
+                              (conj connected-users user))))
 
 (defn do-insert-message [message]
-  (send-off message-box (fn [box] (conj box message))))
+  (send-off message-box (fn [box]
+                          (conj box message))))
+
+(defn do-notify-recipient [msg]
+  (send-off connected-users
+            (fn [users]
+              (if (some #{(:recipient msg)} users)
+                (println " ** Notification:" (:recipient msg)
+                         "has a new message from" (:sender msg))) users)))
+
+(defn mark-inbox-read
+  "Takes a message box and a user ands the message box with the user's messages
+  marked read."
+  [box user]
+  (let [other-msgs (remove #(= (% :recipent) user) box)
+        user-inbox (filter #(= (% :recipient) user) box)]
+    (concat other-msgs (map
+                         #({:sender (:sender %), :recipient (:recipient %),
+                            :body (:body %), :read true}) user-inbox))))
 
 (defn inbox-data [user]
+  (send-off message-box mark-inbox-read)
   (filter (fn [msg] (= (:recipient msg) user)) @message-box))
 
 (defn send-message [sender recipient body]
-  (do-insert-message {:sender sender, :recipient recipient,
-  :body body :read false}))
+  (let [msg {:sender sender, :recipient recipient, :body body :read false}]
+    (do-insert-message msg)
+    (do-notify-recipient msg)))
 
 (defn display-msg [msg]
   (println (str "  From: " (:sender msg) " | Message: " (:body msg))))
   (loop [[msg & remaining] msgs]
     (if msg
       (do (display-msg msg) (recur remaining))
-    (println))))
+      (println))))
+
+(defn do-display-unread [usr]
+  (let [unread (filter #(% :read) (inbox-data usr))]
+    (println (count unread) "messages arrived since you last connected:")
+    (loop [[msg & remaining] unread]
+      (if msg
+        (do (display-msg msg) (recur remaining))
+        (println)))))
 
 (defn connect [arg-str]
-  (register-connected-user arg-str)
+  (do-display-unread arg-str)
+  (do-connect-user arg-str))
 
 (defn send [arg-str]
   (let [[sender recipient body] (seq (.split arg-str " " 3))