diff options
| -rwxr-xr-x | src/site.clj | 49 | ||||
| -rwxr-xr-x | static/pichat.js | 27 | ||||
| -rwxr-xr-x | template/chat.st | 1 |
3 files changed, 45 insertions, 32 deletions
diff --git a/src/site.clj b/src/site.clj index 2bbc2bb..e2bcf5e 100755 --- a/src/site.clj +++ b/src/site.clj @@ -25,6 +25,10 @@ (defstruct user-struct :nick :user_id :avatar :last-seen) (defstruct message-struct :nick :content :created_on) +(defn user-struct-from-session [session] + (struct user-struct (session :nick) (session :user_id) (session :avatar) + (System/currentTimeMillis))) + (def rooms (ref {})) (def run-flusher true) @@ -84,11 +88,6 @@ (doall rs)))) :count)) -;; Room handling - -(defn fetch-rooms [] - (do-select ["SELECT * FROM ROOMS"])) - ;; User authentication (def nick-regex #"^[A-Za-z0-9\-_∆˚†]*$") @@ -106,7 +105,18 @@ (let [db-user (fetch-nick nick)] (and db-user (= (db-user :hash) hash) db-user))) -;; Message handling +;; Room handling + +(defn fetch-rooms [] + (do-select ["SELECT * FROM ROOMS"])) + +(defn login-user [user room] + (alter (room :users) assoc (user :nick) user)) + +(defn add-message [msg room] + (alter (room :messages) (swap cons) msg)) + +;; Output (defn process-message-for-json [d] (assoc d :created_on (.getTime (d :created_on)))) @@ -243,8 +253,6 @@ (defn non-empty-string? [s] (and s (> (count s) 0))) - -; TODO: hide admin-only rooms from profile (defn profile [session profile-nick offset] (let [user-info (fetch-nick profile-nick)] (if user-info @@ -293,10 +301,6 @@ (or (not (room :admin_only)) (session :is_admin)))) -(defn user-struct-from-session [session] - (struct user-struct (session :nick) (session :user_id) (session :avatar) - (System/currentTimeMillis))) - (defn chat [session room] (let [now (System/currentTimeMillis) nick (session :nick) @@ -307,7 +311,7 @@ (reverse (fetch-messages-by-room (room :room_id) false))))] (if nick (dosync - (alter (room :users) assoc nick (user-struct-from-session session)))) + (login-user (user-struct-from-session session) room))) (let [user-list (to-array (prepare-user-list room))] (.setAttribute st "users" user-list)) (.setAttribute st "messages" message-list) @@ -359,12 +363,6 @@ (re-find pic-regex (strip-params content))) true false)) -(defn msg-transaction [nick msg room] - (dosync - (and (contains? (ensure (room :users)) nick) - (alter (room :messages) (swap cons) msg) - true))) - (defn msg-db [user-id room-id msg] (let [content (.trim (msg :content)) is-image (is-image? content)] @@ -381,10 +379,15 @@ content (.trim (params :content)) now (new Date) msg (struct message-struct nick content now)] - (cond (not room) (resp-error "BAD_ROOM") - (not (msg-transaction nick msg room)) (resp-error "MUST_LOGIN") - :else (do (msg-db user-id (room :room_id) msg) - (resp-success "OK"))))) + (if (not room) + (resp-error "BAD_ROOM") + (do + (dosync + (if (not (contains? @(room :users) nick)) + (login-user (user-struct-from-session session) room)) + (add-message msg room)) + (msg-db user-id (room :room_id) msg) + (resp-success "OK"))))) (defn validated-msg [session params] (let [room-key (params :room) diff --git a/static/pichat.js b/static/pichat.js index 4384ed0..8f61c9b 100755 --- a/static/pichat.js +++ b/static/pichat.js @@ -47,12 +47,12 @@ function buildUserDiv(user) { function handleMsgError(resp) { var respText = resp.responseText ? resp.responseText.trim() : false; - if (respText == 'UNKNOWN_USER') { + if (respText == 'MUST_LOGIN') { alert("Can't send message! Please login."); } else if (respText) { - alert("Cannot send message! (" + respText + ")"); + alert("Can't send message! (" + respText + ")"); } else { - alert("Cannot send message!"); + alert("Can't send message!"); } } @@ -124,20 +124,29 @@ function isDuplicateMessage(m) { 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 !isDuplicateMessage(m) }); - updateUI(messages, json.users); + try { + Timestamp = json.timestamp; + var messages = $.grep( + json.messages, + function(m) { return !isDuplicateMessage(m) }); + updateUI(messages, json.users); + } catch(e) { + if (IsAdmin) { + alert("Exception in refresh"); + console.log(e); + } + } setTimeout(refresh, 1000); }; var onError = function(resp, textStatus, errorThrown) { + if (IsAdmin) { + console.log(resp, textStatus, errorThrown); + } setTimeout(refresh, 1000); }; diff --git a/template/chat.st b/template/chat.st index fd69286..d7ae38b 100755 --- a/template/chat.st +++ b/template/chat.st @@ -7,6 +7,7 @@ jQuery(document).ready(initChat); var Nick = $json_user_nick$; var Room = $json_room_key$; + var IsAdmin = $isadmin$; var Timestamp = $timestamp$; var PostedMessages = []; </script> |
