-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMSGraphPlugin.ts
More file actions
111 lines (82 loc) · 3.29 KB
/
MSGraphPlugin.ts
File metadata and controls
111 lines (82 loc) · 3.29 KB
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
import { Editor, MarkdownView, ObsidianProtocolData, Plugin } from 'obsidian';
import { MSALAuthProvider, msalRedirect, refreshAllTokens } from 'authProvider';
import { Client } from '@microsoft/microsoft-graph-client';
import { MSGraphPluginSettings, DEFAULT_SETTINGS, MSGraphPluginSettingsTab } from 'msgraphPluginSettings';
import { MSGraphAccount } from 'types';
import { MailHandler } from 'mailHandler'
import { CalendarHandler } from 'calendarHandler';
export default class MSGraphPlugin extends Plugin {
settings: MSGraphPluginSettings;
msalProviders: Record<string, MSALAuthProvider> = {}
graphClient: Client
calendarHandler: CalendarHandler
mailHandler: MailHandler
authenticateAccount = (account: MSGraphAccount) => {
const provider = new MSALAuthProvider(account)
this.msalProviders[account.displayName] = provider
return provider
}
getGraphClient = (msalProvider: MSALAuthProvider): Client => {
return Client.initWithMiddleware({
debugLogging: true,
authProvider: msalProvider
});
}
checkProvider = (displayName: string) => {
return (
displayName in this.msalProviders &&
this.msalProviders[displayName].isInitialized()
)
}
async onload() {
await this.loadSettings();
this.calendarHandler = new CalendarHandler(this)
this.mailHandler = new MailHandler(this)
this.registerObsidianProtocolHandler('msgraph', (query: ObsidianProtocolData) => {msalRedirect(this, query);})
// register all stored providers
for (const account of this.settings.accounts) {
if (account.displayName.trim() !== "" && account.enabled) {
this.authenticateAccount(account)
}
}
// Append today's events, sorted by start time
this.addCommand({
id: 'append-todays-events-by-start',
name: 'Append today\'s Events, sorted by start date',
editorCallback: async (editor: Editor, view: MarkdownView) => {
const result = this.calendarHandler.formatEvents(await this.calendarHandler.getEventsForToday())
editor.replaceSelection(result);
}
});
this.addCommand({
id: 'get-mails-from-all-folders',
name: 'Append mails from all folders registered in the settings',
editorCallback: async (editor: Editor, view: MarkdownView) => {
const result = await this.mailHandler.formatMails(await this.mailHandler.getMailsForAllFolders(), false)
editor.replaceSelection(result)
}
})
this.addCommand({
id: 'get-mails-from-all-folders-as-tasks',
name: 'Append mails from all folders registered in the settings and format as tasks',
editorCallback: async (editor: Editor, view: MarkdownView) => {
const result = await this.mailHandler.formatMails(await this.mailHandler.getMailsForAllFolders(), true)
editor.replaceSelection(result)
}
})
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new MSGraphPluginSettingsTab(this.app, this));
// Try to refresh the tokens every 30 minutes
this.registerInterval(
window.setInterval(() => refreshAllTokens(this), 1000 * 60 * 30)
)
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}