Browse Source

minor refactoring

master
Christian Mueller 12 years ago
parent
commit
7267a35ab3
  1. 9
      src/NoteHub/api.clj
  2. 22
      src/NoteHub/storage.clj
  3. 4
      src/NoteHub/views/pages.clj

9
src/NoteHub/api.clj

@ -78,8 +78,7 @@
;(log "post-note: %s" {:pid pid :signature signature :password password :note note}) ;(log "post-note: %s" {:pid pid :signature signature :password password :note note})
(let [errors (filter identity (let [errors (filter identity
[(when-not (storage/valid-publisher? pid) "pid invalid") [(when-not (storage/valid-publisher? pid) "pid invalid")
(when-not (= signature (when-not (= signature (get-signature pid (storage/get-psk pid) note))
(get-signature pid (storage/get-psk pid) note))
"signature invalid") "signature invalid")
(when (blank? note) "note is empty")])] (when (blank? note) "note is empty")])]
(if (empty? errors) (if (empty? errors)
@ -108,13 +107,11 @@
(defn update-note [noteID note pid signature password] (defn update-note [noteID note pid signature password]
;(log "update-note: %s" {:pid pid :noteID noteID :signature signature :password password :note note}) ;(log "update-note: %s" {:pid pid :noteID noteID :signature signature :password password :note note})
(let [errors (filter identity (let [errors (filter identity
(seq
[(when-not (storage/valid-publisher? pid) "pid invalid") [(when-not (storage/valid-publisher? pid) "pid invalid")
(when-not (= signature (when-not (= signature (get-signature pid (storage/get-psk pid) noteID note password))
(get-signature pid (storage/get-psk pid) noteID note password))
"signature invalid") "signature invalid")
(when (blank? note) "note is empty") (when (blank? note) "note is empty")
(when-not (storage/valid-password? noteID password) "password invalid")]))] (when-not (storage/valid-password? noteID password) "password invalid")])]
(if (empty? errors) (if (empty? errors)
(do (do
(storage/edit-note noteID note) (storage/edit-note noteID note)

22
src/NoteHub/storage.clj

@ -32,8 +32,8 @@
(defn create-session [] (defn create-session []
(let [token (encrypt (str (rand-int Integer/MAX_VALUE)))] (let [token (encrypt (str (rand-int Integer/MAX_VALUE)))]
(do (redis :sadd :sessions token) (redis :sadd :sessions token)
token))) token))
(defn invalidate-session [token] (defn invalidate-session [token]
(let [was-valid (redis :sismember :sessions token)] (let [was-valid (redis :sismember :sessions token)]
@ -91,14 +91,13 @@
(defn resolve-url [url] (defn resolve-url [url]
(let [value (redis :hget :short-url url)] (let [value (redis :hget :short-url url)]
(when value (when value ; TODO: necessary?
(read-string value)))) (read-string value))))
(defn delete-short-url [noteID] (defn delete-short-url [noteID]
(let [value (redis :hget :short-url noteID)] (let [value (redis :hget :short-url noteID)]
(do (redis :hdel :short-url noteID)
(redis :hdel :short-url noteID) (redis :hdel :short-url value)))
(redis :hdel :short-url value))))
(defn create-short-url (defn create-short-url
"Creates a short url for the given request metadata or extracts "Creates a short url for the given request metadata or extracts
@ -116,9 +115,8 @@
url (first url (first
(remove short-url-exists? (remove short-url-exists?
(map hash-to-string hash-stream)))] (map hash-to-string hash-stream)))]
(do ; we create two mappings: key params -> short url and back,
; we create two mappings: key params -> short url and back, ; s.t. we can later easily check whether a short url already exists
; s.t. we can later easily check whether a short url already exists (redis :hset :short-url url key)
(redis :hset :short-url url key) (redis :hset :short-url key url)
(redis :hset :short-url key url) url))))
url)))))

4
src/NoteHub/views/pages.clj

@ -155,7 +155,7 @@
(let [pid "NoteHub" (let [pid "NoteHub"
psk (storage/get-psk pid)] psk (storage/get-psk pid)]
(if (storage/valid-publisher? pid) (if (storage/valid-publisher? pid)
(let [resp (api/post-note note pid (api/get-signature (str pid psk note)) password)] (let [resp (api/post-note note pid (api/get-signature pid psk note) password)]
(if (and (if (and
(storage/invalidate-session session) (storage/invalidate-session session)
(get-in resp [:status :success])) (get-in resp [:status :success]))
@ -170,7 +170,7 @@
psk (storage/get-psk pid)] psk (storage/get-psk pid)]
(if (storage/valid-publisher? pid) (if (storage/valid-publisher? pid)
(let [resp (api/update-note noteID note pid (let [resp (api/update-note noteID note pid
(api/get-signature (str pid psk noteID note password)) (api/get-signature pid psk noteID note password)
password)] password)]
(if (get-in resp [:status :success]) (if (get-in resp [:status :success])
(redirect (:longURL resp)) (redirect (:longURL resp))

Loading…
Cancel
Save