discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Cc: Xinglu Chen <public@yoctocell.xyz>
Subject: [PATCH 1/5] piem: Add helper to construct message ID link
Date: Sun,  7 Feb 2021 02:57:34 -0500	[thread overview]
Message-ID: <20210207075738.8752-2-kyle@kyleam.com> (raw)
In-Reply-To: <20210207075738.8752-1-kyle@kyleam.com>

There are two spots that use (piem-inbox-get :url ...) and
piem-escape-mid to construct the public-inbox link, and there is about
to be another.  Extract this shared logic.

Cc: Xinglu Chen <public@yoctocell.xyz>
---
 piem-notmuch.el     | 10 +++-------
 piem.el             | 21 ++++++++++++++++-----
 tests/piem-tests.el | 13 +++++++++++++
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/piem-notmuch.el b/piem-notmuch.el
index a222f3f..2a5c525 100644
--- a/piem-notmuch.el
+++ b/piem-notmuch.el
@@ -109,13 +109,9 @@ (defun piem-notmuch-am-ready-mbox ()
 (defun piem-notmuch-show-get-public-inbox-link (mid)
   "Given the message-id MID, return the public-inbox url.
 This will lookup the url in the `piem-inboxes' variable."
-  (let* ((inbox (or (piem-notmuch-get-inbox)
-                    (user-error "No inbox associated with current buffer")))
-         (link (or (piem-inbox-get :url inbox)
-                   (user-error "No url was found for %s" inbox))))
-    (concat
-     (piem--ensure-trailing-slash link)
-     (piem-escape-mid mid))))
+  (piem-mid-url mid
+                (or (piem-notmuch-get-inbox)
+                    (user-error "No inbox associated with current buffer"))))
 
 ;;;###autoload
 (define-minor-mode piem-notmuch-mode
diff --git a/piem.el b/piem.el
index b66894f..24d252e 100644
--- a/piem.el
+++ b/piem.el
@@ -444,7 +444,7 @@ (defun piem-am-ready-mbox ()
         nil))))
 
 \f
-;;;; Download helpers
+;;;; Link handling
 
 (defconst piem--unreserved-chars
   (append url-unreserved-chars
@@ -456,6 +456,20 @@ (defun piem-escape-mid (mid)
   "Escape MID for use in path part of a public-inbox URL."
   (url-hexify-string mid piem--unreserved-chars))
 
+(defun piem-mid-url (mid &optional inbox)
+  "Return a public-inbox URL for MID.
+The URL is determined by INBOX's entry in `piem-inboxes'.  If
+INBOX is nil, use the inbox returned by `piem-inbox'."
+  (concat
+   (piem--ensure-trailing-slash
+    (or (piem-inbox-get :url inbox)
+        (user-error "Couldn't find URL for %s"
+                    (or inbox "current buffer"))))
+   (piem-escape-mid mid)))
+
+\f
+;;;; Download helpers
+
 (defvar piem--has-gunzip)
 (defun piem-check-gunzip ()
   "Return non-nil if gunzip is available."
@@ -550,10 +564,7 @@ (defun piem-inject-thread-into-maildir (mid &optional message-only)
      "`piem-maildir-directory' does not look like a Maildir directory"))
    ((not (or message-only (piem-check-gunzip)))
     (user-error "gunzip executable not found")))
-  (when-let ((url (concat (or (piem-inbox-get :url)
-                              (user-error
-                               "Could not find inbox URL for current buffer"))
-                          (piem-escape-mid mid)
+  (when-let ((url (concat (piem-mid-url mid)
                           (if message-only "/raw" "/t.mbox.gz")))
              (buffer (url-retrieve-synchronously url 'silent)))
     (unwind-protect
diff --git a/tests/piem-tests.el b/tests/piem-tests.el
index 795686a..969c9d0 100644
--- a/tests/piem-tests.el
+++ b/tests/piem-tests.el
@@ -49,6 +49,19 @@ (ert-deftest piem-escape-mid ()
   (should (equal (piem-escape-mid "msg@id") "msg@id"))
   (should (equal (piem-escape-mid "m/g@id") "m%2Fg@id")))
 
+(ert-deftest piem-mid-url ()
+  (let ((piem-inboxes '(("inbox-a" :url "https://example.com/a/")
+                        ("inbox-b" :url "https://example.com/b/"))))
+    (should (equal (piem-mid-url "msg@id" "inbox-a")
+                   "https://example.com/a/msg@id"))
+    (should (equal (piem-mid-url "m/sg@id" "inbox-b")
+                   "https://example.com/b/m%2Fsg@id"))
+    (should-error (piem-mid-url "msg@id")
+                  :type 'user-error))
+  (let ((piem-inboxes '(("inbox-a"))))
+    (should-error (piem-mid-url "msg@id" "inbox-a")
+                  :type 'user-error)))
+
 (ert-deftest piem-name-branch-who-what-v ()
   (should (equal (piem-name-branch-who-what-v
                   (list :from "Foo Bar <f@example.com>"
-- 
2.30.0


  reply	other threads:[~2021-02-07  7:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-07  7:57 [PATCH 0/5] New command for copying public-inbox URLs Kyle Meyer
2021-02-07  7:57 ` Kyle Meyer [this message]
2021-02-07  7:57 ` [PATCH 2/5] piem-dispatch: Add " Kyle Meyer
2021-02-07  7:57 ` [PATCH 3/5] piem-copy-mid-url: Add support for browsing url Kyle Meyer
2021-02-07  7:57 ` [PATCH 4/5] piem-copy-mid-url: Allow overriding browse-url-browser-function Kyle Meyer
2021-02-07  7:57 ` [PATCH 5/5] manual: Add section on "copy public-inbox link" functionality Kyle Meyer
2021-02-07 12:38 ` [PATCH 0/5] New command for copying public-inbox URLs Xinglu Chen
2021-02-07 16:57   ` Kyle Meyer
2021-02-07 17:35     ` Kyle Meyer
2021-02-07 20:25       ` Xinglu Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://git.kyleam.com/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210207075738.8752-2-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \
    --cc=public@yoctocell.xyz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.kyleam.com/piem/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).