Browse Source

bugfix: short-urls repaired

master
Christian Mueller 12 years ago
parent
commit
a4aeb34c49
  1. 2
      Makefile
  2. 3
      settings
  3. 16
      src/NoteHub/api.clj
  4. 7
      src/NoteHub/storage.clj
  5. 9
      src/NoteHub/views/pages.clj
  6. 9
      test/NoteHub/test/api.clj

2
Makefile

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
# starts the app in :dev mode
run:
lein run dev
@DEVMODE=1 lein run
server:
redis-server &

3
settings

@ -1,3 +1,4 @@ @@ -1,3 +1,4 @@
max-title-length = 40
domain = http://notehub.org
prod-domain = http://notehub.org
dev-domain = http://localhost:8080
db-url = http://localhost:6379

16
src/NoteHub/api.clj

@ -11,7 +11,11 @@ @@ -11,7 +11,11 @@
(def version "1.1")
(def domain (get-setting :domain))
(def domain
(get-setting
(if (get-setting :dev-mode)
:dev-domain
:prod-domain)))
(defn log
"Logs args to the server stdout"
@ -42,9 +46,9 @@ @@ -42,9 +46,9 @@
(assoc (create-response success) :message (apply format message params))))
(defn- get-path [noteID & description]
(if description
(str domain "/" (storage/get-short-url noteID))
(let [[year month day title] (split noteID #" ")]
(let [[year month day title] (split noteID #" ")]
(if description
(str domain "/" (storage/create-short-url {:year year :month month :day day :title title}))
(apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)])))))
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
@ -90,10 +94,10 @@ @@ -90,10 +94,10 @@
title (first (drop-while #(storage/note-exists? (build-key date %))
(cons proposed-title
(map #(str proposed-title "-" (+ 2 %)) (range)))))
noteID (build-key date title)]
noteID (build-key date title)
short-url (storage/create-short-url {:year year :month month :day day :title title})]
(do
(storage/add-note noteID note pid password)
(storage/create-short-url noteID)
{:noteID noteID
:longURL (get-path noteID)
:shortURL (get-path noteID :short)

7
src/NoteHub/storage.clj

@ -1,8 +1,7 @@ @@ -1,8 +1,7 @@
(ns NoteHub.storage
(:use [NoteHub.settings]
[clojure.string :only (blank?)]
[noir.util.crypt :only [encrypt]]
[noir.options :only [dev-mode?]])
[noir.util.crypt :only [encrypt]])
(:require [taoensso.carmine :as car :refer (wcar)]))
(def conn {:pool {} :spec {:uri (get-setting :db-url)}})
@ -112,10 +111,10 @@ @@ -112,10 +111,10 @@
(redis :hdel short-url value))))
(defn create-short-url
"Creates a short url for the given request metadata or noteID or extracts
"Creates a short url for the given request metadata or extracts
one if it was already created"
[arg]
(let [key (if (map? arg) (str (into (sorted-map) arg)) arg)]
(let [key (str (into (sorted-map) arg))]
(if (short-url-exists? key)
(redis :hget short-url key)
(let [hash-stream (partition 5 (repeatedly #(rand-int 36)))

9
src/NoteHub/views/pages.clj

@ -11,7 +11,6 @@ @@ -11,7 +11,6 @@
[noir.util.crypt :only [encrypt]]
[hiccup.form]
[hiccup.core]
[noir.options :only [dev-mode?]]
[ring.util.codec :only [url-encode]]
[hiccup.element]
[hiccup.util :only [escape-html]]
@ -38,7 +37,7 @@ @@ -38,7 +37,7 @@
(include-js "/js/main.js")
(include-js "/js/themes.js"))
; google analytics code should appear in prod mode only
(if-not (dev-mode?) (include-js "/js/google-analytics.js"))]
(if-not (get-setting :dev-mode?) (include-js "/js/google-analytics.js"))]
[:body {:onload "onLoad()"} content]))
; Sets a custom message for each needed HTTP status.
@ -97,7 +96,7 @@ @@ -97,7 +96,7 @@
[:div.centered.helvetica.markdown (get-message :footer)]))
; Displays the note
(defpage "/:year/:month/:day/:title" {:keys [year month day title]}
(defpage "/:year/:month/:day/:title" {:keys [year month day title] :as params}
(let [noteID (api/build-key [year month day] title)]
(when (storage/note-exists? noteID)
(let [note (api/get-note noteID)]
@ -105,7 +104,7 @@ @@ -105,7 +104,7 @@
[:article.bottom-space.markdown (:note note)]
(let [links (map #(link-to
(if (= :short-url %)
(:shortURL note)
(url (storage/create-short-url params))
(str (:longURL note) "/" (name %)))
(get-message %))
[:stats :edit :export :short-url])
@ -191,4 +190,4 @@ @@ -191,4 +190,4 @@
(generate-string (api/post-note note pid signature password)))
(defpage [:put "/api/note"] {:keys [version noteID note pid signature password]}
(generate-string (api/update-note noteID note pid signature password)))
(generate-string (api/update-note noteID note pid signature password)))

9
test/NoteHub/test/api.clj

@ -45,11 +45,18 @@ @@ -45,11 +45,18 @@
(is (= note (:note get-response)))
(is (= (:longURL post-response) (:longURL get-response) note-url))
(is (= (:shortURL post-response) (:shortURL get-response)))
(is (storage/note-exists? (:noteID post-response)))
(let [su (last (clojure.string/split (:shortURL get-response) #"/"))]
(is (= su (storage/create-short-url (storage/resolve-url su)))))
(let [resp (send-request
(clojure.string/replace (:shortURL get-response) domain ""))
resp (send-request ((:headers resp) "Location"))]
(is (substring? "hello world"(:body resp))))
(is (= (:publisher get-response) pid))
(is (= (:title get-response) (derive-title note)))
(is (= "1" (get-in get-response [:statistics :views])))
(isnt (get-in get-response [:statistics :edited]))
(is (= "2" (get-in (get-note (:noteID post-response)) [:statistics :views])))))
(is (= "3" (get-in (get-note (:noteID post-response)) [:statistics :views])))))
(testing "creation with wrong signature"
(let [response (post-note note pid (get-signature pid2 psk note))]
(isnt (:success (:status response)))

Loading…
Cancel
Save