@@ -260,12 +260,12 @@ class GmailProvider extends Provider {
260
260
let results = [ ]
261
261
if ( labelIds ) {
262
262
// List out all the threads labelled with that particular label
263
- let allMessages = [ ]
263
+ let allThreads = [ ]
264
264
let nextPageToken = null
265
265
do {
266
- const listResult = await instance . get ( "/gmail/v1/users/me/messages " , {
266
+ const listResult = await instance . get ( "/gmail/v1/users/me/threads/ " , {
267
267
params : {
268
- q : labelIds . map ( id => `label: ${ id } ` ) . join ( " " ) ,
268
+ labelIds : labelIds . join ( " " ) ,
269
269
pageToken : nextPageToken
270
270
}
271
271
} )
@@ -274,64 +274,70 @@ class GmailProvider extends Provider {
274
274
nextPageToken = listResult . data . nextPageToken
275
275
276
276
// Add the files we got right now to the main list
277
- allMessages = allMessages . concat ( listResult . data . messages )
277
+ if ( listResult . data . threads ) {
278
+ allThreads = allThreads . concat ( listResult . data . threads )
279
+ }
278
280
} while ( nextPageToken ) // Keep doing the above list request until there is no nextPageToken returned
279
281
280
282
// Loop through the threads
281
- for ( let thread of allMessages ) {
282
- // If the export type is view, get only the metadata, else get everything
283
- const threadResult = await instance . get ( `/gmail/v1/users/me/threads/${ thread . threadId } ` , {
284
- params : {
285
- format : "METADATA"
286
- }
287
- } )
288
-
289
- // If the thread exists, parse it
290
- if ( threadResult . data && threadResult . data . messages ) {
291
- // Get all its messages
292
- const messages = threadResult . data . messages
293
- if ( messages . length > 0 ) {
294
- // Get the first and last messages
295
- const firstMessage = messages [ 0 ]
296
- const lastMessage = messages [ messages . length - 1 ]
297
- // Get the headers of the messages
298
- let firstHeaders = firstMessage . payload . headers
299
- let lastHeaders = lastMessage . payload . headers
300
-
301
- // Get the subject from the last email, as that is what is seen in
302
- // the user's inbox
303
- let subject = "(Empty Subject)"
304
- const subjectHeaders = lastHeaders . filter ( ( header ) => header . name . toLowerCase ( ) === "subject" )
305
- if ( subjectHeaders . length > 0 ) subject = subjectHeaders [ 0 ] . value
306
-
307
- // The created at time is when the first message was sent
308
- let createdAtDate
309
- const createdAtDateHeaders = firstHeaders . filter ( ( header ) => header . name . toLowerCase ( ) === "date" )
310
- if ( createdAtDateHeaders . length > 0 ) createdAtDate = createdAtDateHeaders [ 0 ] . value
311
-
312
- // The last modified time is when the last message was sent
313
- // Note: would be more accurate to use internalDate, but that
314
- // is only returned when retrieving a specific message
315
- let lastModifiedDate
316
- const lastModifiedDateHeaders = lastHeaders . filter ( ( header ) => header . name . toLowerCase ( ) === "date" )
317
- if ( lastModifiedDateHeaders . length > 0 ) lastModifiedDate = lastModifiedDateHeaders [ 0 ] . value
318
-
319
- // The content URI
320
- let contentURI = `https://mail.google.com/mail/u/0/#inbox/${ threadResult . data . id } ` // View in gmail
321
-
322
- // Add this to the results
323
- results . push ( {
324
- name : `${ formatDate ( lastModifiedDate ) } - ${ threadResult . data . id } - ${ subject || "(No subject)" } ` ,
325
- path : diskPath ( params [ "folderPath" ] , `${ formatDate ( lastModifiedDate ) } - ${ threadResult . data . id } - ${ subject || "(No subject)" } ` ) ,
326
- kind : "file" , // An entire thread can be viewed at once. Labels are folders, not threads
327
- mimeType : "mail/thread" , // Weird mime type invented by me TODO: replace this with a proper one
328
- size : NaN , // We have size of messages+attachments, not threads
329
- createdAtTime : new Date ( createdAtDate ) . toISOString ( ) , // When the first message was sent
330
- lastModifiedTime : new Date ( lastModifiedDate ) . toISOString ( ) , // When the last message was sent
331
- contentURI : contentURI // Content URI
332
- } )
283
+ if ( allThreads . length > 0 ) {
284
+ for ( let thread of allThreads ) {
285
+ // If the export type is view, get only the metadata, else get everything
286
+ const threadResult = await instance . get ( `/gmail/v1/users/me/threads/${ thread . id } ` , {
287
+ params : {
288
+ format : "METADATA"
289
+ }
290
+ } )
291
+
292
+ // If the thread exists, parse it
293
+ if ( threadResult . data && threadResult . data . messages ) {
294
+ // Get all its messages
295
+ const messages = threadResult . data . messages
296
+ if ( messages . length > 0 ) {
297
+ // Get the first and last messages
298
+ const firstMessage = messages [ 0 ]
299
+ const lastMessage = messages [ messages . length - 1 ]
300
+ // Get the headers of the messages
301
+ let firstHeaders = firstMessage . payload . headers
302
+ let lastHeaders = lastMessage . payload . headers
303
+
304
+ // Get the subject from the last email, as that is what is seen in
305
+ // the user's inbox
306
+ let subject = "(Empty Subject)"
307
+ const subjectHeaders = lastHeaders . filter ( ( header ) => header . name . toLowerCase ( ) === "subject" )
308
+ if ( subjectHeaders . length > 0 ) subject = subjectHeaders [ 0 ] . value
309
+
310
+ // The created at time is when the first message was sent
311
+ let createdAtDate
312
+ const createdAtDateHeaders = firstHeaders . filter ( ( header ) => header . name . toLowerCase ( ) === "date" )
313
+ if ( createdAtDateHeaders . length > 0 ) createdAtDate = createdAtDateHeaders [ 0 ] . value
314
+
315
+ // The last modified time is when the last message was sent
316
+ // Note: would be more accurate to use internalDate, but that
317
+ // is only returned when retrieving a specific message
318
+ let lastModifiedDate
319
+ const lastModifiedDateHeaders = lastHeaders . filter ( ( header ) => header . name . toLowerCase ( ) === "date" )
320
+ if ( lastModifiedDateHeaders . length > 0 ) lastModifiedDate = lastModifiedDateHeaders [ 0 ] . value
321
+
322
+ // The content URI
323
+ let contentURI = `https://mail.google.com/mail/u/0/#inbox/${ threadResult . data . id } ` // View in gmail
324
+
325
+ // Add this to the results
326
+ results . push ( {
327
+ name : `${ formatDate ( lastModifiedDate ) } - ${ threadResult . data . id } - ${ subject || "(No subject)" } ` ,
328
+ path : diskPath ( params [ "folderPath" ] , `${ formatDate ( lastModifiedDate ) } - ${ threadResult . data . id } - ${ subject || "(No subject)" } ` ) ,
329
+ kind : "file" , // An entire thread can be viewed at once. Labels are folders, not threads
330
+ mimeType : "mail/thread" , // Weird mime type invented by me TODO: replace this with a proper one
331
+ size : NaN , // We have size of messages+attachments, not threads
332
+ createdAtTime : new Date ( createdAtDate ) . toISOString ( ) , // When the first message was sent
333
+ lastModifiedTime : new Date ( lastModifiedDate ) . toISOString ( ) , // When the last message was sent
334
+ contentURI : contentURI // Content URI
335
+ } )
336
+ }
333
337
}
334
338
}
339
+ } else {
340
+ return [ ]
335
341
}
336
342
} else {
337
343
// Return all the labels the user or Gmail has created
0 commit comments