From 21ec2e9244ef0787908aede4bf4cb2e94bcf395d Mon Sep 17 00:00:00 2001 From: Scott Ostler Date: Tue, 22 Feb 2011 23:11:34 -0500 Subject: refactor tag handling, remove unused tag accessor func --- src/site.clj | 80 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) (limited to 'src/site.clj') diff --git a/src/site.clj b/src/site.clj index beb8a73..2d1b295 100644 --- a/src/site.clj +++ b/src/site.clj @@ -683,8 +683,6 @@ WHERE user_id IN 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) dumps (logger doall (map process-message-for-output dumps))] (if (> (count raw-dumps) *dumps-per-page*) @@ -701,10 +699,9 @@ WHERE user_id IN (defn validated-log [session room-key offset params] (if (is-vip? session) - (let [room-key (if (= (lower-case room-key) "www") "dumpfm" room-key)] - (if (validate-room-access room-key session) - (log session (lookup-room room-key) offset params) - (resp-error "UNKNOWN_ROOM"))) + (if (validate-room-access room-key session) + (log session (lookup-room room-key) offset params) + (resp-error "UNKNOWN_ROOM")) (redirect-to "/"))) ;; Hiscore test... redis test... @@ -757,27 +754,44 @@ WHERE user_id IN (let [path-without-domain (nth (re-find #"//[^/]+/(.+)" url) 1)] (nth (re-split #"/|\?" path-without-domain) position))) +(defn should-msg-fav-change-score? [msg tag user] + (and (= tag "favorite") + (not (:admin_only msg)) + (not= (:user_id msg) (:user_id user)))) + (defn add-tag [user msg tag] (try (do-insert "tags" ["user_id" "message_id" "tag"] [(:user_id user) (:message_id msg) tag]) - (when (and (= tag "favorite") - (not (= (msg :nick) (:nick user)))) - (if-not (or (:admin_only msg) (= (:user_id user) (:user_id msg))) - (incrby-redis-favscore! msg 1)) - (insert-fav-notification! (msg :nick) - (user :nick) - (user :avatar) - (msg :content))) - true - ; catch error when inserting duplicate tags + (insert-fav-notification! (msg :nick) + (user :nick) + (user :avatar) + (msg :content)) + (when (should-msg-fav-change-score? msg tag user) + (incrby-redis-favscore! msg 1)) + (resp-success "OK") + ; catch error when inserting duplicate tags (catch Exception e - (do (println e) - false)))) - + (println e) + (resp-error "TAG_EXISTS_ALREADY_OR_SOMETHING_ELSE_IS_FUCKED")))) + +(defn- postgres-tag-delete! [user-id msg-id tag] + (first (do-delete "tags" + ["user_id = ? AND message_id = ? AND tag = ?" + user-id msg-id tag]))) + +(defn remove-tag [user msg tag] + (if-not (zero? (postgres-tag-delete! (:user_id user) + (:message_id msg) + tag)) + (do + (when (should-msg-fav-change-score? msg tag user) + (incrby-redis-favscore! msg -1)) + (resp-success "OK")) + (resp-error "NO_TAG"))) -(defn validated-add-tag [session params] +(defn validated-tag-access [session params tag-func] (if (session :nick) (let [nick (session :nick) user-id (session :user_id) @@ -789,29 +803,9 @@ WHERE user_id IN (cond (not msg) (resp-error "NO_MSG") (not access) (resp-error "NO_MSG") (not tag) (resp-error "NO_TAG") - :else (if (add-tag session msg tag) - (resp-success "OK") - (resp-error "TAG_EXISTS_ALREADY_OR_SOMETHING_ELSE_IS_FUCKED")))) + :else (tag-func session msg tag))) (resp-error "NO_USER"))) -(defn remove-tag [user-id msg-id tag] - (let [query "user_id = ? AND message_id = ? AND lower(tag) = ?" - msg-id (maybe-parse-int msg-id) - tag (normalize-tag-for-db tag) - msg (fetch-message-by-id msg-id)] - (let [rows-deleted (first (do-delete "tags" [query user-id msg-id tag]))] - (if-not (zero? rows-deleted) - (do - (if-not (or (:admin_only msg) (= user-id (:user_id msg))) - (incrby-redis-favscore! msg -1)) - (resp-success "OK")) - (resp-error "NO_TAG"))))) - -(defn validated-remove-tag [session params] - (if (session :nick) - (remove-tag (session :user_id) (params :message_id) (params :tag)) - (resp-error "NO_USER"))) - ;; message-user-id: get messages posted by this user ;; tag-user-id: get messages tagged by this user (defnk tagged-dumps-template [session params tags url page-title info-bar @@ -1207,8 +1201,8 @@ WHERE user_id IN (GET "/refresh" (validated-refresh session params)) (GET "/tag/:tag" (tagged-dumps session params (request-url request))) (GET "/tag/:tag/:offset" (tagged-dumps session params (request-url request))) - (POST "/cmd/tag/add" (validated-add-tag session params)) - (POST "/cmd/tag/rm" (validated-remove-tag session params)) + (POST "/cmd/tag/add" (validated-tag-access session params add-tag)) + (POST "/cmd/tag/rm" (validated-tag-access session params remove-tag)) ;; Altars (GET "/altars" (altar-log session params)) -- cgit v1.2.3-70-g09d2