From 23013ff6daa54c8268c6f5b791270b78e1ac7eb0 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Sat, 3 Sep 2022 18:32:37 +0200 Subject: improve masto-url-p regexes --- lisp/mastodon.el | 62 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'lisp/mastodon.el') diff --git a/lisp/mastodon.el b/lisp/mastodon.el index 93b24b3..0c6104f 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -279,43 +279,43 @@ not, just browse the URL in the normal fashion." (url-get-url-at-point) (get-text-property (point) 'shr-url) (read-string "Lookup URL: ")))) - ;; TODO: test for a likely masto link and just browse-url if not - (save-match-data - (if (not (mastodon--masto-url-p query)) - (browse-url query) - (message "Performing lookup...") - (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) - (param (concat "resolve=t")) ; webfinger - (response (mastodon-http--get-search-json url query param :silent))) - (cond ((not (seq-empty-p - (alist-get 'statuses response))) - (let* ((statuses (assoc 'statuses response)) - (status (seq-first (cdr statuses))) - (status-id (alist-get 'id status))) - (mastodon-tl--thread status-id))) - ((not (seq-empty-p - (alist-get 'accounts response))) - (let* ((accounts (assoc 'accounts response)) - (account (seq-first (cdr accounts)))) - (mastodon-profile--make-author-buffer account))) - (t - (browse-url query)))))))) + (if (not (mastodon--masto-url-p query)) + (browse-url query) + (message "Performing lookup...") + (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) + (param (concat "resolve=t")) ; webfinger + (response (mastodon-http--get-search-json url query param :silent))) + (cond ((not (seq-empty-p + (alist-get 'statuses response))) + (let* ((statuses (assoc 'statuses response)) + (status (seq-first (cdr statuses))) + (status-id (alist-get 'id status))) + (mastodon-tl--thread status-id))) + ((not (seq-empty-p + (alist-get 'accounts response))) + (let* ((accounts (assoc 'accounts response)) + (account (seq-first (cdr accounts)))) + (mastodon-profile--make-author-buffer account))) + (t + (browse-url query))))))) (defun mastodon--masto-url-p (query) "Check if QUERY resembles a fediverse URL." - ;; regex test the url: ;; calqued off https://github.com/tuskyapp/Tusky/blob/c8fc2418b8f5458a817bba221d025b822225e130/app/src/main/java/com/keylesspalace/tusky/BottomSheetActivity.kt ;; TODO: remove domain and add ^ to regex: ;; (let ((query-path (url-file-nondirectory query))) - (or (string-match "/@" query) - (string-match "/users/[[:alnum:]]+$" query) - (string-match "/notice/[[:alnum:]]+$" query) - (string-match "/objects/[-a-f0-9]+$" query) - (string-match "/notes/[a-z0-9]+$" query) - (string-match "/display/[-a-f0-9]+$" query) - (string-match "/profile/[[:alpha:]]+$" query) - (string-match "/p/[[:alpha:]]+/[[:digit:]]+$" query) - (string-match "/[[:alpha:]]+$" query))) + (let* ((uri-parsed (url-generic-parse-url query)) + (query (url-filename uri-parsed))) + (save-match-data + (or (string-match "^/@[[:alnum:]]+/[[:digit:]]+$" query) + (string-match "^/users/[[:alnum:]]+$" query) + (string-match "^/notice/[[:alnum:]]+$" query) + (string-match "^/objects/[-a-f0-9]+$" query) + (string-match "^/notes/[a-z0-9]+$" query) + (string-match "^/display/[-a-f0-9]+$" query) + (string-match "^/profile/[[:alpha:]]+$" query) + (string-match "^/p/[[:alpha:]]+/[[:digit:]]+$" query) + (string-match "^/[[:alpha:]]+$" query))))) ;;;###autoload (add-hook 'mastodon-mode-hook (lambda () -- cgit v1.2.3