aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/static/drive-sync.js
blob: 3887e4ffe47fb5625152f58332bc1c9314f7a8ce (plain) (blame)
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
// Files and WhatsApp backups: folders chosen through the system's picker,
// sent into `<folder>/<account>-drive/<chosen folder>/…` of the destination
// every backup shares (sync-destination.js), once a day, on an unmetered
// network, like photos (docs/MESHBAY_DESIGN.md §9.13).
//
// The phone (platform.driveSync) lists the files, leaves out what the photo
// backup sends, keeps the ledger and hands each file over a range at a time;
// the running is the photo backup's own (createBackup). Additive by
// construction: a new version of a file goes beside the one sent, and nothing
// here deletes, renames or replaces anything on the node.

import * as platform from './platform.js';
import { t } from './i18n.js';
import { createBackup } from './photo-sync.js';
import { LASTING, failureText } from './phone-sync.js';

const drive = createBackup({
  name: 'files',
  sync: platform.driveSync,
  configured: (status) => Boolean(status.on) && (status.folders || []).length > 0,
  progress: 'drivesync.notif_progress',
  failures: LASTING,
  say: (code) => t('phonesync.notice', { what: t('drivesync.heading'), reason: failureText(code) }),
});

// WhatsApp's own folder: the same runner over its bridge, into
// `<folder>/<account>-whatsapp/` (DriveKind.kt says what is sent).
const whatsapp = createBackup({
  name: 'WhatsApp',
  sync: platform.whatsappSync,
  configured: (status) => Boolean(status.on) && (status.folders || []).length > 0,
  progress: 'whatsappsync.notif_progress',
  failures: LASTING,
  say: (code) => t('phonesync.notice', { what: t('whatsappsync.heading'), reason: failureText(code) }),
});

export function startDriveSync(context) {
  drive.start(context);
  whatsapp.start(context);
}
export function stopDriveSync() {
  drive.stop();
  whatsapp.stop();
}
export const runDriveSync = (opts) => drive.run(opts);
export const subscribe = (fn) => drive.subscribe(fn);
export const runWhatsAppSync = (opts) => whatsapp.run(opts);
export const subscribeWhatsApp = (fn) => whatsapp.subscribe(fn);