generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mailHandler.ts
144 lines (104 loc) · 4.51 KB
/
mailHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { MailFolder, Message, OutlookItem } from "@microsoft/microsoft-graph-types"
import { MSALAuthProvider } from "authProvider"
import MSGraphPlugin from "MSGraphPlugin"
import { SelectMailFolderModal } from "selectMailFolderModal"
// @ts-ignore
import * as Eta from './node_modules/eta/dist/browser.module.mjs'
import { MSGraphMailFolderAccess } from "types"
import moment from "moment"
export class MailHandler {
plugin:MSGraphPlugin
eta:Eta.Eta
constructor(plugin:MSGraphPlugin) {
this.plugin = plugin
this.eta = new Eta.Eta()
}
getMailFolders = async (authProvider: MSALAuthProvider, limit=250):Promise<MailFolder[]> => {
// this could be so much simpler, if we wouldn't support self-hosted exchange installations...
// otherwise, we could just query https://graph.microsoft.com/v1.0/me/mailfolders/delta?$select=displayname
// and would be done...
const graphClient = this.plugin.getGraphClient(authProvider)
const getChildFoldersRecursive = async (fs:Array<MailFolder>): Promise<MailFolder[]> => {
let result:Array<MailFolder> = []
for (const f of fs) {
const request = graphClient
.api(`/me/mailfolders/${f.id}/childFolders?includeHiddenFolders=true`)
.select("displayName,childFolderCount")
.top(limit)
const childFolders = (await request.get()).value
result = result.concat(childFolders)
const remainingChildFolders = childFolders.filter((f:MailFolder) => f.childFolderCount > 0)
const remainingGrandChildren = await getChildFoldersRecursive(remainingChildFolders)
result = result.concat(remainingChildFolders).concat(remainingGrandChildren)
}
return result
}
let request = graphClient
.api("/me/mailfolders?includeHiddenFolders=true")
.select("displayName,childFolderCount")
.top(limit)
let root_folders = (await request.get()).value
root_folders = root_folders.concat(await getChildFoldersRecursive(root_folders.filter((f:MailFolder) => f.childFolderCount > 0)))
request = graphClient
.api("/me/mailFolders/searchfolders/childFolders")
.select("displayName,childFolderCount")
.top(limit)
root_folders = root_folders.concat((await request.get()).value)
return root_folders
}
selectMailFolder = async (account:string) => {
const selector = new SelectMailFolderModal(this.plugin.app, this.plugin)
const folders = await this.getMailFolders(this.plugin.msalProviders[account])
selector.setFolders(folders)
selector.open()
}
getMailsForFolder = async (mf: MSGraphMailFolderAccess) => {
const authProvider = this.plugin.msalProviders[mf.provider]
const graphClient = this.plugin.getGraphClient(authProvider)
let request = graphClient.api(`/me/mailFolders/${mf.id}/messages`)
if (mf.query !== undefined)
request = request.query(mf.query)
if (mf.limit !== undefined)
request = request.top(mf.limit)
if (mf.onlyFlagged)
request = request.filter('flag/flagStatus eq \'flagged\'')
return (await request.get()).value
}
getMailsForAllFolders = async () => {
const mails:Record<string, OutlookItem[]> = {}
for (const mf of this.plugin.settings.mailFolders) {
mails[mf.displayName] = await this.getMailsForFolder(mf)
}
return mails
}
formatMails = (mails:Record<string, unknown[]>, as_tasks=false):string => {
let result = ""
for (const folder in mails) {
result += `# ${folder}\n\n`
for (const m of mails[folder]) {
result += this.eta.renderString(as_tasks
? this.plugin.settings.flaggedMailTemplate
: this.plugin.settings.mailTemplate, m) + "\n\n"
}
result += "\n"
}
return result
}
formatMailsForAllFolders = async (as_tasks=false): Promise<string> => {
return this.formatMails(await this.getMailsForAllFolders(), as_tasks)
}
dueFilter = (m:Message) => {
return (m?.flag?.dueDateTime?.dateTime !== undefined)
? moment.utc(m.flag.dueDateTime.dateTime) <= moment()
: true
}
formatOverdueMailsForAllFolders = async (): Promise<string> => {
const ms = await this.getMailsForAllFolders()
for (const folder in ms) {
if (ms[folder].length > 0) {
ms[folder] = ms[folder].filter(this.dueFilter)
}
}
return this.formatMails(ms, true)
}
}