Skip to content
Browse files

New function user-or-openid.

Factors out #(or (:user %) (:openid %)) pattern.
  • Loading branch information...
1 parent ece51a5 commit c2ae8180a249810424f661106b1da13ac6b0c522 @chrismgray committed
Showing with 16 additions and 7 deletions.
  1. +2 −2 src/foreclojure/template.clj
  2. +5 −5 src/foreclojure/users.clj
  3. +9 −0 src/foreclojure/utils.clj
View
4 src/foreclojure/template.clj
@@ -3,7 +3,7 @@
(:use [hiccup.core :only [html]]
[hiccup.page-helpers :only [doctype javascript-tag link-to]]
[foreclojure.config :only [config repo-url]]
- [foreclojure.utils :only [page-attributes rendering-info login-url approver? can-submit?]]
+ [foreclojure.utils :only [page-attributes rendering-info login-url approver? can-submit? user-or-openid]]
[foreclojure.ring-utils :only [static-url]]
[foreclojure.version-utils :only [css js]]))
@@ -11,7 +11,7 @@
(defn html-doc [body]
(let [attrs (rendering-info (page-attributes body))
user (session/session-get :user)
- user (or (when (string? user) user) (:user user) (:openid user))]
+ user (user-or-openid user)]
(html
(doctype :html5)
[:html
View
10 src/foreclojure/users.clj
@@ -3,7 +3,7 @@
[clojure.string :as string]
[sandbar.stateful-session :as session]
[cheshire.core :as json])
- (:use [foreclojure.utils :only [from-mongo row-class rank-class get-user if-user with-user user-attribute]]
+ (:use [foreclojure.utils :only [from-mongo row-class rank-class get-user if-user with-user user-id user-email user-or-openid]]
[foreclojure.template :only [def-page content-page]]
[foreclojure.ring-utils :only [*http-scheme* universal-url]]
[foreclojure.config :only [config repo-url]]
@@ -36,7 +36,7 @@
(first
(reduce (fn [[user-list position rank] new-group]
[(into user-list
- (for [user (sort-by #(or (:user %) (:openid %)) new-group)]
+ (for [user (sort-by user-or-openid new-group)]
(into user {:rank rank
:position position})))
(inc position)
@@ -46,7 +46,7 @@
(defn get-top-100-and-current-user [username]
(let [ranked-users (get-ranked-users)
- this-user (first (filter (comp #{username} #(or (:user %) (:openid %)))
+ this-user (first (filter (comp #{username} user-or-openid)
ranked-users))
this-user-ranking (update-in this-user [:rank] #(str (or % "?") " out of " (count ranked-users)))]
{:user-ranking this-user-ranking
@@ -189,7 +189,7 @@
(filter ids (get-solved username)))))
(def-page user-profile [username]
- (let [page-title (str "User: " (if (string? username) username (:openid username)))
+ (let [page-title (str "User: " (user-or-openid username))
{user-id :_id email :email} (get-user username)]
{:title page-title
:content
@@ -292,7 +292,7 @@
(defn get-user-possible-openid [username]
(let [decoded-username (URLDecoder/decode username)]
(cond (get-user username) ; registered users don't need decoding
- username
+ {:user username}
(get-user {:openid decoded-username})
{:openid decoded-username}
:else
View
9 src/foreclojure/utils.clj
@@ -122,6 +122,15 @@
(walk/postwalk (to-fix float? int)
data))
+(defn user-or-openid
+ "Returns a string with the best representation of the username.
+ Prefers the registered username to the openid."
+ [user]
+ (or
+ (when (string? user) user)
+ (:user user)
+ (:openid user)))
+
(defn get-user [username]
(cond
(string? username)

0 comments on commit c2ae818

Please sign in to comment.
Something went wrong with that request. Please try again.