diff --git a/commons/src/connections/threads.ts b/commons/src/connections/threads.ts index b1cb51b0..d5f5c00b 100644 --- a/commons/src/connections/threads.ts +++ b/commons/src/connections/threads.ts @@ -17,6 +17,33 @@ export const fetchThreads = ( limit: number, offset: number, ): Promise => { + if (query.thread_ids) { + // &limit=${limit}&offset=${offset} + const page_of_ids = query.thread_ids.slice(offset, offset + limit); + return Promise.all( + page_of_ids.map(async (thread_id) => { + // Nylas API does not support fetching threads in bulk, so must fetch them one + // at a time :( + const queryString = `${getMiddlewareApiUrl( + query.component_id, + )}/threads/${thread_id}?view=expanded`; + // TODO: ideally this wouldn't replicate much of the code from below + return await fetch(queryString, getFetchConfig(query)) + .then((response) => + handleResponse>(response), + ) + .then((json) => json.response) + // TODO: Remove this ugly hack when we fix the API from returning ghost messages (e.g. w/o a from/to field) + .then((thread) => ({ + ...thread, + messages: thread.messages.filter( + (message) => message.from.length !== 0 || message.to.length !== 0, + ), + })) + .catch((error) => handleError(query.component_id, error)); + }), + ); + } let queryString = `${getMiddlewareApiUrl( query.component_id, )}/threads?view=expanded¬_in=trash&limit=${limit}&offset=${offset}`; diff --git a/commons/src/store/mailbox.ts b/commons/src/store/mailbox.ts index 1e44bbec..db27c9ed 100644 --- a/commons/src/store/mailbox.ts +++ b/commons/src/store/mailbox.ts @@ -50,11 +50,14 @@ function initializeThreads() { const queryKey = JSON.stringify(query); if (!query.component_id && !query.access_token) { + // FIXME: This should alert the user return []; } if (totalItems === undefined || forceRefresh) { - const threadCount = await fetchThreadCount(query).catch(silence); + const threadCount = query.thread_ids + ? query.thread_ids.length + : await fetchThreadCount(query).catch(silence); if (threadCount) { totalItems = threadCount; @@ -67,6 +70,7 @@ function initializeThreads() { } if (typeof threadsMap[queryKey][currentPage] === "undefined") { + // FIXME: Shouldn't this be an internal error? return []; } else if (!threadsMap[queryKey][currentPage].isLoaded) { const threads = await fetchThreads( diff --git a/commons/src/types/Nylas.ts b/commons/src/types/Nylas.ts index 747df0f9..eed45a50 100644 --- a/commons/src/types/Nylas.ts +++ b/commons/src/types/Nylas.ts @@ -24,6 +24,7 @@ export interface ConversationQuery extends CommonQuery { export interface MailboxQuery extends CommonQuery { query: ThreadsQuery; + thread_ids?: string[]; keywordToSearch?: string; } @@ -218,6 +219,7 @@ export interface EmailProperties extends Manifest { export interface MailboxProperties extends Manifest { actions_bar: MailboxActions[]; all_threads: Thread[]; + thread_ids: string[]; header: string; items_per_page: number; keyword_to_search: string; diff --git a/components/mailbox/src/Mailbox.svelte b/components/mailbox/src/Mailbox.svelte index 87ecc552..da3c0fd9 100644 --- a/components/mailbox/src/Mailbox.svelte +++ b/components/mailbox/src/Mailbox.svelte @@ -50,6 +50,7 @@ export let actions_bar: MailboxActions[]; export let all_threads: Thread[]; + export let thread_ids: string[]; export let header: string = "Mailbox"; export let items_per_page: number; export let keyword_to_search: string; @@ -70,6 +71,7 @@ actions_bar: [], items_per_page: 13, query_string: "in=inbox", + thread_ids: undefined, show_star: false, show_thread_checkbox: true, show_reply: false, @@ -181,6 +183,7 @@ query: Object.fromEntries( new URLSearchParams(_this.query_string?.replaceAll(" ", "&")), ), + thread_ids, }; if (_this.keyword_to_search) {