Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(email): scope sender Signal/Noise/Block to the thread's inbox (#5694)
Evan Hutnika69c3a1eb555
diff --git a/apps/web/src/features/block-email/component/EmailContext.tsx b/apps/web/src/features/block-email/component/EmailContext.tsxindex 7d48e781de..e5ed8e85b7 100644--- a/apps/web/src/features/block-email/component/EmailContext.tsx+++ b/apps/web/src/features/block-email/component/EmailContext.tsx@@ -763,14 +763,20 @@ export function EmailProvider(props: FlowProps<{ threadID: string }>) {const markSenderSignal = () => {const senderEmail = getSenderEmail();if (!senderEmail) return false;- markSenderSignalWithToast(senderEmail);+ markSenderSignalWithToast(+ senderEmail,+ toHeaderLinkId(threadQuery.data?.link_id)+ );return true;};const markSenderNoise = () => {const senderEmail = getSenderEmail();if (!senderEmail) return false;- markSenderNoiseWithToast(senderEmail);+ markSenderNoiseWithToast(+ senderEmail,+ toHeaderLinkId(threadQuery.data?.link_id)+ );return true;};diff --git a/apps/web/src/features/next-soup/actions/make-block-sender-action.ts b/apps/web/src/features/next-soup/actions/make-block-sender-action.tsindex 67846f83fe..f47d5441c9 100644--- a/apps/web/src/features/next-soup/actions/make-block-sender-action.ts+++ b/apps/web/src/features/next-soup/actions/make-block-sender-action.ts@@ -1,8 +1,11 @@import type { EntityData } from '@entity';+import { useNonPrimaryEmailLinkIdHeader } from '@queries/email/link';import { blockSenderWithToast } from '@queries/email/thread';import type { SoupState } from '../create-soup-state';export const makeBlockSenderAction = () => {+ const toHeaderLinkId = useNonPrimaryEmailLinkIdHeader();+const canExecute = (entity: EntityData): boolean => {return entity.type === 'email' && !!entity.senderEmail;};@@ -10,7 +13,12 @@ export const makeBlockSenderAction = () => {const execute = async (entities: EntityData[]) => {for (const entity of entities) {if (entity.type !== 'email' || !entity.senderEmail) continue;- await blockSenderWithToast(entity.senderEmail);+ // The block creates a Gmail filter on one linked account, so it has to+ // target the inbox the thread arrived in.+ await blockSenderWithToast(+ entity.senderEmail,+ toHeaderLinkId(entity.linkId)+ );}};diff --git a/apps/web/src/features/next-soup/actions/make-sender-filter-action.test.ts b/apps/web/src/features/next-soup/actions/make-sender-filter-action.test.tsnew file mode 100644index 0000000000..da4b246587--- /dev/null+++ b/apps/web/src/features/next-soup/actions/make-sender-filter-action.test.ts@@ -0,0 +1,126 @@+import type { EntityData } from '@entity';+import { createRoot } from 'solid-js';+import { beforeEach, describe, expect, it, vi } from 'vitest';++const mocks = vi.hoisted(() => ({+ blockSenderWithToast: vi.fn(async () => {}),+ filterAction: vi.fn(async () => {}),+ primaryLinkId: 'link-primary',+}));++vi.mock('@queries/email/link', () => ({+ // Mirrors the real hook: the primary inbox's link id maps to `undefined`+ // (the backend defaults to primary when the header is absent).+ useNonPrimaryEmailLinkIdHeader: () => (linkId: string | undefined | null) =>+ !linkId || linkId === mocks.primaryLinkId ? undefined : linkId,+}));++vi.mock('@queries/email/thread', () => ({+ blockSenderWithToast: mocks.blockSenderWithToast,+}));++import { makeBlockSenderAction } from './make-block-sender-action';+import { makeSenderFilterAction } from './make-sender-filter-action';++const emailEntity = (over: Partial<EntityData> = {}) =>+ ({+ type: 'email',+ id: 'thread-1',+ senderEmail: '[email protected]',+ ...over,+ }) as EntityData;++beforeEach(() => {+ vi.clearAllMocks();+});++describe('makeSenderFilterAction', () => {+ it('sends the thread inbox link id for a non-primary inbox', async () => {+ await createRoot(async (dispose) => {+ const action = makeSenderFilterAction(mocks.filterAction);+ await action.execute([emailEntity({ linkId: 'link-secondary' })]);+ dispose();+ });++ expect(mocks.filterAction).toHaveBeenCalledWith(+ '[email protected]',+ 'link-secondary'+ );+ });++ it('omits the link id for the primary inbox', async () => {+ await createRoot(async (dispose) => {+ const action = makeSenderFilterAction(mocks.filterAction);+ await action.execute([emailEntity({ linkId: mocks.primaryLinkId })]);+ dispose();+ });++ expect(mocks.filterAction).toHaveBeenCalledWith(+ '[email protected]',+ undefined+ );+ });++ it('keys the dedupe on the inbox so one sender in two inboxes is two filters', async () => {+ await createRoot(async (dispose) => {+ const action = makeSenderFilterAction(mocks.filterAction);+ await action.execute([+ emailEntity({ id: 'a', linkId: 'link-a' }),+ emailEntity({ id: 'b', linkId: 'link-b' }),+ emailEntity({ id: 'c', linkId: 'link-a' }),+ ]);+ dispose();+ });++ expect(mocks.filterAction).toHaveBeenCalledTimes(2);+ expect(mocks.filterAction).toHaveBeenCalledWith(+ '[email protected]',+ 'link-a'+ );+ expect(mocks.filterAction).toHaveBeenCalledWith(+ '[email protected]',+ 'link-b'+ );+ });++ it('collapses a missing link id and an explicit primary one into one request', async () => {+ await createRoot(async (dispose) => {+ const action = makeSenderFilterAction(mocks.filterAction);+ await action.execute([+ emailEntity({ id: 'a', linkId: undefined }),+ emailEntity({ id: 'b', linkId: mocks.primaryLinkId }),+ ]);+ dispose();+ });++ expect(mocks.filterAction).toHaveBeenCalledTimes(1);+ expect(mocks.filterAction).toHaveBeenCalledWith(+ '[email protected]',+ undefined+ );+ });+});++describe('makeBlockSenderAction', () => {+ it('blocks on the inbox the thread belongs to', async () => {+ await createRoot(async (dispose) => {+ const action = makeBlockSenderAction();+ await action.execute([+ emailEntity({ id: 'a', linkId: 'link-secondary' }),+ emailEntity({ id: 'b', linkId: mocks.primaryLinkId }),+ ]);+ dispose();+ });++ expect(mocks.blockSenderWithToast).toHaveBeenNthCalledWith(+ 1,+ '[email protected]',+ 'link-secondary'+ );+ expect(mocks.blockSenderWithToast).toHaveBeenNthCalledWith(+ 2,+ '[email protected]',+ undefined+ );+ });+});diff --git a/apps/web/src/features/next-soup/actions/make-sender-filter-action.ts b/apps/web/src/features/next-soup/actions/make-sender-filter-action.tsindex b659929055..674ac4075a 100644--- a/apps/web/src/features/next-soup/actions/make-sender-filter-action.ts+++ b/apps/web/src/features/next-soup/actions/make-sender-filter-action.ts@@ -1,9 +1,12 @@import type { EntityData } from '@entity';+import { useNonPrimaryEmailLinkIdHeader } from '@queries/email/link';import type { SoupState } from '../create-soup-state';export const makeSenderFilterAction = (- action: (email: string) => Promise<void>+ action: (email: string, linkId?: string) => Promise<void>) => {+ const toHeaderLinkId = useNonPrimaryEmailLinkIdHeader();+const canExecute = (entity: EntityData): boolean =>entity.type === 'email' && !!entity.senderEmail;@@ -11,10 +14,15 @@ export const makeSenderFilterAction = (const seen = new Set<string>();for (const entity of entities) {if (entity.type !== 'email' || !entity.senderEmail) continue;- const key = entity.senderEmail.trim().toLowerCase();+ // Filters are per-inbox, so the same sender in two inboxes is two+ // distinct filters — key the dedupe on the inbox too. Key off the+ // normalized header value so a missing link id and an explicit primary+ // one collapse to a single request.+ const linkId = toHeaderLinkId(entity.linkId);+ const key = `${linkId ?? ''}:${entity.senderEmail.trim().toLowerCase()}`;if (seen.has(key)) continue;seen.add(key);- await action(entity.senderEmail);+ await action(entity.senderEmail, linkId);}};diff --git a/apps/web/src/lib/queries/email/thread.ts b/apps/web/src/lib/queries/email/thread.tsindex 91d259bafa..bea6e778e1 100644--- a/apps/web/src/lib/queries/email/thread.ts+++ b/apps/web/src/lib/queries/email/thread.ts@@ -778,14 +778,18 @@ export async function blockSenderWithToast(async function upsertSenderFilterWithToast(senderEmail: string,- isImportant: boolean+ isImportant: boolean,+ linkId?: string) {const label = isImportant ? 'Signal' : 'Noise';- const result = await emailClient.upsertEmailFilter({- email_address: senderEmail,- is_important: isImportant,- });+ const result = await emailClient.upsertEmailFilter(+ {+ email_address: senderEmail,+ is_important: isImportant,+ },+ linkId+ );if (result.isErr()) {toast.failure(`Failed to mark sender as ${label}`, {@@ -804,9 +808,10 @@ async function upsertSenderFilterWithToast(label: 'Undo',icon: ArrowCounterClockwise,onClick: async () => {- const undoResult = await emailClient.deleteEmailFilter({- id: filterId,- });+ const undoResult = await emailClient.deleteEmailFilter(+ { id: filterId },+ linkId+ );if (undoResult.isErr()) {toast.failure('Failed to undo', { subtext: senderEmail });} else {@@ -819,8 +824,20 @@ async function upsertSenderFilterWithToast(});}-export const markSenderSignalWithToast = (senderEmail: string) =>- upsertSenderFilterWithToast(senderEmail, true);+/**+ * Marks a sender as Signal for one inbox. `linkId` scopes the filter to the+ * inbox the thread belongs to — omit it only for the primary inbox.+ */+export const markSenderSignalWithToast = (+ senderEmail: string,+ linkId?: string+) => upsertSenderFilterWithToast(senderEmail, true, linkId);-export const markSenderNoiseWithToast = (senderEmail: string) =>- upsertSenderFilterWithToast(senderEmail, false);+/**+ * Marks a sender as Noise for one inbox. See {@link markSenderSignalWithToast}+ * for how `linkId` is used.+ */+export const markSenderNoiseWithToast = (+ senderEmail: string,+ linkId?: string+) => upsertSenderFilterWithToast(senderEmail, false, linkId);diff --git a/apps/web/src/lib/service-clients/service-email/client.ts b/apps/web/src/lib/service-clients/service-email/client.tsindex f24dab7ab5..2cdc9cc9cd 100644--- a/apps/web/src/lib/service-clients/service-email/client.ts+++ b/apps/web/src/lib/service-clients/service-email/client.ts@@ -540,24 +540,29 @@ export const emailClient = {headers: emailLinkHeaders(linkId),});},- async listEmailFilters() {+ // Filters are stored per linked inbox, so every filter call is scoped to+ // `linkId` via the X-Email-Link-Id header; omit for the primary inbox.+ async listEmailFilters(linkId?: string) {return (await emailFetch<ListEmailFiltersResponse>('/email/filters', {method: 'GET',+ headers: emailLinkHeaders(linkId),})).map((result) => result);},- async upsertEmailFilter(args: UpsertEmailFilterRequest) {+ async upsertEmailFilter(args: UpsertEmailFilterRequest, linkId?: string) {return (await emailFetch<UpsertEmailFilterResponse>('/email/filters', {method: 'PUT',body: JSON.stringify(args),+ headers: emailLinkHeaders(linkId),})).map((result) => result);},- async deleteEmailFilter(args: { id: string }) {+ async deleteEmailFilter(args: { id: string }, linkId?: string) {return emailFetch(`/email/filters/${args.id}`, {method: 'DELETE',+ headers: emailLinkHeaders(linkId),});},async listCalendars() {