generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
269 lines (243 loc) · 6.57 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import {
App,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
addIcon,
Editor
} from 'obsidian'
import { AuthenticationConfig } from 'strava-v3'
import { fetchAthleteActivities, fetchAthleteActivity } from 'src/retriever'
import FileManager from 'src/fileManager'
import { ee } from 'src/eventEmitter'
import { DateTime } from 'luxon'
import auth from 'src/auth'
import * as path from 'path'
interface SyncSettings {
lastSyncedAt: string
activityDetailsRetrievedUntil: string
}
interface StravaActivitiesSettings {
authSettings: AuthenticationConfig
syncSettings: SyncSettings
}
const DEFAULT_SETTINGS: StravaActivitiesSettings = {
authSettings: {
access_token: '',
client_id: '',
client_secret: '',
redirect_uri: 'obsidian://obsidianforstrava/callback',
},
syncSettings: {
lastSyncedAt: '', // e.g., '2023-09-14T14:44:56.106Z'
activityDetailsRetrievedUntil: '', // e.g., '2023-01-01T14:44:56.106Z'
},
}
export default class StravaActivities extends Plugin {
settings = DEFAULT_SETTINGS
fileManager: FileManager
async onload() {
addIcon(
'stravaIcon',
`<path
d="M15.387 17.944l-2.089-4.116h-3.065L15.387 24l5.15-10.172h-3.066m-7.008-5.599l2.836 5.598h4.172L10.463 0l-7 13.828h4.169"
transform="scale(4)" />
`
)
await this.loadSettings()
this.fileManager = new FileManager(this.app.vault)
ee.on('activitiesSynced', async () => {
this.settings.syncSettings.lastSyncedAt =
DateTime.utc().toISO() ?? ''
await this.saveSettings()
})
this.registerObsidianProtocolHandler(
'obsidianforstrava/callback',
async (args) => {
await auth.OAuthCallback(args)
}
)
this.addSettingTab(new StravaActivitiesSettingTab(this.app, this))
this.addCommand({
id: 'authenticate',
name: 'Authenticate with Strava',
callback: () => auth.authenticate(this.settings.authSettings),
})
this.addCommand({
id: 'insert-todays-strava-activities',
name: "Insert today's Strava activities",
editorCallback: (editor: Editor) => {
this.handleInsertStravaActivitiesCommand(editor, false)
},
})
this.addCommand({
id: 'insert-todays-strava-activity-maps',
name: "Insert today's Strava activity maps",
editorCallback: (editor: Editor) => {
this.handleInsertStravaActivitiesCommand(editor, true)
},
})
// this.addCommand({
// id: 'activity-details-command',
// name: 'Retrieve detailed activities',
// callback: () =>
// fetchDetailedActivities(
// DateTime.fromISO(
// this.settings.syncSettings.activityDetailsRetrievedUntil
// )
// ),
// })
const ribbonIconEl = this.addRibbonIcon(
'stravaIcon',
'Synchronize Strava activities',
async (evt: MouseEvent) => {
new Notice('Started synchronizing Strava activities')
try {
const activities = await fetchAthleteActivities(
1,
200,
this.settings.syncSettings.lastSyncedAt
)
ee.emit('activitiesRetrieved', activities)
new Notice('Strava activities synchronized')
} catch (err) {
console.error(`Error: ${err}`)
new Notice('Failed synchronizing Strava activities')
}
}
)
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class')
this.registerEvent(
this.app.workspace.on('file-menu', (menu, file) => {
menu.addItem((item) => {
item.setTitle('Get Strava activity detail 🏃♀️')
.setIcon('import')
.onClick(async () => {
try {
const activityDateFolder = path.dirname(
file.path
)
const activityId =
path.basename(activityDateFolder)
await fetchAthleteActivity(
Number(activityId),
true,
file.path
)
new Notice('Activity retrieved')
} catch (error) {
new Notice('Failed retrieving the activity')
}
})
})
})
)
}
handleInsertStravaActivitiesCommand(editor: Editor, onlyMaps: boolean) {
const currentDate = DateTime.now().toISODate() ?? ''
const activityFolderPaths = this.fileManager.getChildrenPathsInFolder(currentDate)
let content = "## Today's Strava Activities\n"
for (const path of activityFolderPaths) {
console.log(path)
content += onlyMaps ? `\n![[${path}/Summary#Map]]\n` : `\n![[${path}/Summary]]\n`
}
content+='\n'
editor.replaceRange(
content,
editor.getCursor()
);
}
onunload() {
this.settings = DEFAULT_SETTINGS
this.saveSettings()
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
)
}
async saveSettings() {
await this.saveData(this.settings)
}
}
class StravaApplicationDetailsModal extends Modal {
plugin: StravaActivities
constructor(app: App, plugin: StravaActivities) {
super(app)
this.plugin = plugin
}
onOpen() {
const { contentEl } = this
const form = contentEl.createEl('div')
form.createEl('label', { text: 'Client ID: ' })
const clientIdElement = form.createEl('input', {
type: 'number',
attr: { id: 'clientId', name: 'clientId' },
value: this.plugin.settings.authSettings.client_id,
})
form.createEl('br')
form.createEl('br')
form.createEl('label', { text: 'Client Secret: ' })
const clientSecretElement = form.createEl('input', {
type: 'password',
attr: { id: 'clientSecret', name: 'clientSecret' },
value: this.plugin.settings.authSettings.client_secret,
})
form.createEl('br')
form.createEl('br')
const saveInputElement = form.createEl('input', {
type: 'button',
value: 'Save',
})
saveInputElement.onClickEvent(() => {
this.plugin.settings.authSettings.client_id = clientIdElement.value
this.plugin.settings.authSettings.client_secret =
clientSecretElement.value
this.plugin.saveSettings()
this.close()
})
}
onClose() {
this.contentEl.empty()
}
}
class StravaActivitiesSettingTab extends PluginSettingTab {
plugin: StravaActivities
constructor(app: App, plugin: StravaActivities) {
super(app, plugin)
this.plugin = plugin
}
display(): void {
const { containerEl } = this
containerEl.empty()
new Setting(containerEl)
.setName('Enter Strava Credentials')
.setDesc('Set Strava Credentials')
.addButton((button) =>
button
.setButtonText('Enter Strava Credentials')
// TODO: set button class
.onClick((me) =>
new StravaApplicationDetailsModal(
this.app,
this.plugin
).open()
)
)
new Setting(containerEl)
.setName('Authenticate')
.setDesc('Authenticate your Strava account')
.addButton((button) =>
button
.setButtonText('Authenticate')
.onClick(() =>
auth.authenticate(this.plugin.settings.authSettings)
)
)
}
}