Working for non-deletes

This commit is contained in:
Andras Schmelczer 2025-03-09 09:07:18 +00:00
commit 054d109ef8
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 546 additions and 575 deletions

View file

@ -12,9 +12,10 @@ import { hash } from "src/utils/hash";
import type { components } from "src/services/types";
import type { Settings } from "src/persistence/settings";
import type { FileOperations } from "src/file-operations/file-operations";
import { findMatchingFileBasedOnHash } from "src/utils/find-matching-file-based-on-hash";
import { findMatchingFile } from "src/utils/find-matching-file";
import { UnrestrictedSyncer } from "./unrestricted-syncer";
import { FileNotFoundError } from "src/file-operations/safe-filesystem-operations";
import { createPromise } from "src/utils/create-promise";
export class Syncer {
private readonly remainingOperationsListeners: ((
@ -74,9 +75,10 @@ export class Syncer {
logger.debug(
`File has been deleted or moved before we had a chance to inspect it, skipping`
);
} else {
throw e;
return undefined;
}
throw e;
}
}
@ -88,77 +90,95 @@ export class Syncer {
public async syncLocallyCreatedFile(
relativePath: RelativePath,
updateTime: Date
updateTime?: Date
): Promise<void> {
let resolve:
| undefined
| ((metadata: DocumentMetadata | undefined) => void) = undefined;
const [promise, resolve, reject] = createPromise();
const creationPromise = new Promise<DocumentMetadata | undefined>(
(r) => (resolve = r)
// Most likely, we're waiting for the previous delete to finish on the file at this path
const document = await this.database.getResolvedDocumentByRelativePath(
relativePath,
promise
);
await this.database.setDocumentPromise({
relativePath,
promise: creationPromise
});
await this.syncQueue.add(async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
resolve!(
await this.internalSyncer.unrestrictedSyncLocallyCreatedFile(
relativePath,
try {
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncLocallyCreatedFile(
() =>
this.database.getDocumentByIdentity(document.identity),
updateTime
)
);
});
resolve();
} catch (e) {
reject(e);
} finally {
this.database.removeDocumentPromise(promise);
}
}
public async syncLocallyDeletedFile(
relativePath: RelativePath
): Promise<void> {
let metadata = this.database.getDocument(relativePath);
if (metadata !== undefined && !(metadata instanceof Promise)) {
metadata = Promise.resolve(metadata);
}
const [promise, resolve, reject] = createPromise();
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncLocallyDeletedFile(
relativePath,
metadata
)
const document = await this.database.getResolvedDocumentByRelativePath(
relativePath,
promise
);
try {
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncLocallyDeletedFile(() =>
this.database.getDocumentByIdentity(document.identity)
)
);
resolve();
} catch (e) {
reject(e);
} finally {
this.database.removeDocumentPromise(promise);
}
}
public async syncLocallyUpdatedFile(args: {
oldPath?: RelativePath;
relativePath: RelativePath;
updateTime: Date;
updateTime?: Date;
}): Promise<void> {
if (args.oldPath === args.relativePath) {
throw new Error(
`Old path and new path are the same: ${args.oldPath}`
);
}
if (args.oldPath !== undefined) {
await this.database.move(args.oldPath, args.relativePath);
if (args.oldPath === args.relativePath) {
throw new Error(
`Old path and new path are the same: ${args.oldPath}`
);
}
this.database.move(args.oldPath, args.relativePath);
}
let metadata = this.database.getDocument(args.relativePath);
if (metadata !== undefined && !(metadata instanceof Promise)) {
metadata = Promise.resolve(metadata);
}
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncLocallyUpdatedFile({
...args,
metadata
})
const [promise, resolve, reject] = createPromise();
const metadata = await this.database.getResolvedDocumentByRelativePath(
args.relativePath,
promise
);
}
public async waitForSyncQueue(): Promise<void> {
return this.syncQueue.onEmpty();
try {
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncLocallyUpdatedFile({
...args,
getLatestDocument: () =>
this.database.getDocumentByIdentity(metadata.identity)
})
);
resolve();
} catch (e) {
reject(e);
} finally {
this.database.removeDocumentPromise(promise);
}
}
public async scheduleSyncForOfflineChanges(): Promise<void> {
@ -217,6 +237,10 @@ export class Syncer {
}
}
public async waitForSyncQueue(): Promise<void> {
return this.syncQueue.onEmpty();
}
public async reset(): Promise<void> {
this.syncQueue.clear();
await this.syncQueue.onEmpty();
@ -229,53 +253,67 @@ export class Syncer {
private async syncRemotelyUpdatedFile(
remoteVersion: components["schemas"]["DocumentVersionWithoutContent"]
): Promise<void> {
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncRemotelyUpdatedFile(
remoteVersion
)
let document = this.database.getDocumentByDocumentId(
remoteVersion.documentId
);
if (document === undefined) {
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncRemotelyUpdatedFile(
remoteVersion
)
);
return;
}
const [promise, resolve, reject] = createPromise();
document = await this.database.getResolvedDocumentByRelativePath(
document.relativePath,
promise
);
try {
await this.syncQueue.add(async () =>
this.internalSyncer.unrestrictedSyncRemotelyUpdatedFile(
remoteVersion,
() => this.database.getDocumentByIdentity(document.identity)
)
);
resolve();
} catch (e) {
reject(e);
} finally {
this.database.removeDocumentPromise(promise);
}
}
private async internalScheduleSyncForOfflineChanges(): Promise<void> {
const allLocalFiles = await this.operations.listAllFiles();
// This includes renamed files for now
let locallyPossiblyDeletedFiles = [
...this.database.resolvedDocuments
].filter(([path, _]) => !allLocalFiles.includes(path));
].filter(({ relativePath }) => !allLocalFiles.includes(relativePath));
const updates = Promise.all(
allLocalFiles.map(async (relativePath) =>
this.syncQueue.add(async () => {
const metadata =
this.database.getResolvedDocument(relativePath);
allLocalFiles.map(async (relativePath) => {
if (
this.database.getDocumentByRelativePath(relativePath)
?.metadata !== undefined
) {
this.logger.debug(
`Document ${relativePath} might have been updated locally, scheduling sync to validate and update it`
);
if (metadata) {
this.logger.debug(
`Document ${relativePath} might have been updated locally, scheduling sync to validate and update it`
);
const updateTime =
await Syncer.forgivingFileNotFoundWrapper(
async () =>
this.operations.getModificationTime(
relativePath
),
this.logger
);
if (updateTime === undefined) {
return;
}
return this.syncLocallyUpdatedFile({
relativePath
});
}
return this.internalSyncer.unrestrictedSyncLocallyUpdatedFile(
{
relativePath,
updateTime,
metadata: Promise.resolve(metadata)
}
);
}
// Perhaps the file has been moved. Let's check by looking at the deleted files
// Perhaps the file has been moved; let's check by looking at the deleted files
const contentHash = await this.syncQueue.add(async () => {
const contentBytes =
await Syncer.forgivingFileNotFoundWrapper(
async () => this.operations.read(relativePath),
@ -284,90 +322,51 @@ export class Syncer {
if (contentBytes === undefined) {
return;
}
return hash(contentBytes);
});
const contentHash = hash(contentBytes);
if (contentHash == undefined) {
// The file was deleted before we had a chance to read it, no need to sync it here
return;
}
// todo: make this smarter so that offline files can be renamed & edited at the same time
const originalFile = findMatchingFileBasedOnHash(
contentHash,
locallyPossiblyDeletedFiles
);
if (originalFile !== undefined) {
// `originalFile` hasn't been deleted but it got moved instead
locallyPossiblyDeletedFiles =
locallyPossiblyDeletedFiles.filter(
(item) => item[0] !== originalFile[0]
);
this.logger.debug(
`Document '${originalFile[0]}' was not found under its current path in the database but was found under a different path (${relativePath}), scheduling sync to move it`
const originalFile = findMatchingFile(
contentHash,
locallyPossiblyDeletedFiles
);
if (originalFile !== undefined) {
// `originalFile` hasn't been deleted but it got moved instead
locallyPossiblyDeletedFiles =
locallyPossiblyDeletedFiles.filter(
(item) =>
item.relativePath !== originalFile.relativePath
);
const updateTime =
await Syncer.forgivingFileNotFoundWrapper(
async () =>
this.operations.getModificationTime(
relativePath
),
this.logger
);
if (updateTime === undefined) {
return;
}
return this.internalSyncer.unrestrictedSyncLocallyUpdatedFile(
{
oldPath: originalFile[0],
relativePath,
updateTime,
metadata: Promise.resolve(
this.database.getResolvedDocument(
relativePath
)
),
optimisations: {
contentBytes,
contentHash
}
}
);
}
this.logger.debug(
`Document ${relativePath} not found in database, scheduling sync to create it`
`Document '${originalFile.relativePath}' was not found under its current path in the database but was found under a different path (${relativePath}), scheduling sync to move it`
);
const updateTime =
await Syncer.forgivingFileNotFoundWrapper(
async () =>
this.operations.getModificationTime(
relativePath
),
this.logger
);
if (updateTime === undefined) {
return;
}
return this.internalSyncer.unrestrictedSyncLocallyCreatedFile(
relativePath,
updateTime
);
})
)
// We're outside of the pqueue, so we need to call the public wrapper
return this.syncLocallyUpdatedFile({
oldPath: originalFile.relativePath,
relativePath
});
}
this.logger.debug(
`Document ${relativePath} not found in database, scheduling sync to create it`
);
// We're outside of the pqueue, so we need to call the public wrapper
return this.syncLocallyCreatedFile(relativePath);
})
);
const deletes = Promise.all(
locallyPossiblyDeletedFiles.map(async ([relativePath, _]) => {
locallyPossiblyDeletedFiles.map(async ({ relativePath }) => {
this.logger.debug(
`Document ${relativePath} has been deleted locally, scheduling sync to delete it`
);
if (await this.operations.exists(relativePath)) {
this.logger.debug(
`Document ${relativePath} actually exists locally, skipping`
);
return Promise.resolve();
}
// We're outside of the pqueue, so we need to call the public wrapper
return this.syncLocallyDeletedFile(relativePath);
})
@ -389,15 +388,7 @@ export class Syncer {
this.logger.info("Applying remote changes locally");
await Promise.all(
remote.latestDocuments
.filter(
(remoteDocument) =>
remoteDocument.vaultUpdateId >
(this.database.getDocumentByDocumentId(
remoteDocument.documentId
)?.[1].parentVersionId ?? -1)
)
.map(this.syncRemotelyUpdatedFile.bind(this))
remote.latestDocuments.map(this.syncRemotelyUpdatedFile.bind(this))
);
const lastSeenUpdateId = this.database.getLastSeenUpdateId();
@ -405,7 +396,7 @@ export class Syncer {
lastSeenUpdateId === undefined ||
remote.lastUpdateId > lastSeenUpdateId
) {
await this.database.setLastSeenUpdateId(remote.lastUpdateId);
this.database.setLastSeenUpdateId(remote.lastUpdateId);
}
}

View file

@ -1,11 +1,12 @@
import type {
Database,
DocumentMetadata,
DocumentRecord,
RelativePath
} from "../persistence/database";
import type { SyncService } from "src/services/sync-service";
import type { Logger } from "src/tracing/logger";
import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history";
import { SyncSource, SyncStatus, SyncType } from "src/tracing/sync-history";
import { EMPTY_HASH, hash } from "src/utils/hash";
@ -31,37 +32,28 @@ export class UnrestrictedSyncer {
}
public async unrestrictedSyncLocallyCreatedFile(
relativePath: RelativePath,
updateTime: Date,
optimisations?: {
contentBytes?: Uint8Array;
contentHash?: string;
}
): Promise<DocumentMetadata | undefined> {
getLatestDocument: () => DocumentRecord,
updateTime?: Date
): Promise<void> {
const { relativePath, metadata } = getLatestDocument();
return this.executeSync(
[relativePath],
SyncType.CREATE,
SyncSource.PUSH,
async () => {
const localMetadata = this.database.getDocument(relativePath);
if (
!(localMetadata instanceof Promise) &&
localMetadata &&
!localMetadata.isDeleted
) {
if (metadata !== undefined && !metadata.isDeleted) {
this.logger.debug(
`Document metadata already exists for ${relativePath}, it must have been downloaded from the server`
`Document ${relativePath} already exists in the database, no need to create it again`
);
return;
}
const contentBytes =
optimisations?.contentBytes ??
(await this.operations.read(relativePath)); // this can throw FileNotFoundError
const contentHash =
optimisations?.contentHash ?? hash(contentBytes);
const contentBytes = await this.operations.read(relativePath); // this can throw FileNotFoundError
const contentHash = hash(contentBytes);
updateTime ??=
await this.operations.getModificationTime(relativePath); // this can throw FileNotFoundError
const response = await this.syncService.create({
relativePath,
@ -69,95 +61,71 @@ export class UnrestrictedSyncer {
createdDate: updateTime
});
const currentMetadata =
this.database.getDocumentByIdentity(localMetadata);
if (!currentMetadata) {
throw new Error(
`Document metadata for ${relativePath} not found after creation`
);
}
const { relativePath: currentRelativePath } =
getLatestDocument();
this.history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
relativePath: currentMetadata[0],
relativePath,
message: `Successfully uploaded locally created file`,
type: SyncType.CREATE
});
const newMetadata = {
relativePath: currentRelativePath,
documentId: response.documentId,
parentVersionId: response.vaultUpdateId,
hash: contentHash,
isDeleted: false
};
await this.database.setDocument({
relativePath: currentMetadata[0],
...newMetadata
});
this.database.setDocument(newMetadata);
await this.tryIncrementVaultUpdateId(response.vaultUpdateId);
return newMetadata;
this.tryIncrementVaultUpdateId(response.vaultUpdateId);
}
);
}
public async unrestrictedSyncLocallyDeletedFile(
relativePath: RelativePath,
metadata: Promise<DocumentMetadata | undefined> | undefined
getLatestDocument: () => DocumentRecord
): Promise<void> {
let document = getLatestDocument();
await this.executeSync(
[relativePath],
[document.relativePath],
SyncType.DELETE,
SyncSource.PUSH,
async () => {
const localMetadata =
metadata !== undefined
? await metadata
: this.database.getResolvedDocument(relativePath);
if (!localMetadata || localMetadata.isDeleted) {
this.logger.info(
`Locally deleted file hasn't been uploaded yet, so there's no need to delete it on the remote server`
if (
document.metadata === undefined ||
document.metadata.isDeleted
) {
this.logger.debug(
`Document ${document.relativePath} has been already deleted, no need to delete it again`
);
return;
}
const response = await this.syncService.delete({
documentId: localMetadata.documentId,
relativePath,
createdDate: new Date() // We got the event now, so it must have been deleted just now
documentId: document.metadata.documentId,
relativePath: document.relativePath,
createdDate: new Date() // We've got the event now, so it must have been deleted just now
});
this.history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
relativePath,
relativePath: document.relativePath,
message: `Successfully deleted locally deleted file on the remote server`,
type: SyncType.DELETE
});
const currentMetadata = this.database.getDocumentByDocumentId(
localMetadata.documentId
);
if (!currentMetadata || currentMetadata[1].isDeleted) {
this.logger.info(
`No metadata found for deleted file, '${relativePath}' must have been deleted by another operation`
);
return;
}
await this.operations.delete(currentMetadata[0]);
document = getLatestDocument();
// We have to have a record of the delete in case there's an in-flight update for the same
// document which finishes after the delete has succeeded and would introduce a phantom metadata record.
await this.database.setDocument({
relativePath: currentMetadata[0],
this.database.setDocument({
relativePath: document.relativePath,
documentId: response.documentId,
parentVersionId: response.vaultUpdateId,
hash: EMPTY_HASH,
@ -169,84 +137,64 @@ export class UnrestrictedSyncer {
public async unrestrictedSyncLocallyUpdatedFile({
oldPath,
relativePath,
metadata,
updateTime,
optimisations
getLatestDocument,
updateTime
}: {
oldPath?: RelativePath;
relativePath: RelativePath;
metadata: Promise<DocumentMetadata | undefined> | undefined;
updateTime: Date;
optimisations?: {
contentBytes?: Uint8Array;
contentHash?: string;
};
getLatestDocument: () => DocumentRecord;
updateTime?: Date;
}): Promise<void> {
let document = getLatestDocument();
await this.executeSync(
[oldPath, relativePath].filter((path) => path !== undefined),
[oldPath, document.relativePath].filter(
(path) => path !== undefined
),
SyncType.UPDATE,
SyncSource.PUSH,
async () => {
const localMetadata =
metadata !== undefined
? await metadata
: this.database.getResolvedDocument(relativePath);
if (!localMetadata || localMetadata.isDeleted) {
// It's fine, a subsequent sync operation must have dealt with this
return;
}
const contentBytes =
optimisations?.contentBytes ??
(await this.operations.read(relativePath)); // this can throw FileNotFoundError
let contentHash =
optimisations?.contentHash ?? hash(contentBytes);
if (
localMetadata.hash === contentHash &&
oldPath === undefined
document.metadata === undefined ||
document.metadata.isDeleted
) {
this.logger.debug(
`File hash of ${relativePath} matches with last synced version and the path hasn't changed; no need to sync`
`Document ${document.relativePath} has been already deleted, no need to update it, ${JSON.stringify(document)}, ${document.metadata?.isDeleted}`
);
return;
}
// Re-fetch based on the documentId instead of the relativePath because
// the relativePath might have changed since this operation was scheduled
let latestMetadata = this.database.getDocumentByDocumentId(
localMetadata.documentId
);
if (!latestMetadata || latestMetadata[1].isDeleted) {
// It's fine, a subsequent sync operation must have dealt with this
const contentBytes = await this.operations.read(
document.relativePath
); // this can throw FileNotFoundError
let contentHash = hash(contentBytes);
if (
document.metadata.hash === contentHash &&
oldPath === undefined
) {
this.logger.debug(
`File hash of ${document.relativePath} matches with last synced version and the path hasn't changed; no need to sync`
);
return;
}
updateTime ??= await this.operations.getModificationTime(
document.relativePath
); // this can throw FileNotFoundError;
const response = await this.syncService.put({
documentId: latestMetadata[1].documentId,
parentVersionId: latestMetadata[1].parentVersionId,
relativePath: latestMetadata[0],
documentId: document.metadata.documentId,
parentVersionId: document.metadata.parentVersionId,
relativePath: document.relativePath,
contentBytes,
createdDate: updateTime
});
latestMetadata = this.database.getDocumentByDocumentId(
response.documentId
);
if (!latestMetadata || latestMetadata[1].isDeleted) {
// The document has been deleted since this operation was scheduled
return;
}
if (
latestMetadata[1].parentVersionId >= response.vaultUpdateId
document.metadata.parentVersionId >= response.vaultUpdateId
) {
this.logger.debug(
`Document ${relativePath} is already more up to date than the fetched version`
`Document ${document.relativePath} is already more up to date than the fetched version`
);
return;
}
@ -254,50 +202,42 @@ export class UnrestrictedSyncer {
this.history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
relativePath,
relativePath: document.relativePath,
message: `Successfully uploaded locally updated file to the remote server`,
type: SyncType.UPDATE
});
// Update relativePath which is the only property that can change while this is running (due to a move)
document = getLatestDocument();
if (response.isDeleted) {
await this.operations.delete(relativePath);
await this.operations.delete(document.relativePath);
this.history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath,
relativePath: document.relativePath,
message:
"The file we tried to update had been deleted remotely, therefore, we have deleted it locally",
type: SyncType.DELETE
});
await this.database.setDocument({
this.database.setDocument({
documentId: response.documentId,
relativePath: latestMetadata[0],
relativePath: document.relativePath,
parentVersionId: response.vaultUpdateId,
hash: EMPTY_HASH,
isDeleted: true
});
await this.tryIncrementVaultUpdateId(
response.vaultUpdateId
);
this.tryIncrementVaultUpdateId(response.vaultUpdateId);
return;
}
if (
latestMetadata[1].parentVersionId >= response.vaultUpdateId
) {
this.logger.debug(
`Document ${relativePath} is already more up to date than the fetched version`
);
return;
}
if (response.relativePath != relativePath) {
if (response.relativePath != document.relativePath) {
await this.operations.move(
latestMetadata[0],
document.relativePath,
response.relativePath,
response.documentId
); // this can throw FileNotFoundError
@ -316,155 +256,85 @@ export class UnrestrictedSyncer {
this.history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath,
relativePath: document.relativePath,
message: `The file we updated had been updated remotely, so we downloaded the merged version`,
type: SyncType.UPDATE
});
}
await this.database.setDocument({
this.database.setDocument({
documentId: response.documentId,
relativePath:
response.relativePath != relativePath
response.relativePath != document.relativePath
? response.relativePath
: latestMetadata[0],
: document.relativePath,
parentVersionId: response.vaultUpdateId,
hash: contentHash,
isDeleted: response.isDeleted
});
await this.tryIncrementVaultUpdateId(response.vaultUpdateId);
this.tryIncrementVaultUpdateId(response.vaultUpdateId);
}
);
}
public async unrestrictedSyncRemotelyUpdatedFile(
remoteVersion: components["schemas"]["DocumentVersionWithoutContent"]
remoteVersion: components["schemas"]["DocumentVersionWithoutContent"],
getLatestDocument?: () => DocumentRecord
): Promise<void> {
await this.executeSync(
[remoteVersion.relativePath],
SyncType.UPDATE,
SyncSource.PULL,
async () => {
const localMetadata =
getLatestDocument?.() ??
this.database.getDocumentByDocumentId(
remoteVersion.documentId
);
if (
localMetadata?.metadata !== undefined &&
!localMetadata.metadata.isDeleted
) {
// If the file exists locally, let's pretend the user has updated it
// and deal with remote update/deletion within `unrestrictedSyncLocallyUpdatedFile`
if (
localMetadata.metadata.parentVersionId >=
remoteVersion.vaultUpdateId
) {
this.logger.debug(
`Document ${remoteVersion.relativePath} is already more up to date than the fetched version`
);
return;
}
return this.unrestrictedSyncLocallyUpdatedFile({
getLatestDocument: () =>
this.database.getDocumentByIdentity(
localMetadata.identity
)
});
}
const content = (
await this.syncService.get({
documentId: remoteVersion.documentId
})
).contentBase64;
const contentBytes = deserialize(content);
const contentHash = hash(contentBytes);
const localMetadata = this.database.getDocumentByDocumentId(
remoteVersion.documentId
);
if (
localMetadata?.[1].documentId ===
remoteVersion.documentId &&
localMetadata[1].parentVersionId >
remoteVersion.vaultUpdateId
) {
this.logger.info(
`Document ${remoteVersion.relativePath} is already up to date`
);
return;
}
const localBytes = await this.operations.read(
remoteVersion.relativePath
); // this can throw FileNotFoundError
const localHash = hash(localBytes);
if (localHash !== localMetadata?.[1].hash) {
this.logger.info(
`Document ${remoteVersion.relativePath} has pending local changes, so we shouldn't update it here`
);
return;
}
if (!localMetadata || localMetadata[1].isDeleted) {
if (remoteVersion.isDeleted) {
this.logger.info(
`Remotely deleted file hasn't been synced yet, so there's no need to delete it locally`
);
return;
}
await this.operations.create(
remoteVersion.relativePath,
contentBytes,
remoteVersion.documentId
);
await this.database.setDocument({
documentId: remoteVersion.documentId,
relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: hash(contentBytes),
isDeleted: remoteVersion.isDeleted
});
this.history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath: remoteVersion.relativePath,
message: `Successfully downloaded remote file which hadn't existed locally`,
type: SyncType.CREATE
});
return;
}
const [relativePath, metadata] = localMetadata;
if (remoteVersion.vaultUpdateId <= metadata.parentVersionId) {
this.logger.debug(
`Document ${relativePath} is already up to date`
);
return;
}
if (remoteVersion.isDeleted) {
await this.operations.delete(relativePath);
this.history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath: remoteVersion.relativePath,
message: `Successfully deleted remotely deleted file locally`,
type: SyncType.DELETE
});
await this.database.setDocument({
documentId: remoteVersion.documentId,
relativePath: relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: EMPTY_HASH,
isDeleted: true
});
return;
}
if (relativePath !== remoteVersion.relativePath) {
// TODO: this can fail, that's bad
await this.operations.move(
// this can throw FileNotFoundError
relativePath,
remoteVersion.relativePath,
remoteVersion.documentId
);
}
// todo: why
await this.operations.create(
remoteVersion.relativePath,
contentBytes,
remoteVersion.documentId
);
await this.database.setDocument({
this.database.setDocument({
documentId: remoteVersion.documentId,
relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: contentHash,
hash: hash(contentBytes),
isDeleted: remoteVersion.isDeleted
});
@ -472,8 +342,8 @@ export class UnrestrictedSyncer {
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath: remoteVersion.relativePath,
message: `Successfully updated remotely updated file locally`,
type: SyncType.UPDATE
message: `Successfully downloaded remote file which hadn't existed locally`,
type: SyncType.CREATE
});
}
);
@ -551,11 +421,9 @@ export class UnrestrictedSyncer {
this.locks.reset();
}
private async tryIncrementVaultUpdateId(
responseVaultUpdateId: number
): Promise<void> {
private tryIncrementVaultUpdateId(responseVaultUpdateId: number): void {
if (this.database.getLastSeenUpdateId() === responseVaultUpdateId - 1) {
await this.database.setLastSeenUpdateId(responseVaultUpdateId);
this.database.setLastSeenUpdateId(responseVaultUpdateId);
}
}
}