<commit_before>(ns braid.server.routes.client
  (:require [compojure.core :refer [GET defroutes]]
            [compojure.coercions :refer [as-uuid]]
            [compojure.route :refer [resources]]
            [clostache.parser :as clostache]
            [braid.server.conf :refer [config]]
            [braid.server.digest :as digest]
            [braid.server.db :as db]
            [braid.server.invite :as invites]))

(defn get-html [client]
  (clostache/render-resource
    (str "public/" client ".html")
    {:algo "sha256"
     :js (str (digest/from-file (str "public/js/" client "/out/braid.js")))
     :api_domain (config :api-domain)}))

(defroutes desktop-client-routes
  ; public group page
  (GET "/group/:group-name" [group-name :as req]
    (if-let [group (db/public-group-with-name group-name)]
      (clostache/render-resource "templates/public_group_desktop.html.mustache"
                                 {:group-name (group :name)
                                  :group-id (group :id)
                                  :api-domain (config :api-domain)})
      {:status 403
       :headers {"Content-Type" "text/plain"}
       :body "No such public group"}))

  ; invite accept page
  (GET "/accept" [invite :<< as-uuid tok]
    (if (and invite tok)
      (if-let [invite (db/invite-by-id invite)]
        {:status 200 :headers {"Content-Type" "text/html"} :body (invites/register-page invite tok)}
        {:status 400 :headers {"Content-Type" "text/plain"} :body "Invalid invite"})
      {:status 400 :headers {"Content-Type" "text/plain"} :body "Bad invite link, sorry"}))

  ; invite link
  (GET "/invite" [group-id :<< as-uuid nonce expiry mac]
    (if (and group-id nonce expiry mac)
      (if (invites/verify-hmac mac (str nonce group-id expiry))
        {:status 200 :headers {"Content-Type" "text/html"} :body (invites/link-signup-page group-id)}
        {:status 400 :headers {"Content-Type" "text/plain"} :body "Parameter verification failed"})
      {:status 400 :headers {"Content-Type" "text/plain"} :body "Missing required parameters"}))

  ; password reset page
  (GET "/reset" [user :<< as-uuid token :as req]
    (if-let [u (and user token
                 (invites/verify-reset-nonce {:id user} token)
                 (db/user-by-id user))]
      {:status 200
       :headers {"Content-Type" "text/html"}
       :body (invites/reset-page u token)}
      {:status 401
       :headers {"Content-Type" "text/plain"}
       :body "Bad user or token"}))

  ; everything else
  (GET "/*" []
    (get-html "desktop")))

(defroutes mobile-client-routes
  ; TODO: add mobile routse for public joining & password resets
  (GET "/*" []
    (get-html "mobile")))

(defroutes resource-routes
  (resources "/"))
<commit_msg>add test oauth endpoint for dev purposes<commit_after>(ns braid.server.routes.client
  (:require [compojure.core :refer [GET defroutes]]
            [compojure.coercions :refer [as-uuid]]
            [compojure.route :refer [resources]]
            [clostache.parser :as clostache]
            [braid.server.conf :refer [config]]
            [braid.server.digest :as digest]
            [braid.server.db :as db]
            [braid.server.invite :as invites]))

(defn get-html [client]
  (clostache/render-resource
    (str "public/" client ".html")
    {:algo "sha256"
     :js (str (digest/from-file (str "public/js/" client "/out/braid.js")))
     :api_domain (config :api-domain)}))

(defroutes desktop-client-routes
  ; public group page
  (GET "/group/:group-name" [group-name :as req]
    (if-let [group (db/public-group-with-name group-name)]
      (clostache/render-resource "templates/public_group_desktop.html.mustache"
                                 {:group-name (group :name)
                                  :group-id (group :id)
                                  :api-domain (config :api-domain)})
      {:status 403
       :headers {"Content-Type" "text/plain"}
       :body "No such public group"}))

  ; invite accept page
  (GET "/accept" [invite :<< as-uuid tok]
    (if (and invite tok)
      (if-let [invite (db/invite-by-id invite)]
        {:status 200 :headers {"Content-Type" "text/html"} :body (invites/register-page invite tok)}
        {:status 400 :headers {"Content-Type" "text/plain"} :body "Invalid invite"})
      {:status 400 :headers {"Content-Type" "text/plain"} :body "Bad invite link, sorry"}))

  ; invite link
  (GET "/invite" [group-id :<< as-uuid nonce expiry mac]
    (if (and group-id nonce expiry mac)
      (if (invites/verify-hmac mac (str nonce group-id expiry))
        {:status 200 :headers {"Content-Type" "text/html"} :body (invites/link-signup-page group-id)}
        {:status 400 :headers {"Content-Type" "text/plain"} :body "Parameter verification failed"})
      {:status 400 :headers {"Content-Type" "text/plain"} :body "Missing required parameters"}))

  ; password reset page
  (GET "/reset" [user :<< as-uuid token :as req]
    (if-let [u (and user token
                 (invites/verify-reset-nonce {:id user} token)
                 (db/user-by-id user))]
      {:status 200
       :headers {"Content-Type" "text/html"}
       :body (invites/reset-page u token)}
      {:status 401
       :headers {"Content-Type" "text/plain"}
       :body "Bad user or token"}))

  ; OAuth redirect
  (GET "/oauth/github" [code state :as req]
    (println "GITHUB OAUTH" (pr-str code) (pr-str state))
    )

  ; everything else
  (GET "/*" []
    (get-html "desktop")))

(defroutes mobile-client-routes
  ; TODO: add mobile routse for public joining & password resets
  (GET "/*" []
    (get-html "mobile")))

(defroutes resource-routes
  (resources "/"))

{'fraction_non_alphanumeric': 0.1552059395439279, 'fraction_numerical': 0.01060632844263744, 'mean_word_length': 2.5428929242329366, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2472443', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1824, 'n_words': 493}