diff options
-rw-r--r-- | emacs/.emacs.d/lisp/my/mastorg.el | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/emacs/.emacs.d/lisp/my/mastorg.el b/emacs/.emacs.d/lisp/my/mastorg.el index 17b50c0..d0db3a0 100644 --- a/emacs/.emacs.d/lisp/my/mastorg.el +++ b/emacs/.emacs.d/lisp/my/mastorg.el @@ -116,6 +116,29 @@ With nonnil WITH-HEADER, include the response headers in the return value." (list (cons 'status status) (cons 'fields fields)))) ;;; mastodon utilities +(defun mastorg-api-search (host url) + (mastorg-url-fetch-json + (format "https://%s/api/v2/search/?q=%s&resolve=true" host url))) + +(defun mastorg-canonical-post-url-by-search (host url) + (let-alist (mastorg-api-search host url) + (if (seq-empty-p .statuses) + (error "No statuses associated with URL %s" url) + (mastorg-canonical-post-url (alist-get 'url (elt .statuses 0)) t)))) + +(defun mastorg-canonical-post-url (url &optional no-fetch) + (pcase-let* ((urlobj (url-generic-parse-url url)) + (`(,path . _) (url-path-and-query urlobj)) + (host (url-host urlobj))) + (cond ((or (string-match-p "^/objects/[-a-f0-9]+$" path) + (string-match-p + "^/user[s]?/[[:alnum:]_]+/statuses/[[:digit:]]+$" path)) + (unless no-fetch (mastorg-canonical-post-url-by-search host url))) + ((or (string-match-p "^/@[^/]+/\\([0-9]+\\)$" path) + (string-match-p "^/notice/\\([[:alnum:]]+\\)$" path)) + url) + (t (error "Unrecognisable URL: %s" url))))) + (defun mastorg-parse-url (url) "Parse mastodon post URL." (pcase-let* ((urlobj (url-generic-parse-url url)) @@ -246,6 +269,7 @@ Including ancestors and descendants, if any." Including the context, i.e. ancestors and descendant toots." (interactive "sToot URL: ") + (setq url (mastorg-canonical-post-url url)) (mastorg-save-text-and-switch-to-buffer (mastorg-format-toot-tree url) (file-name-concat mastorg-dir (mastorg-make-filename url)))) |