Fetching contributors…
Cannot retrieve contributors at this time
125 lines (109 sloc) 3.83 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]
(let [page-title (str "User: " username)]
{:title page-title
[:h2 page-title]
(for [difficulty ["Elementary" "Easy" "Medium" "Hard"]]
(let [solved (count (get-solved username difficulty))
total (count (get-problems difficulty))]
[:td.count-label difficulty]
{:style (str "width: "
(int (* 100 (/ solved total)))
[:td.count-total "TOTAL:" ]
(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)))