Browse Source

publisher removed from the statistics node of the API response

master
Christian Müller 11 years ago
parent
commit
0a099649fb
  1. 207
      src/notehub/handler.clj
  2. 7
      src/notehub/storage.clj
  3. 21
      src/notehub/views.clj

207
src/notehub/handler.clj

@ -1,10 +1,10 @@
(ns notehub.handler (ns notehub.handler
(:use (:use
compojure.core compojure.core
iokv.core iokv.core
notehub.views notehub.views
[clojure.string :rename {replace sreplace} :only [replace]]) [clojure.string :rename {replace sreplace} :only [replace]])
(:require (:require
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[clojure.core.cache :as cache] [clojure.core.cache :as cache]
[hiccup.util :as util] [hiccup.util :as util]
@ -41,119 +41,120 @@
:body content}) :body content})
(defroutes api-routes (defroutes api-routes
(GET "/note" {params :params} (GET "/note" {params :params}
(generate-string (api/version-manager api/get-note params))) (generate-string (api/version-manager api/get-note params)))
(POST "/note" {params :params} (POST "/note" {params :params}
(generate-string (api/version-manager api/post-note params))) (generate-string (api/version-manager api/post-note params)))
(PUT "/note" {params :params} (PUT "/note" {params :params}
(generate-string (api/version-manager api/update-note params)))) (generate-string (api/version-manager api/update-note params))))
(defroutes app-routes (defroutes app-routes
(GET "/api" [] (layout :no-js (get-message :api-title) (GET "/api" [] (layout :no-js (get-message :api-title)
[:article (md-to-html (slurp "API.md"))])) [:article (md-to-html (slurp "API.md"))]))
(context "/api" [] (context "/api" []
#(ring.util.response/content-type (api-routes %) "application/json")) #(ring.util.response/content-type (api-routes %) "application/json"))
(GET "/" [] landing-page) (GET "/" [] landing-page)
(GET "/:year/:month/:day/:title/export" [year month day title] (GET "/:year/:month/:day/:title/export" [year month day title]
(when-let [md-text (:note (api/get-note {:noteID (api/build-key year month day title)}))] (when-let [md-text (:note (api/get-note {:noteID (api/build-key year month day title)}))]
(return-content-type "text/plain; charset=utf-8" md-text))) (return-content-type "text/plain; charset=utf-8" md-text)))
(GET "/:year/:month/:day/:title/stats" [year month day title] (GET "/:year/:month/:day/:title/stats" [year month day title]
(let [note-id (api/build-key year month day title)] (let [note-id (api/build-key year month day title)]
(statistics-page (api/derive-title (storage/get-note note-id)) (statistics-page (api/derive-title (storage/get-note note-id))
(storage/get-note-statistics note-id)))) (storage/get-note-statistics note-id)
(storage/get-publisher note-id))))
(GET "/:year/:month/:day/:title/edit" [year month day title]
(let [note-id (api/build-key year month day title)] (GET "/:year/:month/:day/:title/edit" [year month day title]
(note-update-page (let [note-id (api/build-key year month day title)]
note-id (note-update-page
(:note (api/get-note {:noteID note-id}))))) note-id
(:note (api/get-note {:noteID note-id})))))
(GET "/new" [] (new-note-page
(str (GET "/new" [] (new-note-page
(current-timestamp) (str
(storage/sign (rand-int Integer/MAX_VALUE))))) (current-timestamp)
(storage/sign (rand-int Integer/MAX_VALUE)))))
(GET "/:year/:month/:day/:title" [year month day title :as params]
(let [params (assoc (:query-params params) (GET "/:year/:month/:day/:title" [year month day title :as params]
:year year :month month :day day :title title) (let [params (assoc (:query-params params)
note-id (api/build-key year month day title) :year year :month month :day day :title title)
short-url (storage/create-short-url note-id params)] note-id (api/build-key year month day title)
(when (storage/note-exists? note-id) short-url (storage/create-short-url note-id params)]
(if (cache/has? @page-cache short-url) (when (storage/note-exists? note-id)
(do (if (cache/has? @page-cache short-url)
(swap! page-cache cache/hit short-url) (do
(storage/increment-note-view note-id)) (swap! page-cache cache/hit short-url)
(swap! page-cache cache/miss short-url (storage/increment-note-view note-id))
(note-page (api/get-note {:noteID note-id}) (swap! page-cache cache/miss short-url
(api/url short-url)))) (note-page (api/get-note {:noteID note-id})
(cache/lookup @page-cache short-url)))) (api/url short-url))))
(cache/lookup @page-cache short-url))))
(GET "/:short-url" [short-url]
(when-let [params (storage/resolve-url short-url)] (GET "/:short-url" [short-url]
(let [{:keys [year month day title]} params (when-let [params (storage/resolve-url short-url)]
rest-params (dissoc params :year :month :day :title) (let [{:keys [year month day title]} params
core-url (api/url year month day title) rest-params (dissoc params :year :month :day :title)
long-url (if (empty? rest-params) core-url (util/url core-url rest-params))] core-url (api/url year month day title)
(redirect long-url)))) long-url (if (empty? rest-params) core-url (util/url core-url rest-params))]
(redirect long-url))))
(POST "/post-note" [session note signature password]
(if (and session (POST "/post-note" [session note signature password]
(.startsWith session (if (and session
(str (current-timestamp))) (.startsWith session
(= signature (storage/sign session note))) (str (current-timestamp)))
(let [pid "NoteHub" (= signature (storage/sign session note)))
psk (storage/get-psk pid) (let [pid "NoteHub"
params {:note note psk (storage/get-psk pid)
:pid pid params {:note note
:signature (storage/sign pid psk note) :pid pid
:password password}] :signature (storage/sign pid psk note)
(if (storage/valid-publisher? pid) :password password}]
(let [resp (api/post-note params)] (if (storage/valid-publisher? pid)
(if (get-in resp [:status :success]) (let [resp (api/post-note params)]
(redirect (:longURL resp)) (if (get-in resp [:status :success])
(response 400))) (redirect (:longURL resp))
(response 500))) (response 400)))
(response 400))) (response 500)))
(response 400)))
(POST "/update-note" [noteID note password]
(let [pid "NoteHub" (POST "/update-note" [noteID note password]
psk (storage/get-psk pid) (let [pid "NoteHub"
params {:noteID noteID :note note :password password :pid pid}] psk (storage/get-psk pid)
(if (storage/valid-publisher? pid) params {:noteID noteID :note note :password password :pid pid}]
(let [resp (api/update-note (assoc params (if (storage/valid-publisher? pid)
:signature (let [resp (api/update-note (assoc params
(storage/sign pid psk noteID note password)))] :signature
(if (get-in resp [:status :success]) (storage/sign pid psk noteID note password)))]
(do (if (get-in resp [:status :success])
(doseq [url (storage/get-short-urls noteID)] (do
(swap! page-cache cache/evict url)) (doseq [url (storage/get-short-urls noteID)]
(redirect (:longURL resp))) (swap! page-cache cache/evict url))
(response 403))) (redirect (:longURL resp)))
(response 500)))) (response 403)))
(response 500))))
(route/resources "/")
(route/not-found (response 404))) (route/resources "/")
(route/not-found (response 404)))
(def app (def app
(let [handler (handler/site app-routes)] (let [handler (handler/site app-routes)]
(fn [request] (fn [request]
(let [{:keys [server-name server-port]} request (let [{:keys [server-name server-port]} request
hostURL (str "https://" server-name hostURL (str "https://" server-name
(when (not= 80 server-port) (str ":" server-port))) (when (not= 80 server-port) (str ":" server-port)))
request (assoc-in request [:params :hostURL] hostURL)] request (assoc-in request [:params :hostURL] hostURL)]
(if (get-setting :dev-mode) (if (get-setting :dev-mode)
(handler request) (handler request)
(try (handler request) (try (handler request)
(catch Exception e (catch Exception e
(do (do
;TODO (log e) ;TODO (log e)
(response 500))))))))) (response 500)))))))))
(defn -main [& [port]] (defn -main [& [port]]
(jetty/run-jetty #'app (jetty/run-jetty #'app

7
src/notehub/storage.clj

@ -15,13 +15,13 @@
(.update md5Instance (.getBytes input)) (.update md5Instance (.getBytes input))
(apply str (apply str
(map #(let [c (Integer/toHexString (bit-and 0xff %))] (map #(let [c (Integer/toHexString (bit-and 0xff %))]
(if (= 1 (count c)) (str "0" c) c)) (if (= 1 (count c)) (str "0" c) c))
(.digest md5Instance))))))) (.digest md5Instance)))))))
(defmacro redis [cmd & body] (defmacro redis [cmd & body]
`(car/wcar conn `(car/wcar conn
(~(symbol "car" (name cmd)) (~(symbol "car" (name cmd))
~@body))) ~@body)))
(defn get-current-date [] (defn get-current-date []
(.getTime (java.util.Date.))) (.getTime (java.util.Date.)))
@ -72,8 +72,7 @@
(defn get-note-statistics [noteID] (defn get-note-statistics [noteID]
{:views (get-note-views noteID) {:views (get-note-views noteID)
:published (redis :hget :published noteID) :published (redis :hget :published noteID)
:edited (redis :hget :edited noteID) :edited (redis :hget :edited noteID)})
:publisher (get-publisher noteID)})
(defn note-exists? [noteID] (defn note-exists? [noteID]
(= 1 (redis :hexists :note noteID))) (= 1 (redis :hexists :note noteID)))

