♻️ Refactor internal handling of profile props.

This commit is contained in:
Andrey Antukh 2021-10-05 15:58:45 +02:00 committed by Andrés Moya
parent d36bf188ae
commit 9ecbddc18c
4 changed files with 36 additions and 42 deletions

View file

@ -62,6 +62,13 @@
:cause e) :cause e)
nil))) nil)))
(defn- qualify-props
[provider props]
(reduce-kv (fn [result k v]
(assoc result (keyword (:name provider) (name k)) v))
{}
props))
(defn- retrieve-user-info (defn- retrieve-user-info
[{:keys [provider] :as cfg} tdata] [{:keys [provider] :as cfg} tdata]
(try (try
@ -76,8 +83,8 @@
{:backend (:name provider) {:backend (:name provider)
:email (:email info) :email (:email info)
:fullname (:name info) :fullname (:name info)
:props (dissoc info :name :email)}))) :props (->> (dissoc info :name :email)
(qualify-props provider))})))
(catch Exception e (catch Exception e
(l/error :hint "unexpected exception on retrieve-user-info" (l/error :hint "unexpected exception on retrieve-user-info"
:cause e) :cause e)
@ -138,15 +145,14 @@
;; --- HTTP HANDLERS ;; --- HTTP HANDLERS
(defn extract-props (defn extract-utm-props
"Extracts additional data from user params."
[params] [params]
(reduce-kv (fn [params k v] (reduce-kv (fn [params k v]
(let [sk (name k)] (let [sk (name k)]
(cond-> params (cond-> params
(or (str/starts-with? sk "pm_") (str/starts-with? sk "utm_")
(str/starts-with? sk "pm-") (assoc (->> sk str/kebab (keyword "penpot")) v))))
(str/starts-with? sk "utm_"))
(assoc (-> sk str/kebab keyword) v))))
{} {}
params)) params))
@ -210,7 +216,7 @@
(defn- auth-handler (defn- auth-handler
[{:keys [tokens] :as cfg} {:keys [params] :as request}] [{:keys [tokens] :as cfg} {:keys [params] :as request}]
(let [invitation (:invitation-token params) (let [invitation (:invitation-token params)
props (extract-props params) props (extract-utm-props params)
state (tokens :generate state (tokens :generate
{:iss :oauth {:iss :oauth
:invitation-token invitation :invitation-token invitation

View file

@ -12,7 +12,7 @@
[app.config :as cf] [app.config :as cf]
[app.db :as db] [app.db :as db]
[app.emails :as eml] [app.emails :as eml]
[app.http.oauth :refer [extract-props]] [app.http.oauth :refer [extract-utm-props]]
[app.loggers.audit :as audit] [app.loggers.audit :as audit]
[app.media :as media] [app.media :as media]
[app.metrics :as mtx] [app.metrics :as mtx]
@ -127,23 +127,16 @@
;; --- MUTATION: Register Profile ;; --- MUTATION: Register Profile
(s/def ::accept-terms-and-privacy ::us/boolean) (s/def ::accept-terms-and-privacy ::us/boolean)
(s/def ::accept-newsletter-subscription ::us/boolean)
(s/def ::token ::us/not-empty-string) (s/def ::token ::us/not-empty-string)
(s/def ::register-profile (s/def ::register-profile
(s/keys :req-un [::token ::fullname (s/keys :req-un [::token ::fullname]))
::accept-terms-and-privacy]
:opt-un [::accept-newsletter-subscription]))
(sv/defmethod ::register-profile {:auth false :rlimit :password} (sv/defmethod ::register-profile {:auth false :rlimit :password}
[{:keys [pool] :as cfg} params] [{:keys [pool] :as cfg} params]
(when-not (:accept-terms-and-privacy params)
(ex/raise :type :validation
:code :invalid-terms-and-privacy))
(db/with-atomic [conn pool] (db/with-atomic [conn pool]
(let [cfg (assoc cfg :conn conn)] (-> (assoc cfg :conn conn)
(register-profile cfg params)))) (register-profile params))))
(defn- annotate-profile-register (defn- annotate-profile-register
"A helper for properly increase the profile-register metric once the "A helper for properly increase the profile-register metric once the
@ -162,6 +155,7 @@
(create-profile conn) (create-profile conn)
(create-profile-relations conn) (create-profile-relations conn)
(decode-profile-row))] (decode-profile-row))]
(sid/load-initial-project! conn profile) (sid/load-initial-project! conn profile)
(cond (cond
@ -223,18 +217,17 @@
[conn params] [conn params]
(let [id (or (:id params) (uuid/next)) (let [id (or (:id params) (uuid/next))
props (-> (extract-props params) props (-> (extract-utm-props params)
(merge (:props params)) (merge (:props params))
(assoc :accept-terms-and-privacy (:accept-terms-and-privacy params true))
(assoc :accept-newsletter-subscription (:accept-newsletter-subscription params false))
(db/tjson)) (db/tjson))
password (if-let [password (:password params)] password (if-let [password (:password params)]
(derive-password password) (derive-password password)
"!") "!")
locale (as-> (:locale params) locale locale (:locale params)
(and (string? locale) (not (str/blank? locale)) locale)) locale (when (and (string? locale) (not (str/blank? locale)))
locale)
backend (:backend params "penpot") backend (:backend params "penpot")
is-demo (:is-demo params false) is-demo (:is-demo params false)
@ -591,11 +584,15 @@
(db/with-atomic [conn pool] (db/with-atomic [conn pool]
(let [profile (profile/retrieve-profile-data conn profile-id) (let [profile (profile/retrieve-profile-data conn profile-id)
props (reduce-kv (fn [props k v] props (reduce-kv (fn [props k v]
;; We don't accept namespaced keys
(if (simple-ident? k)
(if (nil? v) (if (nil? v)
(dissoc props k) (dissoc props k)
(assoc props k v))) (assoc props k v))
props))
(:props profile) (:props profile)
props)] props)]
(db/update! conn :profile (db/update! conn :profile
{:props (db/tjson props)} {:props (db/tjson props)}
{:id profile-id}) {:id profile-id})

View file

@ -70,6 +70,10 @@
[conn profile] [conn profile]
(merge profile (retrieve-additional-data conn (:id profile)))) (merge profile (retrieve-additional-data conn (:id profile))))
(defn- filter-profile-props
[props]
(into {} (filter (fn [[k _]] (simple-ident? k))) props))
(defn decode-profile-row (defn decode-profile-row
[{:keys [props] :as row}] [{:keys [props] :as row}]
(cond-> row (cond-> row
@ -90,7 +94,7 @@
(ex/raise :type :not-found (ex/raise :type :not-found
:hint "Object doest not exists.")) :hint "Object doest not exists."))
profile)) (update profile :props filter-profile-props)))
(def ^:private sql:profile-by-email (def ^:private sql:profile-by-email
"select p.* from profile as p "select p.* from profile as p

View file

@ -177,17 +177,6 @@
(t/is (string? token)) (t/is (string? token))
;; try register without accepting terms
(let [data {::th/type :register-profile
:token token
:fullname "foobar"
:accept-terms-and-privacy false}
out (th/mutation! data)]
(let [error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :validation))
(t/is (th/ex-of-code? error :invalid-terms-and-privacy))))
;; try register without token ;; try register without token
(let [data {::th/type :register-profile (let [data {::th/type :register-profile
:fullname "foobar" :fullname "foobar"
@ -205,9 +194,7 @@
:accept-terms-and-privacy true :accept-terms-and-privacy true
:accept-newsletter-subscription true}] :accept-newsletter-subscription true}]
(let [{:keys [result error]} (th/mutation! data)] (let [{:keys [result error]} (th/mutation! data)]
(t/is (nil? error)) (t/is (nil? error))))
(t/is (true? (get-in result [:props :accept-newsletter-subscription])))
(t/is (true? (get-in result [:props :accept-terms-and-privacy])))))
)) ))
(t/deftest prepare-register-with-registration-disabled (t/deftest prepare-register-with-registration-disabled