~williewillus/public-inbox

Implement attachment-fetching for Discord replies v1 APPLIED

: 2
 Implement attachment-fetching for Discord replies
 Fix unfortunate typo

 3 files changed, 38 insertions(+), 18 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~williewillus/public-inbox/patches/25473/mbox | git am -3
Learn more about email & git

[PATCH] Implement attachment-fetching for Discord replies Export this patch

From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>

---
 frontends/discord.rkt | 46 +++++++++++++++++++++++++++----------------
 scribblings/r16.scrbl |  8 ++++++++
 2 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/frontends/discord.rkt b/frontends/discord.rkt
index e5cff23..f481f22 100644
--- a/frontends/discord.rkt
+++ b/frontends/discord.rkt
@@ -163,7 +163,9 @@
      (define message (current-message))
      (define message-contents (hash-ref message 'content))
      (define message-attachments (or (hash-ref message 'attachments #f) null))
      (define reply-message-attachments (or (and~> message (hash-ref 'referenced_message #f) (hash-ref 'attachments #f)) null))
      (define message-author (message-author-id message))
      (define channel-id (hash-ref message 'channel_id))

      (define/contract (emote-image id)
        (-> string? (or/c bytes? #f))
@@ -187,25 +189,33 @@
        (http:attachment data (~a type) name))

      (define attachment-count (length message-attachments))
      (define reply-attachment-count (length reply-message-attachments))

      (define/contract (open-attachment [index 0])
        (->* () (exact-nonnegative-integer?) (or/c input-port? #f))
      (define (fetch-attachment attachments index)
        (let/cc return
          (define chan (make-channel))
          (when (>= index attachment-count)
          (when (>= index (length attachments))
            (return #f))
          (define attachment (list-ref message-attachments index))
          (define attachment (list-ref attachments index))
          #; ;; is an attachment size cap necessary?
          (when (> (hash-ref attachment 'size) OPEN_ATTACHMENT_MAX_SIZE_BYTES)
            (return #f))
          (open-attachment-url
           (current-custodian)
           (string->url (hash-ref attachment 'proxy_url)))))
           (string->url (hash-ref attachment 'url)))))

      (define/contract (open-attachment [index 0])
        (->* () (exact-nonnegative-integer?) (or/c input-port? #f))
        (fetch-attachment message-attachments index))

      (define/contract (open-reply-attachment [index 0])
        (->* () (exact-nonnegative-integer?) (or/c input-port? #f))
        (fetch-attachment reply-message-attachments index))

      (define (storage-info type)
        (match type
          ['guild   (cons 65536 'global)]
          ['channel (cons 8192  (string->symbol (hash-ref message 'channel_id)))]
          ['channel (cons 8192  (string->symbol channel-id))]
          ['user    (cons 2048  (string->symbol (message-author-id message)))]
          [_        (cons 0     #f)]))

@@ -238,17 +248,19 @@
        (void))

      (lambda (base trick-obj _args _parent-context)
        `(((message-contents . ,message-contents)
           (message-author   . ,message-author)
           (emote-lookup     . ,(curry hash-ref emote-lookup-cache))
           (emote-image      . ,emote-image)
           (delete-caller    . ,delete-caller)
           (make-attachment  . ,make-attachment)
           (read-storage     . ,(read-storage trick-obj))
           (write-storage    . ,(write-storage trick-obj))
           (attachment-data  . ,http:attachment-data)
           (open-attachment  . ,open-attachment)
           (attachment-count . ,attachment-count)
        `(((message-contents       . ,message-contents)
           (message-author         . ,message-author)
           (emote-lookup           . ,(curry hash-ref emote-lookup-cache))
           (emote-image            . ,emote-image)
           (delete-caller          . ,delete-caller)
           (make-attachment        . ,make-attachment)
           (read-storage           . ,(read-storage trick-obj))
           (write-storage          . ,(write-storage trick-obj))
           (attachment-data        . ,http:attachment-data)
           (open-attachment        . ,open-attachment)
           (open-reply-attachment  . ,open-reply-attachment)
           (attachment-count       . ,attachment-count)
           (reply-attachment-count . ,reply-attachment-count)
           ,@(car base))
          ,@(cdr base))))

diff --git a/scribblings/r16.scrbl b/scribblings/r16.scrbl
index 7b49b31..2c93d06 100644
--- a/scribblings/r16.scrbl
+++ b/scribblings/r16.scrbl
@@ -155,6 +155,14 @@ Returns @racket[#f] if the message doesn't have an @racket[index]th attachment,
if the attachment couldn't be opened for any other reason.
}

@defproc[(open-reply-attachment [index natural? 0]) (or/c input-port? #f)]{
Same as @racket[open-reply-attachment], except fetching the attachment of the message that the invoking message replied to.
}

@defthing[attachment-count natural?]{
The number of files attached to the message that invoked this sandbox.
}

@defthing[reply-attachment-count natural?]{
The number of files attached to the message that the invoking message replied to.
}
-- 
2.25.1

[PATCH 2/2] Fix unfortunate typo Export this patch

From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>

---
 scribblings/r16.scrbl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scribblings/r16.scrbl b/scribblings/r16.scrbl
index 2c93d06..6423c88 100644
--- a/scribblings/r16.scrbl
+++ b/scribblings/r16.scrbl
@@ -156,7 +156,7 @@ if the attachment couldn't be opened for any other reason.
}

@defproc[(open-reply-attachment [index natural? 0]) (or/c input-port? #f)]{
Same as @racket[open-reply-attachment], except fetching the attachment of the message that the invoking message replied to.
Same as @racket[open-attachment], except fetching the attachment of the message that the invoking message replied to.
}

@defthing[attachment-count natural?]{
-- 
2.25.1