Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Show threads by ID #465

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions commons/src/connections/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ export const fetchThreads = (
limit: number,
offset: number,
): Promise<Thread[]> => {
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`;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a lot faster if the Nylas API had a way to fetch a list of threads in a single call. Also, fetching the expanded view seems like a lot of data. Combined, these queries are slow and would ideally be improved.

// TODO: ideally this wouldn't replicate much of the code from below
return await fetch(queryString, getFetchConfig(query))
.then((response) =>
handleResponse<MiddlewareResponse<Thread>>(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&not_in=trash&limit=${limit}&offset=${offset}`;
Expand Down
6 changes: 5 additions & 1 deletion commons/src/store/mailbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions commons/src/types/Nylas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ConversationQuery extends CommonQuery {

export interface MailboxQuery extends CommonQuery {
query: ThreadsQuery;
thread_ids?: string[];
keywordToSearch?: string;
}

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions components/mailbox/src/Mailbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -181,6 +183,7 @@
query: Object.fromEntries(
new URLSearchParams(_this.query_string?.replaceAll(" ", "&")),
),
thread_ids,
};

if (_this.keyword_to_search) {
Expand Down