From 58f872e6748d51bda45ed78659752498f028ab3f Mon Sep 17 00:00:00 2001 From: Simon Koelsch Date: Sun, 15 Mar 2015 17:32:09 +0100 Subject: [PATCH 1/4] added avatar and user profile link to json format --- src/statuses/routes.clj | 2 ++ src/statuses/routing.clj | 3 ++- src/statuses/views/json.clj | 13 +++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/statuses/views/json.clj 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..9585158 --- /dev/null +++ b/src/statuses/views/json.clj @@ -0,0 +1,13 @@ +(ns statuses.views.json + (:require [statuses.routes :refer [avatar-path profile-path]])) + +(defn add-avatar [items] + (map #(assoc % :avatar (avatar-path (:author %))) items)) + +(defn add-profile [items] + (map #(assoc % :profile (profile-path (:author %))) items)) + +(defn decorate [items] + (->> items + (add-avatar) + (add-profile))) From 9c4cb2b61777082b6103612e71f4785e751e52cc Mon Sep 17 00:00:00 2001 From: Simon Koelsch Date: Sun, 15 Mar 2015 17:47:50 +0100 Subject: [PATCH 2/4] refactored main view to use new profile-path function (now consistent with the usage of avatar-path) --- src/statuses/views/main.clj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 9e2355cf8e24f0fdb74ee3a33aca938ddeab240b Mon Sep 17 00:00:00 2001 From: Simon Koelsch Date: Sun, 15 Mar 2015 21:35:04 +0100 Subject: [PATCH 3/4] added link to self in json format --- src/statuses/views/json.clj | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/statuses/views/json.clj b/src/statuses/views/json.clj index 9585158..8e2a6a7 100644 --- a/src/statuses/views/json.clj +++ b/src/statuses/views/json.clj @@ -1,5 +1,5 @@ (ns statuses.views.json - (:require [statuses.routes :refer [avatar-path profile-path]])) + (:require [statuses.routes :refer [avatar-path profile-path update-path]])) (defn add-avatar [items] (map #(assoc % :avatar (avatar-path (:author %))) items)) @@ -7,7 +7,11 @@ (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-profile) + (add-self))) From 53d3186ba9aa10bb62d1a4adce5c0211effab045 Mon Sep 17 00:00:00 2001 From: Simon Koelsch Date: Sun, 15 Mar 2015 22:57:21 +0100 Subject: [PATCH 4/4] added tests for json decoration --- test/statuses/views/test/json.clj | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test/statuses/views/test/json.clj 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)))