@@ -15,10 +15,32 @@ export default {
15
15
label : "Folder IDs to Monitor" ,
16
16
description : "Specify the folder IDs or names in Outlook that you want to monitor for new emails. Leave empty to monitor all folders." ,
17
17
optional : true ,
18
+ async options ( ) {
19
+ const { value : folders } = await this . listFolders ( ) ;
20
+ return folders ?. map ( ( {
21
+ id : value , displayName : label ,
22
+ } ) => ( {
23
+ value,
24
+ label,
25
+ } ) ) || [ ] ;
26
+ } ,
18
27
} ,
19
28
} ,
20
29
hooks : {
21
30
...common . hooks ,
31
+ async deploy ( ) {
32
+ this . db . set ( "sentItemFolderId" , await this . getSentItemFolderId ( ) ) ;
33
+
34
+ const events = await this . getSampleEvents ( {
35
+ pageSize : 25 ,
36
+ } ) ; console . log ( events ) ;
37
+ if ( ! events || events . length == 0 ) {
38
+ return ;
39
+ }
40
+ for ( const item of events ) {
41
+ this . emitEvent ( item ) ;
42
+ }
43
+ } ,
22
44
async activate ( ) {
23
45
await this . activate ( {
24
46
changeType : "created" ,
@@ -31,6 +53,16 @@ export default {
31
53
} ,
32
54
methods : {
33
55
...common . methods ,
56
+ listFolders ( ) {
57
+ return this . microsoftOutlook . _makeRequest ( {
58
+ path : "/me/mailFolders" ,
59
+ } ) ;
60
+ } ,
61
+ async getSentItemFolderId ( ) {
62
+ const { value : folders } = await this . listFolders ( ) ;
63
+ const { id } = folders . find ( ( { displayName } ) => displayName === "Sent Items" ) ;
64
+ return id ;
65
+ } ,
34
66
async getSampleEvents ( { pageSize } ) {
35
67
const folders = this . folderIds ?. length
36
68
? this . folderIds . map ( ( id ) => `/me/mailFolders/${ id } /messages` )
@@ -51,13 +83,23 @@ export default {
51
83
}
52
84
return results ;
53
85
} ,
86
+ isRelevant ( item ) {
87
+ if ( this . folderIds ?. length ) {
88
+ return true ;
89
+ }
90
+ // if no folderIds are specified, filter out items in Sent Items
91
+ const sentItemFolderId = this . db . get ( "sentItemFolderId" ) ;
92
+ return item . parentFolderId !== sentItemFolderId ;
93
+ } ,
54
94
emitEvent ( item ) {
55
- this . $emit (
56
- {
57
- email : item ,
58
- } ,
59
- this . generateMeta ( item ) ,
60
- ) ;
95
+ if ( this . isRelevant ( item ) ) {
96
+ this . $emit (
97
+ {
98
+ email : item ,
99
+ } ,
100
+ this . generateMeta ( item ) ,
101
+ ) ;
102
+ }
61
103
} ,
62
104
generateMeta ( item ) {
63
105
return {
@@ -78,11 +120,15 @@ export default {
78
120
await this . run ( {
79
121
event,
80
122
emitFn : async ( { resourceId } = { } ) => {
81
- const item = await this . microsoftOutlook . getMessage ( {
82
- resource : folder ,
83
- messageId : resourceId ,
84
- } ) ;
85
- this . emitEvent ( item ) ;
123
+ try {
124
+ const item = await this . microsoftOutlook . getMessage ( {
125
+ resource : folder ,
126
+ messageId : resourceId ,
127
+ } ) ;
128
+ this . emitEvent ( item ) ;
129
+ } catch {
130
+ console . log ( `Could not fetch message with ID: ${ resourceId } ` ) ;
131
+ }
86
132
} ,
87
133
} ) ;
88
134
}
0 commit comments