generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
183 lines (158 loc) · 5.28 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
import { Notice, Plugin, WorkspaceLeaf } from "obsidian";
import { RootView, VIEW_TYPE_ROOT } from "./src/RootView";
import { DateTime } from "luxon";
import { AuthenticationConfig } from "strava-v3";
import { StravaActivitiesSettingTab } from "./src/tabs/strava-activities-setting-tab";
import auth from "./src/services/auth-service";
import { SummaryActivity } from "strava-types";
import athleteService from "./src/services/athlete-servince";
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://obsidian-strava-connector/callback",
},
syncSettings: {
lastSyncedAt: "", // e.g., '2023-09-14T14:44:56.106Z'
activityDetailsRetrievedUntil: "", // e.g., '2023-01-01T14:44:56.106Z'
},
};
/**
* Represents a plugin that connects to Strava.
*/
export default class StravaConnectorPlugin extends Plugin {
settings = DEFAULT_SETTINGS;
async onload() {
await this.loadSettings();
this.addSettingTab(new StravaActivitiesSettingTab(this.app, this));
// Register the root view
this.registerView(
VIEW_TYPE_ROOT,
(leaf) => new RootView(leaf, this.settings)
);
this.registerObsidianProtocolHandler(
"obsidian-strava-connector/callback",
async (args) => {
await auth.OAuthCallback(args);
}
);
// Add a ribbon icon to activate the view
this.addRibbonIcon("activity", "Strava Dashboard", () => {
this.activateView();
});
this.addCommand({
id: "authenticate",
name: "Authenticate with Strava",
callback: () => auth.authenticate(this.settings.authSettings),
});
const ribbonIconEl = this.addRibbonIcon(
"refresh-ccw",
"Synchronize Current Week Activities",
async (evt: MouseEvent) => {
new Notice("Synchronizing current week Strava activities...");
try {
const currentWeekActivities: SummaryActivity[] = await athleteService.getActivities(
{
after: DateTime.utc().startOf("week").toSeconds()
});
if (currentWeekActivities.length === 0) {
new Notice("You've not logged any activities this week.");
return;
}
await this.createOrUpdateAthleteActivitiesFiles(currentWeekActivities);
this.settings.syncSettings.lastSyncedAt = DateTime.utc().toISO();
this.saveSettings();
new Notice("Successfully synchronized current week Strava activities");
} catch (error) {
new Notice("Failed to synchronize Strava activities.");
console.error(error);
}
}
);
ribbonIconEl.addClass("my-plugin-ribbon-class");
}
private async ensureFolderStructureExists(activityDate: string) {
// Ensure the "Strava" folder exists
if (!this.app.vault.getFolderByPath("Strava")) {
await this.app.vault.createFolder("Strava");
}
// Ensure the "Strava/Activities" folder exists
if (!this.app.vault.getFolderByPath("Strava/Activities")) {
await this.app.vault.createFolder("Strava/Activities");
}
// Ensure the "Strava/Activities/DayOfActivity" folder exists
if (!this.app.vault.getFolderByPath(`Strava/Activities/${activityDate}`)) {
await this.app.vault.createFolder(`Strava/Activities/${activityDate}`);
}
}
private async createOrUpdateAthleteActivitiesFiles(currentWeekActivities: SummaryActivity[]) {
try {
for (const currentActivity of currentWeekActivities) {
const activityDate = DateTime.fromISO(currentActivity.start_date).toFormat("dd-MM-yyyy").toString();
const filePath = `Strava/Activities/${activityDate}/Summary.md`;
await this.ensureFolderStructureExists(activityDate);
// Convert cycling data to YAML format for frontmatter
const yamlFrontmatter = `---
title: ${currentActivity.name}
distance: ${Math.fround(currentActivity.distance / 1000).toFixed(2)}
date: ${activityDate}
sport: ${currentActivity.sport_type}
kudos: ${currentActivity.kudos_count}
---
`;
// Combine frontmatter and original JSON data
const mdContent = `${yamlFrontmatter}\n\`\`\`json\n${JSON.stringify(currentActivity, null, 4)}\n\`\`\``;
const file = this.app.vault.getFileByPath(filePath);
if (!file) {
await this.app.vault.create(filePath, mdContent);
} else {
await this.app.vault.modify(file, mdContent);
}
}
} catch (error) {
new Notice("Failed to create or update markdown file: ");
}
}
/**
* Activates the Strava view.
*/
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_ROOT);
if (leaves.length > 0) {
// A leaf with our view already exists, use that
leaf = leaves[0];
} else {
// Our view could not be found in the workspace, create a new leaf
// in the right sidebar for it
leaf = workspace.getLeaf(false);
await leaf!.setViewState({ type: VIEW_TYPE_ROOT, active: true });
}
// "Reveal" the leaf in case it is in a collapsed sidebar
workspace.revealLeaf(leaf!);
}
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);
}
}