diff options
-rw-r--r-- | lisp/mastodon-toot.el | 58 | ||||
-rw-r--r-- | test/mastodon-toot-tests.el | 37 |
2 files changed, 60 insertions, 35 deletions
diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index d1e8cbe..eab0dfd 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -841,33 +841,42 @@ Buffer-local variable `mastodon-toot-previous-window-config' holds the config." (set-window-configuration (car config)) (goto-char (cadr config))) +(defun mastodon-toot--mentions-to-string (mentions) + "Applies mastodon-toot--process-local function to each mention, +removes empty string (self) from result and joins the sequence with whitespace \" \"." + (mapconcat (lambda(mention) mention) + (remove "" (mapcar (lambda(x) (mastodon-toot--process-local x)) + mentions)) + " ")) + (defun mastodon-toot--process-local (acct) "Add domain to local ACCT and replace the curent user name with \"\". -Mastodon requires the full user@domain, even in the case of local accts. -eg. \"user\" -> \"user@local.social \" (when local.social is the domain of the +Mastodon requires the full @user@domain, even in the case of local accts. +eg. \"user\" -> \"@user@local.social\" (when local.social is the domain of the mastodon-instance-url). eg. \"yourusername\" -> \"\" -eg. \"feduser@fed.social\" -> \"feduser@fed.social\"." - (cond ((string-match-p "@" acct) (concat "@" acct " ")) ; federated acct +eg. \"feduser@fed.social\" -> \"@feduser@fed.social\"." + (cond ((string-match-p "@" acct) (concat "@" acct)) ; federated acct ((string= (mastodon-auth--user-acct) acct) "") ; your acct (t (concat "@" acct "@" ; local acct - (cadr (split-string mastodon-instance-url "/" t)) " ")))) + (cadr (split-string mastodon-instance-url "/" t)))))) (defun mastodon-toot--mentions (status) - "Extract mentions from STATUS and process them into a string." + "Extract mentions (not the reply-to author or booster) from STATUS. +The mentioned users look like this: +Local user (including the logged in): `username`. +Federated user: `username@host.co`." (interactive) (let* ((boosted (mastodon-tl--field 'reblog status)) (mentions (if boosted - (alist-get 'mentions (alist-get 'reblog status)) - (alist-get 'mentions status)))) - (mapconcat (lambda(x) (mastodon-toot--process-local - (alist-get 'acct x))) - ;; reverse does not work on vectors in 24.5 - (reverse (append mentions nil)) - ""))) - + (alist-get 'mentions (alist-get 'reblog status)) + (alist-get 'mentions status)))) + ;; reverse does not work on vectors in 24.5 + (mapcar (lambda(x) (alist-get 'acct x)) + (reverse mentions)))) + (defun mastodon-toot--get-bounds (regex) "Get bounds of tag or handle before point using REGEX." ;; needed because # and @ are not part of any existing thing at point @@ -968,26 +977,21 @@ text of the toot being replied to in the compose buffer." (mastodon-toot (when user (if booster (if (and (not (equal user booster)) - (not (string-match booster mentions))) + (not (member booster mentions))) ;; different booster, user and mentions: - (concat (mastodon-toot--process-local user) - ;; "@" booster " " - (mastodon-toot--process-local booster) - mentions) + (mastodon-toot--mentions-to-string (append (list user booster) mentions nil)) ;; booster is either user or in mentions: - (if (not (string-match user mentions)) + (if (not (member user mentions)) ;; user not already in mentions: - (concat (mastodon-toot--process-local user) - mentions) + (mastodon-toot--mentions-to-string (append (list user) mentions nil)) ;; user already in mentions: - mentions)) + (mastodon-toot--mentions-to-string (copy-sequence mentions)))) ;; ELSE no booster: - (if (not (string-match user mentions)) + (if (not (member user mentions)) ;; user not in mentions: - (concat (mastodon-toot--process-local user) - mentions) + (mastodon-toot--mentions-to-string (append (list user) mentions nil)) ;; user in mentions already: - mentions))) + (mastodon-toot--mentions-to-string (copy-sequence mentions))))) id (or base-toot toot)))) diff --git a/test/mastodon-toot-tests.el b/test/mastodon-toot-tests.el index b88510c..69333c0 100644 --- a/test/mastodon-toot-tests.el +++ b/test/mastodon-toot-tests.el @@ -56,21 +56,33 @@ Transfer-Encoding: chunked") (username . "local") (url . "") (acct . "local"))]))) - (defconst mastodon-toot-no-mention '((mentions . []))) +(defconst mastodon-toot--multi-mention-extracted + '("local" "federated@federated.social" "federated@federated.cafe")) + (ert-deftest mastodon-toot--multi-mentions () "Should build a correct mention string from the test toot data. Even the local name \"local\" gets a domain name added." (let ((mastodon-auth--acct-alist '(("https://local.social". "null"))) (mastodon-instance-url "https://local.social")) - (should (string= + (should (equal (mastodon-toot--mentions mastodon-toot--multi-mention) - "@local@local.social @federated@federated.social @federated@federated.cafe ")))) + '("local" "federated@federated.social" "federated@federated.cafe"))))) + +(ert-deftest mastodon-toot--multi-mentions-to-string () + "Should build a correct mention string from the test toot data. + +Even the local name \"local\" gets a domain name added." + (let ((mastodon-auth--acct-alist '(("https://local.social". "null"))) + (mastodon-instance-url "https://local.social")) + (should (string= + (mastodon-toot--mentions-to-string mastodon-toot--multi-mention-extracted) + "@local@local.social @federated@federated.social @federated@federated.cafe")))) -(ert-deftest mastodon-toot--multi-mentions-with-name () +(ert-deftest mastodon-toot--multi-mentions-with-name-to-string () "Should build a correct mention string omitting self. Here \"local\" is the user themselves and gets omitted from the @@ -79,15 +91,24 @@ mention string." '(("https://local.social". "local"))) (mastodon-instance-url "https://local.social")) (should (string= - (mastodon-toot--mentions mastodon-toot--multi-mention) - "@federated@federated.social @federated@federated.cafe ")))) + (mastodon-toot--mentions-to-string mastodon-toot--multi-mention-extracted) + "@federated@federated.social @federated@federated.cafe")))) + +(ert-deftest mastodon-toot--no-mention-to-string () + "Should return and empty string." + (let ((mastodon-auth--acct-alist + '(("https://local.social". "local"))) + (mastodon-instance-url "https://local.social")) + (should (string= + (mastodon-toot--mentions-to-string nil) + "")))) (ert-deftest mastodon-toot--no-mention () - "Should construct an empty mention string without mentions." + "Should construct an empty mention list without mentions." (let ((mastodon-auth--acct-alist '(("https://local.social". "null"))) (mastodon-instance-url "https://local.social")) - (should (string= (mastodon-toot--mentions mastodon-toot-no-mention) "")))) + (should (equal (mastodon-toot--mentions mastodon-toot-no-mention) nil)))) ;; TODO: test y-or-no-p with mastodon-toot--cancel (ert-deftest mastodon-toot--kill () |