diff options
| -rw-r--r-- | src/site.clj | 78 | ||||
| -rw-r--r-- | src/tags.clj | 20 | ||||
| -rwxr-xr-x | src/utils.clj | 1 |
3 files changed, 36 insertions, 63 deletions
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)) diff --git a/src/tags.clj b/src/tags.clj index 19b2531..e325a0d 100644 --- a/src/tags.clj +++ b/src/tags.clj @@ -91,26 +91,6 @@ WHERE EXISTS (defn explain-query [query] (str "EXPLAIN ANALYZE " query)) -;; OFFSET is very slow when it is large -;; so, a subquery could be used when offset is large -;; one other thing we could do is include message_id in 'next page' url (tumblr & reddit do that for example) -;; but probably people don't page back too far anyway -(defn fetch-dumps-by-room-query-faster [image-only] (str -" SELECT - m.content, m.message_id, m.created_on, - u.nick, u.avatar, - array_to_string(ARRAY(SELECT nick || ' ' || tag - FROM tags, users - WHERE message_id = m.message_id AND tags.user_id = users.user_id), ' ') as tags - FROM (SELECT message_id - FROM messages - WHERE room_id = ? " - (if image-only "AND is_image = true " "") - " ORDER BY created_on DESC - LIMIT ? OFFSET ?) as sq, users u, messages m - WHERE sq.message_id = m.message_id - AND u.user_id = m.user_id")) - (defn fetch-dumps-by-room-query [image-only] (str " SELECT m.content, m.message_id, m.created_on, diff --git a/src/utils.clj b/src/utils.clj index f782836..4ed446a 100755 --- a/src/utils.clj +++ b/src/utils.clj @@ -365,7 +365,6 @@ (defn url-decode [text] (URLDecoder/decode text "UTF-8")) -; TODO: this duplicates str-utils, should be removed (defn #^String lower-case "Converts string to all lower-case." [#^String s] |
