summaryrefslogtreecommitdiff
path: root/src/site.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/site.clj')
-rw-r--r--src/site.clj152
1 files changed, 58 insertions, 94 deletions
diff --git a/src/site.clj b/src/site.clj
index 3ec07a5..5b20361 100644
--- a/src/site.clj
+++ b/src/site.clj
@@ -71,9 +71,9 @@
(assoc d :created_on (.getTime (d :created_on))))
(defn message-room-link [m]
- (if (= (:key m) "dumpfm")
- "http://dump.fm/chat"
- (format "http://%s.dump.fm" (:key m))))
+ (if (default-room? (:key m *default-room*))
+ "http://dump.fm/"
+ (format "http://%s.dump.fm/" (:key m))))
(defn process-message-for-output [d]
(escape-html-deep
@@ -306,65 +306,6 @@ WHERE user_id IN
[(session-assoc-from-db db-user)
(resp-success "OK")])))))
-;; Fav scores
-
-(def *score-query* "
-SELECT u.user_id,
- u.nick,
- COUNT(*) AS cnt
-FROM tags t,
- messages m,
- users u
-WHERE t.message_id = m.message_id
- AND m.user_id != t.user_id
- AND m.user_id = u.user_id
-GROUP BY u.user_id, u.nick
-ORDER BY cnt DESC
-")
-
-(defn build-score-list []
- (let [res (vec (do-select [*score-query*]))]
- {:list res
- :map (zipmap (map :nick res) (map :cnt res))}))
-
-;; sostler: stop score refresh until redis cache can be added
-(def *scores-refresh-period-sec* (* 29 60 9999999))
-
-(def *user-scores*
- (scheduled-agent build-score-list
- *scores-refresh-period-sec*
- []))
-
-(def *piece-map*
- (zipmap
- [:pawn :knight :bishop :rook :queen :king :skull]
- ["♟" "♞" "♝" "♜" "♛" "♚" "☠"]))
-
-(defn score-to-piece [score]
- (cond (= score -1) :skull
- (= score 0) :pawn
- (< score 50) :knight
- (< score 150) :bishop
- (< score 300) :rook
- (< score 1000) :queen
- :else :king))
-
-(def score-to-entity (comp *piece-map* score-to-piece))
-
-(defn lookup-score [nick]
- (if (= (lower-case nick) "scottbot")
- -1
- (let [scores (:map (poll *user-scores*))]
- (get scores nick 0))))
-
-(defn get-user-ranking [offset num]
- (if-let [ranking (:list (poll *user-scores*))]
- (let [cnt (count ranking)]
- (subvec ranking
- (min cnt (* offset num))
- (min cnt (* (inc offset) num))))))
-
-
;; Profile
(defn pull-random-dump-images [dumps num]
@@ -375,7 +316,8 @@ ORDER BY cnt DESC
(comp take-images :content)
dumps))))))
-(def use-redis-favscore true)
+(defn pull-recips [dumps]
+ (set (apply concat (map #(get % "recips" []) dumps))))
(defn profile
([session profile-nick] (profile session profile-nick "profile"))
@@ -386,15 +328,13 @@ ORDER BY cnt DESC
nick (:nick session)
logger (make-time-logger)
is-home (and nick (= nick profile-nick))
- score (if use-redis-favscore
- (fetch-redis-favscore profile-nick)
- (lookup-score profile-nick))
+ score (fetch-redis-favscore profile-nick)
dumps (logger tags/fetch-dumps
:user-tag-id (:user_id session)
:nick profile-nick
:limit 10)
- dms (fetch-private-messages (:user_id user-info))
- recips (set (apply concat (map #(get % "recips") dms)))
+ dms (fetch-direct-messages (:user_id user-info))
+ recips (pull-recips dms)
imgs (pull-random-dump-images dumps 5)]
(do
(.setAttribute st "is_home" is-home)
@@ -406,7 +346,7 @@ ORDER BY cnt DESC
(.setAttribute st "score_ent" (score-to-entity score))
(when-not (empty? dms)
(.setAttribute st "dms" dms)
- (.setAttribute st "recips" (json-str (map lower-case recips))))
+ (.setAttribute st "recips" (json-str recips)))
(if (not (empty? imgs))
(.setAttribute st "imgs" imgs))
(.setAttribute st "debug_log_items" (logger))
@@ -457,9 +397,7 @@ ORDER BY cnt DESC
(defn build-mini-profile [user-info]
(let [st (fetch-template-fragment "mini_profile")
nick (user-info :nick)
- score (if use-redis-favscore
- (fetch-redis-favscore nick)
- (lookup-score nick))]
+ score (fetch-redis-favscore nick)]
(doseq [a [:nick :avatar :contact :bio]]
(let [v (user-info a)]
(.setAttribute st (name a)
@@ -486,6 +424,7 @@ ORDER BY cnt DESC
:msg-id msg-id
:date (if msg-id nil date)
:limit (inc *dumps-per-page*))
+ recips (map :nick (get-recips-from-msgs raw-dumps))
back-dumps (if (or date msg-id)
(tags/fetch-dumps
:nick (:nick user-info)
@@ -497,8 +436,9 @@ ORDER BY cnt DESC
(.setAttribute st "nick" (:nick user-info))
(.setAttribute st "is_home" (= (:nick user-info) (:nick session)))
(.setAttribute st "mini_profile" (build-mini-profile user-info))
- (if (> (count dumps) 0)
+ (when (> (count dumps) 0)
(.setAttribute st "dumps" dumps))
+ (.setAttribute st "recips" (json-str recips))
(.setAttribute st "prev"
(if back-dumps
(cond
@@ -518,20 +458,18 @@ ORDER BY cnt DESC
;; Who faved me
-(def use-popular-redis true)
-
(defn popular [session profile-nick]
(if-let [user-info (fetch-nick profile-nick)]
(let [st (fetch-template "popular" session)
profile-nick (:nick user-info)
- raw-dumps (if use-popular-redis
- (fetch-popular-dumps-redis profile-nick (:nick session))
- (fetch-popular-dumps profile-nick (:nick session)))
+ raw-dumps (fetch-popular-dumps-redis profile-nick (:nick session))
raw-dumps (filter #(> (:count %) 0) raw-dumps)
+ recips (map :nick (get-recips-from-msgs raw-dumps))
dumps (map process-message-for-output raw-dumps)]
(.setAttribute st "nick" profile-nick)
(.setAttribute st "mini_profile" (build-mini-profile user-info))
(.setAttribute st "dumps" dumps)
+ (.setAttribute st "recips" (json-str recips))
(.toString st))
(resp-error "NO_USER")))
@@ -603,6 +541,7 @@ ORDER BY cnt DESC
(if (= nick-from-url (:nick message))
(let [st (fetch-template "single_message" session)]
(.setAttribute st "dump" (process-message-for-output message))
+ (.setAttribute st "recips" (json-str (map :nick (get-recips (:content message)))))
(.toString st))
(resp-error "NO_MESSAGE"))
(resp-error "NO_MESSAGE")))
@@ -638,7 +577,7 @@ ORDER BY cnt DESC
(doto st
(.setAttribute "users" (prepare-user-list room true))
(.setAttribute "messages" message-list)
- (.setAttribute "recips" (json-str (map lower-case recips)))
+ (.setAttribute "recips" (json-str recips))
(.setAttribute "roomkey" (room :key))
(.setAttribute "isadminroom" (room :admin_only))
(.setAttribute "json_room_key" (json-str (room :key)))
@@ -670,7 +609,7 @@ ORDER BY cnt DESC
:avatar (session :avatar)}))
(commute users assoc nick (user-struct-from-session session))))
(resp-success (assoc (updates nick room old-ts)
- :timestamp now)))))
+ :timestamp now)))))
(defn validated-refresh [session params]
(let [room-key (params :room)
@@ -703,16 +642,16 @@ ORDER BY cnt DESC
mute (resp-error (format-mute mute))
:else
(let [content (validated-content content session)
- msg-info (insert-message! user-id nick (:room_id room) content)
+ msg-info (insert-message! user-id nick (:avatar session) room content)
msg-id (:msg-id msg-info)]
(dosync
- (if (not (contains? (ensure (room :users)) nick))
- (login-user (user-struct-from-session session) room))
- (add-message (build-msg nick content msg-id (:recips msg-info)) room))
+ (let [msg-struct (build-msg nick content msg-id (:recips msg-info))]
+ (if (not (contains? (ensure (room :users)) nick))
+ (login-user (user-struct-from-session session) room))
+ (add-message msg-struct room)))
(resp-success {:msgid msg-id
:recips (:recips msg-info)})))))
-
(defn validated-msg [session params request]
(cond
(not (validate-room-access (params :room) session)) (resp-error "UNKNOWN_ROOM")
@@ -720,7 +659,6 @@ ORDER BY cnt DESC
;; Browser
-;; TODO: make work for all rooms
(defn browser [session]
(let [room (lookup-room *default-room*)
now (System/currentTimeMillis)
@@ -740,6 +678,21 @@ ORDER BY cnt DESC
(.toString st)))
+;; Topics
+
+(defn topic [session topic]
+ (let [topic (lower-case topic)
+ msgs (map
+ process-message-for-output
+ (fetch-topic (:user_id session) topic))
+ recips (pull-recips msgs)
+ st (fetch-template "topic" session)]
+ (.setAttribute st "recips" (json-str recips))
+ (.setAttribute st "topic" topic)
+ (if-not (empty? msgs)
+ (.setAttribute st "dumps" msgs))
+ (.toString st)))
+
;; Chat Log
(defn log [session room offset params]
@@ -755,7 +708,9 @@ ORDER BY cnt DESC
:image-only image-only
:amount (+ 1 *dumps-per-page*)
:offset dump-offset)
- dumps (map tags/add-favorited-flag (take *dumps-per-page* raw-dumps) (repeat session))
+ dumps (map tags/add-favorited-flag
+ (take *dumps-per-page* raw-dumps)
+ (repeat session))
;; json-tags (for [dump dumps :when (not (empty? (dump :tags)))]
;; (json-str {"id" (dump :message_id) "tags" (dump :tags) }))
dumps (map tags/remove-tags-for-output dumps)
@@ -799,8 +754,6 @@ ORDER BY cnt DESC
(.setAttribute st "dumps" dumps)
(.toString st)))
-
-
;; Altars
;; if :nick is in params, will fetch only altars by that nick
@@ -952,6 +905,7 @@ ORDER BY cnt DESC
:hide-vip (not (:is_admin session))
:date (if msg-id nil date)
:limit (inc *dumps-per-page*))
+ recips (map :nick (get-recips-from-msgs raw-dumps))
back-dumps (if (or date msg-id)
(tags/fetch-tagged-dumps
:nick (:nick user-info)
@@ -971,6 +925,7 @@ ORDER BY cnt DESC
(if (> (count raw-dumps) *dumps-per-page*)
(.setAttribute st "next" (favorites-next-page-link (:nick user-info) (last raw-dumps))))
(.setAttribute st "dumps" dumps)
+ (.setAttribute st "recips" (json-str recips))
(.setAttribute st "infobar" (build-mini-profile user-info))
(.setAttribute st "page_title" (format "%s'S FAVS" (:nick user-info)))
(.setAttribute st "debug_log_items" (logger))
@@ -1134,11 +1089,17 @@ ORDER BY cnt DESC
date (today)
dest (open-file [*image-directory* date] filename)
url (image-url-from-file "images" date dest)
- msg-info (insert-message! (:user_id session) (:nick session)
- (:room_id room) url)]
+ msg-info (insert-message! (:user_id session)
+ (:nick session)
+ (:avatar session)
+ room
+ url)]
(copy (:tempfile image) dest)
(dosync
- (let [msg (build-msg (:nick session) url (:msg-id msg-info) (:recips msg-info))]
+ (let [msg (build-msg (:nick session)
+ url
+ (:msg-id msg-info)
+ (:recips msg-info))]
(add-message msg room)))
[200 "OK"])))
@@ -1245,6 +1206,9 @@ ORDER BY cnt DESC
(GET "/r/:room/log/:offset" (validated-log session (params :room) (params :offset) params))
(GET "/favicon.ico" (serve-static "static" "favicon.ico"))
+
+ (GET "/t/:topic" (topic session (params :topic)))
+
(GET "/u/:nick" (redirect-to (str "/" (params :nick))))
(GET "/u/:nick/" (redirect-to (str "/" (params :nick))))
(GET "/u/:nick/tag/:tag" (tagged-dumps-by-nick session params (request-url request)))
@@ -1441,7 +1405,7 @@ ORDER BY cnt DESC
(start-user-flusher!)
(start-session-pruner!)
-(if (not= *server-url* "http://dump.fm")
- (start! random-poster))
+;(if (not= *server-url* "http://dump.fm")
+; (start! random-poster))