Skip to content
69 changes: 48 additions & 21 deletions components/microsoft_outlook/sources/new-email/new-email.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ 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",
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.",
optional: true,
},
},
hooks: {
...common.hooks,
async activate() {
await this.activate({
changeType: "created",
resource: "/me/mailfolders('inbox')/messages",
resource: "/me/messages",
});
},
async deactivate() {
Expand All @@ -23,17 +32,28 @@ export default {
methods: {
...common.methods,
async getSampleEvents({ pageSize }) {
return this.microsoftOutlook.listMessages({
params: {
$top: pageSize,
$orderby: "createdDateTime desc",
},
});
const folders = this.folderIds?.length
? this.folderIds.map((id) => `/me/mailFolders/${id}/messages`)
: ["/me/messages"];

const results = [];
for (const folder of folders) {
const messages = await this.microsoftOutlook.listMessages({
resource: folder,
params: {
$top: pageSize,
$orderby: "createdDateTime desc",
},
});
results.push(...messages);
}
return results;
},
emitEvent(item) {
this.$emit({
email: item,
}, this.generateMeta(item));
this.$emit(
{ email: item },
this.generateMeta(item)
);
},
generateMeta(item) {
return {
Expand All @@ -44,15 +64,22 @@ export default {
},
},
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 } = {}) => {
const item = await this.microsoftOutlook.getMessage({
resource: folder,
messageId: resourceId,
});
this.emitEvent(item);
},
});
}
},
sampleEmit,
};