From 9d03237d4c067c72b1971e89a70087dce5b13263 Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:16:24 +0200 Subject: [PATCH] Whitelist inbox zero --- apps/web/app/(app)/assess.tsx | 5 +++++ apps/web/env.ts | 1 + apps/web/utils/actions/whitelist.ts | 32 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 apps/web/utils/actions/whitelist.ts diff --git a/apps/web/app/(app)/assess.tsx b/apps/web/app/(app)/assess.tsx index 0a05db197..6042b82da 100644 --- a/apps/web/app/(app)/assess.tsx +++ b/apps/web/app/(app)/assess.tsx @@ -3,11 +3,16 @@ import { useEffect } from "react"; import type { AssessUserResponse } from "@/app/api/user/assess/route"; import { postRequest } from "@/utils/api"; +import { whitelistInboxZeroAction } from "@/utils/actions/whitelist"; export function AssessUser() { useEffect(() => { postRequest("/api/user/assess", {}); }, []); + useEffect(() => { + whitelistInboxZeroAction(); + }, []); + return null; } diff --git a/apps/web/env.ts b/apps/web/env.ts index df1c70309..bde9a708c 100644 --- a/apps/web/env.ts +++ b/apps/web/env.ts @@ -48,6 +48,7 @@ export const env = createEnv({ .transform((value) => value?.split(",")), WEBHOOK_URL: z.string().optional(), INTERNAL_API_KEY: z.string().optional(), + WHITELIST_FROM: z.string().optional(), // license LICENSE_1_SEAT_VARIANT_ID: z.coerce.number().optional(), diff --git a/apps/web/utils/actions/whitelist.ts b/apps/web/utils/actions/whitelist.ts new file mode 100644 index 000000000..8e16ec83b --- /dev/null +++ b/apps/web/utils/actions/whitelist.ts @@ -0,0 +1,32 @@ +"use server"; + +import { env } from "@/env"; +import { getSessionAndGmailClient } from "@/utils/actions/helpers"; +import { withActionInstrumentation } from "@/utils/actions/middleware"; +import { isActionError } from "@/utils/error"; +import { createFilter } from "@/utils/gmail/filter"; +import { INBOX_LABEL_ID, SPAM_LABEL_ID } from "@/utils/gmail/label"; + +export const whitelistInboxZeroAction = withActionInstrumentation( + "whitelistInboxZero", + async () => { + if (!env.WHITELIST_FROM) return; + + const sessionResult = await getSessionAndGmailClient(); + if (isActionError(sessionResult)) return sessionResult; + const { gmail } = sessionResult; + + await createFilter({ + gmail, + from: env.WHITELIST_FROM, + addLabelIds: [INBOX_LABEL_ID], + removeLabelIds: [ + "CATEGORY_PROMOTIONS", + "CATEGORY_SOCIAL", + "CATEGORY_UPDATES", + "CATEGORY_FORUMS", + SPAM_LABEL_ID, + ], + }); + }, +);