-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add 'Delete All Spam' button on /mail/spam route (#886) #888
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
Closed
giteshsarvaiya
wants to merge
3
commits into
Mail-0:staging
from
giteshsarvaiya:feat/spam-delete-all-button-886
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; | ||
| import { appRouter } from '@/app/trpc/root'; | ||
| import { createTRPCContext } from '@/app/trpc/trpc'; | ||
|
|
||
| // Handle tRPC requests | ||
| export const POST = async (req: Request) => { | ||
| return fetchRequestHandler({ | ||
| endpoint: '/api/trpc', | ||
| req, | ||
| router: appRouter, | ||
| createContext: () => createTRPCContext({ headers: req.headers }), | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { router } from '@/app/trpc/trpc'; | ||
| import { mailRouter } from '@/app/trpc/router/mail'; | ||
|
|
||
| export const appRouter = router({ | ||
| mail: mailRouter, | ||
| }); | ||
|
|
||
| export type AppRouter = typeof appRouter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { z } from 'zod'; | ||
| import { procedure, router } from '@/app/trpc/trpc'; | ||
| import { getActiveDriver } from '@/actions/utils'; | ||
| import { bulkDeleteThread } from '@/actions/mail'; | ||
| import type { InitialThread } from '@/types'; | ||
|
|
||
| // Define interface for the response type | ||
| interface SpamFolderResponse { | ||
| threads: InitialThread[]; | ||
| nextPageToken?: string; | ||
| } | ||
|
|
||
| // Function to get all spam emails with pagination | ||
| async function getAllSpamEmails(driver: any) { | ||
| const allThreads: InitialThread[] = []; | ||
| let pageToken: string | undefined = undefined; | ||
|
|
||
| do { | ||
| // Request a large batch size to minimize the number of API calls | ||
| const response: SpamFolderResponse = await driver.list( | ||
| 'spam', // folder | ||
| undefined, // query | ||
| 500, // maxResults - Maximum allowed by Gmail API | ||
| undefined, // labelIds | ||
| pageToken // pageToken | ||
| ); | ||
|
|
||
| if (response.threads && response.threads.length > 0) { | ||
| allThreads.push(...response.threads); | ||
| } | ||
|
|
||
| pageToken = response.nextPageToken; | ||
| } while (pageToken); | ||
|
|
||
| return { threads: allThreads }; | ||
| } | ||
|
|
||
| export const mailRouter = router({ | ||
| deleteAllSpam: procedure | ||
| .mutation(async () => { | ||
| try { | ||
| const driver = await getActiveDriver(); | ||
|
|
||
| // Get ALL emails from spam folder using pagination | ||
| const spamEmails = await getAllSpamEmails(driver); | ||
|
|
||
| if (!spamEmails?.threads?.length) { | ||
| return { | ||
| success: true, | ||
| message: 'No spam emails to delete' | ||
| }; | ||
| } | ||
|
|
||
| // Extract all email IDs | ||
| const emailIds = spamEmails.threads.map((thread) => thread.id); | ||
|
|
||
| // Move them to trash | ||
| await bulkDeleteThread({ ids: emailIds }); | ||
|
|
||
| return { | ||
| success: true, | ||
| message: `Successfully deleted ${emailIds.length} email(s) from spam folder` | ||
| }; | ||
| } catch (error) { | ||
| console.error('Error deleting all spam emails:', error); | ||
| throw error; | ||
| } | ||
| }), | ||
|
|
||
| // Add a separate procedure to check if spam folder has emails | ||
| checkSpamEmails: procedure | ||
| .query(async () => { | ||
| try { | ||
| const driver = await getActiveDriver(); | ||
| const spamEmails = await driver.list('spam'); | ||
|
|
||
| return { | ||
| hasEmails: !!spamEmails?.threads?.length, | ||
| count: spamEmails?.threads?.length || 0 | ||
| }; | ||
| } catch (error) { | ||
| console.error('Error checking spam emails:', error); | ||
| return { hasEmails: false, count: 0 }; | ||
| } | ||
| }), | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { initTRPC } from '@trpc/server'; | ||
| import superjson from 'superjson'; | ||
| import { ZodError } from 'zod'; | ||
|
|
||
| // Create context for tRPC requests | ||
| export const createTRPCContext = async (opts: { headers: Headers }) => { | ||
| return { | ||
| headers: opts.headers, | ||
| }; | ||
| }; | ||
|
|
||
| // Initialize tRPC | ||
| const t = initTRPC.context<typeof createTRPCContext>().create({ | ||
| transformer: superjson, | ||
| errorFormatter({ shape, error }) { | ||
| return { | ||
| ...shape, | ||
| data: { | ||
| ...shape.data, | ||
| zodError: | ||
| error.cause instanceof ZodError ? error.cause.flatten() : null, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| // Export tRPC utilities | ||
| export const { router, procedure, middleware } = t; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // components/providers/trpc-provider.tsx | ||
| 'use client'; | ||
|
|
||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { httpBatchLink, loggerLink } from '@trpc/client'; | ||
| import { useState } from 'react'; | ||
| import { api } from '@/utils/trpc'; | ||
| import superjson from 'superjson'; | ||
|
|
||
| export function TRPCProvider({ children }: { children: React.ReactNode }) { | ||
| const [queryClient] = useState(() => new QueryClient()); | ||
| const [trpcClient] = useState(() => | ||
| api.createClient({ | ||
| links: [ | ||
| loggerLink({ | ||
| enabled: (opts) => | ||
| process.env.NODE_ENV === 'development' || | ||
| (opts.direction === 'down' && opts.result instanceof Error), | ||
| }), | ||
| httpBatchLink({ | ||
| url: '/api/trpc', | ||
| }), | ||
| ], | ||
| transformer: superjson, | ||
| }) | ||
| ); | ||
|
|
||
| return ( | ||
| <api.Provider client={trpcClient} queryClient={queryClient}> | ||
| <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
| </api.Provider> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this deletes the default count of
listwhich is 20 threads in the spam folder, which is the number of threads on the user's page, it doesn't deleteallThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use trpc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yess, I see that the maxResult is set to 20 by default and can go upto 500(as per the gmail api provider). so yes, tRPC would be a better approach.
my next commit will have tRPC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MrgSub pls check now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if we introduced pagination(to fetch all spams with maxResult 500) and implemented it with the same logic as of previous commit, i.e. without tRPC ? is there a specific reason we chose tRPC over server action ?