| 16 | * @returns Object containing threading headers (messageId, references, subject) |
| 17 | */ |
| 18 | export async function fetchThreadingHeaders( |
| 19 | messageId: string, |
| 20 | accessToken: string |
| 21 | ): Promise<{ |
| 22 | messageId?: string |
| 23 | references?: string |
| 24 | subject?: string |
| 25 | }> { |
| 26 | try { |
| 27 | const messageResponse = await fetch( |
| 28 | `${GMAIL_API_BASE}/messages/${messageId}?format=metadata&metadataHeaders=Message-ID&metadataHeaders=References&metadataHeaders=Subject`, |
| 29 | { |
| 30 | headers: { |
| 31 | Authorization: `Bearer ${accessToken}`, |
| 32 | }, |
| 33 | } |
| 34 | ) |
| 35 | |
| 36 | if (messageResponse.ok) { |
| 37 | const messageData = await messageResponse.json() |
| 38 | const headers = messageData.payload?.headers || [] |
| 39 | |
| 40 | return { |
| 41 | messageId: headers.find((h: any) => h.name.toLowerCase() === 'message-id')?.value, |
| 42 | references: headers.find((h: any) => h.name.toLowerCase() === 'references')?.value, |
| 43 | subject: headers.find((h: any) => h.name.toLowerCase() === 'subject')?.value, |
| 44 | } |
| 45 | } |
| 46 | } catch (error) { |
| 47 | // Continue without threading headers rather than failing |
| 48 | } |
| 49 | |
| 50 | return {} |
| 51 | } |
| 52 | |
| 53 | // Helper function to process a Gmail message |
| 54 | export async function processMessage( |