core: download all attachments when full offline mode is on

This commit is contained in:
Abdullah Atta
2024-07-24 12:39:20 +05:00
committed by Abdullah Atta
parent e4a755c69f
commit 57ddaafc16
3 changed files with 34 additions and 1 deletions

View File

@@ -245,6 +245,21 @@ class Sync {
this.logger.info("Stopping sync");
await this.db.setLastSynced(Date.now());
this.db.eventManager.publish(EVENTS.syncCompleted);
if (await this.db.kv().read("fullOfflineMode")) {
const attachments = await this.db.attachments.linked
.fields(["attachments.id", "attachments.hash", "attachments.chunkSize"])
.items();
await this.db.fs().queueDownloads(
attachments.map((a) => ({
filename: a.hash,
chunkSize: a.chunkSize
})),
"download-all-attachments",
{ readOnDownload: false }
);
}
}
async cancel() {

View File

@@ -497,6 +497,22 @@ export class Attachments implements ICollection {
this.db.options?.batchSize
);
}
get linked() {
return this.collection.createFilter<Attachment>(
(qb) =>
qb
.where(isFalse("deleted"))
.where("id", "in", (eb) =>
eb
.selectFrom("relations")
.where("toType", "==", "attachment")
.select("toId as id")
.$narrowType<{ id: string }>()
),
this.db.options?.batchSize
);
}
// get media() {
// return this.all.filter(
// (attachment) =>

View File

@@ -29,6 +29,7 @@ interface KV {
monographs: string[];
deviceId: string;
lastBackupTime: number;
fullOfflineMode: boolean;
}
export const KEYS: (keyof KV)[] = [
@@ -38,7 +39,8 @@ export const KEYS: (keyof KV)[] = [
"token",
"monographs",
"deviceId",
"lastBackupTime"
"lastBackupTime",
"fullOfflineMode"
];
export class KVStorage {