diff options
| author | marty hiatt <martianhiatus [a t] riseup [d o t] net> | 2022-09-03 14:33:01 +0200 | 
|---|---|---|
| committer | marty hiatt <martianhiatus [a t] riseup [d o t] net> | 2022-09-03 14:33:01 +0200 | 
| commit | e2d528fa4c73b04dfe39b12d9fd9eba38db88ed9 (patch) | |
| tree | 1b88331ae9029546f68c74c5619ad3c7c24e4577 /lisp/mastodon.el | |
| parent | c35e0a33be088bdb27031855886e5dd9cbed4f32 (diff) | |
rough regex check for fedi URLs
Diffstat (limited to 'lisp/mastodon.el')
| -rw-r--r-- | lisp/mastodon.el | 55 | 
1 files changed, 37 insertions, 18 deletions
diff --git a/lisp/mastodon.el b/lisp/mastodon.el index 8f49538..b5b1d87 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -279,24 +279,43 @@ not, just browse the URL in the normal fashion."                      (mastodon-tl--property 'shr-url)                      (read-string "Lookup URL: "))))      ;; TODO: test for a likely masto link and just browse-url if not -    (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))) -                    (account-acct (alist-get 'acct account))) -               (mastodon-profile--show-user account-acct))) -            (t -             (browse-url query)))))) +    (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))) +                        (account-acct (alist-get 'acct account))) +                   (mastodon-profile--show-user account-acct))) +                (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)))  ;;;###autoload  (add-hook 'mastodon-mode-hook (lambda ()  | 
