summaryrefslogtreecommitdiff
path: root/src/datalayer.clj
diff options
context:
space:
mode:
authorScott Ostler <scottbot9000@gmail.com>2011-01-04 15:42:23 -0500
committerScott Ostler <scottbot9000@gmail.com>2011-01-04 15:42:23 -0500
commit92b092823c21af1339e0d42716dc856357f93e85 (patch)
tree809221e2a3b3c88306ba5e4b4996eb05297cec02 /src/datalayer.clj
parent7a031af911887a913857fdcebb252231119f4bf9 (diff)
Added topics, refactored recipient handling
Diffstat (limited to 'src/datalayer.clj')
-rw-r--r--src/datalayer.clj90
1 files changed, 57 insertions, 33 deletions
diff --git a/src/datalayer.clj b/src/datalayer.clj
index 24c162a..ecdc6dc 100644
--- a/src/datalayer.clj
+++ b/src/datalayer.clj
@@ -1,4 +1,5 @@
(ns datalayer
+ (:import java.util.Date)
(:require redis
tags)
(:use clojure.contrib.sql
@@ -82,25 +83,6 @@ WHERE u.nick = ANY(?)"
(defn redis-popular-key [nick]
(str "popular:" nick))
-(def popular-dumps-qry "
-select u.nick, u.avatar, r.key, m.message_id, m.content, m.created_on, count(*) as count,
- array_agg(u2.nick) as user_nicks
-from users u, messages m, rooms r, tags t, users u2
-where lower(u.nick) = lower(?)
-and u.user_id = m.user_id and m.message_id = t.message_id
-and m.room_id = r.room_id and m.is_image = true and r.admin_only = false
-and t.tag = 'favorite'
-and t.user_id = u2.user_id
-group by u.nick, u.avatar, r.key, m.message_id, m.content, m.created_on
-order by count desc limit ? offset ?")
-
-(defn fetch-popular-dumps [nick viewer-nick]
- (for [d (do-select [popular-dumps-qry nick 40 0])]
- (let [favers (.getArray (:user_nicks d))]
- (assoc d
- :favers favers
- :favorited (some #(= % viewer-nick) favers)))))
-
(defn fetch-popular-dumps-redis [nick viewer-nick]
(let [msg-ids (redis/with-server redis-server
(redis/zrevrange (redis-popular-key nick)
@@ -177,10 +159,14 @@ order by count desc limit ? offset ?")
;;;; Message insertion
+(defn direct-message-key [u-id]
+ (str "directmessage:" u-id))
+
(def msg-insert-query
"INSERT INTO messages (user_id, room_id, content, is_image, is_text)
VALUES (?, ?, ?, ?, ?) RETURNING message_id, created_on")
+;; Note: direct-message recipients are inserted into postgres, but topics aren't.
(defn insert-message-into-postgres! [author-id room-id content is-image is-text recips]
(with-connection *db*
(transaction
@@ -195,43 +181,81 @@ order by count desc limit ? offset ?")
[msg-id author-id (:user_id r)]))
[msg-id ts]))))
-(defn insert-recips-into-redis! [recips author-id ts content]
- (let [dm-json (json-str {"author_id" author-id
- "recips" (map :nick recips)
- "content" content})]
+(defn insert-recips-into-redis! [recips author-id dt content]
+ (let [msg-json (json-str {"author_id" author-id
+ "recips" (map :nick recips)
+ "content" content})
+ ts (.getTime dt)]
(redis/with-server redis-server
(redis/atomically
(doseq [r recips]
- (redis/zadd (str "directmessage:" (:user_id r))
- (.getTime ts)
- dm-json))))))
+ (redis/zadd (direct-message-key (:user_id r))
+ ts
+ msg-json))))))
-(defn insert-message! [author-id author-nick room-id content]
+(defn topic-key [topic]
+ (str "topic:" topic))
+
+(defn insert-topics-into-redis! [topics recips author-nick author-avatar dt msg-id content]
+ (let [ts (.getTime dt)
+ msg-json (json-str {"nick" author-nick
+ "avatar" author-avatar
+ "recips" (map :recips recips)
+ "content" content
+ "message_id" msg-id
+ "ts" ts})]
+ (redis/with-server redis-server
+ (redis/atomically
+ (doseq [t topics]
+ (redis/lpush (topic-key t)
+ msg-json))))))
+
+(defn insert-message! [author-id author-nick author-avatar room-id content]
(let [msg-type (classify-msg content)
is-image (boolean (#{:image :mixed} msg-type))
is-text (boolean (#{:mixed :text} msg-type))
recips (get-recips content)
- [msg-id ts] (insert-message-into-postgres! author-id
+ topics (get-topics content)
+ [msg-id dt] (insert-message-into-postgres! author-id
room-id
content
is-image
is-text
recips)]
(if-not (empty? recips)
- (insert-recips-into-redis! recips author-id ts content))
+ (insert-recips-into-redis! recips author-id dt content))
+ (if-not (empty? topics)
+ (insert-topics-into-redis! topics recips author-nick author-avatar dt msg-id content))
{:author author-nick
:msg-id msg-id
:room room-id
- :db-ts ts
+ :db-ts dt
:content content
- :recips (map (comp lower-case :nick) recips)}))
+ :recips (map :nick recips)
+ :topics topics
+ }))
-(defn fetch-private-messages [user-id]
+(defn fetch-direct-messages [user-id]
(for [dm (redis/with-server redis-server
- (redis/zrevrange (str "directmessage:" user-id) 0 40))]
+ (redis/zrevrange (direct-message-key user-id) 0 40))]
(let [dm (read-json dm)
info (fetch-user-id (get dm "author_id"))]
{"nick" (:nick info)
"content" (get dm "content")
"recips" (get dm "recips" [])
"avatar" (:avatar info)})))
+
+(def topic-fetch-num 40)
+
+(defn fetch-topic [viewer-id topic]
+ (let [redis-msgs (redis/with-server redis-server
+ (redis/lrange (topic-key topic) 0 topic-fetch-num))
+ raw-msgs (for [m redis-msgs]
+ (let [m (keywordify (read-json m))]
+ (assoc m
+ :created_on (Date. (:ts m)))))]
+ (if viewer-id
+ (tags/add-user-favs-to-msgs
+ raw-msgs
+ viewer-id)
+ raw-msgs)))