<commit_before>(ns clojars.web.user
  (:require [clojars.db :as db :refer [find-user group-membernames add-user
                                reserved-names update-user jars-by-username
                                find-groupnames find-user-by-user-or-email]]
            [clojars.web.common :refer [html-doc error-list jar-link
                                        flash group-link]]
            [clojars.config :refer [config]]
            [clojure.string :refer [blank?]]
            [hiccup.element :refer [link-to unordered-list]]
            [hiccup.form :refer [label text-field
                                 password-field text-area
                                 submit-button
                                 email-field]]
            [clojars.web.safe-hiccup :refer [form-to]]
            [ring.util.response :refer [response redirect]]
            [valip.core :refer [validate]]
            [valip.predicates :as pred]))

(defn register-form [{:keys [errors email username]}]
  (html-doc "Register" {}
            [:div.small-section
             [:h1 "Register"]
             (error-list errors)
             (form-to [:post "/register"]
                      (label :email "Email")
                      (email-field {:value email
                                    :required true
                                    :placeholder "<EMAIL>"}
                                   :email)
                      (label :username "Username")
                      (text-field {:value username
                                   :required true
                                   :placeholder "bob"}
                                  :username)
                      (label :password "Password")
                      (password-field {:placeholder "keep it secret, keep it safe"
                                       :required true}
                                      :password)
                      (label :confirm "Confirm password")
                      (password-field {:placeholder "confirm your password"
                                       :required true}
                                      :confirm)
                      (submit-button "Register"))]))

(defn conj-when [coll test x]
  (if test
    (conj coll x)
    coll))

;; Validations

(defn password-validations [confirm]
  [[:password #(<= 8 (count %)) "Password must be 8 characters or longer"]
   [:password #(= % confirm) "Password and confirm password must match"]])

(defn user-validations []
  [[:email pred/present? "Email can't be blank"]
   [:email #(re-matches #".+@.+" %) "Email must have an @ sign and a domain"]
   [:username #(re-matches #"[a-z0-9_-]+" %)
    (str "Username must consist only of lowercase "
         "letters, numbers, hyphens and underscores.")]
   [:username pred/present? "Username can't be blank"]])

(defn new-user-validations [db confirm]
  (concat [[:password pred/present? "Password can't be blank"]
           [:username #(not (or (reserved-names %)
                                (find-user db %)
                                (seq (group-membernames db %))))
            "Username is already taken"]]
          (user-validations)
          (password-validations confirm)))

(defn reset-password-validations [db confirm]
  (concat
    [[:reset-code #(or (blank? %) (db/find-user-by-password-reset-code db %)) "The reset code does not exist or it has expired."]
     [:reset-code pred/present? "Reset code can't be blank."]]
    (password-validations confirm)))

;;

(defn profile-form [account user flash-msg & [errors]]
  (html-doc "Profile" {:account account}
            [:div.small-section
             (flash flash-msg)
             [:h1 "Profile"]
             (error-list errors)
             (form-to [:post "/profile"]
                      (label :email "Email")<commit_msg>Fix #605 by clarifying profile page (#606)<commit_after>(ns clojars.web.user
  (:require [clojars.db :as db :refer [find-user group-membernames add-user
                                reserved-names update-user jars-by-username
                                find-groupnames find-user-by-user-or-email]]
            [clojars.web.common :refer [html-doc error-list jar-link
                                        flash group-link]]
            [clojars.config :refer [config]]
            [clojure.string :refer [blank?]]
            [hiccup.element :refer [link-to unordered-list]]
            [hiccup.form :refer [label text-field
                                 password-field text-area
                                 submit-button
                                 email-field]]
            [clojars.web.safe-hiccup :refer [form-to]]
            [ring.util.response :refer [response redirect]]
            [valip.core :refer [validate]]
            [valip.predicates :as pred]))

(defn register-form [{:keys [errors email username]}]
  (html-doc "Register" {}
            [:div.small-section
             [:h1 "Register"]
             (error-list errors)
             (form-to [:post "/register"]
                      (label :email "Email")
                      (email-field {:value email
                                    :required true
                                    :placeholder "<EMAIL>"}
                                   :email)
                      (label :username "Username")
                      (text-field {:value username
                                   :required true
                                   :placeholder "bob"}
                                  :username)
                      (label :password "Password")
                      (password-field {:placeholder "keep it secret, keep it safe"
                                       :required true}
                                      :password)
                      (label :confirm "Confirm password")
                      (password-field {:placeholder "confirm your password"
                                       :required true}
                                      :confirm)
                      (submit-button "Register"))]))

(defn conj-when [coll test x]
  (if test
    (conj coll x)
    coll))

;; Validations

(defn password-validations [confirm]
  [[:password #(<= 8 (count %)) "Password must be 8 characters or longer"]
   [:password #(= % confirm) "Password and confirm password must match"]])

(defn user-validations []
  [[:email pred/present? "Email can't be blank"]
   [:email #(re-matches #".+@.+" %) "Email must have an @ sign and a domain"]
   [:username #(re-matches #"[a-z0-9_-]+" %)
    (str "Username must consist only of lowercase "
         "letters, numbers, hyphens and underscores.")]
   [:username pred/present? "Username can't be blank"]])

(defn new-user-validations [db confirm]
  (concat [[:password pred/present? "Password can't be blank"]
           [:username #(not (or (reserved-names %)
                                (find-user db %)
                                (seq (group-membernames db %))))
            "Username is already taken"]]
          (user-validations)
          (password-validations confirm)))

(defn reset-password-validations [db confirm]
  (concat
    [[:reset-code #(or (blank? %) (db/find-user-by-password-reset-code db %)) "The reset code does not exist or it has expired."]
     [:reset-code pred/present? "Reset code can't be blank."]]
    (password-validations confirm)))

;;

(defn profile-form [account user flash-msg & [errors]]
  (html-doc "Profile" {:account account}
            [:div.small-section
             (flash flash-msg)
             [:h1 "Profile"]
             (error-list errors)
             [:p "Your Clojars profile is just your email address and your password. You can change them here, if want."]
             (form-to [:post "/profile"]
                      (label :email "Email")
{'fraction_non_alphanumeric': 0.11847672778561354, 'fraction_numerical': 0.002307988203615848, 'mean_word_length': 1.4459078080903105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4005205', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1954, 'n_words': 564}