enhance: move adding created-by logic to pipeline-hook

This commit is contained in:
rcmerci
2025-04-11 21:44:34 +08:00
parent aa91713460
commit c591598d22
11 changed files with 87 additions and 55 deletions

View File

@@ -2181,7 +2181,8 @@
(ui/tippy
{:html (fn []
[:div.flex.flex-col.gap-1.p-2
(when-let [created-by (and (ldb/get-graph-rtc-uuid (db/get-db)) (:logseq.property/created-by-ref block))]
(when-let [created-by (and (ldb/get-graph-rtc-uuid (db/get-db))
(:logseq.property/created-by-ref block))]
[:div (:block/title created-by)])
[:div "Created: " (date/int->local-time-2 (:block/created-at block))]
[:div "Last edited: " (date/int->local-time-2 (:block/updated-at block))]])}

View File

@@ -14,7 +14,6 @@
[frontend.handler.notification :as notification]
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.user :as user]
[frontend.modules.outliner.op :as outliner-op]
[frontend.modules.outliner.ui :as ui-outliner-tx]
[frontend.state :as state]
@@ -63,9 +62,7 @@
(p/let [options' (if db-based?
(cond-> (update options :tags concat (:block/tags parsed-result))
(nil? (:split-namespace? options))
(assoc :split-namespace? true)
true
(assoc :created-by (user/user-block)))
(assoc :split-namespace? true))
options)
[_page-name page-uuid] (ui-outliner-tx/transact!
{:outliner-op :create-page}

View File

@@ -29,7 +29,6 @@
[frontend.handler.property.file :as property-file]
[frontend.handler.property.util :as pu]
[frontend.handler.route :as route-handler]
[frontend.handler.user :as user]
[frontend.mobile.util :as mobile-util]
[frontend.modules.outliner.op :as outliner-op]
[frontend.modules.outliner.tree :as tree]
@@ -342,8 +341,7 @@
(outliner-op/insert-blocks! [new-block] current-block {:sibling? sibling?
:keep-uuid? keep-uuid?
:ordered-list? ordered-list?
:replace-empty-target? replace-empty-target?
:created-by (user/user-block)}))))
:replace-empty-target? replace-empty-target?}))))
(defn- block-self-alone-when-insert?
[config uuid]

View File

@@ -126,7 +126,8 @@
(state/set-auth-access-token nil)
(state/set-auth-refresh-token nil)
(set-token-to-localstorage! "" "" "")
(clear-cognito-tokens!))
(clear-cognito-tokens!)
(state/<invoke-db-worker :thread-api/update-auth-tokens nil nil nil))
([except-refresh-token?]
(state/set-auth-id-token nil)
(state/set-auth-access-token nil)
@@ -134,18 +135,21 @@
(state/set-auth-refresh-token nil))
(if except-refresh-token?
(set-token-to-localstorage! "" "")
(set-token-to-localstorage! "" "" ""))))
(set-token-to-localstorage! "" "" ""))
(state/<invoke-db-worker :thread-api/update-auth-tokens nil nil (state/get-auth-refresh-token))))
(defn- set-tokens!
([id-token access-token]
(state/set-auth-id-token id-token)
(state/set-auth-access-token access-token)
(set-token-to-localstorage! id-token access-token))
(set-token-to-localstorage! id-token access-token)
(state/<invoke-db-worker :thread-api/update-auth-tokens id-token access-token (state/get-auth-refresh-token)))
([id-token access-token refresh-token]
(state/set-auth-id-token id-token)
(state/set-auth-access-token access-token)
(state/set-auth-refresh-token refresh-token)
(set-token-to-localstorage! id-token access-token refresh-token)))
(set-token-to-localstorage! id-token access-token refresh-token)
(state/<invoke-db-worker :thread-api/update-auth-tokens id-token access-token refresh-token)))
(defn- <refresh-tokens
"return refreshed id-token, access-token"

View File

@@ -721,6 +721,11 @@
:error])
:export-edn-error))))
(def-thread-api :thread-api/update-auth-tokens
[id-token access-token refresh-token]
(worker-state/set-auth-tokens! id-token access-token refresh-token)
nil)
(comment
(def-thread-api :general/dangerousRemoveAllDbs
[]

View File

@@ -34,7 +34,6 @@
* :tags - tag uuids that are added to :block/tags
* :persist-op? - when true, add an update-page op
* :properties - properties to add to the page
* :created-by - user-block, when set, set :logseq.property/created-by-ref, only for db-based-graphs
TODO: Add other options"
[repo conn config title & {:as options}]
(if (ldb/db-based-graph? @conn)

View File

@@ -76,15 +76,14 @@
title))
(defn build-first-block-tx
[page-uuid & {:keys [created-by]}]
[page-uuid]
(let [page-id [:block/uuid page-uuid]]
[(cond-> (sqlite-util/block-with-timestamps
{:block/uuid (ldb/new-block-id)
:block/page page-id
:block/parent page-id
:block/order (db-order/gen-key nil nil)
:block/title ""})
created-by (assoc :logseq.property/created-by-ref created-by))]))
[(sqlite-util/block-with-timestamps
{:block/uuid (ldb/new-block-id)
:block/page page-id
:block/parent page-id
:block/order (db-order/gen-key nil nil)
:block/title ""})]))
(defn- get-page-by-parent-name
[db parent-title child-title]
@@ -170,8 +169,7 @@
"Pure function without side effects"
[db title*
{:keys [create-first-block? properties uuid persist-op? whiteboard?
class? today-journal? split-namespace? skip-existing-page-check?
created-by]
class? today-journal? split-namespace? skip-existing-page-check?]
:or {create-first-block? true
properties nil
uuid nil
@@ -206,8 +204,7 @@
:page-uuid (when (uuid? uuid) uuid)
:skip-existing-page-check? (if (some? skip-existing-page-check?)
skip-existing-page-check?
true)
:created-by created-by})
true)})
[page parents] (if (and (text/namespace-page? title) split-namespace?)
(let [pages (split-namespace-pages db page date-formatter)]
[(last pages) (butlast pages)])
@@ -229,8 +226,7 @@
create-first-block?
(not (or whiteboard? class?))
page-txs)
(build-first-block-tx (:block/uuid (first page-txs))
:created-by created-by))
(build-first-block-tx (:block/uuid (first page-txs))))
txs (concat
;; transact doesn't support entities
(remove de/entity? parents)

View File

@@ -157,7 +157,43 @@
:db-before (:db-before tx-report)))]
{:tx-report final-tx-report}))
(defn- invoke-hooks-default [repo conn {:keys [tx-meta] :as tx-report} context]
(defn- gen-created-by-block
[]
(when-let [id-token (worker-state/get-id-token)]
(let [decoded (worker-util/parse-jwt id-token)
user-uuid (:sub decoded)
user-name (:cognito:username decoded)
email (:email decoded)]
{:block/uuid (uuid user-uuid)
:block/name user-name
:block/title user-name
:block/tags :logseq.class/Page
:logseq.property.user/name user-name
:logseq.property.user/email email})))
(defn- add-created-by-ref-hook
[db-after tx-data tx-meta]
(when (and (not (or (:undo? tx-meta) (:redo? tx-meta) (:rtc-tx? tx-meta)))
(seq tx-data))
(let [created-by-block (gen-created-by-block)
created-by-ent (d/entity db-after [:block/uuid (:block/uuid created-by-block)])
created-by-id (or (:db/id created-by-ent) "created-by-id")
created-by-block' (when-not created-by-ent (assoc created-by-block :db/id "created-by-id"))
add-created-by-tx-data
(keep
(fn [datom]
(when (and (keyword-identical? :block/uuid (:a datom))
(:added datom))
(let [e (:e datom)
ent (d/entity db-after e)]
(when-not (:logseq.property/created-by-ref ent)
[:db/add e :logseq.property/created-by-ref created-by-id]))))
tx-data)]
(cond->> add-created-by-tx-data
created-by-block' (cons created-by-block')))))
(defn- invoke-hooks-default
[repo conn {:keys [tx-meta] :as tx-report} context]
(try
(let [display-blocks-tx-data (add-missing-properties-to-typed-display-blocks (:db-after tx-report) (:tx-data tx-report))
commands-tx (when-not (or (:undo? tx-meta) (:redo? tx-meta) (:rtc-tx? tx-meta))
@@ -165,7 +201,8 @@
;; :block/refs relies on those changes
;; idea: implement insert-templates using a command?
insert-templates-tx (insert-tag-templates repo conn tx-report)
tx-before-refs (concat display-blocks-tx-data commands-tx insert-templates-tx)
created-by-tx (add-created-by-ref-hook (:db-after tx-report) (:tx-data tx-report) (:tx-meta tx-report))
tx-before-refs (concat display-blocks-tx-data commands-tx insert-templates-tx created-by-tx)
tx-report* (if (seq tx-before-refs)
(let [result (ldb/transact! conn tx-before-refs {:pipeline-replace? true
:outliner-op :pre-hook-invoke})]
@@ -194,12 +231,12 @@
(ldb/transact! conn (concat insert-templates-tx block-refs) {:pipeline-replace? true}))
replace-tx (let [db-after (or (:db-after refs-tx-report) (:db-after tx-report*))]
(concat
;; block path refs
;; block path refs
(when (seq blocks')
(let [blocks' (keep (fn [b] (d/entity db-after (:db/id b))) blocks')]
(compute-block-path-refs-tx tx-report* blocks')))
;; update block/tx-id
;; update block/tx-id
(let [updated-blocks (remove (fn [b] (contains? deleted-block-ids (:db/id b)))
(concat pages blocks))
tx-id (get-in (or refs-tx-report tx-report*) [:tempids :db/current-tx])]

View File

@@ -37,6 +37,10 @@
:config {}
:git/current-repo nil
:auth/id-token nil
:auth/access-token nil
:auth/refresh-token nil
:rtc/downloading-graph? false
:undo/repo->page-block-uuid->undo-ops (atom {})
@@ -127,3 +131,14 @@
(defn set-rtc-downloading-graph!
[value]
(swap! *state assoc :rtc/downloading-graph? value))
(defn set-auth-tokens!
[id-token access-token refresh-token]
(swap! *state assoc
:auth/id-token id-token
:auth/access-token access-token
:auth/refresh-token refresh-token))
(defn get-id-token
[]
(:auth/id-token @*state))