mirror of
https://github.com/penpot/penpot.git
synced 2025-01-06 14:50:20 -05:00
WIP: adapt welcome file setup to file-update refactor
This commit is contained in:
parent
34373189b4
commit
aeac70c942
5 changed files with 77 additions and 86 deletions
|
@ -12,6 +12,7 @@
|
||||||
[app.common.data.macros :as dm]
|
[app.common.data.macros :as dm]
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[app.common.logging :as l]
|
[app.common.logging :as l]
|
||||||
|
[app.common.schema :as sm]
|
||||||
[app.common.spec :as us]
|
[app.common.spec :as us]
|
||||||
[app.common.uri :as u]
|
[app.common.uri :as u]
|
||||||
[app.config :as cf]
|
[app.config :as cf]
|
||||||
|
@ -607,17 +608,30 @@
|
||||||
nil
|
nil
|
||||||
session-id))))
|
session-id))))
|
||||||
|
|
||||||
|
(def ^:private schema:auth-params
|
||||||
|
[:map {:title "auth-params"}
|
||||||
|
[:invitation-token {:optional true} :string]
|
||||||
|
[:create-welcome-file {:optional true} :boolean]])
|
||||||
|
|
||||||
|
;; FIXME: this should be changed, on develop sm/default-transformer
|
||||||
|
;; becomes sm/json-transformer and :boolean schema type should be
|
||||||
|
;; replaced with ::sm/boolean for proper and more flexible decoding
|
||||||
|
;; mechanism.
|
||||||
|
(def ^:private decode-auth-params
|
||||||
|
(sm/decoder schema:auth-params sm/default-transformer))
|
||||||
|
|
||||||
(defn- auth-handler
|
(defn- auth-handler
|
||||||
[cfg {:keys [params] :as request}]
|
[cfg {:keys [params] :as request}]
|
||||||
(let [props (audit/extract-utm-params params)
|
(let [props (audit/extract-utm-params params)
|
||||||
|
params (decode-auth-params params)
|
||||||
esid (rpc/get-external-session-id request)
|
esid (rpc/get-external-session-id request)
|
||||||
params {:iss :oauth
|
claims {:iss :oauth
|
||||||
:invitation-token (:invitation-token params)
|
:invitation-token (:invitation-token params)
|
||||||
|
:create-welcome-file (:create-welcome-file params)
|
||||||
:external-session-id esid
|
:external-session-id esid
|
||||||
:props props
|
:props props
|
||||||
:exp (dt/in-future "4h")}
|
:exp (dt/in-future "4h")}
|
||||||
state (tokens/generate (::setup/props cfg)
|
state (tokens/generate (::setup/props cfg) (d/without-nils claims))
|
||||||
(d/without-nils params))
|
|
||||||
uri (build-auth-uri cfg state)]
|
uri (build-auth-uri cfg state)]
|
||||||
{::rres/status 200
|
{::rres/status 200
|
||||||
::rres/body {:redirect-uri uri}}))
|
::rres/body {:redirect-uri uri}}))
|
||||||
|
|
|
@ -27,11 +27,11 @@
|
||||||
[app.rpc.doc :as-alias doc]
|
[app.rpc.doc :as-alias doc]
|
||||||
[app.rpc.helpers :as rph]
|
[app.rpc.helpers :as rph]
|
||||||
[app.setup :as-alias setup]
|
[app.setup :as-alias setup]
|
||||||
[app.setup.welcome-file :as welcome-file]
|
[app.setup.welcome-file :refer [create-welcome-file]]
|
||||||
[app.tokens :as tokens]
|
[app.tokens :as tokens]
|
||||||
[app.util.services :as sv]
|
[app.util.services :as sv]
|
||||||
[app.util.time :as dt]
|
[app.util.time :as dt]
|
||||||
[app.worker :as-alias wrk]
|
[app.worker :as wrk]
|
||||||
[cuerdas.core :as str]))
|
[cuerdas.core :as str]))
|
||||||
|
|
||||||
(def schema:password
|
(def schema:password
|
||||||
|
@ -243,6 +243,7 @@
|
||||||
|
|
||||||
params (d/without-nils params)
|
params (d/without-nils params)
|
||||||
token (tokens/generate (::setup/props cfg) params)]
|
token (tokens/generate (::setup/props cfg) params)]
|
||||||
|
|
||||||
(with-meta {:token token}
|
(with-meta {:token token}
|
||||||
{::audit/profile-id uuid/zero})))
|
{::audit/profile-id uuid/zero})))
|
||||||
|
|
||||||
|
@ -352,7 +353,7 @@
|
||||||
:extra-data ptoken})))
|
:extra-data ptoken})))
|
||||||
|
|
||||||
(defn register-profile
|
(defn register-profile
|
||||||
[{:keys [::db/conn] :as cfg} {:keys [token fullname theme welcome-file] :as params}]
|
[{:keys [::db/conn ::wrk/executor] :as cfg} {:keys [token fullname theme] :as params}]
|
||||||
(let [theme (when (= theme "light") theme)
|
(let [theme (when (= theme "light") theme)
|
||||||
claims (tokens/verify (::setup/props cfg) {:token token :iss :prepared-register})
|
claims (tokens/verify (::setup/props cfg) {:token token :iss :prepared-register})
|
||||||
params (-> claims
|
params (-> claims
|
||||||
|
@ -385,8 +386,10 @@
|
||||||
props (audit/profile->props profile)
|
props (audit/profile->props profile)
|
||||||
|
|
||||||
create-welcome-file-when-needed
|
create-welcome-file-when-needed
|
||||||
(when (some? welcome-file)
|
(fn []
|
||||||
(partial welcome-file/create-welcome-file cfg profile))]
|
(when (:create-welcome-file params)
|
||||||
|
(let [cfg (dissoc cfg ::db/conn)]
|
||||||
|
(wrk/submit! executor (partial create-welcome-file cfg profile)))))]
|
||||||
|
|
||||||
(cond
|
(cond
|
||||||
;; When profile is blocked, we just ignore it and return plain data
|
;; When profile is blocked, we just ignore it and return plain data
|
||||||
|
@ -424,21 +427,22 @@
|
||||||
(if (:is-active profile)
|
(if (:is-active profile)
|
||||||
(-> (profile/strip-private-attrs profile)
|
(-> (profile/strip-private-attrs profile)
|
||||||
(rph/with-transform (session/create-fn cfg (:id profile)))
|
(rph/with-transform (session/create-fn cfg (:id profile)))
|
||||||
|
(rph/with-defer create-welcome-file-when-needed)
|
||||||
(rph/with-meta
|
(rph/with-meta
|
||||||
{::audit/replace-props props
|
{::audit/replace-props props
|
||||||
::audit/context {:action "login"}
|
::audit/context {:action "login"}
|
||||||
::audit/profile-id (:id profile)
|
::audit/profile-id (:id profile)}))
|
||||||
::before-complete-fns [create-welcome-file-when-needed]}))
|
|
||||||
|
|
||||||
(do
|
(do
|
||||||
(when-not (eml/has-reports? conn (:email profile))
|
(when-not (eml/has-reports? conn (:email profile))
|
||||||
(send-email-verification! cfg profile))
|
(send-email-verification! cfg profile))
|
||||||
|
|
||||||
(rph/with-meta {:email (:email profile)}
|
(-> {:email (:email profile)}
|
||||||
{::audit/replace-props props
|
(rph/with-defer create-welcome-file-when-needed)
|
||||||
::audit/context {:action "email-verification"}
|
(rph/with-meta
|
||||||
::audit/profile-id (:id profile)
|
{::audit/replace-props props
|
||||||
::rpc/before-complete-fns [create-welcome-file-when-needed]})))
|
::audit/context {:action "email-verification"}
|
||||||
|
::audit/profile-id (:id profile)}))))
|
||||||
|
|
||||||
:else
|
:else
|
||||||
(let [elapsed? (elapsed-verify-threshold? profile)
|
(let [elapsed? (elapsed-verify-threshold? profile)
|
||||||
|
@ -471,7 +475,7 @@
|
||||||
[:token schema:token]
|
[:token schema:token]
|
||||||
[:fullname [::sm/word-string {:max 100}]]
|
[:fullname [::sm/word-string {:max 100}]]
|
||||||
[:theme {:optional true} [:string {:max 10}]]
|
[:theme {:optional true} [:string {:max 10}]]
|
||||||
[:welcome-file {:optional true} [:boolean]]])
|
[:create-welcome-file {:optional true} :boolean]])
|
||||||
|
|
||||||
(sv/defmethod ::register-profile
|
(sv/defmethod ::register-profile
|
||||||
{::rpc/auth false
|
{::rpc/auth false
|
||||||
|
@ -554,5 +558,3 @@
|
||||||
::sm/params schema:request-profile-recovery}
|
::sm/params schema:request-profile-recovery}
|
||||||
[cfg params]
|
[cfg params]
|
||||||
(db/tx-run! cfg request-profile-recovery params))
|
(db/tx-run! cfg request-profile-recovery params))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,60 +7,26 @@
|
||||||
(ns app.setup.welcome-file
|
(ns app.setup.welcome-file
|
||||||
(:require
|
(:require
|
||||||
[app.common.logging :as l]
|
[app.common.logging :as l]
|
||||||
[app.common.types.pages-list :as ctpl]
|
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
[app.features.fdata :as feat.fdata]
|
[app.rpc.commands.files-update :as fupdate]
|
||||||
[app.rpc :as-alias rpc]
|
|
||||||
[app.rpc.climit :as-alias climit]
|
|
||||||
[app.rpc.commands.management :as management]
|
[app.rpc.commands.management :as management]
|
||||||
[app.rpc.commands.profile :as profile]
|
[app.rpc.commands.profile :as profile]
|
||||||
[app.rpc.doc :as-alias doc]
|
[app.setup.templates :as tmpl]))
|
||||||
[app.setup :as-alias setup]
|
|
||||||
[app.setup.templates :as tmpl]
|
|
||||||
[app.util.blob :as blob]
|
|
||||||
[app.util.pointer-map :as pmap]
|
|
||||||
[app.worker :as-alias wrk]))
|
|
||||||
|
|
||||||
(defn- decode-row
|
(def ^:private page-id #uuid "2c6952ee-d00e-8160-8004-d2250b7210cb")
|
||||||
"A generic decode row helper"
|
(def ^:private shape-id #uuid "765e9f82-c44e-802e-8004-d72a10b7b445")
|
||||||
[{:keys [data features] :as row}]
|
|
||||||
(cond-> row
|
|
||||||
features (assoc :features (db/decode-pgarray features #{}))
|
|
||||||
data (assoc :data (blob/decode data))))
|
|
||||||
|
|
||||||
|
(def ^:private update-path
|
||||||
|
[:pages-index page-id :objects shape-id
|
||||||
|
:content :children 0 :children 0 :children 0])
|
||||||
|
|
||||||
(defn- update-welcome-text
|
(defn- update-welcome-shape
|
||||||
[conn file name]
|
[_ file name]
|
||||||
(let [page-id #uuid "2c6952ee-d00e-8160-8004-d2250b7210cb"
|
(let [text (str "Welcome to Penpot, " name "!")]
|
||||||
text-id #uuid "765e9f82-c44e-802e-8004-d72a10b7b445"
|
(update file :data update-in update-path assoc :text text)))
|
||||||
fdata (:data file)
|
|
||||||
page (ctpl/get-page fdata page-id)
|
|
||||||
objects (:objects page)
|
|
||||||
text (-> (get objects text-id)
|
|
||||||
(assoc :name "Welcome to Penpot" :position-data nil)
|
|
||||||
(assoc-in [:content :children 0 :children 0 :children 0 :text] (str "Welcome to Penpot, " name "!")))
|
|
||||||
|
|
||||||
fdata (assoc-in fdata [:pages-index page-id :objects text-id] text)]
|
|
||||||
|
|
||||||
(db/update! conn :file
|
|
||||||
{:data (blob/encode fdata)}
|
|
||||||
{:id (:id file)})))
|
|
||||||
|
|
||||||
|
|
||||||
(defn- update-welcome-file
|
|
||||||
[{:keys [::db/conn] :as cfg} file-id name]
|
|
||||||
(binding [pmap/*load-fn* (partial feat.fdata/load-pointer cfg file-id)]
|
|
||||||
(when-let [file (db/get* conn :file {:id file-id}
|
|
||||||
{::db/remove-deleted false})]
|
|
||||||
(let [file (-> file
|
|
||||||
(decode-row)
|
|
||||||
(update :data feat.fdata/process-pointers deref)
|
|
||||||
(update :data feat.fdata/process-objects (partial into {})))]
|
|
||||||
(update-welcome-text conn file name)))))
|
|
||||||
|
|
||||||
|
|
||||||
(defn create-welcome-file
|
(defn create-welcome-file
|
||||||
[cfg profile]
|
[cfg {:keys [id] :as profile}]
|
||||||
(try
|
(try
|
||||||
(let [cfg (dissoc cfg ::db/conn)
|
(let [cfg (dissoc cfg ::db/conn)
|
||||||
params {:profile-id (:id profile)
|
params {:profile-id (:id profile)
|
||||||
|
@ -70,8 +36,9 @@
|
||||||
first)]
|
first)]
|
||||||
|
|
||||||
(db/tx-run! cfg (fn [cfg]
|
(db/tx-run! cfg (fn [cfg]
|
||||||
(update-welcome-file cfg file-id (:fullname profile))
|
(fupdate/update-file cfg file-id update-welcome-shape name)
|
||||||
(profile/update-profile-props cfg (:id profile) {:welcome-file-id file-id}))))
|
(profile/update-profile-props cfg id {:welcome-file-id file-id}))))
|
||||||
|
|
||||||
(catch Throwable cause
|
(catch Throwable cause
|
||||||
(l/error :hint "unexpected error on create welcome file " :cause cause))))
|
(l/error :hint "unexpected error on create welcome file " :cause cause))))
|
||||||
|
|
||||||
|
|
|
@ -49,21 +49,24 @@
|
||||||
(defn- login-with-oidc
|
(defn- login-with-oidc
|
||||||
[event provider params]
|
[event provider params]
|
||||||
(dom/prevent-default event)
|
(dom/prevent-default event)
|
||||||
(->> (rp/cmd! :login-with-oidc (assoc params :provider provider))
|
(let [params (-> params
|
||||||
(rx/subs! (fn [{:keys [redirect-uri] :as rsp}]
|
(assoc :provider provider)
|
||||||
(if redirect-uri
|
(assoc :create-welcome-file (cf/external-feature-flag "onboarding-03" "test")))]
|
||||||
(.replace js/location redirect-uri)
|
(->> (rp/cmd! :login-with-oidc params)
|
||||||
(log/error :hint "unexpected response from OIDC method"
|
(rx/subs! (fn [{:keys [redirect-uri] :as rsp}]
|
||||||
:resp (pr-str rsp))))
|
(if redirect-uri
|
||||||
(fn [cause]
|
(.replace js/location redirect-uri)
|
||||||
(let [{:keys [type code] :as error} (ex-data cause)]
|
(log/error :hint "unexpected response from OIDC method"
|
||||||
(cond
|
:resp (pr-str rsp))))
|
||||||
(and (= type :restriction)
|
(fn [cause]
|
||||||
(= code :provider-not-configured))
|
(let [{:keys [type code] :as error} (ex-data cause)]
|
||||||
(st/emit! (msg/error (tr "errors.auth-provider-not-configured")))
|
(cond
|
||||||
|
(and (= type :restriction)
|
||||||
|
(= code :provider-not-configured))
|
||||||
|
(st/emit! (msg/error (tr "errors.auth-provider-not-configured")))
|
||||||
|
|
||||||
:else
|
:else
|
||||||
(st/emit! (msg/error (tr "errors.generic")))))))))
|
(st/emit! (msg/error (tr "errors.generic"))))))))))
|
||||||
|
|
||||||
(s/def ::email ::us/email)
|
(s/def ::email ::us/email)
|
||||||
(s/def ::password ::us/not-empty-string)
|
(s/def ::password ::us/not-empty-string)
|
||||||
|
|
|
@ -87,7 +87,8 @@
|
||||||
(fm/validate-not-empty :password (tr "auth.password-not-empty"))]
|
(fm/validate-not-empty :password (tr "auth.password-not-empty"))]
|
||||||
:initial initial)
|
:initial initial)
|
||||||
|
|
||||||
submitted? (mf/use-state false)
|
submitted?
|
||||||
|
(mf/use-state false)
|
||||||
|
|
||||||
on-submit
|
on-submit
|
||||||
(mf/use-fn
|
(mf/use-fn
|
||||||
|
@ -227,9 +228,8 @@
|
||||||
:validators validators
|
:validators validators
|
||||||
:initial params)
|
:initial params)
|
||||||
|
|
||||||
welcome-file? (cf/external-feature-flag "onboarding-03" "test")
|
submitted?
|
||||||
|
(mf/use-state false)
|
||||||
submitted? (mf/use-state false)
|
|
||||||
|
|
||||||
on-success
|
on-success
|
||||||
(mf/use-fn
|
(mf/use-fn
|
||||||
|
@ -248,8 +248,13 @@
|
||||||
(mf/use-fn
|
(mf/use-fn
|
||||||
(fn [form _]
|
(fn [form _]
|
||||||
(reset! submitted? true)
|
(reset! submitted? true)
|
||||||
(let [params (cond-> (:clean-data @form)
|
(let [create-welcome-file?
|
||||||
welcome-file? (assoc :welcome-file true))]
|
(cf/external-feature-flag "onboarding-03" "test")
|
||||||
|
|
||||||
|
params
|
||||||
|
(cond-> (:clean-data @form)
|
||||||
|
create-welcome-file? (assoc :create-welcome-file true))]
|
||||||
|
|
||||||
(->> (rp/cmd! :register-profile params)
|
(->> (rp/cmd! :register-profile params)
|
||||||
(rx/finalize #(reset! submitted? false))
|
(rx/finalize #(reset! submitted? false))
|
||||||
(rx/subs! on-success on-error)))))]
|
(rx/subs! on-success on-error)))))]
|
||||||
|
|
Loading…
Reference in a new issue