discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Subject: [PATCH 5/5] b4: Try to download thread from piem-inboxes URL
Date: Sun, 16 Aug 2020 21:33:43 -0400	[thread overview]
Message-ID: <20200817013343.15615-6-kyle@kyleam.com> (raw)
In-Reply-To: <20200817013343.15615-1-kyle@kyleam.com>

If a call to piem-b4-am-from-mid fails to generate the thread for a
message ID via piem-mid-to-thread-functions, b4 is called without a
local mbox.  In this case, b4 tries to download the thread from the
URL specified by b4.midmask in the caller's Git configuration.

That works, but it's inconvenient because the user needs to configure
the URL in two places.  If the current buffer is associated with an
inbox in piem-inboxes, try to download the thread from its :url before
falling back to b4's midmask.
 piem-b4.el | 19 +++++++++++++++----
 piem.el    | 15 +++++++++++++++
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/piem-b4.el b/piem-b4.el
index 537abed..39d32be 100644
--- a/piem-b4.el
+++ b/piem-b4.el
@@ -47,10 +47,6 @@ (defcustom piem-b4-b4-executable "b4"
 ;;;; Internals
-;; In many cases, we don't really need b4 to download the mbox for us,
-;; as we already have our own mbox to URL mapping.  Perhaps we should
-;; default to using that, but it should still be an option to use b4
-;; so that we honor its customization/URL resolution.
 (defun piem-b4--get-am-files (mid coderepo args)
   (let* ((outdir (file-name-as-directory
                   (make-temp-file "piem-b4-" t)))
@@ -63,6 +59,21 @@ (defun piem-b4--get-am-files (mid coderepo args)
         (funcall fn)
         (unless (= (point-max) 1)
           (setq local-mbox-p t))))
+    ;; `piem-mid-to-thread-functions' didn't generate an mbox.  Next
+    ;; try to download it from a URL at `piem-inboxes'.  Finally, fall
+    ;; back to b4's configuration.
+    (unless local-mbox-p
+      (when-let ((url (piem-inbox-url))
+                 (mid (piem-mid))
+                 (buffer (condition-case nil
+                             (piem-download-and-decompress
+                              (concat url mid "/t.mbox.gz"))
+                           (user-error nil))))
+        (when (buffer-live-p buffer)
+          (with-current-buffer buffer
+            (write-region nil nil mbox-thread))
+          (kill-buffer buffer)
+          (setq local-mbox-p t))))
     ;; Move to the coderepo so that we pick up any b4 configuration
     ;; from there.
     (apply #'piem-process-call coderepo piem-b4-b4-executable "am"
diff --git a/piem.el b/piem.el
index 94fa1ad..0fd1edc 100644
--- a/piem.el
+++ b/piem.el
@@ -382,6 +382,21 @@ (defun piem--url-decompress ()
   (delete-region (point) (point-max))
   (goto-char (point-min)))
+(defun piem--decompress-callback (status)
+  (if (plist-get status :error)
+      (kill-buffer (current-buffer))
+    (piem--url-remove-header)
+    (piem--url-decompress)))
+(defun piem-download-and-decompress (url)
+  "Retrieve gzipped content at URL and decompress it.
+A buffer with the decompressed content is returned.  A live
+buffer indicates that the request did not result in an error."
+  (unless (piem-check-gunzip)
+    (user-error "gunzip executable not found"))
+  (let ((url-asynchronous nil))
+    (url-retrieve url #'piem--decompress-callback)))
 (defun piem--write-mbox-to-maildir ()
   (let ((n-messages 0))
     (while (and (not (eobp))

      parent reply	other threads:[~2020-08-17  1:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-17  1:33 [PATCH 0/5] b4: Try to download thread from piem-inboxes URL Kyle Meyer
2020-08-17  1:33 ` [PATCH 1/5] Use url-http-end-of-headers move to payload Kyle Meyer
2020-08-17  1:33 ` [PATCH 2/5] Move "has gunzip?" check to helper Kyle Meyer
2020-08-17  1:33 ` [PATCH 3/5] Extract logic from inject-thread-callback Kyle Meyer
2020-08-17  1:33 ` [PATCH 4/5] b4: Use a clearer variable name Kyle Meyer
2020-08-17  1:33 ` Kyle Meyer [this message]

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:

  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=20200817013343.15615-6-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \


* 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


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).