summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-03-14 21:51:29 -0400
committerDrew DeVault <sir@cmpwn.com>2019-03-14 21:51:29 -0400
commit11f0a7267fd1a9d1c6dd55e1dc044b8ed639bbc0 (patch)
tree80e2456edadfdcd7c6a8e02ab4830c1139816174
parent0f8b7a1203309ebec0dc78baf3f195671eadac2d (diff)
downloadaerc-11f0a7267fd1a9d1c6dd55e1dc044b8ed639bbc0.tar.gz
Implement message store side of message fetching
-rw-r--r--widgets/account.go4
-rw-r--r--widgets/msglist.go62
-rw-r--r--worker/imap/open.go9
-rw-r--r--worker/types/messages.go4
4 files changed, 58 insertions, 21 deletions
diff --git a/widgets/account.go b/widgets/account.go
index 8857535..5747f4b 100644
--- a/widgets/account.go
+++ b/widgets/account.go
@@ -58,7 +58,7 @@ func NewAccountView(conf *config.AccountConfig,
 	dirlist := NewDirectoryList(conf, logger, worker)
 	grid.AddChild(ui.NewBordered(dirlist, ui.BORDER_RIGHT)).Span(2, 1)
 
-	msglist := NewMessageList(logger, worker)
+	msglist := NewMessageList(logger)
 	grid.AddChild(msglist).At(0, 1)
 
 	acct := &AccountView{
@@ -183,7 +183,7 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
 		if store, ok := acct.msgStores[msg.Name]; ok {
 			store.Update(msg)
 		} else {
-			acct.msgStores[msg.Name] = NewMessageStore(msg)
+			acct.msgStores[msg.Name] = NewMessageStore(acct.worker, msg)
 		}
 	case *types.DirectoryContents:
 		store := acct.msgStores[acct.dirlist.selected]
diff --git a/widgets/msglist.go b/widgets/msglist.go
index 8e3c2eb..fef396b 100644
--- a/widgets/msglist.go
+++ b/widgets/msglist.go
@@ -3,6 +3,7 @@ package widgets
 import (
 	"log"
 
+	"github.com/emersion/go-imap"
 	"github.com/gdamore/tcell"
 
 	"git.sr.ht/~sircmpwn/aerc2/config"
@@ -12,20 +13,52 @@ import (
 
 type MessageStore struct {
 	DirInfo  types.DirectoryInfo
-	Messages map[uint64]*types.MessageInfo
+	Messages map[uint32]*types.MessageInfo
+	// Map of uids we've asked the worker to fetch
+	onUpdate       func(store *MessageStore)
+	pendingBodies  map[uint32]interface{}
+	pendingHeaders map[uint32]interface{}
+	worker         *types.Worker
 }
 
-func NewMessageStore(dirInfo *types.DirectoryInfo) *MessageStore {
-	return &MessageStore{DirInfo: *dirInfo}
+func NewMessageStore(worker *types.Worker,
+	dirInfo *types.DirectoryInfo) *MessageStore {
+
+	return &MessageStore{
+		DirInfo: *dirInfo,
+
+		pendingBodies:  make(map[uint32]interface{}),
+		pendingHeaders: make(map[uint32]interface{}),
+		worker:         worker,
+	}
+}
+
+func (store *MessageStore) FetchHeaders(uids []uint32) {
+	// TODO: this could be optimized by pre-allocating toFetch and trimming it
+	// at the end. In practice we expect to get most messages back in one frame.
+	var toFetch imap.SeqSet
+	for _, uid := range uids {
+		if _, ok := store.pendingHeaders[uid]; !ok {
+			toFetch.AddNum(uint32(uid))
+			store.pendingHeaders[uid] = nil
+		}
+	}
+	if !toFetch.Empty() {
+		store.worker.PostAction(&types.FetchMessageHeaders{
+			Uids: toFetch,
+		}, nil)
+	}
 }
 
 func (store *MessageStore) Update(msg types.WorkerMessage) {
+	update := false
 	switch msg := msg.(type) {
 	case *types.DirectoryInfo:
 		store.DirInfo = *msg
+		update = true
 		break
 	case *types.DirectoryContents:
-		newMap := make(map[uint64]*types.MessageInfo)
+		newMap := make(map[uint32]*types.MessageInfo)
 		for _, uid := range msg.Uids {
 			if msg, ok := store.Messages[uid]; ok {
 				newMap[uid] = msg
@@ -34,11 +67,23 @@ func (store *MessageStore) Update(msg types.WorkerMessage) {
 			}
 		}
 		store.Messages = newMap
+		update = true
 		break
 	case *types.MessageInfo:
 		store.Messages[msg.Uid] = msg
+		if _, ok := store.pendingHeaders[msg.Uid]; msg.Envelope != nil && ok {
+			delete(store.pendingHeaders, msg.Uid)
+		}
+		update = true
 		break
 	}
+	if update && store.onUpdate != nil {
+		store.onUpdate(store)
+	}
+}
+
+func (store *MessageStore) OnUpdate(fn func(store *MessageStore)) {
+	store.onUpdate = fn
 }
 
 type MessageList struct {
@@ -47,15 +92,13 @@ type MessageList struct {
 	onInvalidate func(d ui.Drawable)
 	spinner      *Spinner
 	store        *MessageStore
-	worker       *types.Worker
 }
 
 // TODO: fish in config
-func NewMessageList(logger *log.Logger, worker *types.Worker) *MessageList {
+func NewMessageList(logger *log.Logger) *MessageList {
 	ml := &MessageList{
 		logger:  logger,
 		spinner: NewSpinner(),
-		worker:  worker,
 	}
 	ml.spinner.OnInvalidate(func(_ ui.Drawable) {
 		ml.Invalidate()
@@ -84,7 +127,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
 	}
 
 	var (
-		needsHeaders []uint64
+		needsHeaders []uint32
 		row          int = 0
 	)
 
@@ -102,12 +145,11 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
 	}
 
 	if len(needsHeaders) != 0 {
+		ml.store.FetchHeaders(needsHeaders)
 		ml.spinner.Start()
 	} else {
 		ml.spinner.Stop()
 	}
-
-	// TODO: Fetch these messages
 }
 
 func (ml *MessageList) SetStore(store *MessageStore) {
diff --git a/worker/imap/open.go b/worker/imap/open.go
index 0f25c5e..87c4fb3 100644
--- a/worker/imap/open.go
+++ b/worker/imap/open.go
@@ -29,7 +29,7 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents(
 	go func() {
 		seqSet := &imap.SeqSet{}
 		seqSet.AddRange(1, imapw.selected.Messages)
-		uid32, err := imapw.client.UidSearch(&imap.SearchCriteria{
+		uids, err := imapw.client.UidSearch(&imap.SearchCriteria{
 			SeqNum: seqSet,
 		})
 		if err != nil {
@@ -38,12 +38,7 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents(
 				Error:   err,
 			}, nil)
 		} else {
-			imapw.worker.Logger.Printf("Found %d UIDs", len(uid32))
-			var uids []uint64
-			for _, uid := range uid32 {
-				uids = append(uids,
-					(uint64(imapw.selected.UidValidity)<<32)|uint64(uid))
-			}
+			imapw.worker.Logger.Printf("Found %d UIDs", len(uids))
 			imapw.worker.PostMessage(&types.DirectoryContents{
 				Message: types.RespondTo(msg),
 				Uids:    uids,
diff --git a/worker/types/messages.go b/worker/types/messages.go
index d44624d..3f1a39f 100644
--- a/worker/types/messages.go
+++ b/worker/types/messages.go
@@ -110,7 +110,7 @@ type DirectoryInfo struct {
 
 type DirectoryContents struct {
 	Message
-	Uids []uint64
+	Uids []uint32
 }
 
 type MessageInfo struct {
@@ -120,5 +120,5 @@ type MessageInfo struct {
 	InternalDate time.Time
 	Mail         *mail.Message
 	Size         uint32
-	Uid          uint64
+	Uid          uint32
 }
g' href='/danisanti/profani-tty/commit/src/log.c?id=e4ac23e9a654004e8e5806c11353887cc1a1aafb'>e4ac23e9 ^
e8314106 ^


a2726b6a ^

e8314106 ^
9c433e30 ^




9312333b ^



















































291c6bc3 ^
279737ba ^
a2726b6a ^
39627be1 ^
2bbac1c8 ^

a2726b6a ^
0f3c1e56 ^


2bbac1c8 ^

e6987387 ^
2bbac1c8 ^
a2726b6a ^
2bbac1c8 ^


a2726b6a ^
0f3c1e56 ^


2bbac1c8 ^

e6987387 ^
2bbac1c8 ^
a2726b6a ^
2bbac1c8 ^


a2726b6a ^
0f3c1e56 ^


2bbac1c8 ^

e6987387 ^
2bbac1c8 ^
a2726b6a ^
2bbac1c8 ^


a2726b6a ^
0f3c1e56 ^


2bbac1c8 ^
5eaf687d ^

279737ba ^
a2726b6a ^
bbb0fbed ^
2bbac1c8 ^
0277ffe6 ^




25e56fb4 ^
75cfe388 ^
25e56fb4 ^

74a88ad5 ^

55f49f12 ^
67a10992 ^

f8ff9323 ^
67a10992 ^

74a88ad5 ^
2bbac1c8 ^
74a88ad5 ^
2bbac1c8 ^
4d190a9c ^

279737ba ^

4d190a9c ^
f8ff9323 ^
c08d0e9c ^
41724218 ^
2fc588be ^

bbb0fbed ^
2bbac1c8 ^

a2726b6a ^
2bbac1c8 ^
41724218 ^
82a2b17c ^
2bbac1c8 ^
ff8065ea ^
6dbea7e1 ^
4220a183 ^
6dbea7e1 ^

2bbac1c8 ^
6bad38c2 ^
2bbac1c8 ^
6d329343 ^
291c6bc3 ^
0277ffe6 ^
d6e92f62 ^
a4a23fdf ^


291c6bc3 ^
2bbac1c8 ^


e9aaba93 ^

c90c83f7 ^
e9aaba93 ^
c90c83f7 ^
e9aaba93 ^
c90c83f7 ^
e9aaba93 ^
c90c83f7 ^
e9aaba93 ^
c90c83f7 ^
e9aaba93 ^
0030ae58 ^
e9aaba93 ^
b8207207 ^
e9aaba93 ^

c90c83f7 ^
e9aaba93 ^
c90c83f7 ^

b302c604 ^
















e8314106 ^


a2726b6a ^

e8314106 ^

e8314106 ^










35aecd42 ^
e8314106 ^













9c433e30 ^

3ecb5424 ^









e8314106 ^



e8314106 ^




e8314106 ^




3ecb5424 ^
a2726b6a ^
3ecb5424 ^


e8314106 ^




































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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374