diff options
| -rwxr-xr-x | src/site.clj | 29 | ||||
| -rwxr-xr-x | template/profile.st | 9 |
2 files changed, 31 insertions, 7 deletions
diff --git a/src/site.clj b/src/site.clj index dbd1432..a450377 100755 --- a/src/site.clj +++ b/src/site.clj @@ -77,6 +77,12 @@ (with-query-results rs query (doall rs)))) +(defn do-count [query] + ((first (with-connection db + (with-query-results rs query + (doall rs)))) + :count)) + ;; User authentication (def nick-regex #"^[A-Za-z0-9\-_∆˚†]*$") @@ -132,6 +138,13 @@ (defn maybe-parse-int [s f] (if s (Integer/parseInt s) f)) +(defn count-messages-by-room [room-id image-only] + (let [query (str "SELECT m.content, m.created_on, u.nick " + "FROM messages m, users u " + "WHERE room_id = ? AND m.user_id = u.user_id " + (if image-only "AND m.is_image = true " ""))] + (do-count [query room-id]))) + (defn fetch-messages-by-room ([room-id image-only] (fetch-messages-by-room room-id image-only 1)) ([room-id image-only offset] @@ -143,6 +156,13 @@ "LIMIT " dumps-per-page " OFFSET ?")] (do-select [query room-id offset])))) +(defn count-messages-by-nick [nick image-only] + (let [query (str "SELECT COUNT(*) " + "FROM messages m, users u " + "WHERE m.user_id = u.user_id AND u.nick = ? " + (if image-only "AND m.is_image = true " ""))] + (do-count [query nick]))) + (defn fetch-messages-by-nick ([nick image-only] (fetch-messages-by-nick nick image-only 0)) ([nick image-only offset] @@ -224,6 +244,7 @@ offset (maybe-parse-int offset 0) dump-offset (* offset dumps-per-page) dumps (fetch-messages-by-nick profile-nick true dump-offset) + dump-count (count-messages-by-nick profile-nick true) st (fetch-template "profile" session)] (do @@ -234,6 +255,10 @@ (if (non-empty-string? v) (encode-html-entities v))))) (.setAttribute st "dumps" (to-array (map process-message-for-output dumps))) + (if (< (+ dump-offset dumps-per-page) dump-count) + (.setAttribute st "next" (inc offset))) + (if (not= offset 0) + (.setAttribute st "prev" (max (dec offset) 0))) (.toString st))) (resp-error "NO_USER")))) @@ -342,8 +367,8 @@ dump-offset (* offset dumps-per-page) dumps (to-array (map process-message-for-output (fetch-messages-by-room 1 true dump-offset))) - more-dumps true] - (if more-dumps + dump-count (count-messages-by-room 1 true)] + (if (< (+ dump-offset dumps-per-page) dump-count) (.setAttribute st "next" (inc offset))) (if (not= offset 0) (.setAttribute st "prev" (max (dec offset) 0))) diff --git a/template/profile.st b/template/profile.st index 45dbb32..25ea88f 100755 --- a/template/profile.st +++ b/template/profile.st @@ -24,7 +24,7 @@ <a href="/u/$nick$/$prev$">PREV DUMPS</a> $endif$ - $if(dumps)$ + $if(next)$ <a href="/u/$nick$/$next$">MORE DUMPS</a> $endif$ </div> @@ -38,16 +38,15 @@ To get started join <a href="/chat">Room A</a> and post a few pictures.</h3> $endif$ <p> </p> + - <span style="border: 2px solid blue; background-color: gray"> $if(prev)$ <a href="/u/$nick$/$prev$">PREV DUMPS</a> - $endif$ + $endif$ - $if(dumps)$ + $if(next)$ <a href="/u/$nick$/$next$">MORE DUMPS</a> $endif$ - </span> <br><br> |
