diff options
author | Thanos Apollo <[email protected]> | 2024-12-17 05:33:59 +0200 |
---|---|---|
committer | Thanos Apollo <[email protected]> | 2024-12-17 05:33:59 +0200 |
commit | dbaaf5b2b0390add21aef5b75d8e021545836bc5 (patch) | |
tree | 46dc6df32a4ad00500b1b03386e13b812e2f004a | |
parent | cea3f7ec73dc3e6dc996acd116cc4e940718ba59 (diff) |
Refactor org-roam-ui--send-graphdata
* Break it down into smaller functions.
-rw-r--r-- | org-roam-ui.el | 150 |
1 files changed, 72 insertions, 78 deletions
diff --git a/org-roam-ui.el b/org-roam-ui.el index b642fb0..198cef6 100644 --- a/org-roam-ui.el +++ b/org-roam-ui.el @@ -429,61 +429,61 @@ unchanged." ("FILELESS" . t)) 'nil)) -(defun org-roam-ui--send-graphdata () - "Get roam data, make JSON, send through websocket to org-roam-ui." - (let* ((nodes-names - [id - file - title - level - pos - olp - properties - tags]) - (old (not (fboundp 'org-roam-db-map-citations))) - (links-db-rows (if old - (org-roam-ui--separate-ref-links - (org-roam-ui--get-links old)) - (seq-concatenate - 'list - (org-roam-ui--separate-ref-links - (org-roam-ui--get-cites)) - (org-roam-ui--get-links)))) - (links-with-empty-refs (org-roam-ui--filter-citations links-db-rows)) - (empty-refs (delete-dups (seq-map - (lambda (link) - (nth 1 link)) - links-with-empty-refs))) - (nodes-db-rows (org-roam-ui--get-nodes)) - (fake-nodes (seq-map #'org-roam-ui--create-fake-node empty-refs)) - ;; Try to update real nodes that are reference with a title build - ;; from their bibliography entry. Check configuration here for avoid - ;; unneeded iteration though nodes. - (retitled-nodes-db-rows (if org-roam-ui-retitle-ref-nodes - (seq-map #'org-roam-ui--retitle-node - nodes-db-rows) - nodes-db-rows)) - (complete-nodes-db-rows (append retitled-nodes-db-rows fake-nodes)) - (response `((nodes . ,(mapcar - (apply-partially - #'org-roam-ui-sql-to-alist - (append nodes-names nil)) - complete-nodes-db-rows)) - (links . ,(mapcar - (apply-partially - #'org-roam-ui-sql-to-alist - '(source target type)) - links-db-rows)) - (tags . ,(seq-mapcat - #'seq-reverse - (org-roam-db-query - [:select :distinct tag :from tags])))))) - (when old - (message "[org-roam-ui] You are not using the latest version of org-roam. -This database model won't be supported in the future, please consider upgrading.")) - (websocket-send-text org-roam-ui-ws-socket (json-encode - `((type . "graphdata") - (data . ,response)))))) +(defun org-roam-ui-get-links (&optional old-db) + "Fetch links from the org-roam database. +If OLD-DB is non-nil, use the older method of getting links." + (if old-db + (org-roam-ui--separate-ref-links + (org-roam-ui--get-links t)) + (nconc (org-roam-ui--separate-ref-links + (org-roam-ui--get-cites)) + (org-roam-ui--get-links)))) + +(defun org-roam-ui-get-tags () + "Fetch tags from the org-roam database." + (seq-mapcat #'seq-reverse + (org-roam-db-query + [:select :distinct tag :from tags]))) + +(defun org-roam-ui-get-nodes (&optional nodes links-db) + "Fetch nodes and create fake nodes based on LINKS-DB. +If NODES is provided, use it directly." + (let* ((nodes-db (or nodes (org-roam-ui--get-nodes))) + (fake-nodes (seq-map #'org-roam-ui--create-fake-node + (delete-dups + (mapcar #'cadr + (org-roam-ui--filter-citations links-db)))))) + (append (if org-roam-ui-retitle-ref-nodes + (seq-map #'org-roam-ui--retitle-node nodes-db) + nodes-db) + fake-nodes))) + +(defun org-roam-ui--send-graphdata (&optional nodes links tags) + "Prepare and send graph data to org-roam-ui, optionally using NODES, LINKS, and TAGS. +If not provided, data is retrieved from the org-roam database." + (let* ((links-db (or links (funcall org-roam-ui-links-function))) + (nodes-db (or nodes (funcall org-roam-ui-nodes-function))) + (tags-db (or tags (funcall org-roam-ui-tags-function))) + (nodes-alist '()) + (links-alist '())) + + ;; Convert nodes to alist + (dolist (node nodes-db) + (push (org-roam-ui-sql-to-alist + '(id file title level pos olp properties tags) node) + nodes-alist)) + + ;; Convert links to alist + (dolist (link links-db) + (push (org-roam-ui-sql-to-alist + '(source target type) link) + links-alist)) + + (let ((response `((nodes . ,nodes-alist) + (links . ,links-alist) + (tags . ,tags-db)))) + (websocket-send-text org-roam-ui-ws-socket + (json-encode `((type . "graphdata") (data . ,response))))))) (defun org-roam-ui--filter-citations (links) @@ -494,21 +494,21 @@ This database model won't be supported in the future, please consider upgrading. links)) (defun org-roam-ui--get-nodes () - "." + "Nodes to display in org-roam-ui." (org-roam-db-query [:select [id - file - title - level - pos - olp - properties - (funcall group-concat tag - (emacsql-escape-raw \, ))] - :as tags - :from nodes - :left-join tags - :on (= id node_id) - :group :by id])) + file + title + level + pos + olp + properties + (funcall group-concat tag + (emacsql-escape-raw \, ))] + :as tags + :from nodes + :left-join tags + :on (= id node_id) + :group :by id])) (defun org-roam-ui--get-links (&optional old) "Get the cites and links tables as rows from the org-roam db. @@ -603,13 +603,7 @@ from all other links." (defun org-roam-ui--send-variables (ws) "Send miscellaneous org-roam variables through the websocket WS." - (let ((daily-dir (if (boundp 'org-roam-dailies-directory) - (if (file-name-absolute-p org-roam-dailies-directory) - (expand-file-name org-roam-dailies-directory) - (expand-file-name - org-roam-dailies-directory - org-roam-directory)) - "/dailies")) + (let ((daily-dir org-roam-ui-dailies-directory) (attach-dir (if (boundp 'org-attach-id-dir) org-attach-id-dir (expand-file-name ".attach/" org-directory))) @@ -629,7 +623,7 @@ from all other links." ,attach-dir) ("useInheritance" . ,use-inheritance) - ("roamDir" . ,org-roam-directory) + ("roamDir" . ,org-roam-ui-directory) ("katexMacros" . ,org-roam-ui-latex-macros)))))))) (defun org-roam-ui-sql-to-alist (column-names rows) @@ -668,7 +662,7 @@ ROWS is the sql result, while COLUMN-NAMES is the columns to use." TODO: Exclude org-attach dirs." (seq-filter (lambda (file) (and (file-directory-p file) (org-roam-ui-allowed-directory-p file))) - (directory-files-recursively org-roam-directory + (directory-files-recursively org-roam-ui-directory ".*" t #'org-roam-ui-allowed-directory-p))) (defun org-roam-ui-allowed-directory-p (dir) |