summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsostler <sbostler@gmail.com>2010-03-09 08:14:30 -0500
committersostler <sbostler@gmail.com>2010-03-09 08:14:30 -0500
commitcedfbcf91928d6666a13e133106a6b876aba61a1 (patch)
treec4fc41c6cd53cefaefba672917a47ba77cf1b04a /src
parent8ec592855b71751897c5448cdc591d1595ee6f86 (diff)
User directory ROUGH
Diffstat (limited to 'src')
-rwxr-xr-xsrc/site.clj58
1 files changed, 47 insertions, 11 deletions
diff --git a/src/site.clj b/src/site.clj
index 2717b0e..39050e0 100755
--- a/src/site.clj
+++ b/src/site.clj
@@ -353,7 +353,7 @@
(with-connection *db*
(update-values "users" ["user_id = ?" user-id] {attr val})))
-(defn update-avatar [session url]
+(defn update-avatar [session url]
(update-user-db (session :user_id) "avatar" url)
[(session-assoc :avatar url)
(resp-success url)])
@@ -372,23 +372,58 @@
;; Directory
+(def *directory-agent* (agent nil))
+(def *directory-listing* (ref []))
(def *per-directory-page* 25)
-(def *update-directory* true)
-(def *update-directory-ms* (* 60 60 1000))
+(def *run-update-directory* true)
+(def *update-directory-sleep-ms* (* 60 60 1000))
(defn directory-search [offset]
- (let [qry (str "SELECT u.nick, u.avatar, m.content "
+ (let [directory @*directory-listing*
+ users (subvec directory
+ (* offset *per-directory-page*)
+ (min (count directory)
+ (* (inc offset) *per-directory-page*)))
+ user-ids (apply str (interpose ", " (map #(%1 :user_id) users)))
+ qry (str "SELECT u.user_id, u.avatar, m.content "
"FROM users u, messages m "
- "WHERE m.message_id = "
- " (SELECT n.message_id FROM messages n "
- " WHERE n.user_id = u.user_id ORDER BY n.created_on DESC LIMIT 1) "
- "ORDER BY (SELECT COUNT(*) FROM messages where user_id = u.user_id) "
- "LIMIT ? OFFSET ?")]
- (do-select [qry *per-directory-page* (* offset *per-directory-page*)])))
+ "WHERE u.user_id in (" user-ids ") "
+ "AND m.created_on = (select max(created_on) from messages "
+ " where user_id = u.user_id)")]
+ (when (> (count user-ids) 0)
+ (let [res (do-select [qry])
+ keys (map :user_id res)
+ dict (zipmap keys res)]
+ (map (fn [u]
+ (let [u-id (u :user_id)]
+ (merge u (dict u-id))))
+ users)))))
+
+
+
+(defn update-directory! []
+ (let [qry (str "SELECT u.user_id, COUNT(m) as cnt "
+ "FROM users u, messages m "
+ "WHERE u.user_id = m.user_id "
+ "GROUP BY u.user_id "
+ "ORDER BY COUNT(m) DESC")
+ res (vec (do-select [qry]))]
+ (dosync (ref-set *directory-listing* res))
+ res))
+
+(defn update-directory-agent-func [x]
+ (update-directory!)
+ (Thread/sleep *update-directory-sleep-ms*)
+ (when *run-update-directory*
+ (send *directory-agent* #'update-directory-agent-func))
+ x)
+
+(defn start-directory-updater! []
+ (send *directory-agent* update-directory-agent-func))
(defn directory [session offset]
(let [st (fetch-template "directory" session)
- users (to-array (map process-directory-listing (directory-search offset)))]
+ users (to-array (directory-search offset))]
(.setAttribute st "users" users)
(cond (= offset 0) (.setAttribute st "prev" false)
(= offset 1) (.setAttribute st "prev" "")
@@ -770,4 +805,5 @@
(start-user-flusher!)
(start-session-pruner!)
+(start-directory-updater!)
(start-server (options :port)) \ No newline at end of file