diff --git a/components/clear_books/clear_books.app.mjs b/components/clear_books/clear_books.app.mjs index 33887c22cdf6c..e10a5b47a6bf8 100644 --- a/components/clear_books/clear_books.app.mjs +++ b/components/clear_books/clear_books.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/dixa/dixa.app.mjs b/components/dixa/dixa.app.mjs index bd8332a61a29b..b026f32de8633 100644 --- a/components/dixa/dixa.app.mjs +++ b/components/dixa/dixa.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/egnyte/egnyte.app.mjs b/components/egnyte/egnyte.app.mjs index 829562d8cff8e..ca18e17720b3a 100644 --- a/components/egnyte/egnyte.app.mjs +++ b/components/egnyte/egnyte.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/linkup/linkup.app.mjs b/components/linkup/linkup.app.mjs index a6d44e8df9492..8f7cd90ab89c5 100644 --- a/components/linkup/linkup.app.mjs +++ b/components/linkup/linkup.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/mailboxlayer/mailboxlayer.app.mjs b/components/mailboxlayer/mailboxlayer.app.mjs index d9185e4c5beae..b2f9290e7c9d2 100644 --- a/components/mailboxlayer/mailboxlayer.app.mjs +++ b/components/mailboxlayer/mailboxlayer.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/microsoft_outlook/package.json b/components/microsoft_outlook/package.json index a392c43a2ae96..26ee21486028d 100644 --- a/components/microsoft_outlook/package.json +++ b/components/microsoft_outlook/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/microsoft_outlook", - "version": "1.0.3", + "version": "1.0.4", "description": "Pipedream Microsoft Outlook Components", "main": "microsoft_outlook.app.mjs", "keywords": [ @@ -14,6 +14,7 @@ "dependencies": { "axios": "^0.21.1", "js-base64": "^3.7.2", + "md5": "^2.3.0", "mime-types": "^2.1.35" }, "publishConfig": { diff --git a/components/microsoft_outlook/sources/new-email/new-email.mjs b/components/microsoft_outlook/sources/new-email/new-email.mjs index 5b1da7b4c0f64..f850b3606c53f 100644 --- a/components/microsoft_outlook/sources/new-email/new-email.mjs +++ b/components/microsoft_outlook/sources/new-email/new-email.mjs @@ -1,19 +1,53 @@ import common from "../common.mjs"; +import md5 from "md5"; import sampleEmit from "./test-event.mjs"; export default { ...common, key: "microsoft_outlook-new-email", name: "New Email Event (Instant)", - description: "Emit new event when an email received", - version: "0.0.9", + description: "Emit new event when an email is received in specified folders.", + version: "0.0.10", type: "source", + dedupe: "unique", + props: { + ...common.props, + folderIds: { + type: "string[]", + label: "Folder IDs to Monitor", + description: "Specify the folder IDs or names in Outlook that you want to monitor for new emails. Leave empty to monitor all folders (excluding \"Sent Items\" and \"Drafts\").", + optional: true, + async options() { + const { value: folders } = await this.listFolders(); + return folders?.map(({ + id: value, displayName: label, + }) => ({ + value, + label, + })) || []; + }, + }, + }, hooks: { ...common.hooks, + async deploy() { + this.db.set("sentItemFolderId", await this.getFolderIdByName("Sent Items")); + this.db.set("draftsFolderId", await this.getFolderIdByName("Drafts")); + + const events = await this.getSampleEvents({ + pageSize: 25, + }); + if (!events || events.length == 0) { + return; + } + for (const item of events) { + this.emitEvent(item); + } + }, async activate() { await this.activate({ changeType: "created", - resource: "/me/mailfolders('inbox')/messages", + resource: "/me/messages", }); }, async deactivate() { @@ -22,37 +56,86 @@ export default { }, methods: { ...common.methods, - async getSampleEvents({ pageSize }) { - return this.microsoftOutlook.listMessages({ - params: { - $top: pageSize, - $orderby: "createdDateTime desc", - }, + listFolders() { + return this.microsoftOutlook._makeRequest({ + path: "/me/mailFolders", }); }, + async getFolderIdByName(name) { + const { value: folders } = await this.listFolders(); + const { id } = folders.find(({ displayName }) => displayName === name); + return id; + }, + async getSampleEvents({ pageSize }) { + const folders = this.folderIds?.length + ? this.folderIds.map((id) => `/me/mailFolders/${id}/messages`) + : [ + "/me/messages", + ]; + + const results = []; + for (const folder of folders) { + const { value: messages } = await this.microsoftOutlook.listMessages({ + resource: folder, + params: { + $top: pageSize, + $orderby: "createdDateTime desc", + }, + }); + results.push(...messages); + } + return results; + }, + isRelevant(item) { + if (this.folderIds?.length) { + return this.folderIds.includes(item.parentFolderId); + } + // if no folderIds are specified, filter out items in Sent Items & Drafts + const sentItemFolderId = this.db.get("sentItemFolderId"); + const draftsFolderId = this.db.get("draftsFolderId"); + return item.parentFolderId !== sentItemFolderId && item.parentFolderId !== draftsFolderId; + }, emitEvent(item) { - this.$emit({ - email: item, - }, this.generateMeta(item)); + if (this.isRelevant(item)) { + this.$emit( + { + email: item, + }, + this.generateMeta(item), + ); + } }, generateMeta(item) { return { - id: item.id, + id: md5(item.id), // id > 64 characters, so dedupe on hash of id summary: `New email (ID:${item.id})`, ts: Date.parse(item.createdDateTime), }; }, }, async run(event) { - await this.run({ - event, - emitFn: async ({ resourceId } = {}) => { - const item = await this.microsoftOutlook.getMessage({ - messageId: resourceId, - }); - this.emitEvent(item); - }, - }); + const folders = this.folderIds?.length + ? this.folderIds.map((id) => `/me/mailFolders/${id}/messages`) + : [ + "/me/messages", + ]; + + for (const folder of folders) { + await this.run({ + event, + emitFn: async ({ resourceId } = {}) => { + try { + const item = await this.microsoftOutlook.getMessage({ + resource: folder, + messageId: resourceId, + }); + this.emitEvent(item); + } catch { + console.log(`Could not fetch message with ID: ${resourceId}`); + } + }, + }); + } }, sampleEmit, }; diff --git a/components/nextdoor/nextdoor.app.mjs b/components/nextdoor/nextdoor.app.mjs index a4a642f0273ce..24e8246661805 100644 --- a/components/nextdoor/nextdoor.app.mjs +++ b/components/nextdoor/nextdoor.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/osu/osu.app.mjs b/components/osu/osu.app.mjs index a677053ac2c61..657629379c64b 100644 --- a/components/osu/osu.app.mjs +++ b/components/osu/osu.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/components/uber_direct/uber_direct.app.mjs b/components/uber_direct/uber_direct.app.mjs index 6dcd1fd2faa30..c7734c0140eba 100644 --- a/components/uber_direct/uber_direct.app.mjs +++ b/components/uber_direct/uber_direct.app.mjs @@ -8,4 +8,4 @@ export default { console.log(Object.keys(this.$auth)); }, }, -}; \ No newline at end of file +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bc1472e8f3848..fb6afae221d92 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3174,8 +3174,7 @@ importers: components/efinder: {} - components/egnyte: - specifiers: {} + components/egnyte: {} components/elastic_email: {} @@ -6468,6 +6467,9 @@ importers: js-base64: specifier: ^3.7.2 version: 3.7.7 + md5: + specifier: ^2.3.0 + version: 2.3.0 mime-types: specifier: ^2.1.35 version: 2.1.35 @@ -7361,8 +7363,7 @@ importers: specifier: ^3.0.3 version: 3.0.3 - components/osu: - specifiers: {} + components/osu: {} components/otter_waiver: dependencies: @@ -11130,8 +11131,7 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/uber_direct: - specifiers: {} + components/uber_direct: {} components/uberduck: dependencies: