|
|
|
@ -18,21 +18,13 @@ |
|
|
|
(def sessions "sessions") |
|
|
|
(def sessions "sessions") |
|
|
|
(def short-url "short-url") |
|
|
|
(def short-url "short-url") |
|
|
|
|
|
|
|
|
|
|
|
; Concatenates all fields to a string |
|
|
|
|
|
|
|
(defn build-key |
|
|
|
|
|
|
|
"Returns a storage-key for the given note coordinates" |
|
|
|
|
|
|
|
[[year month day] title] |
|
|
|
|
|
|
|
(print-str year month day title)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn create-session |
|
|
|
(defn create-session |
|
|
|
"Creates a random session token" |
|
|
|
|
|
|
|
[] |
|
|
|
[] |
|
|
|
(let [token (encrypt (str (rand-int Integer/MAX_VALUE)))] |
|
|
|
(let [token (encrypt (str (rand-int Integer/MAX_VALUE)))] |
|
|
|
(do (redis/sadd db sessions token) |
|
|
|
(do (redis/sadd db sessions token) |
|
|
|
token))) |
|
|
|
token))) |
|
|
|
|
|
|
|
|
|
|
|
(defn invalidate-session |
|
|
|
(defn invalidate-session |
|
|
|
"Invalidates given session" |
|
|
|
|
|
|
|
[token] |
|
|
|
[token] |
|
|
|
; Jedis is buggy & returns an NPE for token == nil |
|
|
|
; Jedis is buggy & returns an NPE for token == nil |
|
|
|
(when token |
|
|
|
(when token |
|
|
|
@ -41,49 +33,43 @@ |
|
|
|
was-valid)))) |
|
|
|
was-valid)))) |
|
|
|
|
|
|
|
|
|
|
|
(defn update-note |
|
|
|
(defn update-note |
|
|
|
"Updates a note with the given store key if the specified password is correct" |
|
|
|
[noteID text passwd] |
|
|
|
[key text passwd] |
|
|
|
(let [stored-password (redis/hget db password noteID)] |
|
|
|
(let [stored-password (redis/hget db password key)] |
|
|
|
|
|
|
|
(when (and stored-password (= passwd stored-password)) |
|
|
|
(when (and stored-password (= passwd stored-password)) |
|
|
|
(redis/hset db note key text)))) |
|
|
|
(redis/hset db note noteID text)))) |
|
|
|
|
|
|
|
|
|
|
|
(defn set-note |
|
|
|
(defn add-note |
|
|
|
"Creates a note with the given title and text in the given date namespace" |
|
|
|
([noteID text] (add-note noteID text nil)) |
|
|
|
([date title text] (set-note date title text nil)) |
|
|
|
([noteID text passwd] |
|
|
|
([date title text passwd] |
|
|
|
(do |
|
|
|
(let [key (build-key date title)] |
|
|
|
(redis/hset db note noteID text) |
|
|
|
(do |
|
|
|
(when (not (blank? passwd)) |
|
|
|
(redis/hset db note key text) |
|
|
|
(redis/hset db password noteID passwd))))) |
|
|
|
(when (not (blank? passwd)) |
|
|
|
|
|
|
|
(redis/hset db password key passwd)))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn get-note |
|
|
|
(defn get-note |
|
|
|
"Gets the note from the given date namespaces for the specified title" |
|
|
|
[noteID] |
|
|
|
[date title] |
|
|
|
(let [text (redis/hget db note noteID)] |
|
|
|
(let [key (build-key date title) |
|
|
|
|
|
|
|
text (redis/hget db note key)] |
|
|
|
|
|
|
|
(when text |
|
|
|
(when text |
|
|
|
(do |
|
|
|
(do |
|
|
|
(redis/hincrby db views key 1) |
|
|
|
(redis/hincrby db views noteID 1) |
|
|
|
text)))) |
|
|
|
text)))) |
|
|
|
|
|
|
|
|
|
|
|
(defn get-note-views |
|
|
|
(defn get-note-views |
|
|
|
"Returns the number of views for the specified date and note title" |
|
|
|
"Returns the number of views for the specified noteID" |
|
|
|
[date title] |
|
|
|
[noteID] |
|
|
|
(redis/hget db views (build-key date title))) |
|
|
|
(redis/hget db views noteID)) |
|
|
|
|
|
|
|
|
|
|
|
(defn note-exists? |
|
|
|
(defn note-exists? |
|
|
|
"Returns true if the note with the specified title and date exists" |
|
|
|
"Returns true if the note with the specified noteID" |
|
|
|
[date title] |
|
|
|
[noteID] |
|
|
|
(redis/hexists db note (build-key date title))) |
|
|
|
(redis/hexists db note noteID)) |
|
|
|
|
|
|
|
|
|
|
|
(defn delete-note |
|
|
|
(defn delete-note |
|
|
|
"Deletes the note with the specified coordinates" |
|
|
|
"Deletes the note with the specified coordinates" |
|
|
|
[date title] |
|
|
|
[noteID] |
|
|
|
(let [key (build-key date title)] |
|
|
|
(doseq [kw [password views note]] |
|
|
|
(doseq [kw [password views note]] |
|
|
|
; TODO: delete short url by looking for the title |
|
|
|
; TODO: delete short url by looking for the title |
|
|
|
(redis/hdel db kw noteID))) |
|
|
|
(redis/hdel db kw key)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn short-url-exists? |
|
|
|
(defn short-url-exists? |
|
|
|
"Checks whether the provided short url is taken (for testing only)" |
|
|
|
"Checks whether the provided short url is taken (for testing only)" |
|
|
|
@ -99,10 +85,10 @@ |
|
|
|
|
|
|
|
|
|
|
|
(defn delete-short-url |
|
|
|
(defn delete-short-url |
|
|
|
"Deletes a short url (for testing only)" |
|
|
|
"Deletes a short url (for testing only)" |
|
|
|
[key] |
|
|
|
[noteID] |
|
|
|
(let [value (redis/hget db short-url key)] |
|
|
|
(let [value (redis/hget db short-url noteID)] |
|
|
|
(do |
|
|
|
(do |
|
|
|
(redis/hdel db short-url key) |
|
|
|
(redis/hdel db short-url noteID) |
|
|
|
(redis/hdel db short-url value)))) |
|
|
|
(redis/hdel db short-url value)))) |
|
|
|
|
|
|
|
|
|
|
|
(defn create-short-url |
|
|
|
(defn create-short-url |
|
|
|
|