diff options
Diffstat (limited to 'src/site.clj')
| -rw-r--r-- | src/site.clj | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/src/site.clj b/src/site.clj index 6430cc2..57dc4b6 100644 --- a/src/site.clj +++ b/src/site.clj @@ -557,17 +557,19 @@ FROM users u (resp-error "UNKNOWN_ROOM")))) ;; http://snippets.dzone.com/posts/show/6995 -(def single-url-regex #"(?i)^((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$") -(def pic-regex #"(?i)\.(jpg|jpeg|png|gif|bmp|svg)$") +(def url-regex #"(?i)^((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$") +(def pic-regex #"(?i)\.(jpg|jpeg|png|gif|bmp|svg)") -(defn strip-params [s] - (.replaceFirst s "\\?.*$" "")) +(defn is-url? [word] + (and (re-find url-regex word) + (re-find pic-regex word))) -; TODO: is-image? is broken for messages w/ multiple image links. - -(defn is-image? [content] - (boolean (and (re-find single-url-regex content) - (re-find pic-regex (strip-params content))))) +(defn classify-msg [msg] + (let [words (.split msg " ") + urls (map is-url? words)] + (cond (every? boolean urls) :image + (some boolean urls) :mixed + :else :text))) ;; admins can post arbitrary html if wrapped in <safe> ;; this is temporary so that i can test generating html messages @@ -579,11 +581,13 @@ FROM users u (str content))) (defn msg-db [user-id room-id content] - (let [is-image (is-image? content) - qry (str "INSERT INTO messages (user_id, room_id, content, is_image) " - "VALUES (?, ?, ?, ?) RETURNING message_id")] + (let [msg-type (classify-msg content) + is-image (boolean (#{:image :mixed} msg-type)) + is-text (boolean (#{:mixed :text} msg-type)) + qry (str "INSERT INTO messages (user_id, room_id, content, is_image, is_text) " + "VALUES (?, ?, ?, ?, ?) RETURNING message_id")] (with-connection *db* - ((first (do-select [qry user-id room-id content is-image])) + ((first (do-select [qry user-id room-id content is-image is-text])) :message_id)))) (defn msg [session params] |
