generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
147 lines (132 loc) · 4.38 KB
/
main.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
145
146
147
import { Plugin, TFile, MarkdownView, Notice, RequestUrlParam, requestUrl } from 'obsidian';
import { ICalSettings, DEFAULT_SETTINGS } from "./src/settings/ICalSettings"
import ICalSettingsTab from "./src/settings/ICalSettingsTab"
import { Event } from './src/ICalEvent/Event'
import SelectEventsModal from "./src/ICalEvent/SelectEventsModal"
import moment from 'moment'
export default class ICal extends Plugin {
settings: ICalSettings;
db: any;
eventLineTemplates: { name: string, template: string }[] = []
eventNoteTemplates: { name: string, template: string }[] = []
async getEventLineTemplate(): Promise<void> {
await Promise.all(this.settings.iCalEventNoteTemplates.map(async t => {
try {
const templateFile = this.app.vault.getAbstractFileByPath(t.lineTemplatePath)
if (templateFile instanceof TFile) {
this.eventLineTemplates.push({ name: t.name, template: await this.app.vault.read(templateFile) })
}
} catch (error) { }
}))
}
async getEventNoteTemplate(): Promise<void> {
await Promise.all(this.settings.iCalEventNoteTemplates.map(async t => {
try {
const templateFile = this.app.vault.getAbstractFileByPath(t.noteTemplatePath)
if (templateFile instanceof TFile) {
this.eventNoteTemplates.push({ name: t.name, template: await this.app.vault.read(templateFile) })
}
} catch (error) { }
}))
}
async getParticipantsForItem(calendarItemId: number) {
const params: RequestUrlParam = {
url: `${this.settings.apiUrl}/attendees`,
method: "GET",
//contentType: "application/json",
body: JSON.stringify({
databasePath: this.settings.calendarDbPath,
calendarItemId: calendarItemId
})
}
let participants = []
try {
participants = (await requestUrl(params)).json
} catch (error) {
console.log(error)
new Notice("Error in connecting to the Calendar.sqlitedb", 5000)
}
return participants;
}
async getEventsWithDate(date: string): Promise<any[]> {
const unixDate = moment(date).unix()
// call the GET request
const params: RequestUrlParam = {
url: `${this.settings.apiUrl}/fetchEvents`,
method: "GET",
//contentType: "application/json",
body: JSON.stringify({
databasePath: this.settings.calendarDbPath,
unixDate: unixDate
})
}
let calendarItems = []
try {
calendarItems = (await requestUrl(params)).json
} catch (error) {
console.log(error)
new Notice("Error in connecting to the Calendar.sqlitedb", 5000)
}
const events = Promise.all(calendarItems.map(async (item: any) => {
const participants: any[] = await this.getParticipantsForItem(item.eventId);
const organizer = participants.find((p: any) => p.role === 0)?.name
const attendees = participants.filter((p: any) => p.role !== 0).map((p: any) => p.name)
const event = new Event(
this,
date,
item.eventId,
item.status,
item.summary,
item.unixStart,
item.unixEnd,
organizer,
attendees
)
event.templateName = this.settings.iCalEventNoteTemplates.find(t => t.name === this.settings.defaultTemplate).name || ""
event.build()
return event
}));
return events;
}
async onload() {
console.log('loading ical plugin');
await this.loadSettings();
this.addSettingTab(new ICalSettingsTab(this.app, this))
this.addCommand({
id: "import_events",
name: "import events",
hotkeys: [
{
modifiers: ["Alt"],
key: 'T',
},
],
callback: async () => {
await this.getEventLineTemplate()
await this.getEventNoteTemplate()
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView)
if (activeView) {
const _fileDate = moment(activeView.file.basename, this.settings.dailyNoteDateFormat)
if (_fileDate.isValid()) {
const fileDate = _fileDate.format("YYYY-MM-DD")
const fs = require('fs');
const events = await this.getEventsWithDate(fileDate)
new SelectEventsModal(this, activeView.file, events, fileDate).open()
} else {
new Notice('iCal - you are not in a daily note')
}
}
}
})
}
onunload() {
console.log('unloading ical plugin');
if (this.db) this.db.close();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}