1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-03 08:35:29 +02:00
joplin/ReactNativeClient/lib/registry.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-07-06 21:48:17 +02:00
import { Logger } from 'lib/logger.js';
import { Setting } from 'lib/models/setting.js';
import { OneDriveApi } from 'lib/onedrive-api.js';
2017-07-08 00:25:03 +02:00
import { parameters } from 'lib/parameters.js';
2017-07-06 21:48:17 +02:00
import { FileApi } from 'lib/file-api.js';
import { Database } from 'lib/database.js';
2017-07-06 21:48:17 +02:00
import { Synchronizer } from 'lib/synchronizer.js';
import { FileApiDriverOneDrive } from 'lib/file-api-driver-onedrive.js';
import { shim } from 'lib/shim.js';
import { FileApiDriverMemory } from 'lib/file-api-driver-memory.js';
2017-07-06 21:48:17 +02:00
const reg = {};
reg.logger = () => {
2017-07-15 01:12:32 +02:00
if (!reg.logger_) {
console.warn('Calling logger before it is initialized');
return new Logger();
}
2017-07-06 21:48:17 +02:00
return reg.logger_;
}
reg.setLogger = (l) => {
reg.logger_ = l;
}
2017-07-06 21:48:17 +02:00
reg.oneDriveApi = () => {
if (reg.oneDriveApi_) return reg.oneDriveApi_;
const isPublic = Setting.value('appType') != 'cli';
2017-07-06 21:48:17 +02:00
2017-07-08 00:25:03 +02:00
reg.oneDriveApi_ = new OneDriveApi(parameters().oneDrive.id, parameters().oneDrive.secret, isPublic);
reg.oneDriveApi_.setLogger(reg.logger());
2017-07-06 21:48:17 +02:00
reg.oneDriveApi_.on('authRefreshed', (a) => {
2017-07-06 23:30:45 +02:00
reg.logger().info('Saving updated OneDrive auth.');
Setting.setValue('sync.onedrive.auth', a ? JSON.stringify(a) : null);
2017-07-06 21:48:17 +02:00
});
let auth = Setting.value('sync.onedrive.auth');
if (auth) {
try {
auth = JSON.parse(auth);
} catch (error) {
reg.logger().warn('Could not parse OneDrive auth token');
reg.logger().warn(error);
auth = null;
}
reg.oneDriveApi_.setAuth(auth);
}
2017-07-06 21:48:17 +02:00
return reg.oneDriveApi_;
}
reg.synchronizer = async (syncTargetId) => {
if (!reg.synchronizers_) reg.synchronizers_ = [];
if (reg.synchronizers_[syncTargetId]) return reg.synchronizers_[syncTargetId];
2017-07-06 21:48:17 +02:00
if (!reg.db()) throw new Error('Cannot initialize synchronizer: db not initialized');
2017-07-06 21:48:17 +02:00
let fileApi = null;
2017-07-06 21:48:17 +02:00
if (syncTargetId == 'onedrive') {
2017-07-06 21:48:17 +02:00
if (!reg.oneDriveApi().auth()) throw new Error('User is not authentified');
let appDir = await reg.oneDriveApi().appDirectory();
fileApi = new FileApi(appDir, new FileApiDriverOneDrive(reg.oneDriveApi()));
2017-07-06 21:48:17 +02:00
} else if (syncTargetId == 'memory') {
fileApi = new FileApi('joplin', new FileApiDriverMemory());
} else if (syncTargetId == 'filesystem') {
let syncDir = Setting.value('sync.filesystem.path');
if (!syncDir) throw new Error(_('Please set the "sync.filesystem.path" config value to the desired synchronisation destination.'));
await shim.fs.mkdirp(syncDir, 0o755);
fileApi = new FileApi(syncDir, new shim.FileApiDriverLocal());
} else {
throw new Error('Unknown sync target: ' + syncTargetId);
}
fileApi.setLogger(reg.logger());
let sync = new Synchronizer(reg.db(), fileApi, Setting.value('appType'));
sync.setLogger(reg.logger());
sync.dispatch = reg.dispatch;
reg.synchronizers_[syncTargetId] = sync;
2017-07-06 21:48:17 +02:00
return sync;
2017-07-06 21:48:17 +02:00
}
2017-07-19 21:15:55 +02:00
reg.scheduleSync = async (delay = null) => {
if (delay === null) delay = 1000 * 10;
2017-07-17 22:22:05 +02:00
if (reg.scheduleSyncId_) {
clearTimeout(reg.scheduleSyncId_);
reg.scheduleSyncId_ = null;
}
2017-07-16 23:17:22 +02:00
reg.logger().info('Scheduling sync operation...');
reg.scheduleSyncId_ = setTimeout(async () => {
reg.scheduleSyncId_ = null;
reg.logger().info('Doing scheduled sync');
if (!reg.oneDriveApi().auth()) {
reg.logger().info('Synchronizer is missing credentials - manual sync required to authenticate.');
return;
}
const sync = await reg.synchronizer();
2017-07-19 21:15:55 +02:00
let context = Setting.value('sync.context');
context = context ? JSON.parse(context) : {};
let newContext = await sync.start({ context: context });
Setting.setValue('sync.context', JSON.stringify(newContext));
}, delay);
2017-07-16 23:17:22 +02:00
}
2017-07-06 21:48:17 +02:00
reg.setDb = (v) => {
reg.db_ = v;
}
reg.db = () => {
return reg.db_;
}
export { reg }