mirror of
https://git.sr.ht/~rjarry/aerc
synced 2026-03-02 18:23:33 +01:00
Pass the message context to Do() and Download() methods so that JMAP requests can be cancelled when the user navigates away or closes a message view. This builds on the previous commit that added context to all worker messages. Handle context.Canceled errors in the Run() loop by sending a Cancelled response instead of treating it as a regular error. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Simon Martin <simon@nasilyan.com>
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package jmap
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.sr.ht/~rockorager/go-jmap"
|
|
"git.sr.ht/~rockorager/go-jmap/mail/email"
|
|
"git.sr.ht/~rockorager/go-jmap/mail/thread"
|
|
)
|
|
|
|
func (w *JMAPWorker) fetchEntireThreads(ctx context.Context, threads []jmap.ID) ([]*email.Email, error) {
|
|
var req jmap.Request
|
|
|
|
if len(threads) == 0 {
|
|
return []*email.Email{}, nil
|
|
}
|
|
|
|
threadGetId := req.Invoke(&thread.Get{
|
|
Account: w.AccountId(),
|
|
IDs: threads,
|
|
})
|
|
|
|
// Opportunistically fetch all emails in this thread. We could wait for
|
|
// the result, check which ones we don't have, then fetch only those.
|
|
// However we can do this all in a single request which ends up being
|
|
// faster than two requests for most contexts
|
|
req.Invoke(&email.Get{
|
|
Account: w.AccountId(),
|
|
ReferenceIDs: &jmap.ResultReference{
|
|
ResultOf: threadGetId,
|
|
Name: "Thread/get",
|
|
Path: "/list/*/emailIds",
|
|
},
|
|
Properties: emailProperties,
|
|
BodyProperties: bodyProperties,
|
|
})
|
|
|
|
resp, err := w.Do(ctx, &req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
emailsToReturn := make([]*email.Email, 0)
|
|
for _, inv := range resp.Responses {
|
|
switch r := inv.Args.(type) {
|
|
case *thread.GetResponse:
|
|
if err = w.cache.PutThreadState(r.State); err != nil {
|
|
w.w.Warnf("PutThreadState: %s", err)
|
|
}
|
|
for _, thread := range r.List {
|
|
if err = w.cache.PutThread(thread.ID, thread.EmailIDs); err != nil {
|
|
w.w.Warnf("PutThread: %s", err)
|
|
}
|
|
}
|
|
case *email.GetResponse:
|
|
emailsToReturn = append(emailsToReturn, r.List...)
|
|
if err = w.cache.PutEmailState(r.State); err != nil {
|
|
w.w.Warnf("PutEmailState: %s", err)
|
|
}
|
|
case *jmap.MethodError:
|
|
return nil, wrapMethodError(r)
|
|
}
|
|
}
|
|
|
|
return emailsToReturn, nil
|
|
}
|