Permalink
Browse files

Evo 4clojure#205:

Create the page to list in readonly mode the people someone follows.

This is the bare minimum to list as this is a startup step.
Also, this could be moved in the user profile page.
  • Loading branch information...
ardumont committed Feb 11, 2012
1 parent f9a850a commit 1affee16f6c17b9aa92959340497d33ea20e69ca
Showing with 62 additions and 0 deletions.
  1. +41 −0 src/foreclojure/users.clj
  2. +21 −0 test/foreclojure/test/users.clj
View
@@ -19,6 +19,11 @@
(when (:golfing-active config)
[:golfer])))
+(defn get-user-from-id "Retrieve a user by its id"
+ [id]
+ (fetch-one :users
+ :where {:_id id}))
+
(defn get-user-id [name]
(:_id
(fetch-one :users
@@ -132,6 +137,24 @@
[:td (following-checkbox user-id following _id user)]])
user-set)])))
+(defn generate-following-list [user-set table-name]
+ (let [[user-id following] (if-user [{:keys [_id following]}]
+ [_id (set following)])]
+ (list
+ [:br]
+ [:table.my-table {:id table-name}
+ [:thead
+ [:tr
+ [:th {:style "width: 200px;"} "Username"]
+ [:th {:style "width: 180px;"} "Problems Solved"]]]
+ (map-indexed (fn [rownum {:keys [_id email user contributor solved]}]
+ [:tr (row-class rownum)
+ [:td
+ (gravatar-img {:email email :class "gravatar"})
+ [:a.user-profile-link {:href (str "/user/" user)} user (when contributor [:span.contributor " *"])]]
+ [:td.centered (count solved)]])
+ user-set)])))
+
(defn generate-datatable-users-list [user-set]
(let [[user-id following] (if-user [{:keys [_id following]}]
[_id (set following)])]
@@ -228,6 +251,16 @@
(count (get-solved username)) "/"
(count (get-problems))]]])}))
+;; page referencing the users 'username' follows
+(def-page following-page [user]
+ {:title "Following people"
+ :content
+ (content-page
+ {:heading "People I follow"
+ :heading-note [:span#all-users-link]
+ :main (let [following (map get-user-from-id (:following user))]
+ (generate-following-list following "following"))})})
+
(defn follow-user [username follow?]
(with-user [{:keys [_id]}]
(let [follow-id (:_id (get-user username))
@@ -298,6 +331,14 @@
(if (nil? (get-user username))
{:status 404 :headers {"Content-Type" "text/plain"} :body "Error: This user does not exist, nice try though."}
(user-profile username)))
+ (GET "/user/:username/following" [username]
+ (let [user (get-user username)]
+ (if (nil? user)
+ {:status 404
+ :headers {"Content-Type" "text/plain"}
+ :body "Error: This user does not exist, nice try though."}
+ (following-page user))))
+
(POST "/user/follow/:username" [username] (static-follow-user username true))
(POST "/user/unfollow/:username" [username] (static-follow-user username false))
(POST "/rest/user/follow/:username" [username] (rest-follow-user username true))
@@ -45,3 +45,24 @@
(:user (first (datatable-filter "4" users))) => "user4"
(:user (second (datatable-filter "1" users))) => "user10"
(count (datatable-filter nil users)) => 10))
+
+;; some test to try and own the code regarding the followers
+
+(fact "follow-url"
+ (follow-url "user-test" true) => "/user/follow/user-test"
+ (follow-url "user-test" false) => "/user/unfollow/user-test")
+
+(fact "following checkbox - if the user follows someone, the checkbox on this line for this someone will be checked. The post action will be to unfollow him."
+ (following-checkbox :cur-user #{:user-id-followed :do :not :care}
+ :user-id-followed "user-name-followed")
+ => [:form {:method "POST" :action "/user/unfollow/user-name-followed"}
+ [:input.following {:type "checkbox", :checked true}]
+ [:span.following "yes"]])
+
+;; new function to retrieve a user by its id
+(fact
+ (get-user-from-id :id-some-user) => :some-user
+ (provided
+ (somnium.congomongo/fetch-one :users :where {:_id :id-some-user}) => :some-user))
+
+

0 comments on commit 1affee1

Please sign in to comment.