Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
108 lines (92 sloc) 3.52 KB
(ns foreclojure.users
(:require [ring.util.response :as response])
(:use [foreclojure.utils :only [from-mongo def-page row-class get-user with-user]]
[foreclojure.config :only [config repo-url]]
[somnium.congomongo :only [fetch-one fetch update!]]
[compojure.core :only [defroutes GET]]
[ :only [link-to]]))
(def golfer-tags (into [:contributor]
(when (:golfing-active config)
(defn get-user-id [name]
(fetch-one :users
:where {:user name}
:only [:_id])))
(def sort-by-solved-and-date (juxt (comp count :solved) :last-login))
(defn users-sort [users]
(reverse (sort-by sort-by-solved-and-date users)))
(defn get-users []
(let [users (from-mongo
(fetch :users
:only [:user :solved :contributor]))
sortfn (comp - count :solved)]
(sort-by sortfn users)))
(defn golfer? [user]
(some user golfer-tags))
(defn disable-codebox? [user]
(true? (:disable-code-box user)))
(defn email-address [username]
(:email (fetch-one :users :where {:user username})))
(defn mailto [username]
(link-to (str "mailto:" (email-address username))
(def-page users-page []
{:title "Top Users"
[:span.contributor "*"] " "
(link-to repo-url "4clojure contributor")]
[:th {:style "width: 40px;"} "Rank"]
[:th "Username"]
[:th "Problems Solved"]]]
(map-indexed (fn [rownum {:keys [user contributor solved]}]
[:tr (row-class rownum)
[:td (inc rownum)]
(when contributor [:span.contributor "* "])
[:a.user-profile-link {:href (str "/user/" user)} user]]
[:td.centered (count solved)]])
;; TODO: this is snagged from problems.clj but can't be imported due to cyclic dependency, must refactor this out.
(defn get-problems
(fetch :problems
:only [:_id :difficulty]
:where {:approved true}
:sort {:_id 1})))
(get (group-by :difficulty (get-problems)) difficulty [{}])))
(defn get-solved
(:solved (get-user username)))
([username difficulty]
(let [ids (->> (from-mongo
(fetch :problems
:only [:_id]
:where {:approved true, :difficulty difficulty}))
(map :_id)
(filter ids (get-solved username)))))
(def-page user-profile [username]
[:h2 "User: " username]
(for [difficulty ["Elementary" "Easy" "Medium" "Hard"]]
[:tr [:td.count-label difficulty] [:td.count-value [:div.progress-bar-bg [:div.progress-bar {:style (str "width: " (* 100 (/ (count (get-solved username difficulty)) (count (get-problems difficulty)))) "%")}]]]])
[:tr [:td.count-total "TOTAL:" ] [:td.count-value (count (get-solved username)) "/" (count (get-problems))]]])
(defn set-disable-codebox [disable-flag]
(with-user [{:keys [_id]}]
(update! :users
{:_id _id}
{:$set {:disable-code-box (boolean disable-flag)}})
(response/redirect "/problems")))
(defroutes users-routes
(GET "/users" [] (users-page))
(GET "/user/:username" [username] (user-profile username)))
Jump to Line
Something went wrong with that request. Please try again.