File: threads.go

package info (click to toggle)
aerc 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (64 lines) | stat: -rw-r--r-- 1,668 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package jmap

import (
	"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(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(&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
}