Browse Source

session key problem at preview fixed

master
Christian Mueller 14 years ago
parent
commit
a1527fec21
  1. 8
      src-cljs/main.cljs
  2. 6
      src/NoteHub/views/common.clj
  3. 39
      src/NoteHub/views/pages.clj

8
src-cljs/main.cljs

@ -10,6 +10,7 @@
; frequently used selectors ; frequently used selectors
(def $draft ($ :#draft)) (def $draft ($ :#draft))
(def $preview ($ :#preview)) (def $preview ($ :#preview))
(def $session-key ($ :#session-key))
(def $preview-start-line ($ :#preview-start-line)) (def $preview-start-line ($ :#preview-start-line))
(defn scroll-to (defn scroll-to
@ -30,12 +31,13 @@
(.click ($ :#preview-button) (.click ($ :#preview-button)
(fn [e] (fn [e]
(do (do
(fm/remote (md-to-html (val $draft)) [result] (fm/remote (get-preview-md (val $session-key) (val $draft)) [{:keys [preview session-key]}]
(show $preview-start-line) (show $preview-start-line)
(inner $preview result) (inner $preview preview)
(val $session-key session-key)
(scroll-to $preview-start-line))))) (scroll-to $preview-start-line)))))
(.click ($ :#publish-button) (.click ($ :#publish-button)
(fn [e] (fn [e]
(val ($ :#session-value) (val ($ :#session-value)
(nh/hash #(.charCodeAt % 0) (str (val $draft) (val ($ :#session-key))))))) (nh/hash #(.charCodeAt % 0) (str (val $draft) (val $session-key))))))

6
src/NoteHub/views/common.clj

@ -89,14 +89,16 @@
:border-bottom [:1px :dashed :gray] :border-bottom [:1px :dashed :gray]
:margin-bottom :5em) :margin-bottom :5em)
(rule "h1, h2, h3, h4" (rule "h1, h2, h3, h4"
:font-family (gen-comma-list "'Noticia Text'" "Georgia")))) :font-family (gen-comma-list "'Noticia Text'" "'PT Serif'"))))
(defpartial layout [title & content] (defpartial layout [title & content]
(html5 (html5
[:head [:head
[:title "NoteHub — " title] [:title "NoteHub — " title]
(include-js "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js") (include-js "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")
[:link {:href "http://fonts.googleapis.com/css?family=Noticia+Text:400,700" [:link {:href (str "http://fonts.googleapis.com/css?family="
"PT+Serif:700|Noticia+Text:700"
"&subset=latin,cyrillic" )
:rel "stylesheet" :rel "stylesheet"
:type "text/css"}] :type "text/css"}]
[:style {:type "text/css"} global-css]] [:style {:type "text/css"} global-css]]

39
src/NoteHub/views/pages.clj

@ -20,8 +20,19 @@
(def max-title-length 80) (def max-title-length 80)
; Markdown -> HTML mapper ; Markdown -> HTML mapper
(defremote md-to-html [draft] (defn md-to-html [md-text]
(.markdownToHtml (PegDownProcessor.) draft)) (.markdownToHtml (PegDownProcessor.) md-text))
(defn get-flash-key []
(let [k (encrypt (str (rand-int Integer/MAX_VALUE)))]
(do (flash-put! k true)
(print-str k))))
; This function answers to a corresponding AJAX request
(defremote get-preview-md [session-key md]
(when (flash-get session-key)
{:session-key (get-flash-key)
:preview (md-to-html md)}))
; Template for the error sites ; Template for the error sites
(defn page-setter [code message] (defn page-setter [code message]
@ -50,8 +61,7 @@
(common/layout "New Markdown Note" (common/layout "New Markdown Note"
[:div.central-element [:div.central-element
(form-to [:post "/post-note"] (form-to [:post "/post-note"]
(hidden-field :session-key (let [k (encrypt (str (rand-int Integer/MAX_VALUE)))] (hidden-field :session-key (get-flash-key))
(do (flash-put! k true) (print-str k))))
(hidden-field {:id :session-value} :session-value) (hidden-field {:id :session-value} :session-value)
(text-area {:class :max-width} :draft) (text-area {:class :max-width} :draft)
[:div#buttons.hidden [:div#buttons.hidden
@ -88,14 +98,15 @@
(let [valid-session (flash-get session-key) ; it was posted from a newly generated form (let [valid-session (flash-get session-key) ; it was posted from a newly generated form
valid-draft (not (empty? draft)) ; the note is non-empty valid-draft (not (empty? draft)) ; the note is non-empty
valid-hash (= (Short/parseShort session-value) ; the hash code is correct valid-hash (= (Short/parseShort session-value) ; the hash code is correct
(nh/hash #(.codePointAt % 0) (str draft session-key)))] (nh/hash #(.codePointAt % 0) (str draft session-key)))]
(do
(println "session:" valid-session "draft:" valid-draft "hash:"
(Short/parseShort session-value)
(nh/hash #(.codePointAt % 0) (str draft session-key)))
(if (and valid-session valid-draft valid-hash)
(do (do
(set-note date title draft) ; TODO: delete this if tests are written
; TODO: the redirect is broken if title contains UTF chars (println "session:" valid-session "draft:" valid-draft "hash:"
(redirect (apply str (interpose "/" ["" year month day title])))) (Short/parseShort session-value)
(get-page 400)))))) (nh/hash #(.codePointAt % 0) (str draft session-key)))
(if (and valid-session valid-draft valid-hash)
(do
(set-note date title draft)
; TODO: the redirect is broken if title contains UTF chars
(redirect (apply str (interpose "/" ["" year month day title]))))
(get-page 400))))))

Loading…
Cancel
Save