2026-04-20 04:04:19 +05:30
|
|
|
import { Notice, Plugin } from "obsidian";
|
|
|
|
|
import { SurfSenseApiClient } from "./api-client";
|
|
|
|
|
import { PersistentQueue } from "./queue";
|
|
|
|
|
import { SurfSenseSettingTab } from "./settings";
|
|
|
|
|
import { StatusBar } from "./status-bar";
|
|
|
|
|
import { SyncEngine } from "./sync-engine";
|
|
|
|
|
import {
|
|
|
|
|
DEFAULT_SETTINGS,
|
|
|
|
|
type QueueItem,
|
|
|
|
|
type StatusState,
|
|
|
|
|
type SurfsensePluginSettings,
|
|
|
|
|
} from "./types";
|
|
|
|
|
|
2026-04-20 18:19:30 +05:30
|
|
|
/** SurfSense plugin entry point. */
|
2026-04-20 04:04:19 +05:30
|
|
|
export default class SurfSensePlugin extends Plugin {
|
|
|
|
|
settings!: SurfsensePluginSettings;
|
|
|
|
|
api!: SurfSenseApiClient;
|
|
|
|
|
queue!: PersistentQueue;
|
|
|
|
|
engine!: SyncEngine;
|
2026-04-20 18:19:30 +05:30
|
|
|
/**
|
|
|
|
|
* Per-install identifier kept in `app.saveLocalStorage` rather than
|
|
|
|
|
* `data.json`, so it does NOT travel through Obsidian Sync — each
|
|
|
|
|
* machine on a synced vault stays distinguishable.
|
|
|
|
|
*/
|
|
|
|
|
deviceId = "";
|
2026-04-20 04:04:19 +05:30
|
|
|
private statusBar: StatusBar | null = null;
|
|
|
|
|
lastStatus: StatusState = { kind: "idle", queueDepth: 0 };
|
|
|
|
|
serverCapabilities: string[] = [];
|
|
|
|
|
serverApiVersion: string | null = null;
|
|
|
|
|
private settingTab: SurfSenseSettingTab | null = null;
|
2026-04-19 23:48:18 +05:30
|
|
|
|
|
|
|
|
async onload() {
|
|
|
|
|
await this.loadSettings();
|
2026-04-20 04:04:19 +05:30
|
|
|
this.seedIdentity();
|
|
|
|
|
await this.saveSettings();
|
|
|
|
|
|
|
|
|
|
this.api = new SurfSenseApiClient({
|
|
|
|
|
getServerUrl: () => this.settings.serverUrl,
|
|
|
|
|
getToken: () => this.settings.apiToken,
|
2026-04-19 23:48:18 +05:30
|
|
|
});
|
|
|
|
|
|
2026-04-20 04:04:19 +05:30
|
|
|
this.queue = new PersistentQueue(this.settings.queue ?? [], {
|
|
|
|
|
persist: async (items) => {
|
|
|
|
|
this.settings.queue = items;
|
|
|
|
|
await this.saveData(this.settings);
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-04-19 23:48:18 +05:30
|
|
|
|
2026-04-20 04:04:19 +05:30
|
|
|
this.engine = new SyncEngine({
|
|
|
|
|
app: this.app,
|
|
|
|
|
apiClient: this.api,
|
|
|
|
|
queue: this.queue,
|
|
|
|
|
getSettings: () => this.settings,
|
|
|
|
|
saveSettings: async (mut) => {
|
|
|
|
|
mut(this.settings);
|
|
|
|
|
await this.saveSettings();
|
|
|
|
|
this.settingTab?.renderStatus();
|
|
|
|
|
},
|
2026-04-20 18:19:30 +05:30
|
|
|
getDeviceId: () => this.deviceId,
|
2026-04-20 04:04:19 +05:30
|
|
|
setStatus: (s) => {
|
|
|
|
|
this.lastStatus = s;
|
|
|
|
|
this.statusBar?.update(s);
|
|
|
|
|
this.settingTab?.renderStatus();
|
|
|
|
|
},
|
|
|
|
|
onCapabilities: (caps, apiVersion) => {
|
|
|
|
|
this.serverCapabilities = [...caps];
|
|
|
|
|
this.serverApiVersion = apiVersion;
|
|
|
|
|
this.settingTab?.renderStatus();
|
|
|
|
|
},
|
2026-04-19 23:48:18 +05:30
|
|
|
});
|
2026-04-20 04:04:19 +05:30
|
|
|
|
|
|
|
|
this.queue.setFlushHandler(() => {
|
|
|
|
|
if (this.settings.syncMode !== "auto") return;
|
|
|
|
|
void this.engine.flushQueue();
|
2026-04-19 23:48:18 +05:30
|
|
|
});
|
2026-04-20 04:04:19 +05:30
|
|
|
|
|
|
|
|
this.settingTab = new SurfSenseSettingTab(this.app, this);
|
|
|
|
|
this.addSettingTab(this.settingTab);
|
|
|
|
|
|
|
|
|
|
const statusHost = this.addStatusBarItem();
|
|
|
|
|
this.statusBar = new StatusBar(statusHost);
|
|
|
|
|
this.statusBar.update(this.lastStatus);
|
|
|
|
|
|
|
|
|
|
this.registerEvent(
|
|
|
|
|
this.app.vault.on("create", (file) => this.engine.onCreate(file)),
|
|
|
|
|
);
|
|
|
|
|
this.registerEvent(
|
|
|
|
|
this.app.vault.on("modify", (file) => this.engine.onModify(file)),
|
|
|
|
|
);
|
|
|
|
|
this.registerEvent(
|
|
|
|
|
this.app.vault.on("delete", (file) => this.engine.onDelete(file)),
|
|
|
|
|
);
|
|
|
|
|
this.registerEvent(
|
|
|
|
|
this.app.vault.on("rename", (file, oldPath) =>
|
|
|
|
|
this.engine.onRename(file, oldPath),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
this.registerEvent(
|
|
|
|
|
this.app.metadataCache.on("changed", (file, data, cache) =>
|
|
|
|
|
this.engine.onMetadataChanged(file, data, cache),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-19 23:48:18 +05:30
|
|
|
this.addCommand({
|
2026-04-20 04:04:19 +05:30
|
|
|
id: "resync-vault",
|
|
|
|
|
name: "Re-sync entire vault",
|
|
|
|
|
callback: async () => {
|
|
|
|
|
try {
|
|
|
|
|
await this.engine.maybeReconcile(true);
|
|
|
|
|
new Notice("Surfsense: re-sync started.");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
new Notice(`Surfsense: re-sync failed — ${(err as Error).message}`);
|
2026-04-19 23:48:18 +05:30
|
|
|
}
|
2026-04-20 04:04:19 +05:30
|
|
|
},
|
2026-04-19 23:48:18 +05:30
|
|
|
});
|
|
|
|
|
|
2026-04-20 04:04:19 +05:30
|
|
|
this.addCommand({
|
|
|
|
|
id: "sync-current-note",
|
|
|
|
|
name: "Sync current note",
|
|
|
|
|
checkCallback: (checking) => {
|
|
|
|
|
const file = this.app.workspace.getActiveFile();
|
|
|
|
|
if (!file || file.extension.toLowerCase() !== "md") return false;
|
|
|
|
|
if (checking) return true;
|
|
|
|
|
this.queue.enqueueUpsert(file.path);
|
|
|
|
|
void this.engine.flushQueue();
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2026-04-19 23:48:18 +05:30
|
|
|
});
|
|
|
|
|
|
2026-04-20 04:04:19 +05:30
|
|
|
this.addCommand({
|
|
|
|
|
id: "open-settings",
|
|
|
|
|
name: "Open settings",
|
|
|
|
|
callback: () => {
|
2026-04-20 18:19:30 +05:30
|
|
|
// `app.setting` isn't in the d.ts; fall back silently if it moves.
|
2026-04-20 04:04:19 +05:30
|
|
|
type SettingHost = {
|
|
|
|
|
open?: () => void;
|
|
|
|
|
openTabById?: (id: string) => void;
|
|
|
|
|
};
|
|
|
|
|
const setting = (this.app as unknown as { setting?: SettingHost }).setting;
|
|
|
|
|
if (setting?.open) setting.open();
|
|
|
|
|
if (setting?.openTabById) setting.openTabById(this.manifest.id);
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-04-19 23:48:18 +05:30
|
|
|
|
2026-04-20 18:19:30 +05:30
|
|
|
// Wait for layout so the metadataCache is warm before reconcile.
|
2026-04-20 04:04:19 +05:30
|
|
|
this.app.workspace.onLayoutReady(() => {
|
|
|
|
|
void this.engine.start();
|
|
|
|
|
});
|
2026-04-19 23:48:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onunload() {
|
2026-04-20 04:04:19 +05:30
|
|
|
this.queue?.cancelFlush();
|
|
|
|
|
this.queue?.requestStop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get queueDepth(): number {
|
|
|
|
|
return this.queue?.size ?? 0;
|
2026-04-19 23:48:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async loadSettings() {
|
2026-04-20 04:04:19 +05:30
|
|
|
const data = (await this.loadData()) as Partial<SurfsensePluginSettings> | null;
|
|
|
|
|
this.settings = {
|
|
|
|
|
...DEFAULT_SETTINGS,
|
|
|
|
|
...(data ?? {}),
|
|
|
|
|
queue: (data?.queue ?? []).map((i: QueueItem) => ({ ...i })),
|
|
|
|
|
tombstones: { ...(data?.tombstones ?? {}) },
|
|
|
|
|
excludePatterns: data?.excludePatterns?.length
|
|
|
|
|
? [...data.excludePatterns]
|
|
|
|
|
: [...DEFAULT_SETTINGS.excludePatterns],
|
|
|
|
|
};
|
2026-04-19 23:48:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async saveSettings() {
|
|
|
|
|
await this.saveData(this.settings);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 18:19:30 +05:30
|
|
|
/**
|
|
|
|
|
* Mint vault_id (in data.json, travels with the vault) and device_id
|
|
|
|
|
* (in `app.saveLocalStorage`, stays per-install) on first run.
|
|
|
|
|
*/
|
2026-04-20 04:04:19 +05:30
|
|
|
private seedIdentity(): void {
|
|
|
|
|
if (!this.settings.vaultId) {
|
|
|
|
|
this.settings.vaultId = generateUuid();
|
|
|
|
|
}
|
2026-04-20 18:19:30 +05:30
|
|
|
|
|
|
|
|
// loadLocalStorage / saveLocalStorage aren't in the d.ts; cast at the boundary.
|
|
|
|
|
const localStore = this.app as unknown as {
|
|
|
|
|
loadLocalStorage: (key: string) => string | null;
|
|
|
|
|
saveLocalStorage: (key: string, value: string | null) => void;
|
|
|
|
|
};
|
|
|
|
|
const storageKey = "surfsense:deviceId";
|
|
|
|
|
let deviceId = localStore.loadLocalStorage(storageKey);
|
|
|
|
|
if (!deviceId) {
|
|
|
|
|
deviceId = generateUuid();
|
|
|
|
|
localStore.saveLocalStorage(storageKey, deviceId);
|
2026-04-20 04:04:19 +05:30
|
|
|
}
|
2026-04-20 18:19:30 +05:30
|
|
|
this.deviceId = deviceId;
|
|
|
|
|
|
2026-04-20 04:04:19 +05:30
|
|
|
if (!this.settings.vaultName) {
|
|
|
|
|
this.settings.vaultName = this.app.vault.getName();
|
|
|
|
|
}
|
2026-04-19 23:48:18 +05:30
|
|
|
}
|
2026-04-20 04:04:19 +05:30
|
|
|
}
|
2026-04-19 23:48:18 +05:30
|
|
|
|
2026-04-20 04:04:19 +05:30
|
|
|
function generateUuid(): string {
|
|
|
|
|
const c = globalThis.crypto;
|
|
|
|
|
if (c?.randomUUID) return c.randomUUID();
|
|
|
|
|
const buf = new Uint8Array(16);
|
|
|
|
|
c.getRandomValues(buf);
|
|
|
|
|
buf[6] = ((buf[6] ?? 0) & 0x0f) | 0x40;
|
|
|
|
|
buf[8] = ((buf[8] ?? 0) & 0x3f) | 0x80;
|
|
|
|
|
const hex = Array.from(buf, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
|
|
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(
|
|
|
|
|
16,
|
|
|
|
|
20,
|
|
|
|
|
)}-${hex.slice(20)}`;
|
2026-04-19 23:48:18 +05:30
|
|
|
}
|