[chronometrist] OSM hook function - bugfix and refactor

This commit is contained in:
contrapunctus 2021-08-03 20:24:37 +05:30
parent edbedbf907
commit d9bc25796d
1 changed files with 33 additions and 30 deletions

View File

@ -656,11 +656,42 @@ Return nil (and run `magit-status') if the user answers no."
(defun my-get-saved-changeset-id (chronometrist-file)
(-let* (((&plist :changesets saved-changesets)
(chronometrist-loop-file for plist in chronometrist-file
;; the very latest OSM plist is the one we just created -
;; we want the one before that
initially do (backward-list)
when (equal "OSM" (plist-get plist :name))
return plist))
(((&plist :osm-url last-url)) (last saved-changesets)))
(when last-url
(last (split-string last-url "/")))))
(first (last (split-string last-url "/"))))))
(cl-defun my-save-osm-details (&key data &allow-other-keys)
(-let* ((last-id (my-get-saved-changeset-id chronometrist-file))
(response-changesets (esxml-node-children data))
(new-changesets
(if last-id
(cl-loop with id
for changeset in response-changesets do
(setq id (alist-get
'id (esxml-node-attributes changeset)))
if (equal id last-id)
return (reverse changesets)
else collect
(list
:osm-url
(my-make-osm-url id)
:osm-comment
(my-get-changeset-comment changeset)) into changesets)
;; no changeset information in file - just use
;; the latest changeset from the response
(let* ((changeset (first response-changesets))
(id (alist-get 'id (esxml-node-attributes changeset)))
(comment (my-get-changeset-comment changeset)))
`((:osm-url ,(my-make-osm-url id)
:osm-comment ,comment))))))
(chronometrist-sexp-replace-last
(chronometrist-plist-update
(chronometrist-last) `(:changesets ,new-changesets)))))
(defun my-save-osm-changeset-details ()
"Save OSM changeset details in `chronometrist-file'."
@ -668,35 +699,7 @@ Return nil (and run `magit-status') if the user answers no."
"https://api.openstreetmap.org/api/0.6/changesets"
:params '(("display_name" . "contrapunctus"))
:parser (lambda () (libxml-parse-xml-region (point) (point-max)))
:success
(cl-function
(lambda (&key data &allow-other-keys)
(-let* ((last-id (my-get-saved-changeset-id chronometrist-file))
(response-changesets (esxml-node-children data))
(new-changesets
(if last-id
(cl-loop with id
for changeset in response-changesets do
(setq id (alist-get
'id (esxml-node-attributes changeset)))
if (equal id last-id)
return (reverse changesets)
else collect
(list
:osm-url
(my-make-osm-url id)
:osm-comment
(my-get-changeset-comment changeset)) into changesets)
;; no changeset information in file - just use
;; the latest changeset from the response
(let* ((changeset (first response-changes))
(id (alist-get 'id (esxml-node-attributes changeset)))
(comment (my-get-changeset-comment changeset)))
`((:osm-url ,(my-make-osm-url id)
:osm-comment ,comment))))))
(chronometrist-sexp-replace-last
(chronometrist-plist-update
(chronometrist-last) `(:changesets ,new-changesets))))))))
:success #'my-save-osm-details))
(defun contrapunctus-after-project-stop (project)
(pcase project