session key problem at preview fixed
This commit is contained in:
+5
-3
@@ -10,6 +10,7 @@
|
||||
; frequently used selectors
|
||||
(def $draft ($ :#draft))
|
||||
(def $preview ($ :#preview))
|
||||
(def $session-key ($ :#session-key))
|
||||
(def $preview-start-line ($ :#preview-start-line))
|
||||
|
||||
(defn scroll-to
|
||||
@@ -30,12 +31,13 @@
|
||||
(.click ($ :#preview-button)
|
||||
(fn [e]
|
||||
(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)
|
||||
(inner $preview result)
|
||||
(inner $preview preview)
|
||||
(val $session-key session-key)
|
||||
(scroll-to $preview-start-line)))))
|
||||
|
||||
(.click ($ :#publish-button)
|
||||
(fn [e]
|
||||
(val ($ :#session-value)
|
||||
(nh/hash #(.charCodeAt % 0) (str (val $draft) (val ($ :#session-key)))))))
|
||||
(nh/hash #(.charCodeAt % 0) (str (val $draft) (val $session-key))))))
|
||||
|
||||
@@ -89,14 +89,16 @@
|
||||
:border-bottom [:1px :dashed :gray]
|
||||
:margin-bottom :5em)
|
||||
(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]
|
||||
(html5
|
||||
[:head
|
||||
[:title "NoteHub — " title]
|
||||
(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"
|
||||
:type "text/css"}]
|
||||
[:style {:type "text/css"} global-css]]
|
||||
|
||||
+25
-14
@@ -20,8 +20,19 @@
|
||||
(def max-title-length 80)
|
||||
|
||||
; Markdown -> HTML mapper
|
||||
(defremote md-to-html [draft]
|
||||
(.markdownToHtml (PegDownProcessor.) draft))
|
||||
(defn md-to-html [md-text]
|
||||
(.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
|
||||
(defn page-setter [code message]
|
||||
@@ -50,8 +61,7 @@
|
||||
(common/layout "New Markdown Note"
|
||||
[:div.central-element
|
||||
(form-to [:post "/post-note"]
|
||||
(hidden-field :session-key (let [k (encrypt (str (rand-int Integer/MAX_VALUE)))]
|
||||
(do (flash-put! k true) (print-str k))))
|
||||
(hidden-field :session-key (get-flash-key))
|
||||
(hidden-field {:id :session-value} :session-value)
|
||||
(text-area {:class :max-width} :draft)
|
||||
[:div#buttons.hidden
|
||||
@@ -88,14 +98,15 @@
|
||||
(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-hash (= (Short/parseShort session-value) ; the hash code is correct
|
||||
(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)
|
||||
(nh/hash #(.codePointAt % 0) (str draft session-key)))]
|
||||
(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))))))
|
||||
; TODO: delete this if tests are written
|
||||
(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
|
||||
(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))))))
|
||||
|
||||
Reference in New Issue
Block a user