summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Ostler <scottbot9000@gmail.com>2011-02-21 23:28:46 -0500
committerScott Ostler <scottbot9000@gmail.com>2011-02-21 23:28:46 -0500
commitd6c8d8ab019721ba47a289de97c95e9786dd5ded (patch)
treefbafe9199d175250477f3881f5b5e5808df81df8
parentf2525430e94460085f369a526986b2b0a176b99f (diff)
sostler add favorite to frontpage
-rw-r--r--src/datalayer.clj14
-rw-r--r--src/multikey_cache.clj15
-rw-r--r--src/site.clj4
-rw-r--r--src/tags.clj5
-rwxr-xr-xsrc/utils.clj5
-rw-r--r--static/js/pichat.js83
-rw-r--r--template/frontpage.st6
-rw-r--r--template/frontpage_share_buttons.st10
-rw-r--r--template/hall_log_dump.st17
9 files changed, 93 insertions, 66 deletions
diff --git a/src/datalayer.clj b/src/datalayer.clj
index c74315a..56d5a60 100644
--- a/src/datalayer.clj
+++ b/src/datalayer.clj
@@ -85,7 +85,8 @@ WHERE u.nick = ANY(?)"
(def message-cache-size 50000)
(def message-cache (multikey-lru-cache
fetch-messages-by-id-uncached
- message-cache-size))
+ message-cache-size
+ to-int))
(defn fetch-messages-by-id [m-ids]
(get-keys message-cache m-ids))
@@ -154,14 +155,17 @@ WHERE u.nick = ANY(?)"
{symbol (.getElement t)
:score (int (.getScore t))}))
-(defn fetch-redis-daily-hall [date-str offset num]
+(defn fetch-redis-daily-hall [date-str offset num user-id]
(let [msg-ids (fetch-redis-zset (redis-daily-hall-key date-str)
:message_id
offset
num)
- full-msgs (fetch-messages-by-id (map :message_id msg-ids))]
- (for [[info full] (map list msg-ids full-msgs)]
- (assoc full :score (:score info)))))
+ full-msgs (fetch-messages-by-id (map :message_id msg-ids))
+ full-msgs (for [[info full] (map list msg-ids full-msgs)]
+ (assoc full :score (:score info)))]
+ (if user-id
+ (tags/add-user-favs-to-msgs full-msgs user-id)
+ full-msgs)))
(defn fetch-redis-directory [page num]
(vec
diff --git a/src/multikey_cache.clj b/src/multikey_cache.clj
index 74822a8..ac462ca 100644
--- a/src/multikey_cache.clj
+++ b/src/multikey_cache.clj
@@ -5,14 +5,16 @@
cache-dot-clj.cache
))
-(defn multikey-lru-cache [f max-size]
- {:f f
- :lru (java.util.Collections/synchronizedMap (LRUMap. max-size))
+(defn multikey-lru-cache [func max-size canonicalizer]
+ {:func func
+ :lru (java.util.Collections/synchronizedMap (LRUMap. max-size))
+ :canonicalizer canonicalizer
})
(defn get-keys [c keys]
- (let [f (:f c)
+ (let [f (:func c)
lru (:lru c)
+ keys (map (:canonicalizer c) keys)
uncached (filter (complement #(.containsKey lru %)) keys)]
(when-not (empty? uncached)
(doseq [[k v] (map list uncached (f uncached))]
@@ -23,8 +25,9 @@
(first (get-keys c [key])))
(defn has-key [c key]
- (.containsKey (:lru c) key))
+ (.containsKey (:lru c)
+ ((:canonicalizer c) key)))
(defn invalidate-key [c key]
- (.remove (:lru c) key)
+ (.remove (:lru c) ((:canonicalizer c) key))
nil)
diff --git a/src/site.clj b/src/site.clj
index 2e1bfe8..98524d4 100644
--- a/src/site.clj
+++ b/src/site.clj
@@ -196,7 +196,7 @@
(let [st (fetch-template "frontpage" session)
date-str (format-yyyymmdd (Date.))
dumps (map process-message-for-output
- (fetch-redis-daily-hall date-str 0 20))]
+ (fetch-redis-daily-hall date-str 0 20 (:user_id session)))]
(.setAttribute st "dumps" dumps)
(.toString st)))
@@ -788,8 +788,8 @@ WHERE user_id IN
(let [nick (session :nick)
user-id (session :user_id)
msg-id (params :message_id)
- tag (validate-tag (params :tag))
msg (fetch-message-by-id msg-id)
+ tag (validate-tag (params :tag))
access (or (is-vip? session)
(not (:admin_only msg)))]
(cond (not msg) (resp-error "NO_MSG")
diff --git a/src/tags.clj b/src/tags.clj
index 76bc762..19b2531 100644
--- a/src/tags.clj
+++ b/src/tags.clj
@@ -86,9 +86,8 @@ WHERE EXISTS
(sql-array "int" (map :message_id msgs))
user-id])))]
(for [m msgs]
- (if (contains? faved-ids (:message_id m))
- (assoc m :favorited true)
- m))))
+ (assoc m :favorited
+ (contains? faved-ids (:message_id m))))))
(defn explain-query [query] (str "EXPLAIN ANALYZE " query))
diff --git a/src/utils.clj b/src/utils.clj
index eb89cf1..22a0b5d 100755
--- a/src/utils.clj
+++ b/src/utils.clj
@@ -321,6 +321,11 @@
;; Parsing
+(defn to-int [x]
+ (cond (integer? x) x
+ (string? x) (Integer/parseInt x)
+ :else (throw (Exception. (str "Invalid argument " x)))))
+
(defn maybe-parse-int
([s] (maybe-parse-int s 0))
([s a]
diff --git a/static/js/pichat.js b/static/js/pichat.js
index 24b8f0c..8d849bf 100644
--- a/static/js/pichat.js
+++ b/static/js/pichat.js
@@ -1124,42 +1124,55 @@ Share = {
}
Tag = {
- "favorite": function(button){
- var message = getMessageInfo(button)
- var favorited = ($(button).parents(".dump").hasClass("favorite")) ? true : false
- if (favorited) {
- Tag.rm(message.id, "favorite")
- $(button).parents(".dump").removeClass("favorite")
- if (RawFavs[message.id]) {
- delete RawFavs[message.id]
- paletteImageCache = false
- }
- } else {
- Tag.add(message.id, "favorite")
- $(button).parents(".dump").addClass("favorite")
- if (RawFavs && MessageContentCache[message.id]) { // chat ui stuff
- if ($("#palette-button").css("display") == "none")
- paletteButtonShowAnim()
- RawFavs[message.id] = MessageContentCache[message.id]
- paletteImageCache = false
- }
+ "favorite": function(button) {
+ var message = getMessageInfo(button);
+ var favorited = $(button).parents(".dump").hasClass("favorite");
+ if (favorited) {
+ Tag.rm(message.id, "favorite");
+ $(button).parents(".dump").removeClass("favorite");
+ if (RawFavs[message.id]) {
+ delete RawFavs[message.id];
+ paletteImageCache = false;
+ }
+ } else {
+ Tag.add(message.id, "favorite");
+ $(button).parents(".dump").addClass("favorite");
+ if (RawFavs && MessageContentCache[message.id]) { // chat ui stuff
+ if ($("#palette-button").css("display") == "none")
+ paletteButtonShowAnim();
+ RawFavs[message.id] = MessageContentCache[message.id];
+ paletteImageCache = false;
+ }
+ }
+ },
+ "add": function(message_id, tag) {
+ Tag.ajax("/cmd/tag/add", {"message_id": message_id, "tag": tag});
+ },
+ "rm": function(message_id, tag) {
+ Tag.ajax("/cmd/tag/rm", {"message_id": message_id, "tag": tag});
+ },
+ "ajax": function(url, data) {
+ $.ajax({
+ "type": 'POST',
+ "timeout": 5000,
+ "url": url,
+ "data": data,
+ "cache": false
+ });
+ },
+ "frontpage_fav": function(button) {
+ Tag.favorite(button);
+ console.log("frontpage_fav");
+ var dump = $(button).parents(".dump");
+ var $score = dump.find('.hallscore');
+ var inc = dump.hasClass("favorite") ? 1 : -1;
+ var oldScore = parseInt($score.text(), 10);
+ console.log($score, oldScore + inc);
+ $score.text(oldScore + inc);
+ $score.animate({ 'font-size': '60px' }, 250, function() {
+ $score.animate({ 'font-size': '40px' }, 250);
+ });
}
- },
- "add": function(message_id, tag){
- Tag.ajax("/cmd/tag/add", {"message_id": message_id, "tag": tag})
- },
- "rm": function(message_id, tag){
- Tag.ajax("/cmd/tag/rm", {"message_id": message_id, "tag": tag})
- },
- "ajax": function(url, data) {
- $.ajax({
- "type": 'POST',
- "timeout": 5000,
- "url": url,
- "data": data,
- "cache": false
- });
- }
}
/*
diff --git a/template/frontpage.st b/template/frontpage.st
index f75f9db..be10d14 100644
--- a/template/frontpage.st
+++ b/template/frontpage.st
@@ -104,7 +104,7 @@ z-index:4;
.thumb.favorite {
position: absolute;
- margin-left: 40px;
+ margin-left: 5px;
margin-top: 0px;
height: 27px;
width: 27px;z-index:4;
@@ -133,9 +133,9 @@ z-index:4;
-moz-box-shadow:#777 0 0 0 1px;
-webkit-box-shadow:#777 0 0 0 1px;
text-shadow:rgba(0,0,0,.4) 0 1px 0;}
-#hallscore{margin-right:-20px;color:#fff;text-align:center;margin-top:-20px;float:right;font-size:40px;z-index:1;padding:6px;background-image:url('/static/img/hallheart.png');background-position:top left;font-style: oblique;font-weight:100;height:50px;width:50px;text-shadow:-1px 1px 0px #000;line-height:37px;text-indent:4px;
+.hallscore{margin-right:-20px;color:#fff;text-align:center;margin-top:-20px;float:right;font-size:40px;z-index:1;padding:6px;background-image:url('/static/img/hallheart.png');background-position:top left;font-style: oblique;font-weight:100;height:50px;width:50px;text-shadow:-1px 1px 0px #000;line-height:37px;text-indent:4px;
}
-#hallnick{font-size:16px;}
+.hallnick{font-size:16px;}
#login-container {
float: right;
diff --git a/template/frontpage_share_buttons.st b/template/frontpage_share_buttons.st
new file mode 100644
index 0000000..003795e
--- /dev/null
+++ b/template/frontpage_share_buttons.st
@@ -0,0 +1,10 @@
+<span class="buttons frontpage">
+ <a class="permalink" href="$domain$/p/$dump.nick$/$dump.message_id$"><img src="$domain$/static/img/link.gif"></a>
+$if(dump.favorited)$
+ <img src="/static/img/thumbs/heartfaved.gif" class="thumb favorite" onclick="Tag.frontpage_fav(this)">
+$else$
+ <div style="position: relative">
+ <img src="/static/img/thumbs/heart.gif" class="thumb" onclick="Tag.frontpage_fav(this)">
+ </div>
+$endif$
+</span>
diff --git a/template/hall_log_dump.st b/template/hall_log_dump.st
index 0056e6f..9cde345 100644
--- a/template/hall_log_dump.st
+++ b/template/hall_log_dump.st
@@ -1,13 +1,6 @@
-$if(dump.ztags)$
- <div class="logged-dump dump" id="message-$dump.message_id$" nick="$dump.nick$" tags="$dump.tags: { tag | $tag.nick$:$tag.tag$ }$">
-$else$
- <div class="logged-dump dump $if(dump.favorited)$favorite$endif$" id="message-$dump.message_id$" nick="$dump.nick$">
-$endif$
-
-
- <a href="/$dump.nick$"><b id="hallnick">$dump.nick$</a> </b>$if(dump.score)$ <span id="hallscore" onclick="Tag.favorite(this)">$dump.score$</span>$endif$
-<div class="content" data-category="posts">$dump.content$</div>
-<hr />
-
-$share_buttons()$
+<div class="logged-dump dump $if(dump.favorited)$favorite$endif$" id="message-$dump.message_id$" nick="$dump.nick$">
+ <a href="/$dump.nick$"><b class="hallnick">$dump.nick$</a></b>$if(dump.score)$<span class="hallscore" onclick="Tag.frontpage_fav(this)">$dump.score$</span>$endif$
+ <div class="content" data-category="posts">$dump.content$</div>
+ <hr />
+ $frontpage_share_buttons()$
</div>