Skip to content

Commit

Permalink
Fix load more test emails and use batch query
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Jan 3, 2025
1 parent c72ba84 commit e526510
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions apps/web/app/api/google/messages/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { type NextRequest, NextResponse } from "next/server";
import { auth } from "@/app/api/auth/[...nextauth]/auth";
import { getGmailClient } from "@/utils/gmail/client";
import { parseMessage } from "@/utils/mail";
import { getMessage } from "@/utils/gmail/message";
import { queryBatchMessages } from "@/utils/gmail/message";
import { withError } from "@/utils/middleware";
import { SafeError } from "@/utils/error";
import { messageQuerySchema } from "@/app/api/google/messages/validation";
import { isDefined } from "@/utils/types";

export type MessagesResponse = Awaited<ReturnType<typeof getMessages>>;

Expand All @@ -19,46 +17,34 @@ async function getMessages({
}) {
const session = await auth();
if (!session?.user.email) throw new SafeError("Not authenticated");
if (!session.accessToken) throw new SafeError("Missing access token");

const gmail = getGmailClient(session);

const messages = await gmail.users.messages.list({
userId: "me",
maxResults: 20,
pageToken: pageToken ?? undefined,
q: query?.trim(),
});

const fullMessages = (
await Promise.all(
(messages.data.messages || []).map(async (m) => {
if (!m.id) return null;
const message = await getMessage(m.id, gmail);
return parseMessage(message);
}),
)
).filter(isDefined);
const { messages, nextPageToken } = await queryBatchMessages(
gmail,
session.accessToken,
{
query: query?.trim(),
maxResults: 20,
pageToken: pageToken ?? undefined,
},
);

// filter out messages from the user
// NOTE: -from:me doesn't work because it filters out messages from threads where the user responded
const incomingMessages = fullMessages.filter(
const incomingMessages = messages.filter(
(message) => !message.headers.from.includes(session.user.email!),
);

return {
messages: incomingMessages,
nextPageToken: messages.data.nextPageToken,
};
return { messages: incomingMessages, nextPageToken };
}

export const GET = withError(async (request: NextRequest) => {
const { searchParams } = new URL(request.url);
const query = searchParams.get("q");
const pageToken = searchParams.get("page");
const pageToken = searchParams.get("pageToken");
const r = messageQuerySchema.parse({ q: query, pageToken });
const result = await getMessages({
query: r.q,
pageToken: r.pageToken,
});
const result = await getMessages({ query: r.q, pageToken: r.pageToken });
return NextResponse.json(result);
});

1 comment on commit e526510

@vercel
Copy link

@vercel vercel bot commented on e526510 Jan 3, 2025

Choose a reason for hiding this comment

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

Please sign in to comment.