Skip to content

Commit 69dedbd

Browse files
committed
fix: gmail, google_drive: remove undefined result while paginating
- check if results returned are undefined, if so, don't add them - also check if final files/messages array length is more than 0
1 parent 6ead253 commit 69dedbd

File tree

4 files changed

+67
-59
lines changed

4 files changed

+67
-59
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dabbu-server",
3-
"version": "1.5.1",
3+
"version": "1.5.2",
44
"description": "A unified API interface to access all your data online",
55
"directories": {
66
"doc": "docs"

src/modules/gmail.js

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ class GmailProvider extends Provider {
260260
let results = []
261261
if (labelIds) {
262262
// List out all the threads labelled with that particular label
263-
let allMessages = []
263+
let allThreads = []
264264
let nextPageToken = null
265265
do {
266-
const listResult = await instance.get("/gmail/v1/users/me/messages", {
266+
const listResult = await instance.get("/gmail/v1/users/me/threads/", {
267267
params: {
268-
q: labelIds.map(id => `label:${id}`).join(" "),
268+
labelIds: labelIds.join(" "),
269269
pageToken: nextPageToken
270270
}
271271
})
@@ -274,64 +274,70 @@ class GmailProvider extends Provider {
274274
nextPageToken = listResult.data.nextPageToken
275275

276276
// 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+
}
278280
} while (nextPageToken) // Keep doing the above list request until there is no nextPageToken returned
279281

280282
// 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+
}
333337
}
334338
}
339+
} else {
340+
return []
335341
}
336342
} else {
337343
// Return all the labels the user or Gmail has created

src/modules/google_drive.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ class GoogleDriveDataProvider extends Provider {
286286
// Get the next page token (incase Google Drive returned incomplete results)
287287
nextPageToken = listResult.data.nextPageToken
288288
// Add the files we got right now to the main list
289-
allFiles = allFiles.concat(listResult.data.items)
289+
if (listResult.data.items) {
290+
allFiles = allFiles.concat(listResult.data.items)
291+
}
290292
} while (nextPageToken) // Keep doing the above list request until there is no nextPageToken returned
291293

292294
// Once we get everything, parse and print the files

0 commit comments

Comments
 (0)