21
src/notehub/views.clj

@ -1,5 +1,5 @@
(ns notehub.views (ns notehub.views
(:use (:use
iokv.core iokv.core
[clojure.string :rename {replace sreplace} :only [replace]] [clojure.string :rename {replace sreplace} :only [replace]]
[hiccup.form] [hiccup.form]
@ -68,23 +68,24 @@
[:a.landing-button {:href "/new" :style "color: white"} (get-message :new-page)]] [:a.landing-button {:href "/new" :style "color: white"} (get-message :new-page)]]
[:div#dashed-line] [:div#dashed-line]
[:article.helvetica.bottom-space [:article.helvetica.bottom-space
{:style "font-size: 1em"} {:style "font-size: 1em"}
(md-to-html (slurp "LANDING.md"))] (md-to-html (slurp "LANDING.md"))]
[:div#footer (md-to-html (get-message :footer))])) [:div#footer (md-to-html (get-message :footer))]))
(defn statistics-page [note-title stats] (defn statistics-page [note-title stats publisher]
(let [page-title (get-message :statistics)] (let [page-title (get-message :statistics)
info (assoc stats :publisher publisher)]
(layout :no-js page-title (layout :no-js page-title
[:h2.central-element note-title] [:h2.central-element note-title]
[:h3.central-element.helvetica page-title] [:h3.central-element.helvetica page-title]
[:table#stats.helvetica.central-element [:table#stats.helvetica.central-element
(map (map
#(when-let [v (% stats)] #(when-let [v (% info)]
[:tr [:tr
[:td (str (get-message %) ":")] [:td (str (get-message %) ":")]
[:td (if (or (= % :published) (= % :edited)) [:td (if (or (= % :published) (= % :edited))
(str (java.util.Date. (Long/parseLong v))) v)]]) (str (java.util.Date. (Long/parseLong v))) v)]])
[:published :edited :publisher :views])]))) [:published :edited :publisher :views])])))
(defn note-update-page [note-id note] (defn note-update-page [note-id note]

Loading…
Cancel
Save