AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
fix(microsoft-outlook): make New Attachment filters reliable and add file extension filter (#16108)
Alexandru Andronicbac987b69c47
diff --git a/bun.lock b/bun.lockindex e3c2380c3d2..02fc2777272 100644--- a/bun.lock+++ b/bun.lock@@ -5886,7 +5886,7 @@},"packages/pieces/community/microsoft-outlook": {"name": "@activepieces/piece-microsoft-outlook",- "version": "0.6.1",+ "version": "0.6.2","dependencies": {"@activepieces/core-piece-types": "workspace:*","@activepieces/core-utils": "workspace:*",diff --git a/packages/pieces/community/microsoft-outlook/package.json b/packages/pieces/community/microsoft-outlook/package.jsonindex 03f46098690..027d3d77b91 100644--- a/packages/pieces/community/microsoft-outlook/package.json+++ b/packages/pieces/community/microsoft-outlook/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/piece-microsoft-outlook",- "version": "0.6.1",+ "version": "0.6.2","main": "./dist/src/index.js","types": "./dist/src/index.d.ts","dependencies": {diff --git a/packages/pieces/community/microsoft-outlook/src/lib/triggers/new-attachment.test.ts b/packages/pieces/community/microsoft-outlook/src/lib/triggers/new-attachment.test.tsnew file mode 100644index 00000000000..e7b80198282--- /dev/null+++ b/packages/pieces/community/microsoft-outlook/src/lib/triggers/new-attachment.test.ts@@ -0,0 +1,375 @@+import { describe, expect, it, vi } from 'vitest';++type FakeMessage = {+ id: string;+ receivedDateTime: string;+ from?: { emailAddress: { address?: string } };+ sender?: { emailAddress: { address?: string } };+};++const { state } = vi.hoisted(() => ({+ state: {+ messages: [] as FakeMessage[],+ attachments: {} as Record<+ string,+ { id?: string; name: string; contentBytes?: string; '@odata.type'?: string }[]+ >,+ requests: [] as string[],+ },+}));++const PAGE_SIZE = 2;++function messagePage(messages: FakeMessage[], offset: number, order: string, pageSize = PAGE_SIZE) {+ return {+ value: messages.slice(offset, offset + pageSize),+ '@odata.nextLink':+ offset + pageSize < messages.length ? `next:${offset + pageSize}:${order}` : undefined,+ };+}++function orderedMessages(order: string): FakeMessage[] {+ if (order !== 'receivedDateTime desc') {+ return state.messages;+ }+ return [...state.messages].sort(+ (a, b) => Date.parse(b.receivedDateTime) - Date.parse(a.receivedDateTime),+ );+}++vi.mock('../common/client', () => ({+ outlookCommon: {+ createClient: () => ({+ api: (url: string) => {+ state.requests.push(url);+ let order = '';+ let pageSize = PAGE_SIZE;+ const request = {+ select: () => request,+ top: (value: number) => {+ pageSize = value;+ return request;+ },+ orderby: (value: string) => {+ order = value;+ return request;+ },+ responseType: () => request,+ get: async () => {+ if (url.endsWith('/$value')) {+ return new TextEncoder().encode('downloaded').buffer;+ }+ const attachmentMatch = url.match(/\/messages\/([^/]+)\/attachments$/);+ if (attachmentMatch) {+ return { value: state.attachments[attachmentMatch[1]] ?? [] };+ }+ const nextMatch = url.match(/^next:(\d+):(.*)$/);+ if (nextMatch) {+ return messagePage(orderedMessages(nextMatch[2]), Number(nextMatch[1]), nextMatch[2]);+ }+ return messagePage(orderedMessages(order), 0, order, pageSize);+ },+ };+ return request;+ },+ }),+ mailboxPrefix: () => '/me',+ },+}));++import {+ attachmentMatchesFilters,+ messageMatchesSender,+ newAttachmentTrigger,+} from './new-attachment';++const bytes = Buffer.from('x').toString('base64');+const FILE = '#microsoft.graph.fileAttachment';++function message(+ id: string,+ address: string | undefined,+ receivedDateTime = '2026-10-05T10:00:00Z',+): FakeMessage {+ return {+ id,+ receivedDateTime,+ from: address ? { emailAddress: { address } } : undefined,+ sender: address ? { emailAddress: { address } } : undefined,+ };+}++function buildContext(+ propsValue: Record<string, unknown>,+ initialStore: Record<string, unknown> = {},+) {+ const store = new Map<string, unknown>(Object.entries(initialStore));+ return {+ auth: { access_token: 'token' },+ propsValue,+ files: {+ write: async ({ fileName, data }: { fileName: string; data: Buffer }) =>+ `file://${fileName}#${data.toString()}`,+ },+ store: {+ get: async (key: string) => store.get(key),+ put: async (key: string, value: unknown) => store.set(key, value),+ },+ _store: store,+ };+}++describe('messageMatchesSender', () => {+ it('rejects a message with no sender when a sender filter is set', () => {+ expect(+ messageMatchesSender({+ message: message('m1', undefined) as never,+ sender: '[email protected]',+ }),+ ).toBe(false);+ });++ it('matches on from even when sender differs', () => {+ const delegated = {+ ...message('m1', '[email protected]'),+ from: { emailAddress: { address: '[email protected]' } },+ };+ expect(messageMatchesSender({ message: delegated as never, sender: '[email protected]' })).toBe(+ true,+ );+ });++ it('passes everything when no filter is set', () => {+ expect(messageMatchesSender({ message: message('m1', undefined) as never, sender: ' ' })).toBe(+ true,+ );+ });+});++describe('attachmentMatchesFilters', () => {+ it('accepts extensions with or without a dot, case-insensitively', () => {+ expect(attachmentMatchesFilters({ name: 'Scan.PDF', filters: { fileExtension: '.pdf' } })).toBe(+ true,+ );+ expect(+ attachmentMatchesFilters({ name: 'report.docx', filters: { fileExtension: 'pdf, docx' } }),+ ).toBe(true);+ expect(attachmentMatchesFilters({ name: 'logo.png', filters: { fileExtension: 'pdf' } })).toBe(+ false,+ );+ });++ it('does not treat a name containing the extension as a match', () => {+ expect(+ attachmentMatchesFilters({ name: 'pdf-guide.png', filters: { fileExtension: 'pdf' } }),+ ).toBe(false);+ });++ it('applies the name filter and the extension filter together', () => {+ expect(+ attachmentMatchesFilters({+ name: 'CamScanner 1.pdf',+ filters: { attachmentNameFilter: 'camscanner', fileExtension: 'pdf' },+ }),+ ).toBe(true);+ expect(+ attachmentMatchesFilters({+ name: 'CamScanner 1.jpg',+ filters: { attachmentNameFilter: 'camscanner', fileExtension: 'pdf' },+ }),+ ).toBe(false);+ });+});++describe('newAttachmentTrigger', () => {+ it('test only returns attachments matching every filter', async () => {+ state.messages = [+ message('m1', '[email protected]'),+ message('m2', '[email protected]'),+ message('m3', undefined),+ ];+ state.attachments = {+ m1: [+ { name: 'image001.png', contentBytes: bytes, '@odata.type': FILE },+ { name: 'CamScanner 10_4_26.pdf', contentBytes: bytes, '@odata.type': FILE },+ ],+ m2: [{ name: 'CamScanner 10_4_26.pdf', contentBytes: bytes, '@odata.type': FILE }],+ m3: [{ name: 'CamScanner 10_4_26.pdf', contentBytes: bytes, '@odata.type': FILE }],+ };++ const result = (await newAttachmentTrigger.test(+ buildContext({+ sender: '[email protected]',+ attachmentNameFilter: 'CamScanner',+ fileExtension: 'pdf',+ }) as never,+ )) as Record<string, unknown>[];++ expect(result.map((item) => [item['messageId'], item['name']])).toEqual([+ ['m1', 'CamScanner 10_4_26.pdf'],+ ]);+ });++ it('run advances lastPoll past non-matching messages so they are not rescanned', async () => {+ const lastPoll = Date.parse('2026-10-05T09:00:00Z');+ state.messages = [message('m1', '[email protected]', '2026-10-05T10:00:00Z')];+ state.attachments = { m1: [{ name: 'other.pdf', contentBytes: bytes, '@odata.type': FILE }] };+ const context = buildContext(+ { sender: '[email protected]' },+ { pollCursor: { epochMilliSeconds: lastPoll, seenMessageIds: [] } },+ );++ const result = await newAttachmentTrigger.run(context as never);++ expect(result).toEqual([]);+ expect(context._store.get('pollCursor')).toEqual({+ epochMilliSeconds: Date.parse('2026-10-05T10:00:00Z'),+ seenMessageIds: ['m1'],+ });+ });++ it('downloads the bytes separately when Graph omits contentBytes, and skips non-file attachments', async () => {+ state.messages = [message('m1', '[email protected]')];+ state.attachments = {+ m1: [+ { id: 'a1', name: 'CamScanner big.pdf', '@odata.type': FILE },+ { id: 'a2', name: 'Forwarded mail.pdf', '@odata.type': '#microsoft.graph.itemAttachment' },+ ],+ };+ state.requests = [];++ const result = (await newAttachmentTrigger.test(+ buildContext({ fileExtension: 'pdf' }) as never,+ )) as Record<string, unknown>[];++ expect(result.map((item) => item['file'])).toEqual(['file://CamScanner big.pdf#downloaded']);+ expect(state.requests).toContain('/me/messages/m1/attachments/a1/$value');+ });++ it('test samples the newest matches from one request of the most recent messages', async () => {+ state.requests = [];+ state.messages = [+ message('old1', '[email protected]', '2023-04-28T10:00:00Z'),+ message('old2', '[email protected]', '2023-04-29T10:00:00Z'),+ message('new1', '[email protected]', '2026-10-05T11:46:00Z'),+ message('other', '[email protected]', '2026-10-05T11:50:00Z'),+ message('new2', '[email protected]', '2026-10-05T11:27:00Z'),+ ];+ state.attachments = Object.fromEntries(+ state.messages.map((m) => [+ m.id,+ [{ name: `${m.id}.pdf`, contentBytes: bytes, '@odata.type': FILE }],+ ]),+ );++ const result = (await newAttachmentTrigger.test(+ buildContext({ sender: '[email protected]' }) as never,+ )) as Record<string, unknown>[];++ expect(result.map((item) => item['messageId'])).toEqual(['old1', 'old2', 'new2', 'new1']);+ expect(state.requests.some((url) => url.startsWith('next:'))).toBe(false);+ });++ it('run emits a matching message that shares the cursor time with an already-seen one, exactly once', async () => {+ const cursor = '2026-10-05T10:00:00Z';+ state.messages = [+ message('seen', '[email protected]', cursor),+ message('late', '[email protected]', cursor),+ ];+ state.attachments = {+ seen: [{ name: 'other.pdf', contentBytes: bytes, '@odata.type': FILE }],+ late: [{ name: 'pod.pdf', contentBytes: bytes, '@odata.type': FILE }],+ };+ const context = buildContext(+ { sender: '[email protected]' },+ { pollCursor: { epochMilliSeconds: Date.parse(cursor), seenMessageIds: ['seen'] } },+ );++ const first = (await newAttachmentTrigger.run(context as never)) as Record<string, unknown>[];+ const second = await newAttachmentTrigger.run(context as never);++ expect(first.map((item) => item['messageId'])).toEqual(['late']);+ expect(second).toEqual([]);+ expect(+ state.requests.some((url) =>+ url.includes(`receivedDateTime ge ${new Date(cursor).toISOString()}`),+ ),+ ).toBe(true);+ });++ it('run on state saved by the old version does not replay mail at the saved time', async () => {+ const legacyLastPoll = Date.parse('2026-10-05T10:00:00Z');+ state.messages = [+ message('alreadyEmitted', '[email protected]', '2026-10-05T10:00:00Z'),+ message('new', '[email protected]', '2026-10-05T10:05:00Z'),+ ];+ state.attachments = {+ alreadyEmitted: [{ name: 'old.pdf', contentBytes: bytes, '@odata.type': FILE }],+ new: [{ name: 'new.pdf', contentBytes: bytes, '@odata.type': FILE }],+ };+ const context = buildContext({ sender: '[email protected]' }, { lastPoll: legacyLastPoll });++ const result = (await newAttachmentTrigger.run(context as never)) as Record<string, unknown>[];++ expect(result.map((item) => item['messageId'])).toEqual(['new']);+ expect(context._store.get('pollCursor')).toEqual({+ epochMilliSeconds: Date.parse('2026-10-05T10:05:00Z'),+ seenMessageIds: ['new'],+ });+ });++ it('test filters on receivedDateTime first, since Graph rejects ordering by a field missing from the filter', async () => {+ state.messages = [];+ state.requests = [];++ await newAttachmentTrigger.test(buildContext({}) as never);++ expect(state.requests[0]).toMatch(/\$filter=receivedDateTime ge [^&]+ and hasAttachments eq true$/);+ });++ it('onEnable saves the cursor as one value', async () => {+ const context = buildContext({});++ await newAttachmentTrigger.onEnable(context as never);++ expect([...context._store.keys()]).toEqual(['pollCursor']);+ });++ it('onEnable on republish keeps the existing cursor', async () => {+ const existing = {+ epochMilliSeconds: Date.parse('2026-10-05T10:00:00Z'),+ seenMessageIds: ['m1'],+ };+ const context = { ...buildContext({}, { pollCursor: existing }), isRepublish: true };++ await newAttachmentTrigger.onEnable(context as never);++ expect(context._store.get('pollCursor')).toEqual(existing);+ });++ it('onEnable on republish keeps a cursor saved by the old version', async () => {+ const legacyLastPoll = Date.parse('2026-10-05T10:00:00Z');+ const context = { ...buildContext({}, { lastPoll: legacyLastPoll }), isRepublish: true };++ await newAttachmentTrigger.onEnable(context as never);++ expect([...context._store.entries()]).toEqual([['lastPoll', legacyLastPoll]]);+ });++ it('onEnable without republish resets the cursor to now', async () => {+ const context = buildContext(+ {},+ { pollCursor: { epochMilliSeconds: 0, seenMessageIds: ['m1'] } },+ );++ await newAttachmentTrigger.onEnable(context as never);++ expect(context._store.get('pollCursor')).toEqual({+ epochMilliSeconds: expect.any(Number),+ seenMessageIds: [],+ });+ expect(+ (context._store.get('pollCursor') as { epochMilliSeconds: number }).epochMilliSeconds,+ ).toBeGreaterThan(0);+ });+});diff --git a/packages/pieces/community/microsoft-outlook/src/lib/triggers/new-attachment.ts b/packages/pieces/community/microsoft-outlook/src/lib/triggers/new-attachment.tsindex 9fe572a705a..187b392657a 100644--- a/packages/pieces/community/microsoft-outlook/src/lib/triggers/new-attachment.ts+++ b/packages/pieces/community/microsoft-outlook/src/lib/triggers/new-attachment.ts@@ -1,59 +1,27 @@-import { FilesService, TriggerStrategy, createTrigger, Property } from '@activepieces/pieces-framework';-import { Client, PageCollection } from '@microsoft/microsoft-graph-client';+import { Client, PageCollection, ResponseType } from '@microsoft/microsoft-graph-client';import { Message, FileAttachment } from '@microsoft/microsoft-graph-types';import dayjs from 'dayjs';++import {+ FilesService,+ TriggerStrategy,+ createTrigger,+ isNil,+ Property,+ Store,+} from '@activepieces/pieces-framework';++import { outlookAtomicCommon } from '../common/atomic-common';import { microsoftOutlookAuth } from '../common/auth';import { outlookCommon } from '../common/client';import { mailFolderIdDropdown } from '../common/props';-import { isNil } from '@activepieces/pieces-framework';import { newAttachmentTriggerOutputSchema } from '../output-schemas';-async function enrichAttachments(- client: Client,- mailboxPrefix: string,- messages: Message[],- files: FilesService,- nameFilter?: string,- senderFilter?: string,-): Promise<Record<string, any>[]> {- const attachments: Record<string, any>[] = [];-- for (const message of messages) {- if (senderFilter && message.sender?.emailAddress?.address && !message.sender.emailAddress.address.toLowerCase().includes(senderFilter.toLowerCase())) {- continue;- }-- const attachmentResponse: PageCollection = await client- .api(`${mailboxPrefix}/messages/${message.id}/attachments`)- .get();-- for (const attachment of attachmentResponse.value as FileAttachment[]) {- const { contentBytes, ...rest } = attachment;-- if (nameFilter && attachment.name && !attachment.name.toLowerCase().includes(nameFilter.toLowerCase())) {- continue;- }-- if (attachment.name && contentBytes) {- const file = await files.write({- fileName: attachment.name,- data: Buffer.from(contentBytes, 'base64'),- });-- attachments.push({- file,- messageId: message.id!,- messageSubject: message.subject,- messageSender: message.sender,- messageReceivedDateTime: message.receivedDateTime,- parentFolderId: message.parentFolderId,- ...rest,- });- }- }- }- return attachments;-}+const MESSAGE_FIELDS = 'id,subject,from,sender,receivedDateTime,parentFolderId';+const TEST_SAMPLE_SIZE = 5;+const TEST_MAX_MESSAGES = 100;+const POLL_CURSOR_KEY = 'pollCursor';+const LEGACY_LAST_POLL_KEY = 'lastPoll';export const newAttachmentTrigger = createTrigger({auth: microsoftOutlookAuth,@@ -62,7 +30,8 @@ export const newAttachmentTrigger = createTrigger({displayName: 'New Attachment',description: 'Triggers when a new email containing one or more attachments arrives.',aiMetadata: {- description: 'Fires once per attachment when a new email carrying one or more file attachments arrives, optionally scoped to a folder, sender, or attachment-name filter. Each fire represents a single attachment from a newly received message.',+ description:+ 'Fires once per attachment when a new email carrying one or more file attachments arrives, optionally scoped to a folder, sender, attachment-name, or file-extension filter. Each fire represents a single attachment from a newly received message.',},outputSchema: newAttachmentTriggerOutputSchema,props: {@@ -73,84 +42,301 @@ export const newAttachmentTrigger = createTrigger({}),sender: Property.ShortText({displayName: 'From (Sender Email)',- description: 'Filter emails from a specific sender (optional). Leave empty to for all senders.',+ description:+ 'Filter emails from a specific sender (contains). Leave empty to include all senders.',required: false,}),attachmentNameFilter: Property.ShortText({displayName: 'Attachment Name Filter',description: 'Filter attachments by name (contains). Leave empty to include all attachments.',- required: false,+ required: false,+ }),+ fileExtension: Property.ShortText({+ displayName: 'File Extension',+ description:+ 'Only attachments with this extension. Separate several with commas, e.g. pdf, docx.',+ placeholder: 'pdf',+ required: false,}),},sampleData: {},type: TriggerStrategy.POLLING,async onEnable(context) {- await context.store.put('lastPoll', Date.now());+ if (context.isRepublish && (await hasPollCursor(context.store))) {+ return;+ }+ await context.store.put<PollCursor>(POLL_CURSOR_KEY, {+ epochMilliSeconds: Date.now(),+ seenMessageIds: [],+ });},async onDisable(context) {// return},async test(context) {- const { folderId, attachmentNameFilter, sender } = context.propsValue;+ const { folderId, ...filters } = context.propsValue;const client = outlookCommon.createClient(context.auth);- const baseUrl = folderId ? `${outlookCommon.mailboxPrefix(context.auth)}/mailFolders/${folderId}/messages` : `${outlookCommon.mailboxPrefix(context.auth)}/messages`;+ const mailboxPrefix = outlookCommon.mailboxPrefix(context.auth);+ const baseUrl = folderId+ ? `${mailboxPrefix}/mailFolders/${folderId}/messages`+ : `${mailboxPrefix}/messages`;const response: PageCollection = await client- .api(`${baseUrl}?$filter=hasAttachments eq true`)- .top(10)+ .api(`${baseUrl}?$filter=receivedDateTime ge 1900-01-01T00:00:00Z and hasAttachments eq true`)+ .select(MESSAGE_FIELDS)+ .orderby('receivedDateTime desc')+ .top(TEST_MAX_MESSAGES).get();- const attachments = await enrichAttachments(client, outlookCommon.mailboxPrefix(context.auth), response.value as Message[], context.files, attachmentNameFilter, sender);+ const attachments = await enrichAttachments({+ client,+ mailboxPrefix,+ messages: response.value,+ files: context.files,+ filters,+ limit: TEST_SAMPLE_SIZE,+ });+ return attachments.reverse();+ },+ async run(context) {+ const cursor = await readPollCursor(context.store);+ const { folderId, ...filters } = context.propsValue;+ const client = outlookCommon.createClient(context.auth);+ const mailboxPrefix = outlookCommon.mailboxPrefix(context.auth);+ const baseUrl = folderId+ ? `${mailboxPrefix}/mailFolders/${folderId}/messages`+ : `${mailboxPrefix}/messages`;++ const since = dayjs(cursor.epochMilliSeconds).toISOString();+ const messages = await listMessages({+ client,+ url: `${baseUrl}?$filter=receivedDateTime ge ${since} and hasAttachments eq true`,+ });- const items = attachments.map((attachment) => ({- epochMilliSeconds: dayjs(attachment['messageReceivedDateTime']).valueOf(),- data: attachment,- }));+ const attachments = await enrichAttachments({+ client,+ mailboxPrefix,+ messages: messages.filter((message) => isUnseenMessage({ message, cursor })),+ files: context.files,+ filters,+ });- return items.map((item) => item.data);+ await context.store.put<PollCursor>(POLL_CURSOR_KEY, advanceCursor({ cursor, messages }));+ return attachments;},- async run(context) {- const lastFetchEpochMS = await context.store.get<number>('lastPoll');- if (isNil(lastFetchEpochMS)) {- throw new Error("lastPoll doesn't exist in the store.");+});++export function messageMatchesSender({+ message,+ sender,+}: {+ message: Message;+ sender?: string;+}): boolean {+ const wanted = sender?.trim().toLowerCase();+ if (!wanted) {+ return true;+ }+ return [message.from?.emailAddress?.address, message.sender?.emailAddress?.address].some(+ (address) => !isNil(address) && address.toLowerCase().includes(wanted),+ );+}++export function attachmentMatchesFilters({+ name,+ filters,+}: {+ name: string | null | undefined;+ filters: AttachmentFilters;+}): boolean {+ const lowerName = name?.toLowerCase();+ const nameFilter = filters.attachmentNameFilter?.trim().toLowerCase();+ if (nameFilter && !lowerName?.includes(nameFilter)) {+ return false;+ }+ const extensions = parseExtensions(filters.fileExtension);+ if (+ extensions.length > 0 &&+ !extensions.some((extension) => lowerName?.endsWith(`.${extension}`))+ ) {+ return false;+ }+ return true;+}++function parseExtensions(fileExtension?: string): string[] {+ return (fileExtension ?? '')+ .split(',')+ .map((extension) => extension.trim().toLowerCase().replace(/^\./, ''))+ .filter((extension) => extension.length > 0);+}++function isUnseenMessage({ message, cursor }: { message: Message; cursor: PollCursor }): boolean {+ const messageTime = dayjs(message.receivedDateTime).valueOf();+ if (messageTime !== cursor.epochMilliSeconds) {+ return messageTime > cursor.epochMilliSeconds;+ }+ return !isNil(cursor.seenMessageIds) && !cursor.seenMessageIds.includes(message.id!);+}++function advanceCursor({+ cursor,+ messages,+}: {+ cursor: PollCursor;+ messages: Message[];+}): PollCursor {+ const epochMilliSeconds = messages.reduce(+ (latest, message) => Math.max(latest, dayjs(message.receivedDateTime).valueOf()),+ cursor.epochMilliSeconds,+ );+ const seenAtLatest = messages+ .filter((message) => dayjs(message.receivedDateTime).valueOf() === epochMilliSeconds)+ .map((message) => message.id!);+ const carriedOver =+ epochMilliSeconds === cursor.epochMilliSeconds ? cursor.seenMessageIds ?? [] : [];+ return {+ epochMilliSeconds,+ seenMessageIds: [...new Set([...seenAtLatest, ...carriedOver])],+ };+}++async function hasPollCursor(store: Store): Promise<boolean> {+ const [cursor, legacyLastPoll] = await Promise.all([+ store.get<PollCursor>(POLL_CURSOR_KEY),+ store.get<number>(LEGACY_LAST_POLL_KEY),+ ]);+ return !isNil(cursor) || !isNil(legacyLastPoll);+}++async function readPollCursor(store: Store): Promise<PollCursor> {+ const cursor = await store.get<PollCursor>(POLL_CURSOR_KEY);+ if (!isNil(cursor)) {+ return cursor;+ }+ const legacyLastPoll = await store.get<number>(LEGACY_LAST_POLL_KEY);+ if (isNil(legacyLastPoll)) {+ throw new Error("lastPoll doesn't exist in the store.");+ }+ return { epochMilliSeconds: legacyLastPoll, seenMessageIds: null };+}++async function listMessages({ client, url }: { client: Client; url: string }): Promise<Message[]> {+ let response: PageCollection = await client+ .api(url)+ .select(MESSAGE_FIELDS)+ .orderby('receivedDateTime desc')+ .get();++ const messages: Message[] = [];++ while (response.value.length > 0) {+ messages.push(...(response.value as Message[]));+ if (!response['@odata.nextLink']) {+ break;}+ response = await client.api(response['@odata.nextLink']).get();+ }+ return messages;+}- const { folderId, attachmentNameFilter, sender } = context.propsValue;- const client = outlookCommon.createClient(context.auth);+async function downloadAttachment({+ client,+ mailboxPrefix,+ messageId,+ attachmentId,+}: {+ client: Client;+ mailboxPrefix: string;+ messageId: string;+ attachmentId: string;+}): Promise<Buffer> {+ const messagePath = `${mailboxPrefix}/messages/${outlookAtomicCommon.encodeGraphId(messageId)}`;+ const bytes = await client+ .api(`${messagePath}/attachments/${outlookAtomicCommon.encodeGraphId(attachmentId)}/$value`)+ .responseType(ResponseType.ARRAYBUFFER)+ .get();+ return Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);+}- const baseUrl = folderId ? `${outlookCommon.mailboxPrefix(context.auth)}/mailFolders/${folderId}/messages` : `${outlookCommon.mailboxPrefix(context.auth)}/messages`;- let response: PageCollection = await client- .api(- `${baseUrl}?$filter=receivedDateTime gt ${dayjs(- lastFetchEpochMS,- ).toISOString()} and hasAttachments eq true`,- )- .orderby('receivedDateTime desc')+async function enrichAttachments({+ client,+ mailboxPrefix,+ messages,+ files,+ filters,+ limit,+}: {+ client: Client;+ mailboxPrefix: string;+ messages: Message[];+ files: FilesService;+ filters: AttachmentFilters;+ limit?: number;+}): Promise<Record<string, any>[]> {+ const attachments: Record<string, any>[] = [];++ for (const message of messages) {+ if (!messageMatchesSender({ message, sender: filters.sender })) {+ continue;+ }++ const attachmentResponse: PageCollection = await client+ .api(`${mailboxPrefix}/messages/${message.id}/attachments`).get();- const messages: Message[] = [];+ for (const attachment of attachmentResponse.value as (FileAttachment & {+ '@odata.type'?: string;+ })[]) {+ const { contentBytes, ...rest } = attachment;- while (response.value.length > 0) {- messages.push(...(response.value as Message[]));+ if (!attachmentMatchesFilters({ name: attachment.name, filters })) {+ continue;+ }- if (response['@odata.nextLink']) {- response = await client.api(response['@odata.nextLink']).get();- } else {- break;+ if (!attachment.name || attachment['@odata.type'] !== '#microsoft.graph.fileAttachment') {+ continue;+ }++ const data = contentBytes+ ? Buffer.from(contentBytes, 'base64')+ : await downloadAttachment({+ client,+ mailboxPrefix,+ messageId: message.id!,+ attachmentId: attachment.id!,+ });++ const file = await files.write({+ fileName: attachment.name,+ data,+ });++ attachments.push({+ file,+ messageId: message.id!,+ messageSubject: message.subject,+ messageSender: message.sender,+ messageReceivedDateTime: message.receivedDateTime,+ parentFolderId: message.parentFolderId,+ ...rest,+ });++ if (!isNil(limit) && attachments.length >= limit) {+ return attachments;}}- const attachments = await enrichAttachments(client, outlookCommon.mailboxPrefix(context.auth), messages, context.files, attachmentNameFilter, sender);-- const items = attachments.map((attachment) => ({- epochMilliSeconds: dayjs(attachment['messageReceivedDateTime']).valueOf(),- data: attachment,- }));-- const newLastEpochMilliSeconds = items.reduce(- (acc, item) => Math.max(acc, item.epochMilliSeconds),- lastFetchEpochMS,- );- await context.store.put('lastPoll', newLastEpochMilliSeconds);- return items.filter((f) => f.epochMilliSeconds > lastFetchEpochMS).map((item) => item.data);- },-});+ }+ return attachments;+}++type PollCursor = {+ epochMilliSeconds: number;+ seenMessageIds: string[] | null;+};++type AttachmentFilters = {+ sender?: string;+ attachmentNameFilter?: string;+ fileExtension?: string;+};