diff --git a/src/statuses/routes.clj b/src/statuses/routes.clj index 120de1f..10b9e10 100644 --- a/src/statuses/routes.clj +++ b/src/statuses/routes.clj @@ -39,3 +39,5 @@ (defn avatar-path [username] (clojure.string/replace (config :avatar-url) "{username}" username)) +(defn profile-path [username] + (str (config :profile-url-prefix) username)) diff --git a/src/statuses/routing.clj b/src/statuses/routing.clj index 6b34c50..a0e3d5a 100644 --- a/src/statuses/routing.clj +++ b/src/statuses/routing.clj @@ -7,6 +7,7 @@ [statuses.backend.persistence :refer [db]] [statuses.routes :as route] [statuses.views.atom :as atom] + [statuses.views.json :as json-decorator] [statuses.views.info :as info-view] [statuses.views.main :refer [list-page reply-form]] [statuses.views.too-long :as too-long-view])) @@ -54,7 +55,7 @@ (cond (= format "json") (content-type "application/json" - (json/as-json {:items items, :next next})) + (json/as-json {:items (json-decorator/decorate items), :next next})) (= format "atom") (content-type "application/atom+xml;charset=utf-8" (atom/render-atom items diff --git a/src/statuses/views/json.clj b/src/statuses/views/json.clj new file mode 100644 index 0000000..8e2a6a7 --- /dev/null +++ b/src/statuses/views/json.clj @@ -0,0 +1,17 @@ +(ns statuses.views.json + (:require [statuses.routes :refer [avatar-path profile-path update-path]])) + +(defn add-avatar [items] + (map #(assoc % :avatar (avatar-path (:author %))) items)) + +(defn add-profile [items] + (map #(assoc % :profile (profile-path (:author %))) items)) + +(defn add-self [items] + (map #(assoc % :self (update-path (:id %))) items)) + +(defn decorate [items] + (->> items + (add-avatar) + (add-profile) + (add-self))) diff --git a/src/statuses/views/main.clj b/src/statuses/views/main.clj index 51803a0..599c07b 100644 --- a/src/statuses/views/main.clj +++ b/src/statuses/views/main.clj @@ -5,7 +5,7 @@ [hiccup.form :refer [form-to hidden-field text-field]] [statuses.configuration :refer [config]] [statuses.routes :refer [avatar-path update-path - updates-path]] + updates-path profile-path]] [statuses.views.common :as common :refer [icon]] [statuses.views.layout :as layout])) @@ -41,7 +41,7 @@ (defn update [is-current {:keys [id text author time in-reply-to conversation can-delete?]}] (list [:div.avatar - (link-to (str (config :profile-url-prefix) author) [:img {:src (avatar-path author) :alt author}])] + (link-to (profile-path author) [:img {:src (avatar-path author) :alt author}])] [:div.meta [:span.author (link-to (str (updates-path) "?author=" author) author)] (if in-reply-to diff --git a/test/statuses/views/test/json.clj b/test/statuses/views/test/json.clj new file mode 100644 index 0000000..b0e9db2 --- /dev/null +++ b/test/statuses/views/test/json.clj @@ -0,0 +1,56 @@ +(ns statuses.views.test.json + (:require [clojure.test :refer [deftest is]] + [clj-time.core :refer [date-time]] + [statuses.routes :refer [avatar-path profile-path update-path]] + [statuses.views.json :refer [decorate]])) + +(defn- item [can-delete? id time author text] + {:can-delete? can-delete? :id id :time time :author author :text text}) + +(defn- test-item [item ref-item] + + (is (= (:id item) + (:id ref-item)) + "id is unchanged") + + (is (= (:can-delete? item) + (:can-delete? ref-item)) + "can-delete? matches") + + (is (= (:author item) + (:author ref-item)) + "author is unchanged") + + (is (= (:time item) + (:time ref-item)) + "time is unchanged") + + (is (= (:text item) + (:text ref-item)) + "text is unchanged") + + (is (= (:self item) + (update-path (:id ref-item))) + "self reference is set") + + (is (= (:avatar item) + (avatar-path (:author ref-item))) + "avatar is set") + + (is (= (:profile item) + (profile-path (:author ref-item))) + "profile is set")) + +(deftest test-decorate + (let [item1 (item false 42 (date-time 2015 2 20 0 0 0 0) "hein" "Irgendwann wird auch mal jemand verstehen, was Phillip hier meint.") + item2 (item true 41 (date-time 2015 2 20 0 0 0 0) "hein" "We know some foreign languages, too, so let's throw in an English text.") + item3 (item false 40 (date-time 2015 2 20 0 0 0 0) "uiui" "Hoffentlich sieht das hier nie irgend jemand.") + + items [item1 item2 item3] + + decorated (decorate items)] + + ;; could be more "functional" but for the sake of readability + (test-item (first decorated) item1) + (test-item (second decorated) item2) + (test-item (nth decorated 2) item3)))