Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render avatar, profile and self in format json #166

Merged
merged 4 commits into from
Apr 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/statuses/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
3 changes: 2 additions & 1 deletion src/statuses/routing.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/statuses/views/json.clj
Original file line number Diff line number Diff line change
@@ -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)))
4 changes: 2 additions & 2 deletions src/statuses/views/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions test/statuses/views/test/json.clj
Original file line number Diff line number Diff line change
@@ -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)))