arcology-fastapi/arroyo-arcology.el

195 lines
8.7 KiB
EmacsLisp

;; [[file:arcology-arroyo.org::+begin_src emacs-lisp][No heading:1]]
(add-to-list 'arroyo-db-keywords "ARCOLOGY_KEY")
(add-to-list 'arroyo-db-keywords "ARCOLOGY_FEED")
(add-to-list 'arroyo-db-keywords "ARCOLOGY_TOOT_VISIBILITY")
(add-to-list 'arroyo-db-keywords "ARCOLOGY_ALLOW_CRAWL")
;; No heading:1 ends here
;; [[file:arcology-arroyo.org::*Arcology Page][Arcology Page:4]]
(add-to-list 'arroyo-db--schemata
'(arcology-pages
[(file :not-null)
(key :not-null)
(site :not-null)
(title :not-null)
(root-id :not-null)
(allow-crawl)
(hash :not-null)]))
(defun arroyo-arcology--insert-page (file kw site title root-id allow-crawl hash)
(arroyo-db-query [:delete :from arcology-pages
:where (= file $s1)]
file)
(arroyo-db-query [:insert :into arcology-pages :values $v1]
(vector file kw site title root-id allow-crawl hash)))
;; Arcology Page:4 ends here
;; [[file:arcology-arroyo.org::*Arcology Tags][Arcology Tags:2]]
(add-to-list 'arroyo-db--schemata
'(arcology-tags
[(file :not-null)
(tag :not-null)
(node-id :not-null)]))
(defun arroyo-arcology--insert-tags (file node-tags)
(arroyo-db-query [:delete :from arcology-tags
:where (= file $s1)]
file)
(pcase-dolist (`(,tag ,node-id) node-tags)
(arroyo-db-query [:insert :into arcology-tags
:values $v1]
(vector file tag node-id))))
;; Arcology Tags:2 ends here
;; [[file:arcology-arroyo.org::*Arcology Links][Arcology Links:2]]
(add-to-list 'arroyo-db--schemata
'(arcology-links
[source-title
(source-file :not-null)
(source-id :not-null)
(dest-file :not-null)
(dest-id :not-null)]))
(defun arcology--published-page? (file)
(not (not (arroyo-db-get "ARCOLOGY_KEY" file))))
(defun arroyo-arcology--insert-links (file source-title links)
(arroyo-db-query [:delete :from arcology-links
:where (= source-file $s1)]
file)
(pcase-dolist (`(,source ,dest ,type ,props) links)
(cond ((equal type "id")
(pcase-let* ((dest-file (caar (org-roam-db-query
[:select file :from nodes
:where (= id $s1)]
dest)))
(`(,immediate-source-title ,immediate-source-level)
(car (org-roam-db-query
[:select [title level] :from nodes
:where (= id $s1)]
source)))
;; "level 0 -> level n" unless n == 0
(composed-node-title
(if (= 0 immediate-source-level)
source-title
(concat source-title " -> " immediate-source-title))))
(when (and dest-file (arcology--published-page? dest-file)
(arroyo-db-query [:insert :into arcology-links
:values $v1]
(vector composed-node-title file source dest-file dest))))))
;; insert https link?
((equal type "https") nil)
((equal type "http") nil)
((equal type "roam") nil)
(t nil))))
;; Arcology Links:2 ends here
;; [[file:arcology-arroyo.org::*Arcology Nodes][Arcology Nodes:2]]
(add-to-list 'arroyo-db--schemata
'(arcology-nodes
[(node-id :not-null)
(file :not-null)
(level :not-null)]))
(defun arroyo-arcology--insert-nodes (file nodes)
(arroyo-db-query [:delete :from arcology-nodes
:where (= file $s1)]
file)
(pcase-dolist (`(,file ,id ,level) nodes)
(arroyo-db-query [:insert :into arcology-nodes
:values $v1]
(vector id file level))))
;; Arcology Nodes:2 ends here
;; [[file:arcology-arroyo.org::*Arcology References][Arcology References:2]]
(add-to-list 'arroyo-db--schemata
'(arcology-refs
[(file :not-null)
(ref :not-null)
(node-id :not-null)]))
(defun arroyo-arcology--insert-refs (file node-refs)
(arroyo-db-query [:delete :from arcology-refs
:where (= file $s1)]
file)
(pcase-dolist (`(,ref ,type ,node-id) node-refs)
(arroyo-db-query [:insert :into arcology-refs
:values $v1]
(vector file (format "%s:%s" type ref) node-id))))
;; Arcology References:2 ends here
;; [[file:arcology-arroyo.org::*INPROGRESS Arcology Feeds][INPROGRESS Arcology Feeds:2]]
(add-to-list 'arroyo-db--schemata
'(arcology-feeds
[(file :not-null)
(key :not-null)
(title :not-null)
(site :not-null)
(post-visibility :not-null)]))
(defun arroyo-arcology--insert-feeds (file)
(arroyo-db-query [:delete :from arcology-feeds
:where (= file $s1)]
file)
(if-let* ((key (car (arroyo-db-get "ARCOLOGY_FEED" file)))
(site (replace-regexp-in-string "/.*" "" key)))
(let* ((title (arroyo-db--get-file-title-from-org-roam file))
(post-visibility (car (arroyo-db-get "ARCOLOGY_TOOT_VISIBILITY" file))))
(arroyo-db-query [:insert :into arcology-feeds
:values $v1]
(vector file key title site post-visibility)))))
;; INPROGRESS Arcology Feeds:2 ends here
;; [[file:arcology-arroyo.org::*Arcology \[\[id:arroyo/arroyo\]\[Arroyo System\]\] Database Generator][Arcology [[id:arroyo/arroyo][Arroyo System]] Database Generator:1]]
(defun arroyo-arcology-update-file (&optional file)
(interactive)
(when-let* ((file (or file (buffer-file-name)))
(page-keyword (first (arroyo-db-get "ARCOLOGY_KEY" file)))
(site-key (first (split-string page-keyword "/")))
(page-nodes (org-roam-db-query [:select [file id level title] :from nodes
:where (= file $s1)]
file))
(file-hash (caar (org-roam-db-query [:select [hash] :from files :where (= file $s1)]
file)))
(page-node-ids (apply #'vector (--map (second it) page-nodes)))
(level-0-node (--first (eq 0 (third it)) page-nodes))
(level-0-id (elt level-0-node 1))
(level-0-title (elt level-0-node 3)))
; remove the map here -- there will only ever be one level-0 node hopefully but this is hard to understand
(let* ((allow-crawl (first (arroyo-db-get "ARCOLOGY_ALLOW_CRAWL" file)))
(allow-crawl (and allow-crawl
(not (equal allow-crawl "nil")))) ; make sure writing "nil" in the key is respected
(all-node-refs (org-roam-db-query [:select [ref type node_id] :from refs
:where (in node_id $v1)]
page-node-ids))
(all-node-tags (org-roam-db-query [:select [tag node_id] :from tags
:where (in node_id $v1)]
page-node-ids))
(links (org-roam-db-query [:select [source dest type properties] :from links
:where (in source $v1)]
page-node-ids)))
(arroyo-arcology--insert-page file page-keyword site-key level-0-title level-0-id allow-crawl file-hash)
(arroyo-arcology--insert-nodes file page-nodes)
(arroyo-arcology--insert-tags file all-node-tags)
(arroyo-arcology--insert-refs file all-node-refs)
(arroyo-arcology--insert-feeds file)
(arroyo-arcology--insert-links file level-0-title links))))
(defun arroyo-arcology-update-db (&optional _wut)
(interactive)
(->>
(arroyo-db-get "ARCOLOGY_KEY")
(-map #'car)
(-uniq)
;; this runs *after* db is updated... what to do here?
;; (-filter #'arroyo-db-file-updated-p)
(-map #'arroyo-arcology-update-file)
)
)
(add-function :after (symbol-function 'arroyo-db-update-all-roam-files) #'arroyo-arcology-update-db)
;; (add-to-list 'arroyo-db-update-functions #'arroyo-arcology-update-file)
(provide 'arroyo-arcology)
;; Arcology [[id:arroyo/arroyo][Arroyo System]] Database Generator:1 ends here