From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:863f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms12 with LMTPS id SYLYKHudH2CWTwAAsNZ9tg (envelope-from ); Sun, 07 Feb 2021 07:57:47 +0000 Received: from out1.migadu.com ([2001:41d0:2:863f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id eDOLFHmdH2BRQQAAbx9fmQ (envelope-from ); Sun, 07 Feb 2021 07:57:45 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kyleam.com; s=key1; t=1612684665; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x4DevyNjqg8570C6kfm+Q1lHIcUulSUN95v+mwuU4TQ=; b=b1uQoDPoNzdDPDNnxIslrxptIBhS7CRPVUKWIgvF4zMvkger/llA+TSSmceOJZUKjw7Q7D utNVfZ9M6BIx8nxLGa3GzJPOmcqhHuWHjBSc+Od3awctItDBd6V5hzQEbToLM2pKTGS9TM c8iDR2m/PnlQBRHGrRE6mQi1ppbfjZG5hIQwC0sutPsG3YFK082KmHtwdZsOcDbsqmC8R0 Ud7SXrfnzQQVnwlCExBPEXqSJXQHngp1MHotzb/XzkSw/mfgPIa1XrWgxP6T/sHcU0iDp+ cJKKbkh8i8I5mCLbo9SXCxWzlfJ8d+3WWAEEXKQ7Gzx/Xgid803O/vyYt1eTbw== From: Kyle Meyer To: piem@inbox.kyleam.com Cc: Xinglu Chen Subject: [PATCH 1/5] piem: Add helper to construct message ID link Date: Sun, 7 Feb 2021 02:57:34 -0500 Message-Id: <20210207075738.8752-2-kyle@kyleam.com> In-Reply-To: <20210207075738.8752-1-kyle@kyleam.com> References: <20210207075738.8752-1-kyle@kyleam.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: kyle@kyleam.com X-TUID: RQRfrqq1LcTd 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 --- 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)))) -;;;; 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))) + + +;;;; 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 " -- 2.30.0