                        {$set {:random_point [(rand) 0]}}))
     result))
 
+(defn get-phrase-for-auth [user-id]
+  "Get a random phrase for user authentication."
+  (get-phrase {:users user-id :usersCount {$gt 5}}))
+
+(defn get-phrase-for-training [user-id]
+  "Get a random phrase for user training."
+  (get-phrase {:users {$ne user-id}}))
+
+
 (defn get-training-data
   "Returns cadences to be used to train a classifier as a tuple of bad cadences
   and good cadences."

  "Get the current authenticaion map of the signed in user. This is effectively
  the document from mongodb.

      EXAMPLE:

          {:_id ObjectId(\"4fbe571a593e1633b6dfa6ad\"
           :username \"Johnny\"
           :name \"John Doe\"
           :email \"johnny@example.com\"}"
  [] ((:authentications friend/*identity*) (:current friend/*identity*)))

(defn get-phrase
  "Find a phrase for the given user. If the seconf argument is true find one
  they have already done training for. If it is false find one for which the
  user is untrained."
  [user-id for-auth?]
  (let [result (mc/find-one-as-map "phrases"
                                   (if for-auth?
                                     {:users user-id
                                      :usersCount {$gt 5}
                                      :random_point {"$near" [(rand) 0]}}
                                     {:users {$ne user-id}
                                      :random_point {"$near" [(rand) 0]}})
                                   {:phrase 1})]
    ; Changes the random_point for increased randomlyishness.
    (when (not (nil? result))

  (let [find-cadences (fn [by-user lim]
                        (mq/with-collection "cadences"
                          (mq/find {:phrase phrase
                                    :user_id (if by-user
                                               user-id
                                               {$ne user-id})
                                    :random_point {"$near" [(rand) 0]}})
                          (mq/fields [:timeline :phrase])
                          (mq/limit lim)
                          (mq/snapshot)))]
