From 8a981f390c286cb9c5935290e7846df903148278 Mon Sep 17 00:00:00 2001 From: Scott Ostler Date: Mon, 22 Feb 2010 22:29:02 -0500 Subject: Avatar-uploading --- static/js/pichat.js | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'static/js/pichat.js') diff --git a/static/js/pichat.js b/static/js/pichat.js index d3da949..06c859d 100755 --- a/static/js/pichat.js +++ b/static/js/pichat.js @@ -225,7 +225,7 @@ function initProfile() { $('.logged-dump .content').each(function() { var t = $(this); t.html(buildMsgContent(t.text())); - }); + }); var onSubmit = function(attr, newVal, oldVal) { newVal = $.trim(newVal); @@ -260,6 +260,10 @@ function initProfile() { 'callbackShowErrors': false }; $('#contact.editable, #bio.editable').editInPlace(textareaOpts); + if ($('#upload').length > 0) { + setupUploadAvatar('upload'); + } + }; function initLog() { @@ -267,7 +271,6 @@ function initLog() { var t = $(this); t.html(buildMsgContent(t.text())); }); - } // TODO @@ -275,13 +278,42 @@ function favoriteImage() {}; function setupUpload(elementId, roomKey) { new AjaxUpload(elementId, { - action: '/upload', + action: '/upload/message', autoSubmit: true, name: 'image', data: { room: roomKey } }); } +function setupUploadAvatar(elementId) { + // NOTE: AjaxUpload responses aren't converted from JSON. + var onSubmit = function(file, error) { + $('#spinner').show(); + }; + var onComplete = function(file, resp) { + $('#spinner').hide(); + if (resp == 'INVALID_REQUEST') { + location.href = location.href; + } else if (resp == 'NOT_LOGGED_IN') { + location.href = location.href; + } else if (resp == 'INVALID_IMAGE') { + alert("Sorry, dump.fm can't deal with your image. Pick another :("); + return; + } + var s = ''; + $('#avatarPic').replaceWith(s).show(); + $('#avatar').text(resp); + }; + new AjaxUpload(elementId, { + action: '/upload/avatar', + autoSubmit: true, + name: 'image', + onSubmit: onSubmit, + onComplete: onComplete + }); +} + + // scrolling stuff // this code keeps the div scrolled to the bottom, but will also let the user scroll up, without jumping down -- cgit v1.2.3-70-g09d2 From 10e5c6b24b63e5a83922058d47380577808b8d3e Mon Sep 17 00:00:00 2001 From: Scott Ostler Date: Wed, 24 Feb 2010 08:04:03 -0500 Subject: Added avatar uploading, images to logs --- src/site.clj | 61 ++++++++++++++++++++++++++----------------------- static/js/home.js | 3 ++- static/js/pichat.js | 1 - template/log.st | 7 ++---- template/logged_dump.st | 1 + 5 files changed, 37 insertions(+), 36 deletions(-) (limited to 'static/js/pichat.js') diff --git a/src/site.clj b/src/site.clj index 2936f13..4ffa3cf 100755 --- a/src/site.clj +++ b/src/site.clj @@ -123,6 +123,7 @@ (defn process-message-for-output [d] {"nick" (escape-html (d :nick)) + "avatar" (escape-html (d :avatar)) "message_id" (d :message_id) "created_on" (.format formatter (d :created_on)) "content" (escape-html (d :content))}) @@ -165,7 +166,7 @@ (defn fetch-messages-by-room ([room-id image-only] (fetch-messages-by-room room-id image-only 0)) ([room-id image-only offset] - (let [query (str "SELECT m.content, m.message_id, m.created_on, u.nick " + (let [query (str "SELECT m.content, m.message_id, m.created_on, u.nick, u.avatar " "FROM messages m, users u " "WHERE room_id = ? AND m.user_id = u.user_id " (if image-only "AND m.is_image = true " "") @@ -184,7 +185,7 @@ (defn fetch-messages-by-nick ([nick image-only] (fetch-messages-by-nick nick image-only 0)) ([nick image-only offset] - (let [query (str "SELECT m.content, m.created_on, u.nick " + (let [query (str "SELECT m.content, m.created_on, u.nick, u.avatar " "FROM messages m, users u, rooms r " "WHERE m.user_id = u.user_id AND u.nick = ? " "AND r.room_id = m.room_id AND r.admin_only = false " @@ -240,22 +241,25 @@ (defn parse-login-token [token] (let [x (.split token "\\%")] - (if (not (= (alength x) 3)) - nil) - (try [(aget x 0) (Long/parseLong (aget x 1)) (aget x 2)] - (catch NumberFormatException _ nil)))) - + (if (= (alength x) 3) + (try [(aget x 0) (Long/parseLong (aget x 1)) (aget x 2)] + (catch NumberFormatException _ nil))))) (defn read-login-token [token] - nil) + (if-let [[nick expiry token-hash] (parse-login-token token)] + (if (>= expiry (System/currentTimeMillis)) + (let [db-info (fetch-nick nick) + computed-hash (sha1-hash (db-info :hash) expiry)] + (if (= token-hash computed-hash) + (select-keys db-info [:user_id :nick :is_admin :avatar])))))) (defn make-login-token [{nick :nick hash :hash}] (let [expiration (ms-in-future *login-token-expiry*)] - (set-cookie *login-token-key* (encode-login-token nick - hash - expiration) - :expires (gmt-string (new Date expiration))))) + (set-cookie *login-token-key* + (encode-login-token nick hash expiration) + :expires + (gmt-string (new Date expiration))))) ;; Landing @@ -328,27 +332,26 @@ (resp-error "NO_USER"))) -;; TODO: update chat w/ new avatar -(defn do-update-avatar [session params] - nil) - -(defn do-update-profile [user-id attr val] +(defn update-user-db [user-id attr val] (with-connection db - (update-values "users" ["user_id = ?" user-id] {attr val}))) + (update-values "users" ["user_id = ?" user-id] {attr val}))) + +(defn download-avatar [session url] + (let [url false] + (update-user-db (session :user_id) "avatar" url) + (resp-success url))) (defn update-profile [session params] (let [user-id (session :user_id) attr (params :attr) val (params :val) attr-set #{"avatar" "contact" "bio"}] - (if (and user-id attr val - (contains? attr-set attr)) - (do - (do-update-profile attr val) - (if (= attr "avatar") - [(session-assoc :avatar val) "OK"] - "OK")) - (resp-error "BAD_REQUEST")))) + (cond (not user-id) (resp-error "MUST_LOGIN") + (not (and user-id attr val)) (resp-error "BAD_REQUEST") + (not (contains? attr-set attr)) (resp-error "BAD_REQUEST") + (= attr "avatar") (download-avatar session val) + :else (do (update-user-db user-id attr val) + (resp-success "OK"))))) ;; Chat @@ -395,7 +398,7 @@ (if-let [user-info (@users nick)] ; Incorporate avatar updates (commute users assoc nick (merge user-info {:last-seen now - :avatar (user-info :avatar)})) + :avatar (session :avatar)})) (commute (room :users) assoc nick (user-struct-from-session session)))) (resp-success (assoc (updates room since) :timestamp now))))) @@ -543,14 +546,14 @@ ; TODO: resize (copy image dest)) -;; NOTE -- Upload responses arent JSON-evaluated +;; N.B. -- Upload responses aren't JSON-evaluated (defn do-upload-avatar [session image] (let [filename (format-filename (:filename image)) dest (File. (rel-join *avatar-directory* filename)) url (image-url-from-file "avatars" dest)] (do (copy-and-resize (:tempfile image) dest) - (do-update-profile (session :user_id) "avatar" url) + (update-user-db (session :user_id) "avatar" url) [(session-assoc :avatar url) [200 url]]))) diff --git a/static/js/home.js b/static/js/home.js index 60b5d07..e9be833 100755 --- a/static/js/home.js +++ b/static/js/home.js @@ -152,7 +152,8 @@ function login() { var onSuccess = function(json) { location.href = location.href; if (typeof pageTracker !== 'undefined') { - pageTracker._setCustomVar(1, "logged-in", nick, + pageTracker._setCustomVar(1, "logged-in", nick); + } }; var onError = function(resp, textStatus, errorThrown) { diff --git a/static/js/pichat.js b/static/js/pichat.js index 566bd10..92d24a1 100755 --- a/static/js/pichat.js +++ b/static/js/pichat.js @@ -241,7 +241,6 @@ function initChat() { } function initProfile() { - jQuery(".linkify").each(function() { var text = jQuery(this).text(); jQuery(this).html(linkify(text)); diff --git a/template/log.st b/template/log.st index 0a12550..97eaa78 100755 --- a/template/log.st +++ b/template/log.st @@ -16,12 +16,9 @@
-
- -
+

-
$if(dumps)$ $dumps: { d | $logged_dump(dump=d)$ }$ @@ -31,7 +28,7 @@
$if(next)$ - + $endif$   diff --git a/template/logged_dump.st b/template/logged_dump.st index 86e23fc..01700b5 100755 --- a/template/logged_dump.st +++ b/template/logged_dump.st @@ -31,6 +31,7 @@ img{ }
+
$dump.created_on$ -- by $dump.nick$
$dump.content$

-- cgit v1.2.3-70-g09d2