| 1 |
package jmap |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
|
| 6 |
"git.sr.ht/~rockorager/go-jmap" |
| 7 |
"git.sr.ht/~rockorager/go-jmap/mail/email" |
| 8 |
"git.sr.ht/~rockorager/go-jmap/mail/thread" |
| 9 |
) |
| 10 |
|
| 11 |
func (w *JMAPWorker) fetchEntireThreads(ctx context.Context, threads []jmap.ID) ([]*email.Email, error) { |
| 12 |
var req jmap.Request |
| 13 |
|
| 14 |
if len(threads) == 0 { |
| 15 |
return []*email.Email{}, nil |
| 16 |
} |
| 17 |
|
| 18 |
threadGetId := req.Invoke(&thread.Get{ |
| 19 |
Account: w.AccountId(), |
| 20 |
IDs: threads, |
| 21 |
}) |
| 22 |
|
| 23 |
// Opportunistically fetch all emails in this thread. We could wait for |
| 24 |
// the result, check which ones we don't have, then fetch only those. |
| 25 |
// However we can do this all in a single request which ends up being |
| 26 |
// faster than two requests for most contexts |
| 27 |
req.Invoke(&email.Get{ |
| 28 |
Account: w.AccountId(), |
| 29 |
ReferenceIDs: &jmap.ResultReference{ |
| 30 |
ResultOf: threadGetId, |
| 31 |
Name: "Thread/get", |
| 32 |
Path: "/list/*/emailIds", |
| 33 |
}, |
| 34 |
Properties: emailProperties, |
| 35 |
BodyProperties: bodyProperties, |
| 36 |
}) |
| 37 |
|
| 38 |
resp, err := w.Do(ctx, &req) |
| 39 |
if err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
|
| 43 |
emailsToReturn := make([]*email.Email, 0) |
| 44 |
for _, inv := range resp.Responses { |
| 45 |
switch r := inv.Args.(type) { |
| 46 |
case *thread.GetResponse: |
| 47 |
if err = w.cache.PutThreadState(r.State); err != nil { |
| 48 |
w.w.Warnf("PutThreadState: %s", err) |
| 49 |
} |
| 50 |
for _, thread := range r.List { |
| 51 |
if err = w.cache.PutThread(thread.ID, thread.EmailIDs); err != nil { |
| 52 |
w.w.Warnf("PutThread: %s", err) |
| 53 |
} |
| 54 |
} |
| 55 |
case *email.GetResponse: |
| 56 |
emailsToReturn = append(emailsToReturn, r.List...) |
| 57 |
if err = w.cache.PutEmailState(r.State); err != nil { |
| 58 |
w.w.Warnf("PutEmailState: %s", err) |
| 59 |
} |
| 60 |
case *jmap.MethodError: |
| 61 |
return nil, wrapMethodError(r) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return emailsToReturn, nil |
| 66 |
} |