Add status bar
This commit is contained in:
parent
de8a7b607d
commit
ec87a65e82
12 changed files with 80 additions and 12 deletions
2
.github/workflows/docker-publish.yml
vendored
2
.github/workflows/docker-publish.yml
vendored
|
|
@ -77,7 +77,7 @@ jobs:
|
||||||
|
|
||||||
# Sign the resulting Docker image digest except on PRs.
|
# Sign the resulting Docker image digest except on PRs.
|
||||||
# This will only write to the public Rekor transparency log when the Docker
|
# This will only write to the public Rekor transparency log when the Docker
|
||||||
# repository is public to avoid leaking data. If you would like to publish
|
# repository is public to avoid leaking data. If you would like to publish
|
||||||
# transparency data even for private images, pass --force to cosign below.
|
# transparency data even for private images, pass --force to cosign below.
|
||||||
# https://github.com/sigstore/cosign
|
# https://github.com/sigstore/cosign
|
||||||
- name: Sign the published Docker image
|
- name: Sign the published Docker image
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { TAbstractFile, TFile } from "obsidian";
|
import { TAbstractFile, TFile } from "obsidian";
|
||||||
import { FileEventHandler } from "./file-event-handler";
|
import { FileEventHandler } from "./file-event-handler";
|
||||||
import { Logger } from "src/logger";
|
import { Logger } from "src/logger";
|
||||||
import { SyncService } from "src/services/sync_service";
|
import { SyncService } from "src/services/sync-service";
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { syncLocallyDeletedFile } from "src/sync-operations/sync-locally-deleted-file";
|
import { syncLocallyDeletedFile } from "src/sync-operations/sync-locally-deleted-file";
|
||||||
import { syncLocallyUpdatedFile } from "src/sync-operations/sync-locally-updated-file";
|
import { syncLocallyUpdatedFile } from "src/sync-operations/sync-locally-updated-file";
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,12 @@ import { SyncView } from "./views/sync-view";
|
||||||
|
|
||||||
import { Logger } from "./logger";
|
import { Logger } from "./logger";
|
||||||
import { SyncEventHandler } from "./events/sync-event-handler";
|
import { SyncEventHandler } from "./events/sync-event-handler";
|
||||||
import { SyncService } from "./services/sync_service";
|
import { SyncService } from "./services/sync-service";
|
||||||
import { Database } from "./database/database";
|
import { Database } from "./database/database";
|
||||||
import { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
|
import { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
|
||||||
import { ObsidianFileOperations } from "./file-operations/obsidian-file-operations";
|
import { ObsidianFileOperations } from "./file-operations/obsidian-file-operations";
|
||||||
import { applyLocalChangesRemotely } from "./sync-operations/apply-local-changes-remotely";
|
import { applyLocalChangesRemotely } from "./sync-operations/apply-local-changes-remotely";
|
||||||
|
import { StatusBar } from "./views/status-bar";
|
||||||
|
|
||||||
export default class SyncPlugin extends Plugin {
|
export default class SyncPlugin extends Plugin {
|
||||||
private remoteListenerIntervalId: number | null = null;
|
private remoteListenerIntervalId: number | null = null;
|
||||||
|
|
@ -44,6 +45,8 @@ export default class SyncPlugin extends Plugin {
|
||||||
|
|
||||||
const syncServer = new SyncService(database);
|
const syncServer = new SyncService(database);
|
||||||
|
|
||||||
|
new StatusBar(this, syncServer);
|
||||||
|
|
||||||
this.addSettingTab(
|
this.addSettingTab(
|
||||||
new SyncSettingsTab(this.app, this, database, syncServer)
|
new SyncSettingsTab(this.app, this, database, syncServer)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
290
plugin/src/services/sync-service.ts
Normal file
290
plugin/src/services/sync-service.ts
Normal file
|
|
@ -0,0 +1,290 @@
|
||||||
|
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
||||||
|
|
||||||
|
import createClient, { Client } from "openapi-fetch";
|
||||||
|
import type { components, paths } from "./types.js"; // generated by openapi-typescript
|
||||||
|
import { Logger } from "src/logger";
|
||||||
|
import { Database } from "src/database/database";
|
||||||
|
import { SyncSettings } from "src/database/sync-settings";
|
||||||
|
import {
|
||||||
|
VaultUpdateId,
|
||||||
|
RelativePath,
|
||||||
|
DocumentId,
|
||||||
|
} from "src/database/document-metadata";
|
||||||
|
import PQueue from "p-queue";
|
||||||
|
|
||||||
|
export interface RequestCountStatus {
|
||||||
|
waiting: number;
|
||||||
|
success: number;
|
||||||
|
failure: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SyncService {
|
||||||
|
private client: Client<paths>;
|
||||||
|
|
||||||
|
private promiseQueue: PQueue;
|
||||||
|
private requestCountListeners: Array<(status: RequestCountStatus) => void> =
|
||||||
|
[];
|
||||||
|
private status: RequestCountStatus = {
|
||||||
|
waiting: 0,
|
||||||
|
success: 0,
|
||||||
|
failure: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
public constructor(private database: Database) {
|
||||||
|
this.createClient(database.getSettings());
|
||||||
|
this.promiseQueue = new PQueue({
|
||||||
|
concurrency: database.getSettings().uploadConcurrency,
|
||||||
|
});
|
||||||
|
|
||||||
|
database.addOnSettingsChangeHandlers((s) => {
|
||||||
|
this.createClient(s);
|
||||||
|
this.promiseQueue.concurrency = s.uploadConcurrency;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.promiseQueue.on("active", () => {
|
||||||
|
this.status.waiting = this.promiseQueue.pending;
|
||||||
|
this.emitRequestCountChange();
|
||||||
|
});
|
||||||
|
|
||||||
|
this.promiseQueue.on("completed", () => {
|
||||||
|
this.status.success++;
|
||||||
|
this.emitRequestCountChange();
|
||||||
|
});
|
||||||
|
|
||||||
|
this.promiseQueue.on("error", () => {
|
||||||
|
this.status.failure++;
|
||||||
|
this.emitRequestCountChange();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public addRequestCountChangeListener(
|
||||||
|
listener: (status: RequestCountStatus) => void
|
||||||
|
): void {
|
||||||
|
this.requestCountListeners.push(listener);
|
||||||
|
listener({ ...this.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
private emitRequestCountChange(): void {
|
||||||
|
this.requestCountListeners.forEach((listener) =>
|
||||||
|
listener({ ...this.status })
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private createClient(settings: SyncSettings) {
|
||||||
|
this.client = createClient<paths>({
|
||||||
|
baseUrl: settings.remoteUri,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private enqueue<T>(fn: () => Promise<T>): Promise<T> {
|
||||||
|
return this.promiseQueue.add(fn) as Promise<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ping(): Promise<components["schemas"]["PingResponse"]> {
|
||||||
|
const response = await this.enqueue(() =>
|
||||||
|
this.client.GET("/ping", {
|
||||||
|
params: {
|
||||||
|
header: {
|
||||||
|
authorization:
|
||||||
|
"Bearer " + this.database.getSettings().token,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
"Ping response: " + JSON.stringify(response.data)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.data) {
|
||||||
|
throw new Error(`Failed to ping server: ${response.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async create({
|
||||||
|
relativePath,
|
||||||
|
contentBytes,
|
||||||
|
createdDate,
|
||||||
|
}: {
|
||||||
|
relativePath: RelativePath;
|
||||||
|
contentBytes: Uint8Array;
|
||||||
|
createdDate: Date;
|
||||||
|
}): Promise<components["schemas"]["DocumentVersion"]> {
|
||||||
|
const response = await this.enqueue(() =>
|
||||||
|
this.client.POST("/vaults/{vault_id}/documents", {
|
||||||
|
params: {
|
||||||
|
path: {
|
||||||
|
vault_id: this.database.getSettings().vaultName,
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
authorization:
|
||||||
|
"Bearer " + this.database.getSettings().token,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
contentBase64: lib.bytes_to_base64(contentBytes),
|
||||||
|
createdDate: createdDate.toISOString(),
|
||||||
|
relativePath,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.data) {
|
||||||
|
throw new Error(`Failed to create document: ${response.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
"Created document " + JSON.stringify(response.data)
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async put({
|
||||||
|
parentVersionId,
|
||||||
|
documentId,
|
||||||
|
relativePath,
|
||||||
|
contentBytes,
|
||||||
|
createdDate,
|
||||||
|
}: {
|
||||||
|
parentVersionId: VaultUpdateId;
|
||||||
|
documentId: DocumentId;
|
||||||
|
relativePath: RelativePath;
|
||||||
|
contentBytes: Uint8Array;
|
||||||
|
createdDate: Date;
|
||||||
|
}): Promise<components["schemas"]["DocumentVersion"]> {
|
||||||
|
const response = await this.enqueue(() =>
|
||||||
|
this.client.PUT("/vaults/{vault_id}/documents/{document_id}", {
|
||||||
|
params: {
|
||||||
|
path: {
|
||||||
|
vault_id: this.database.getSettings().vaultName,
|
||||||
|
document_id: documentId,
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
authorization:
|
||||||
|
"Bearer " + this.database.getSettings().token,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
parentVersionId,
|
||||||
|
contentBase64: lib.bytes_to_base64(contentBytes),
|
||||||
|
createdDate: createdDate.toISOString(),
|
||||||
|
relativePath,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.data) {
|
||||||
|
throw new Error(`Failed to update document: ${response.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
"Updated document " + JSON.stringify(response.data)
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async delete({
|
||||||
|
documentId,
|
||||||
|
relativePath,
|
||||||
|
createdDate,
|
||||||
|
}: {
|
||||||
|
documentId: DocumentId;
|
||||||
|
relativePath: RelativePath;
|
||||||
|
createdDate: Date;
|
||||||
|
}): Promise<void> {
|
||||||
|
const response = await this.enqueue(() =>
|
||||||
|
this.client.DELETE("/vaults/{vault_id}/documents/{document_id}", {
|
||||||
|
params: {
|
||||||
|
path: {
|
||||||
|
vault_id: this.database.getSettings().vaultName,
|
||||||
|
document_id: documentId,
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
authorization:
|
||||||
|
"Bearer " + this.database.getSettings().token,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
createdDate: createdDate.toISOString(),
|
||||||
|
relativePath,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.error) {
|
||||||
|
throw new Error(`Failed to delete document`);
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
"Updated document " + JSON.stringify(response.data)
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async get({
|
||||||
|
documentId,
|
||||||
|
}: {
|
||||||
|
documentId: DocumentId;
|
||||||
|
}): Promise<components["schemas"]["DocumentVersion"]> {
|
||||||
|
const response = await this.enqueue(() =>
|
||||||
|
this.client.GET("/vaults/{vault_id}/documents/{document_id}", {
|
||||||
|
params: {
|
||||||
|
path: {
|
||||||
|
vault_id: this.database.getSettings().vaultName,
|
||||||
|
document_id: documentId,
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
authorization:
|
||||||
|
"Bearer " + this.database.getSettings().token,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.data) {
|
||||||
|
throw new Error(`Failed to get document: ${response.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
"Get document " + JSON.stringify(response.data)
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getAll(
|
||||||
|
since?: VaultUpdateId
|
||||||
|
): Promise<components["schemas"]["FetchLatestDocumentsResponse"]> {
|
||||||
|
const response = await this.enqueue(() =>
|
||||||
|
this.client.GET("/vaults/{vault_id}/documents", {
|
||||||
|
params: {
|
||||||
|
path: {
|
||||||
|
vault_id: this.database.getSettings().vaultName,
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
authorization:
|
||||||
|
"Bearer " + this.database.getSettings().token,
|
||||||
|
},
|
||||||
|
query: {
|
||||||
|
since_update_id: since,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.data) {
|
||||||
|
throw new Error(`Failed to get documents: ${response.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
"Get document " + JSON.stringify(response.data)
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { Database } from "../database/database";
|
import { Database } from "../database/database";
|
||||||
import { SyncService } from "../services/sync_service";
|
import { SyncService } from "../services/sync-service";
|
||||||
import { Logger } from "../logger";
|
import { Logger } from "../logger";
|
||||||
import { FileOperations } from "../file-operations/file-operations";
|
import { FileOperations } from "../file-operations/file-operations";
|
||||||
import { syncLocallyCreatedFile } from "./sync-locally-created-file";
|
import { syncLocallyCreatedFile } from "./sync-locally-created-file";
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { FileOperations } from "src/file-operations/file-operations";
|
import { FileOperations } from "src/file-operations/file-operations";
|
||||||
import { Logger } from "src/logger";
|
import { Logger } from "src/logger";
|
||||||
import { SyncService } from "src/services/sync_service";
|
import { SyncService } from "src/services/sync-service";
|
||||||
import { syncRemotelyUpdatedFile } from "./sync-remotely-updated-file";
|
import { syncRemotelyUpdatedFile } from "./sync-remotely-updated-file";
|
||||||
|
|
||||||
let isRunning = false;
|
let isRunning = false;
|
||||||
|
|
@ -14,6 +14,8 @@ export async function applyRemoteChangesLocally(
|
||||||
if (isRunning) {
|
if (isRunning) {
|
||||||
Logger.getInstance().info("Pull sync already in progress, skipping");
|
Logger.getInstance().info("Pull sync already in progress, skipping");
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
Logger.getInstance().info("Starting pull sync");
|
||||||
}
|
}
|
||||||
|
|
||||||
isRunning = true;
|
isRunning = true;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { Logger } from "src/logger";
|
import { Logger } from "src/logger";
|
||||||
import { SyncService } from "src/services/sync_service";
|
import { SyncService } from "src/services/sync-service";
|
||||||
import { hash } from "src/utils/hash";
|
import { hash } from "src/utils/hash";
|
||||||
import { unlockDocument, waitForDocumentLock } from "./locks";
|
import { unlockDocument, waitForDocumentLock } from "./locks";
|
||||||
import { FileOperations } from "src/file-operations/file-operations";
|
import { FileOperations } from "src/file-operations/file-operations";
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { RelativePath } from "src/database/document-metadata";
|
import { RelativePath } from "src/database/document-metadata";
|
||||||
import { Logger } from "src/logger";
|
import { Logger } from "src/logger";
|
||||||
import { SyncService } from "src/services/sync_service";
|
import { SyncService } from "src/services/sync-service";
|
||||||
import { unlockDocument, waitForDocumentLock } from "./locks";
|
import { unlockDocument, waitForDocumentLock } from "./locks";
|
||||||
|
|
||||||
export async function syncLocallyDeletedFile({
|
export async function syncLocallyDeletedFile({
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { Logger } from "src/logger";
|
import { Logger } from "src/logger";
|
||||||
import { SyncService } from "src/services/sync_service";
|
import { SyncService } from "src/services/sync-service";
|
||||||
import { hash } from "src/utils/hash";
|
import { hash } from "src/utils/hash";
|
||||||
import { unlockDocument, waitForDocumentLock } from "./locks";
|
import { unlockDocument, waitForDocumentLock } from "./locks";
|
||||||
import { FileOperations } from "src/file-operations/file-operations";
|
import { FileOperations } from "src/file-operations/file-operations";
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { unlockDocument, waitForDocumentLock } from "./locks";
|
import { unlockDocument, waitForDocumentLock } from "./locks";
|
||||||
import { SyncService } from "src/services/sync_service";
|
import { SyncService } from "src/services/sync-service";
|
||||||
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
||||||
import { hash } from "src/utils/hash";
|
import { hash } from "src/utils/hash";
|
||||||
import { Logger } from "src/logger";
|
import { Logger } from "src/logger";
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { App, Notice, PluginSettingTab, Setting } from "obsidian";
|
||||||
|
|
||||||
import SyncPlugin from "src/plugin";
|
import SyncPlugin from "src/plugin";
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { SyncService } from "src/services/sync_service";
|
import { SyncService } from "src/services/sync-service";
|
||||||
|
|
||||||
export class SyncSettingsTab extends PluginSettingTab {
|
export class SyncSettingsTab extends PluginSettingTab {
|
||||||
private editedVaultName: string;
|
private editedVaultName: string;
|
||||||
|
|
|
||||||
21
plugin/src/views/status-bar.ts
Normal file
21
plugin/src/views/status-bar.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { Plugin } from "obsidian";
|
||||||
|
import { RequestCountStatus, SyncService } from "src/services/sync-service";
|
||||||
|
|
||||||
|
export class StatusBar {
|
||||||
|
private statusBarItem: HTMLElement;
|
||||||
|
|
||||||
|
public constructor(plugin: Plugin, service: SyncService) {
|
||||||
|
this.statusBarItem = plugin.addStatusBarItem();
|
||||||
|
service.addRequestCountChangeListener((status) =>
|
||||||
|
this.updateStatus(status)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateStatus({
|
||||||
|
waiting,
|
||||||
|
success,
|
||||||
|
failure,
|
||||||
|
}: RequestCountStatus): void {
|
||||||
|
this.statusBarItem.setText(`${waiting} 🔄 ${success} ✅ ${failure} ❌`);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue