diff options
| author | sostler <sbostler@gmail.com> | 2010-01-07 23:54:07 -0500 |
|---|---|---|
| committer | sostler <sbostler@gmail.com> | 2010-01-07 23:54:07 -0500 |
| commit | 4e837788114e07330be2dfb869efd70e525b2bc4 (patch) | |
| tree | c4a644a6e0981c302d3f997d61bf5619eedf728c | |
| parent | ca746303a8fc20f304bb3ccbeb74f8ae4631a37e (diff) | |
| parent | cce37b4db8a981ef00db0ebb75f7c98f1c336543 (diff) | |
Added multiple-window posting, removed posting delay
| -rwxr-xr-x | src/site.clj | 78 | ||||
| -rw-r--r-- | static/header.css | 4 | ||||
| -rw-r--r-- | static/moverc.png | bin | 0 -> 170 bytes | |||
| -rw-r--r-- | static/pichat.css | 53 | ||||
| -rwxr-xr-x | static/pichat.js | 31 | ||||
| -rw-r--r-- | static/profile.css | 15 | ||||
| -rwxr-xr-x | template/chat.st | 7 | ||||
| -rwxr-xr-x | template/footer.st | 1 |
8 files changed, 100 insertions, 89 deletions
diff --git a/src/site.clj b/src/site.clj index 796e833..1d27753 100755 --- a/src/site.clj +++ b/src/site.clj @@ -116,12 +116,10 @@ "created_on" (.format formatter (d :created_on)) "content" (encode-html-entities (d :content))}) -(defn new-messages - ([room since-ts] - (let [since-date (new Date (long since-ts))] - (reverse (take-while (fn [m] (.after (m :created_on) since-date)) - @(room :messages))))) - ([room] (reverse (take 25 @(room :messages))))) +(defn new-messages [room since-ts] + (let [since-date (new Date (long since-ts))] + (reverse (take-while (fn [m] (.after (m :created_on) since-date)) + @(room :messages))))) (defn process-user [u] (if (u :avatar) @@ -133,11 +131,10 @@ (map process-user (sort-by #(% :nick) (vals @(room :users))))) -(defn updates - ([room] {"users" (prepare-user-list room) - "messages" (map process-message-for-json (new-messages room))}) - ([room since] {"users" (prepare-user-list room) - "messages" (map process-message-for-json (new-messages room since))})) +(defn updates [room since] + {"users" (prepare-user-list room) + "messages" (map process-message-for-json + (new-messages room since))}) (def dumps-per-page 20) @@ -204,10 +201,9 @@ :avatar (user-info :avatar))) (defn landing [session] - (let [nick (session :nick)] - (if nick - (redirect-to "/chat") - (serve-file "static" "index.html")))) + (if (session :nick) + (redirect-to "/chat") + (serve-file "static" "index.html"))) (defn login [session params] (let [nick (params :nick) @@ -313,13 +309,12 @@ (.setAttribute st "users" user-list)) (.setAttribute st "messages" message-list) (.setAttribute st "roomkey" (room :key)) + (.setAttribute st "isadmin" (session :is_admin)) (.setAttribute st "json_room_key" (json-str (room :key))) - (.setAttribute st "roomname" (room :name)) (.setAttribute st "json_user_nick" (if nick (json-str nick) "null")) - (if nick - (.toString st) - [(session-assoc :last-seen now) - (.toString st)]))) + (.setAttribute st "roomname" (room :name)) + (.setAttribute st "timestamp" now) + (.toString st))) (defn validated-chat [session room-key] (let [room (@rooms room-key)] @@ -327,21 +322,16 @@ (chat session room) (resp-error "UNKNOWN_ROOM")))) -; TODO: handle anonymous users in multiple rooms (defn refresh [session params room] (dosync (let [now (System/currentTimeMillis) + since (Long/parseLong (params :since)) nick (session :nick) users (room :users)] - (if (or nick (session :last-seen)) - (if (contains? @users nick) - (let [last-seen (get-in @users [nick :last-seen])] - (alter users assoc-in [nick :last-seen] now) - (resp-success (updates room last-seen))) - (let [last-seen (session :last-seen)] - [(session-assoc :last-seen now) - (resp-success (updates room last-seen))])) - (resp-error "NOT_IN_CHAT"))))) + (if (contains? @users nick) + (alter users assoc-in [nick :last-seen] now)) + (resp-success (assoc (updates room since) + :timestamp now))))) (defn validated-refresh [session params] (let [room-key (params :room) @@ -353,7 +343,7 @@ ;; http://snippets.dzone.com/posts/show/6995 (def single-url-regex #"(?i)^((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$") -(def pic-regex #"(?i)\.(jpg|jpeg|png|gif|bmp)$") +(def pic-regex #"(?i)\.(jpg|jpeg|png|gif|bmp|ico)$") (defn strip-params [s] (.replaceFirst s "\\?.*$" "")) @@ -369,7 +359,6 @@ (alter (room :messages) (swap cons) msg) true))) -; TODO: validate user (defn msg-db [user-id room-id msg] (let [content (.trim (msg :content)) is-image (is-image? content)] @@ -401,7 +390,6 @@ ;; Chat Log ; TODO: Optimize dump counts - (defn log [session room offset] (let [st (fetch-template "log" session) offset (maybe-parse-int offset 0) @@ -436,16 +424,24 @@ [{:headers {"Cache-Control" "no-cache, no-store, max-age=0, must-revalidate"}} resp]) +(defn serve-static [path] + ; TODO: cache policy for other static files (js, css, etc.) + (let [cache-header (if (re-find pic-regex path) + {:headers {"Cache-Control" + "post-check=3600,pre-check=43200"}} + {})] + [cache-header + (serve-file "static" path)])) + (defroutes pichat (GET "/" (no-cache (landing session))) - (GET "/static/*" (or (serve-file "static" (params :*)) - :next)) - (GET "/favicon.ico" (serve-file "static" "favicon.ico")) - (GET "/u/:nick" (profile session (-> request :route-params :nick) "0")) - (GET "/u/:nick/" (profile session (-> request :route-params :nick) "0")) - (GET "/u/:nick/:offset" (profile session - (-> request :route-params :nick) - (-> request :route-params :offset))) + (GET "/static/*" (serve-static (params :*))) + (GET "/favicon.ico" (serve-static "favicon.ico")) + (GET "/u/:nick" (profile session (params :nick) "0")) + (GET "/u/:nick/" (profile session (params :nick) "0")) + (GET "/u/:nick/:offset" (profile session + (params :nick) + (params :offset))) (GET "/update-profile" (update-profile session params)) (GET "/login" (login session params)) (GET "/logout" (logout session)) diff --git a/static/header.css b/static/header.css index d981931..c32510c 100644 --- a/static/header.css +++ b/static/header.css @@ -64,11 +64,11 @@ filter: progid:DXImageTransform.Microsoft.dropShadow(color=#036aca, offX=3, offY } #logo7{ top:20px; - margin-left: 1%; + margin-left: 15px; margin-right: 10%; z-index:1000; float:left; - margin-top: 3px; + margin-top: 4px; } #rapper7{ top: 0px; diff --git a/static/moverc.png b/static/moverc.png Binary files differnew file mode 100644 index 0000000..709f78d --- /dev/null +++ b/static/moverc.png diff --git a/static/pichat.css b/static/pichat.css index 9c337e1..40720b7 100644 --- a/static/pichat.css +++ b/static/pichat.css @@ -3,7 +3,7 @@ body { background-color:#e3e3e3; - + margin:0; } @@ -56,24 +56,20 @@ color:000; #messagePane { border: 2px solid #c8cbce; - padding: 5px; -top:58px; + +top:56px; bottom:65px; position:fixed; - width: 98%; + width: 99%; background-color:#FFF; - left:7px; - right:10px; +left:0.3%; - float: left; - min-width:500px; z-index:5; } #messageList { height: 100%; width: 100%; - position:inherit overflow-y: auto; overflow-x: hidden; } @@ -94,8 +90,8 @@ bottom:65px; position:relative; min-width:500px; z-index:100; -left:0px; -bottom:5px; +left:1px; +bottom:10px; width: 100%; } @@ -104,7 +100,7 @@ bottom:5px; z-index:100; background-color:#FFF; margin-top: 15px; - left:0px; + left:-3px; position:relative; border:2px solid #c8cbce; height:36px; @@ -188,7 +184,7 @@ top:15px; -webkit-border-bottom-right-radius:5px; -moz-border-radius-bottomleft:5px; -moz-border-radius-bottomright:5px; - max-height:250px; + max-height:450px; box-shadow: 3px 4px 4px #c8cbce; -webkit-box-shadow: 3px 4px 4px #c8cbce; -moz-box-shadow: 3px 3px 4px #c8cbce; @@ -198,7 +194,7 @@ filter: progid:DXImageTransform.Microsoft.dropShadow(color=#c8cbce, offX=3, offY text-align: left; } .username{height:30px; -margin-top:2px; +margin-top:6px; line-height:20px; text-indent:6px; z-index:18; @@ -216,7 +212,9 @@ text-decoration:none; display:block; width:100%; height:100%; -color:#000; +background-image:url(/static/moverc.png); +background-repeat:repeat-x; +color:#fff; text-decoration:none; background-color:#f3f3f3; border-top-right-radius:5px; @@ -232,22 +230,23 @@ background-color:#f3f3f3; max-height:30px; height: expression(this.width > 30 ? 30: true); max-width:30px; + float:right; right:1px; z-index:77; height:30px; - border-top-left-radius:5px; - border-top-right-radius:5px; - -webkit-border-top-left-radius:5px; - -webkit-border-top-right-radius:5px; - -moz-border-radius-topleft:5px; - -moz-border-radius-topright:5px; - border-bottom-left-radius:5px; - border-bottom-right-radius:5px; - -webkit-border-bottom-left-radius:5px; - -webkit-border-bottom-right-radius:5px; - -moz-border-radius-bottomleft:5px; - -moz-border-radius-bottomright:5px; + border-top-left-radius:3px; + border-top-right-radius:3px; + -webkit-border-top-left-radius:3px; + -webkit-border-top-right-radius:3px; + -moz-border-radius-topleft:3px; + -moz-border-radius-topright:3px; + border-bottom-left-radius:3px; + border-bottom-right-radius:3px; + -webkit-border-bottom-left-radius:3px; + -webkit-border-bottom-right-radius:3px; + -moz-border-radius-bottomleft:3px; + -moz-border-radius-bottomright:3px; } #userListicon { diff --git a/static/pichat.js b/static/pichat.js index 51489a4..3267b49 100755 --- a/static/pichat.js +++ b/static/pichat.js @@ -57,15 +57,12 @@ function handleMsgError(resp) { } function submitMessage() { - var content = $('#msgInput').val(); + var content = $.trim($('#msgInput').val()); if (content == '') { return; } - - $('#msgInput, #msgSubmit').attr('disabled', 'disabled'); - var onSuccess = function(json) { - $('#msgInput, #msgSubmit').removeAttr('disabled'); - $('#msgInput').val(''); - }; - + PostedMessages.push(content); + $('#msgInput').val(''); + + var onSuccess = function(json) {}; var onError = function(resp, textStatus, errorThrown) { $('#msgInput, #msgSubmit').removeAttr('disabled'); handleMsgError(resp); @@ -120,12 +117,24 @@ function updateUI(msgs, users) { } } +// A duplicate message is a message that was likely to have +// originated from this browser. +function isDuplicateMessage(m) { + if (m.nick != Nick || $.inArray(m.content, PostedMessages) == -1) { + return false; + } + var now = new Date().getTime(); + console.log(now, m.created_on); + return m.created_on - now < 5000; +} + function refresh() { var onSuccess = function(json) { + Timestamp = json.timestamp; var messages = $.grep( json.messages, - function(m) { return m.nick != Nick }); - updateUI(messages, json.users); + function(m) { return !isDuplicateMessage(m) }); + updateUI(messages, json.users); setTimeout(refresh, 1000); }; var onError = function(resp, textStatus, errorThrown) { @@ -136,7 +145,7 @@ function refresh() { type: 'GET', timeout: 5000, url: '/refresh', - data: { 'room': Room }, + data: { 'room': Room, 'since': Timestamp }, cache: false, dataType: 'json', success: onSuccess, diff --git a/static/profile.css b/static/profile.css index 4b9e2b4..a8989db 100644 --- a/static/profile.css +++ b/static/profile.css @@ -25,16 +25,16 @@ -webkit-border-bottom-right-radius:5px; -moz-border-radius-bottomleft:5px; -moz-border-radius-bottomright:5px; - box-shadow: -3px 4px 10px #c8cbce; --webkit-box-shadow: -3px 4px 10px #c8cbce; --moz-box-shadow: -10px -3px 10px #c8cbce; + box-shadow: 10px 3px 10px #c8cbce; +-webkit-box-shadow:10px 3px 10px #c8cbce; +-moz-box-shadow:10px 3px 10px #c8cbce; line-height:1.6; } #chatrap{ width:600 ; - margin-left: auto ; + margin-left: 18%; margin-right: auto ; @@ -67,13 +67,13 @@ letter-spacing:2px; -webkit-border-bottom-right-radius:5px; -moz-border-radius-bottomleft:5px; -moz-border-radius-bottomright:5px; - box-shadow: 3px 4px 10px #c8cbce; --webkit-box-shadow: 3px 4px 10px #c8cbce; + box-shadow: 15px 15px 10px #c8cbce; +-webkit-box-shadow: 15px 15px 10px #c8cbce; -moz-box-shadow: 15px 15px 10px #c8cbce; } #pnav a { font-size: 12px; - color: #000; + color:#444; } pnav a:link { text-decoration: none; @@ -209,6 +209,7 @@ color:#087cff; } #loghead { font-size:1px; + } body { diff --git a/template/chat.st b/template/chat.st index c5f176e..1bbdaf4 100755 --- a/template/chat.st +++ b/template/chat.st @@ -7,9 +7,14 @@ jQuery(document).ready(initChat); var Nick = $json_user_nick$; var Room = $json_room_key$; + var Timestamp = $timestamp$; + var PostedMessages = []; </script> </head> - <body> + <body> + $if(isadmin)$ + <h1>FUCK WE AN ADMIN</h1> + $endif$ $banner()$ <div id="chatrap"><div id="logc"> <div id="content"> diff --git a/template/footer.st b/template/footer.st index 82569f2..9fb6471 100755 --- a/template/footer.st +++ b/template/footer.st @@ -4,6 +4,7 @@ <a title="Web Analytics" href="http://getclicky.com/170656"><img alt="Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a> <script src="http://static.getclicky.com/js" type="text/javascript"></script> <script type="text/javascript">clicky.init(170656);</script> + <noscript><p><img alt="Clicky" width="1" height="1" src="http://static.getclicky.com/170656ns.gif" /></p></noscript> <script type="text/javascript"> |
