<commit_before>(ns clojurelive-web.actions
  (:require [clojurelive-web.data.reset-password :as reset-password]
            [clojurelive-web.data.topic :as topic]
            [clojurelive-web.data.user :as user]
            [clojurelive-web.mailer :as mailer]
            [clojurelive-web.view.login :as login-view]
            [clojurelive-web.view.signup :as signup-view]
            [clj-uuid :as uuid]
            [digest :as digest]
            [ring.util.response :as ring-response]))

(defn signup [req]
  (let [params (:params req)
        session (:session req)
        r (user/create params)]
    (if (:errors r)
      (signup-view/show session r)
      (let [session (assoc session :clojurelive/username (:username params))]
        (-> (ring-response/redirect "/")
            (assoc :session session))))))

(defn login [req]
  (let [params (:params req)
        session (:session req)
        authenticated? (user/authenticate (:username params) (:password params))]
    (if authenticated?
      (let [session (assoc session :clojurelive/username (:username params))]
        (-> (ring-response/redirect "/")
            (assoc :session session)))
      (login-view/show session {:errors "invalid username or password"}))))

(defn logout [req]
  (-> (ring-response/redirect "/")
      (assoc :session nil)))

(defn for-topic [req]
  (topic/for-uuid (:uuid (:params req))))

(defn newest-topics []
  (topic/newest 0 25))

(defn new-link [req]
  (let [topic (first (topic/create (:clojurelive/username (:session req))
                                   {:title (:title (:params req))
                                    :content (:content (:params req))
                                    :type "link"}))]
    (ring-response/redirect (str "/t/" (:uuid topic)) :see-other)))

(defn new-text [req]
  (let [topic (first (topic/create (:clojurelive/username (:session req))
                                   {:title (:title (:params req))
                                    :content (:content (:params req))
                                    :type "text"}))]
    (ring-response/redirect (str "/t/" (:uuid topic)) :see-other)))

(defn send-reset-password-email [req]
  (let [email (:email (:params req))
        token (uuid/v1)]
    (reset-password/create email token)
    (mailer/reset-password email token)
    (ring-response/redirect "/")))

(defn reset-password [req]
  (reset-password/update (:uuid (:params req)) (:password (:params req)))
  (ring-response/redirect "/login"))<commit_msg>redirect to login if unauthorized creation of topic<commit_after>(ns clojurelive-web.actions
  (:require [clojurelive-web.data.reset-password :as reset-password]
            [clojurelive-web.data.topic :as topic]
            [clojurelive-web.data.user :as user]
            [clojurelive-web.mailer :as mailer]
            [clojurelive-web.view.login :as login-view]
            [clojurelive-web.view.signup :as signup-view]
            [clj-uuid :as uuid]
            [digest :as digest]
            [ring.util.response :as ring-response]))

(defn signup [req]
  (let [params (:params req)
        session (:session req)
        r (user/create params)]
    (if (:errors r)
      (signup-view/show session r)
      (let [session (assoc session :clojurelive/username (:username params))]
        (-> (ring-response/redirect "/")
            (assoc :session session))))))

(defn login [req]
  (let [params (:params req)
        session (:session req)
        authenticated? (user/authenticate (:username params) (:password params))]
    (if authenticated?
      (let [session (assoc session :clojurelive/username (:username params))]
        (-> (ring-response/redirect "/")
            (assoc :session session)))
      (login-view/show session {:errors "invalid username or password"}))))

(defn logout [req]
  (-> (ring-response/redirect "/")
      (assoc :session nil)))

(defn for-topic [req]
  (topic/for-uuid (:uuid (:params req))))

(defn newest-topics []
  (topic/newest 0 25))

(defn new-link [req]
  (if (empty? (:clojurelive/username (:session req)))
    (ring-response/redirect "/login")
    (let [topic (first (topic/create (:clojurelive/username (:session req))
                                     {:title (:title (:params req))
                                      :content (:content (:params req))
                                      :type "link"}))]
      (ring-response/redirect (str "/t/" (:uuid topic)) :see-other))))

(defn new-text [req]
  (if (empty? (:clojurelive/username (:session req)))
    (ring-response/redirect "/login")
    (let [topic (first (topic/create (:clojurelive/username (:session req))
                                     {:title (:title (:params req))
                                      :content (:content (:params req))
                                      :type "text"}))]
      (ring-response/redirect (str "/t/" (:uuid topic)) :see-other))))

(defn send-reset-password-email [req]
  (let [email (:email (:params req))
        token (uuid/v1)]
    (reset-password/create email token)
    (mailer/reset-password email token)
    (ring-response/redirect "/")))

(defn reset-password [req]
  (reset-password/update (:uuid (:params req)) (:password (:params req)))
  (ring-response/redirect "/login"))
{'fraction_non_alphanumeric': 0.15492957746478872, 'fraction_numerical': 0.0015226494099733537, 'mean_word_length': 2.2926065162907268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7534663', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1553, 'n_words': 412}