Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for gravatar

  • Loading branch information...
commit a989824cfe7ef13e1dcf9bbc2ce573791ab4761a 1 parent 23acc28
@maio maio authored
View
8 resources/public/main.css
@@ -0,0 +1,8 @@
+img {
+ float: left;
+ margin-right: 10px;
+}
+
+p {
+ margin-bottom: 30px;
+}
View
22 src/demo/core.clj
@@ -3,32 +3,34 @@
[ring.adapter.jetty :as jetty]
[demo.page :as page]
[demo.message :as msg]
+ [demo.gravatar :as gravatar]
[ring.util.response :as resp]
[ring.middleware session stacktrace params keyword-params])
(:use [compojure core route]))
(defonce rooms (atom {}))
-(defn add-message-to-room! [ROOM AUTHOR BODY]
+(defn add-message-to-room! [ROOM AUTHOR EMAIL BODY]
(when-not (empty? BODY)
(swap! rooms update-in [ROOM]
- msg/log-add-message AUTHOR BODY
+ msg/log-add-message AUTHOR (gravatar/generate-token EMAIL) BODY
(str (java.util.Date.)))))
(defroutes room-route
(GET "/" [room :as req]
(let [logs (@rooms room)]
- (page/render-room room logs (-> req :session :author))))
- (POST "/" [room author body :as req]
+ (page/render-room room logs (-> req :session :author) (-> req :session :email))))
+ (POST "/" [room author email body :as req]
(let [logs (@rooms room)
written-by (or (when-not (empty? author) author)
(-> req :session :author)
"Anonymous")]
- (add-message-to-room! room author body)
+ (add-message-to-room! room author email body)
;; redirect back to room
(-> (resp/redirect-after-post (str "/room/" room))
;; and add a author into session as a cookie
- (assoc-in [:session :author] author)))))
+ (assoc-in [:session :author] author)
+ (assoc-in [:session :email] email)))))
(defn room-validate
"Validates room parameter - if room already exits"
@@ -53,11 +55,11 @@
(GET "/" []
(page/list-chatrooms @rooms))
(context "/room/:room" []
- room-validate
- room-route)
+ #'room-validate
+ #'room-route)
(context "/api/:room" []
- room-validate
- api-route)
+ #'room-validate
+ #'api-route)
(resources "/")
(POST "/new-room" [:as r]
(let [new-name (-> r :params :room-id
View
15 src/demo/gravatar.clj
@@ -0,0 +1,15 @@
+(ns demo.gravatar)
+
+(defn md5
+ "Generate a md5 checksum for the given string"
+ [token]
+ (let [hash-bytes
+ (doto (java.security.MessageDigest/getInstance "MD5")
+ (.reset)
+ (.update (.getBytes token)))]
+ (.toString
+ (new java.math.BigInteger 1 (.digest hash-bytes)) ; Positive and the size of the number
+ 16)))
+
+(defn generate-token [EMAIL]
+ (md5 EMAIL))
View
17 src/demo/message.clj
@@ -7,12 +7,13 @@
(sorted-map 1 MESSAGE)
(assoc LOG (-> LOG rseq first first inc) MESSAGE)))
-(defn log-add-message
- [LOG AUTHOR BODY DATE]
- (log-add LOG {:body BODY,
- :author AUTHOR,
- :date DATE}))
+(defn log-add-message [LOG AUTHOR GRAVATAR BODY DATE]
+ (log-add LOG {:body BODY,
+ :author AUTHOR,
+ :gravatar-token GRAVATAR,
+ :date DATE}))
-(defn render-message
- [[ID {:keys [author date body]}]]
- [:p {:id (str "message-" ID)} [:b author] " " date [:br] body])
+(defn render-message [[ID {:keys [author gravatar-token date body]}]]
+ [:p {:id (str "message-" ID)}
+ [:img {:src (str "http://www.gravatar.com/avatar/" gravatar-token "?s=48")}]
+ [:b author] " " date [:br] body])
View
4 src/demo/page.clj
@@ -5,6 +5,7 @@
(defn page [TITLE & BODY]
(html5 [:html
[:head
+ [:link {:rel "stylesheet" :href "/main.css"}]
[:title TITLE]]
(into [:body] BODY)]))
@@ -23,13 +24,14 @@
[:form {:method "post" :action "new-room"}
"Room name:" (text-field "room-id") (submit-button "Enter")]))
-(defn render-room [NAME LOGS AUTHOR]
+(defn render-room [NAME LOGS AUTHOR EMAIL]
(page (str "Room " NAME)
(include-cljs)
[:h1 NAME " room"]
;; input part
[:form {:method "post" :action (str "/room/" (url-encode NAME))}
(text-field :author (or AUTHOR "Anonymous")) [:br]
+ (text-field {:placeholder "Email"} :email (or EMAIL "")) [:br]
(text-area {:rows 5 :cols 40} :body) [:br]
(submit-button {:id "submit-message"} "Submit")]
;; messages
Please sign in to comment.
Something went wrong with that request. Please try again.