generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
calendarHandler.ts
126 lines (94 loc) · 4.49 KB
/
calendarHandler.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
import MSGraphPlugin from "MSGraphPlugin";
import { htmlToMarkdown } from "obsidian";
import { EventWithProvider } from "types";
import { Event } from '@microsoft/microsoft-graph-types'
import { DateTime } from "luxon";
// @ts-ignore
import * as Eta from './node_modules/eta/dist/browser.module.mjs'
import { AppointmentSchema, BasePropertySet, CalendarFolder, CalendarView, DateTime as EWSDateTime, EmailMessageSchema, EwsLogging, ExchangeService, FolderId, ItemView, Mailbox, OAuthCredentials, PropertySet, Uri, WellKnownFolderName } from "ews-javascript-api"
export class CalendarHandler {
plugin:MSGraphPlugin
eta:Eta.Eta
constructor(plugin:MSGraphPlugin) {
this.plugin = plugin
this.eta = new Eta.Eta()
}
getEventsForTimeRange = async (start:DateTime, end:DateTime):Promise<Record<string, [Event]>> => {
const events_by_provider:Record<string, [Event]> = {}
// todo: fetch in parallel using Promise.all()?
for (const authProviderName in this.plugin.msalProviders) {
const authProvider = this.plugin.msalProviders[authProviderName]
if (authProvider.account.type == "MSGraph") {
const graphClient = this.plugin.getGraphClient(authProvider)
const dateQuery = `startDateTime=${encodeURIComponent(start.toISO())}&endDateTime=${encodeURIComponent(end.toISO())}`;
const events = await graphClient
.api('/me/calendarView').query(dateQuery)
//.select('subject,start,end')
.orderby(`start/DateTime`)
.get();
events_by_provider[authProviderName] = events.value
} else if (authProvider.account.type == "EWS") {
const authProvider = this.plugin.msalProviders[authProviderName]
const token = await authProvider.getAccessToken()
const svc = new ExchangeService();
//EwsLogging.DebugLogEnabled = true; // false to turnoff debugging.
svc.Url = new Uri(`${authProvider.account.baseUri}/EWS/Exchange.asmx`);
svc.Credentials = new OAuthCredentials(token);
// Initialize values for the start and end times, and the number of appointments to retrieve.
// Initialize the calendar folder object with only the folder ID.
const calendar = await CalendarFolder.Bind(svc, WellKnownFolderName.Calendar, new PropertySet());
// Set the start and end time and number of appointments to retrieve.
const calendarView = new CalendarView(new EWSDateTime(start.toMillis()), new EWSDateTime(end.toMillis()));
// Limit the properties returned to the appointment's subject, start time, and end time.
calendarView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.IsAllDayEvent);
// Retrieve a collection of appointments by using the calendar view.
const appointments = await calendar.FindAppointments(calendarView);
const results = appointments.Items.map(({ Subject, Start, End, IsAllDayEvent }) => (
{
subject: Subject,
start: {dateTime: Start.ToISOString()},
end: {dateTime: End.ToISOString()},
isAllDay: IsAllDayEvent
} as Event)) as [Event];
events_by_provider[authProviderName] = results
}
}
return events_by_provider
}
getEventsForToday = async () => {
const today = DateTime.now().startOf('day')
const tomorrow = DateTime.now().endOf('day')
return await this.getEventsForTimeRange(today, tomorrow)
}
flattenAndSortEventsByStartTime = (events_by_provider:Record<string, [Event]>) => {
const result:Array<EventWithProvider> = []
for (const provider in events_by_provider) {
for (const event of events_by_provider[provider]) {
event.subject = htmlToMarkdown(event.subject)
result.push({...event, provider: provider})
}
}
result.sort((a,b):number => {
const at = DateTime.fromISO(a.start.dateTime, {zone: a.start.timeZone})
const bt = DateTime.fromISO(b.start.dateTime, {zone: b.start.timeZone})
return at.toMillis() - bt.toMillis()
})
return result
}
formatEvents = (events:Record<string,[Event]>):string => {
let result = ""
const flat_events = this.flattenAndSortEventsByStartTime(events)
try {
for (const e of flat_events) {
console.log(e)
result += this.eta.renderString(this.plugin.settings.eventTemplate, {...e, luxon:DateTime }) + "\n\n"
}
} catch (e) {
console.error(e)
}
return result
}
formatEventsForToday = async () => {
return this.formatEvents(await this.getEventsForToday())
}
}