diff --git a/apps/mail/app/api/driver/google.ts b/apps/mail/app/api/driver/google.ts index be4b2c51fe..2fb404e73f 100644 --- a/apps/mail/app/api/driver/google.ts +++ b/apps/mail/app/api/driver/google.ts @@ -35,6 +35,12 @@ const findHtmlBody = (parts: any[]): string => { return ""; }; +const auth = new google.auth.OAuth2( + process.env.GOOGLE_CLIENT_ID as string, + process.env.GOOGLE_CLIENT_SECRET as string, + process.env.GOOGLE_REDIRECT_URI as string, +) + interface ParsedDraft { id: string; to?: string[]; @@ -78,12 +84,59 @@ const parseDraft = (draft: gmail_v1.Schema$Draft): ParsedDraft | null => { }; }; +interface ProcessMultipleThreadsProps { + threadIds: string[], + addLabelIds: string[], + removeLabelIds: string[] +} + +async function processMultipleThreads(props: ProcessMultipleThreadsProps){ + + const { threadIds, addLabelIds = [], removeLabelIds = [] } = props + const gmail = google.gmail({version: 'v1', auth}) + + if(threadIds.length === 0){ + throw new Error("No thread IDs provided!") + } + + if(addLabelIds.length === 0 && removeLabelIds.length === 0){ + return + } + + try { + const threadPromises = threadIds.map(async (threadId) => { + const threadResponse = await gmail.users.threads.get({ + userId: "me", + id: threadId + }) + + return threadResponse.data.messages?.map(message => message.id).filter((id): id is string => id != null && id != undefined) || [] + }) + + const messageArrays = await Promise.all(threadPromises) + const allMessagesIds = messageArrays.flat() + + if(allMessagesIds.length > 0){ + await gmail.users.messages.batchModify({ + userId: "me", + requestBody: { + ids: allMessagesIds, + removeLabelIds: removeLabelIds, + addLabelIds: addLabelIds + } + }) + + + + } +} catch(error){ + console.error("Error while processing multiple threads:", error) + throw error +} + +} + export const driver = async (config: IConfig): Promise => { - const auth = new google.auth.OAuth2( - process.env.GOOGLE_CLIENT_ID as string, - process.env.GOOGLE_CLIENT_SECRET as string, - process.env.GOOGLE_REDIRECT_URI as string, - ); const getScope = () => [ @@ -177,22 +230,30 @@ export const driver = async (config: IConfig): Promise => { } }, markAsRead: async (id: string[]) => { - await gmail.users.messages.batchModify({ - userId: "me", - requestBody: { - ids: id, - removeLabelIds: ["UNREAD"], - }, - }); - }, + try { + const args = { + threadIds: id, + addLabelIds: [], + removeLabelIds: ["UNREAD"] + } + await processMultipleThreads(args) + } catch (error) { + console.error('Error marking messages as read:', error); + throw error + } + }, markAsUnread: async (id: string[]) => { - await gmail.users.messages.batchModify({ - userId: "me", - requestBody: { - ids: id, + try { + const args = { + threadIds: id, addLabelIds: ["UNREAD"], - }, - }); + removeLabelIds: [] + } + await processMultipleThreads(args) + } catch (error) { + console.error('Error marking messages as read:', error); + throw error + } }, getScope, getUserInfo: (tokens: { access_token: string; refresh_token: string }) => { diff --git a/apps/mail/components/mail/mail-list.tsx b/apps/mail/components/mail/mail-list.tsx index bcce204982..4e78a0a682 100644 --- a/apps/mail/components/mail/mail-list.tsx +++ b/apps/mail/components/mail/mail-list.tsx @@ -440,14 +440,16 @@ export const MailList = memo(({ isCompact }: MailListProps) => { ref={scrollRef} style={{ height: '100%' }} totalCount={items.length} - itemContent={(_: number, data: InitialThread) => } + itemContent={(_: number, data: InitialThread) => ( + + )} endReached={handleScroll} data={items} className="hide-scrollbar" diff --git a/apps/mail/components/mail/mail.tsx b/apps/mail/components/mail/mail.tsx index 70164e903d..a0ac8528b0 100644 --- a/apps/mail/components/mail/mail.tsx +++ b/apps/mail/components/mail/mail.tsx @@ -501,7 +501,7 @@ function BulkSelectActions() { const categories = [ { - id:'Primary', + id: 'Primary', name: 'common.mailCategories.primary', searchValue: '', icon: , @@ -509,7 +509,7 @@ const categories = [ 'border-0 bg-gray-200 text-gray-700 dark:bg-gray-800/50 dark:text-gray-400 dark:hover:bg-gray-800/70', }, { - id:'Important', + id: 'Important', name: 'common.mailCategories.important', searchValue: 'is:important', icon: , @@ -517,7 +517,7 @@ const categories = [ 'border-0 text-amber-800 bg-amber-100 dark:bg-amber-900/20 dark:text-amber-500 dark:hover:bg-amber-900/30', }, { - id:'Personal', + id: 'Personal', name: 'common.mailCategories.personal', searchValue: 'is:personal', icon: , @@ -525,7 +525,7 @@ const categories = [ 'border-0 text-green-800 bg-green-100 dark:bg-green-900/20 dark:text-green-500 dark:hover:bg-green-900/30', }, { - id:'Updates', + id: 'Updates', name: 'common.mailCategories.updates', searchValue: 'is:updates', icon: , @@ -533,7 +533,7 @@ const categories = [ 'border-0 text-purple-800 bg-purple-100 dark:bg-purple-900/20 dark:text-purple-500 dark:hover:bg-purple-900/30', }, { - id:'Promotions', + id: 'Promotions', name: 'common.mailCategories.promotions', searchValue: 'is:promotions', icon: ,