Skip to content

Commit

Permalink
enhance: add created-by prop to blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed Jan 14, 2025
1 parent 685e825 commit 3e025dd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
5 changes: 3 additions & 2 deletions deps/graph-parser/src/logseq/graph_parser/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
as there's no chance to introduce timestamps via editing in page
`skip-existing-page-check?`: if true, allows pages to have the same name"
[original-page-name db with-timestamp? date-formatter
& {:keys [page-uuid class?] :as options}]
& {:keys [page-uuid class? created-by] :as options}]
(when-not (and db (common-util/uuid-string? original-page-name)
(not (ldb/page? (d/entity db [:block/uuid (uuid original-page-name)]))))
(let [original-page-name (-> (string/trim original-page-name)
Expand All @@ -393,7 +393,8 @@
(let [tags (if class? [:logseq.class/Tag]
(or (:block/tags page)
[:logseq.class/Page]))]
(assoc page :block/tags tags))
(cond-> (assoc page :block/tags tags)
created-by (assoc :logseq.property/created-by created-by)))
(assoc page :block/type (or (:block/type page) "page")))))))

(defn- db-namespace-page?
Expand Down
18 changes: 15 additions & 3 deletions deps/outliner/src/logseq/outliner/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
(let [updated-at (common-util/time-ms)]
(assoc block :block/updated-at updated-at)))

(defn- update-property-created-by
[block created-by]
(cond-> block
(and created-by (nil? (:logseq.property/created-by block)))
(assoc :logseq.property/created-by created-by)))

(defn- filter-top-level-blocks
[db blocks]
(let [parent-ids (set/intersection (set (map (comp :db/id :block/parent) blocks))
Expand Down Expand Up @@ -622,8 +628,12 @@
`replace-empty-target?`: If the `target-block` is an empty block, whether
to replace it, it defaults to be `false`.
`update-timestamps?`: whether to update `blocks` timestamps.
`created-by`: user-uuid, update `:logseq.property/created-by` if exists
``"
[repo conn blocks target-block {:keys [_sibling? keep-uuid? keep-block-order? outliner-op replace-empty-target? update-timestamps?] :as opts
[repo conn blocks target-block {:keys [_sibling? keep-uuid? keep-block-order?
outliner-op replace-empty-target? update-timestamps?
created-by]
:as opts
:or {update-timestamps? true}}]
{:pre [(seq blocks)
(m/validate block-map-or-entity target-block)]}
Expand All @@ -642,11 +652,13 @@
blocks' (let [blocks' (blocks-with-level blocks)]
(cond->> (blocks-with-ordered-list-props repo blocks' target-block sibling?)
update-timestamps?
(mapv (fn [b] (block-with-timestamps (dissoc b :block/created-at :block/updated-at))))
(mapv #(dissoc % :block/created-at :block/updated-at))
true
(mapv block-with-timestamps)
db-based?
(mapv (fn [b] (dissoc b :block/properties)))))
(mapv #(-> %
(dissoc :block/properties)
(update-property-created-by created-by)))))
insert-opts {:sibling? sibling?
:replace-empty-target? replace-empty-target?
:keep-uuid? keep-uuid?
Expand Down
37 changes: 20 additions & 17 deletions src/main/frontend/handler/common/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
and favorite fns. This ns should be agnostic of file or db concerns but there
is still some file-specific tech debt to remove from create!"
(:require [clojure.string :as string]
[datascript.core :as d]
[frontend.config :as config]
[frontend.db :as db]
[frontend.db.conn :as conn]
[frontend.fs :as fs]
[frontend.handler.block :as block-handler]
[frontend.handler.config :as config-handler]
[frontend.handler.db-based.editor :as db-editor-handler]
[frontend.handler.notification :as notification]
[frontend.handler.route :as route-handler]
[frontend.state :as state]
[logseq.common.util :as common-util]
[logseq.common.config :as common-config]
[frontend.handler.ui :as ui-handler]
[frontend.config :as config]
[frontend.fs :as fs]
[promesa.core :as p]
[frontend.handler.block :as block-handler]
[logseq.db :as ldb]
[frontend.db.conn :as conn]
[datascript.core :as d]
[frontend.modules.outliner.ui :as ui-outliner-tx]
[frontend.handler.user :as user]
[frontend.modules.outliner.op :as outliner-op]
[frontend.handler.db-based.editor :as db-editor-handler]
[frontend.modules.outliner.ui :as ui-outliner-tx]
[frontend.state :as state]
[logseq.common.config :as common-config]
[logseq.common.util :as common-util]
[logseq.common.util.page-ref :as page-ref]
[frontend.handler.notification :as notification]))
[logseq.db :as ldb]
[promesa.core :as p]))

(defn- wrap-tags
"Tags might have multiple words"
Expand Down Expand Up @@ -59,11 +60,13 @@
(if (and has-tags? (nil? title'))
(notification/show! "Page name can't include \"#\"." :warning)
(when-not (string/blank? title')
(p/let [options' (if db-based?
(cond->
(update options :tags concat (:block/tags parsed-result))
(p/let [current-user-id (user/user-uuid)
options' (if db-based?
(cond-> (update options :tags concat (:block/tags parsed-result))
(nil? (:split-namespace? options))
(assoc :split-namespace? true))
(assoc :split-namespace? true)
current-user-id
(assoc :created-by current-user-id))
options)
result (ui-outliner-tx/transact!
{:outliner-op :create-page}
Expand Down
7 changes: 5 additions & 2 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
[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]
Expand Down Expand Up @@ -334,14 +335,16 @@
true

:else
(not has-children?))]
(not has-children?))
current-user-id (user/user-uuid)]
(ui-outliner-tx/transact!
{:outliner-op :insert-blocks}
(save-current-block! {:current-block current-block})
(outliner-op/insert-blocks! [new-block] current-block {:sibling? sibling?
:keep-uuid? keep-uuid?
:ordered-list? ordered-list?
:replace-empty-target? replace-empty-target?}))))
:replace-empty-target? replace-empty-target?
:created-by current-user-id}))))

(defn- block-self-alone-when-insert?
[config uuid]
Expand Down
1 change: 1 addition & 0 deletions src/main/frontend/worker/handler/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* :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 - when set, set :logseq.property/created-by, only for db-based-graphs
TODO: Add other options"
[repo conn config title & {:as options}]
(if (ldb/db-based-graph? @conn)
Expand Down
7 changes: 5 additions & 2 deletions src/main/frontend/worker/handler/page/db_based/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@
(defn create
"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?]
{:keys [create-first-block? properties uuid persist-op? whiteboard?
class? today-journal? split-namespace? skip-existing-page-check?
created-by]
:or {create-first-block? true
properties nil
uuid nil
Expand Down Expand Up @@ -203,7 +205,8 @@
:page-uuid (when (uuid? uuid) uuid)
:skip-existing-page-check? (if (some? skip-existing-page-check?)
skip-existing-page-check?
true)})
true)
:created-by created-by})
[page parents] (if (and (text/namespace-page? title) split-namespace?)
(let [pages (split-namespace-pages db page date-formatter)]
[(last pages) (butlast pages)])
Expand Down

0 comments on commit 3e025dd

Please sign in to comment.