Skip to content

Commit c56764c

Browse files
committed
improvement(org): list Gmail threads and read the mailbox address together
1 parent 528d661 commit c56764c

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎apps/sim/lib/organizations/home/provider-reads.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,27 @@ describe('sharing a Gmail read across accounts', () => {
325325
expect(reconnected.threads[0].url).toBe(gmailThreadUrl('t', 'new@example.com'))
326326
})
327327

328+
it('does not wait for the listing before asking for the mailbox address', async () => {
329+
let listed: (response: Response) => void = () => {}
330+
providerFetch.mockImplementation((url: string) =>
331+
url.includes('/threads?')
332+
? new Promise((resolve) => {
333+
listed = resolve
334+
})
335+
: Promise.resolve(respond(gmailThread('t', [metadataMessage('Ada', 'S', 'P')])))
336+
)
337+
const read = readGmailThreadsPage({
338+
accessToken: 'token',
339+
signal,
340+
credentialId: 'mailbox-parallel',
341+
accountId: 'account',
342+
count: 1,
343+
})
344+
await vi.waitFor(() => expect(mailboxEmail).toHaveBeenCalledOnce())
345+
listed(respond({ threads: [{ id: 't' }] }))
346+
await expect(read).resolves.toMatchObject({ threads: [{ id: 't' }] })
347+
})
348+
328349
it('does not cache a failed mailbox lookup', async () => {
329350
providerFetch.mockImplementation(async (url: string) =>
330351
url.includes('/threads?')

‎apps/sim/lib/organizations/home/provider-reads.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,15 @@ export async function readGmailThreads({
217217
threads: HomeEmailThread[]
218218
cursor: GmailThreadCursor
219219
}> {
220-
const list = await runTool(
221-
gmailListThreadsV2Tool,
222-
{ accessToken, labelIds: 'INBOX', maxResults: HOME_EMAIL_THREAD_LIMIT },
223-
signal
224-
)
220+
const [list, mailbox] = await Promise.all([
221+
runTool(
222+
gmailListThreadsV2Tool,
223+
{ accessToken, labelIds: 'INBOX', maxResults: HOME_EMAIL_THREAD_LIMIT },
224+
signal
225+
),
226+
readGmailMailbox({ accessToken, signal, credentialId, accountId }),
227+
])
225228
if (!list.success) throw new HomeProviderReadError('unavailable')
226-
const mailbox = await readGmailMailbox({ accessToken, signal, credentialId, accountId })
227229
const cursor: GmailThreadCursor = {
228230
mailbox,
229231
unreadThreadIds: list.output.threads

0 commit comments

Comments
 (0)