1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

All: Started moving sync target logic under SyncTarget classes

This commit is contained in:
Laurent Cozic 2017-11-24 18:06:30 +00:00
parent d7f3cfd778
commit 946ad7c71a
10 changed files with 68 additions and 73 deletions

View File

@ -168,9 +168,9 @@ class Application extends BaseApplication {
await doExit(); await doExit();
}, 5000); }, 5000);
if (await reg.syncStarted()) { if (await reg.syncTarget().syncStarted()) {
this.stdout(_('Cancelling background synchronisation... Please wait.')); this.stdout(_('Cancelling background synchronisation... Please wait.'));
const sync = await reg.synchronizer(Setting.value('sync.target')); const sync = await reg.syncTarget().synchronizer();
await sync.cancel(); await sync.cancel();
} }

View File

@ -64,7 +64,7 @@ class Command extends BaseCommand {
async doAuth(syncTargetId) { async doAuth(syncTargetId) {
const syncTarget = reg.syncTarget(this.syncTargetId_); const syncTarget = reg.syncTarget(this.syncTargetId_);
this.oneDriveApiUtils_ = new OneDriveApiNodeUtils(syncTarget.oneDriveApi()); this.oneDriveApiUtils_ = new OneDriveApiNodeUtils(syncTarget.api());
const auth = await this.oneDriveApiUtils_.oauthDance({ const auth = await this.oneDriveApiUtils_.oauthDance({
log: (...s) => { return this.stdout(...s); } log: (...s) => { return this.stdout(...s); }
}); });

View File

@ -132,7 +132,7 @@ class Application extends BaseApplication {
} }
if (['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) { if (['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
if (!await reg.syncStarted()) reg.scheduleSync(); if (!await reg.syncTarget().syncStarted()) reg.scheduleSync();
} }
const result = await super.generalMiddleware(store, next, action); const result = await super.generalMiddleware(store, next, action);

View File

@ -47,7 +47,7 @@ class OneDriveLoginScreenComponent extends React.Component {
this.authCode_ = code[1]; this.authCode_ = code[1];
try { try {
await reg.oneDriveApi().execTokenRequest(this.authCode_, this.redirectUrl(), true); await reg.syncTarget().api().execTokenRequest(this.authCode_, this.redirectUrl(), true);
this.props.dispatch({ type: 'NAV_BACK' }); this.props.dispatch({ type: 'NAV_BACK' });
reg.scheduleSync(0); reg.scheduleSync(0);
} catch (error) { } catch (error) {
@ -62,11 +62,11 @@ class OneDriveLoginScreenComponent extends React.Component {
} }
startUrl() { startUrl() {
return reg.oneDriveApi().authCodeUrl(this.redirectUrl()); return reg.syncTarget().api().authCodeUrl(this.redirectUrl());
} }
redirectUrl() { redirectUrl() {
return reg.oneDriveApi().nativeClientRedirectUrl(); return reg.syncTarget().api().nativeClientRedirectUrl();
} }
render() { render() {

View File

@ -11,7 +11,7 @@ class SyncTarget3 extends BaseSyncTarget {
constructor(db) { constructor(db) {
super(); super();
this.oneDriveApi_ = null; this.api_ = null;
} }
id() { id() {
@ -27,18 +27,18 @@ class SyncTarget3 extends BaseSyncTarget {
} }
isAuthenticated() { isAuthenticated() {
return this.oneDriveApi_ && this.oneDriveApi_.auth(); return this.api().auth();
} }
oneDriveApi() { api() {
if (this.oneDriveApi_) return this.oneDriveApi_; if (this.api_) return this.api_;
const isPublic = Setting.value('appType') != 'cli'; const isPublic = Setting.value('appType') != 'cli';
this.oneDriveApi_ = new OneDriveApi(parameters().oneDrive.id, parameters().oneDrive.secret, isPublic); this.api_ = new OneDriveApi(parameters().oneDrive.id, parameters().oneDrive.secret, isPublic);
this.oneDriveApi_.setLogger(this.logger()); this.api_.setLogger(this.logger());
this.oneDriveApi_.on('authRefreshed', (a) => { this.api_.on('authRefreshed', (a) => {
this.logger().info('Saving updated OneDrive auth.'); this.logger().info('Saving updated OneDrive auth.');
Setting.setValue('sync.' + this.id() + '.auth', a ? JSON.stringify(a) : null); Setting.setValue('sync.' + this.id() + '.auth', a ? JSON.stringify(a) : null);
}); });
@ -53,28 +53,24 @@ class SyncTarget3 extends BaseSyncTarget {
auth = null; auth = null;
} }
this.oneDriveApi_.setAuth(auth); this.api_.setAuth(auth);
} }
return this.oneDriveApi_; return this.api_;
} }
async initSynchronizer() { async initSynchronizer() {
let fileApi = null; let fileApi = null;
if (!this.oneDriveApi().auth()) throw new Error('User is not authentified'); if (!this.api().auth()) throw new Error('User is not authentified');
const appDir = await this.oneDriveApi().appDirectory(); const appDir = await this.api().appDirectory();
fileApi = new FileApi(appDir, new FileApiDriverOneDrive(this.oneDriveApi())); fileApi = new FileApi(appDir, new FileApiDriverOneDrive(this.api()));
fileApi.setSyncTargetId(this.id()); fileApi.setSyncTargetId(this.id());
fileApi.setLogger(this.logger()); fileApi.setLogger(this.logger());
return new Synchronizer(this.db(), fileApi, Setting.value('appType')); return new Synchronizer(this.db(), fileApi, Setting.value('appType'));
} }
isAuthenticated() {
return true;
}
} }
module.exports = SyncTarget3; module.exports = SyncTarget3;

View File

@ -28,11 +28,11 @@ class OneDriveLoginScreenComponent extends BaseScreenComponent {
} }
startUrl() { startUrl() {
return reg.oneDriveApi().authCodeUrl(this.redirectUrl()); return reg.syncTarget().api().authCodeUrl(this.redirectUrl());
} }
redirectUrl() { redirectUrl() {
return reg.oneDriveApi().nativeClientRedirectUrl(); return reg.syncTarget().api().nativeClientRedirectUrl();
} }
async webview_load(noIdeaWhatThisIs) { async webview_load(noIdeaWhatThisIs) {
@ -48,7 +48,7 @@ class OneDriveLoginScreenComponent extends BaseScreenComponent {
this.authCode_ = code[1]; this.authCode_ = code[1];
try { try {
await reg.oneDriveApi().execTokenRequest(this.authCode_, this.redirectUrl(), true); await reg.syncTarget().api().execTokenRequest(this.authCode_, this.redirectUrl(), true);
this.props.dispatch({ type: 'NAV_BACK' }); this.props.dispatch({ type: 'NAV_BACK' });
reg.scheduleSync(0); reg.scheduleSync(0);
} catch (error) { } catch (error) {

View File

@ -36,7 +36,7 @@ shared.synchronize_press = async function(comp) {
const action = comp.props.syncStarted ? 'cancel' : 'start'; const action = comp.props.syncStarted ? 'cancel' : 'start';
if (Setting.value('sync.target') == Setting.SYNC_TARGET_ONEDRIVE && !reg.oneDriveApi().auth()) { if (!reg.syncTarget().isAuthenticated()) {
comp.props.dispatch({ comp.props.dispatch({
type: 'NAV_GO', type: 'NAV_GO',
routeName: 'OneDriveLogin', routeName: 'OneDriveLogin',
@ -46,7 +46,7 @@ shared.synchronize_press = async function(comp) {
let sync = null; let sync = null;
try { try {
sync = await reg.synchronizer(Setting.value('sync.target')) sync = await reg.syncTarget().synchronizer();
} catch (error) { } catch (error) {
reg.logger().info('Could not acquire synchroniser:'); reg.logger().info('Could not acquire synchroniser:');
reg.logger().info(error); reg.logger().info(error);

View File

@ -32,7 +32,6 @@ class Setting extends BaseModel {
if (!this.metadata_.hasOwnProperty(n)) continue; if (!this.metadata_.hasOwnProperty(n)) continue;
this.keys_.push(n); this.keys_.push(n);
} }
//this.keys_.sort();
} }
if (appType || publicOnly) { if (appType || publicOnly) {

View File

@ -4,8 +4,6 @@ const { parameters } = require('lib/parameters.js');
const { FileApi } = require('lib/file-api.js'); const { FileApi } = require('lib/file-api.js');
const { Database } = require('lib/database.js'); const { Database } = require('lib/database.js');
const { Synchronizer } = require('lib/synchronizer.js'); const { Synchronizer } = require('lib/synchronizer.js');
const SyncTarget1 = require('lib/SyncTarget1.js');
const SyncTarget3 = require('lib/SyncTarget3.js');
const { FileApiDriverOneDrive } = require('lib/file-api-driver-onedrive.js'); const { FileApiDriverOneDrive } = require('lib/file-api-driver-onedrive.js');
const { shim } = require('lib/shim.js'); const { shim } = require('lib/shim.js');
const { time } = require('lib/time-utils.js'); const { time } = require('lib/time-utils.js');
@ -16,12 +14,12 @@ const reg = {};
reg.initSynchronizerStates_ = {}; reg.initSynchronizerStates_ = {};
reg.syncTargetClasses_ = { reg.syncTargetClasses_ = {
1: SyncTarget1, 1: require('lib/SyncTarget1.js'),
// 2: SyncTarget2, // 2: require('lib/SyncTarget2.js'),
3: SyncTarget3, 3: require('lib/SyncTarget3.js'),
}; };
reg.syncTargets_ = {}; reg.syncTargets_ = {};
reg.synchronizers_ = {}; //reg.synchronizers_ = {};
reg.logger = () => { reg.logger = () => {
if (!reg.logger_) { if (!reg.logger_) {
@ -49,42 +47,42 @@ reg.syncTarget = (syncTargetId = null) => {
return target; return target;
} }
reg.synchronizer = async (syncTargetId) => { // reg.synchronizer = async (syncTargetId) => {
if (reg.synchronizers_[syncTargetId]) return reg.synchronizers_[syncTargetId]; // if (reg.synchronizers_[syncTargetId]) return reg.synchronizers_[syncTargetId];
if (!reg.db()) throw new Error('Cannot initialize synchronizer: db not initialized'); // if (!reg.db()) throw new Error('Cannot initialize synchronizer: db not initialized');
if (reg.initSynchronizerStates_[syncTargetId] == 'started') { // if (reg.initSynchronizerStates_[syncTargetId] == 'started') {
// Synchronizer is already being initialized, so wait here till it's done. // // Synchronizer is already being initialized, so wait here till it's done.
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
const iid = setInterval(() => { // const iid = setInterval(() => {
if (reg.initSynchronizerStates_[syncTargetId] == 'ready') { // if (reg.initSynchronizerStates_[syncTargetId] == 'ready') {
clearInterval(iid); // clearInterval(iid);
resolve(reg.synchronizers_[syncTargetId]); // resolve(reg.synchronizers_[syncTargetId]);
} // }
if (reg.initSynchronizerStates_[syncTargetId] == 'error') { // if (reg.initSynchronizerStates_[syncTargetId] == 'error') {
clearInterval(iid); // clearInterval(iid);
reject(new Error('Could not initialise synchroniser')); // reject(new Error('Could not initialise synchroniser'));
} // }
}, 1000); // }, 1000);
}); // });
} else { // } else {
reg.initSynchronizerStates_[syncTargetId] = 'started'; // reg.initSynchronizerStates_[syncTargetId] = 'started';
try { // try {
const sync = await reg.initSynchronizer_(syncTargetId); // const sync = await reg.initSynchronizer_(syncTargetId);
reg.synchronizers_[syncTargetId] = sync; // reg.synchronizers_[syncTargetId] = sync;
reg.initSynchronizerStates_[syncTargetId] = 'ready'; // reg.initSynchronizerStates_[syncTargetId] = 'ready';
return sync; // return sync;
} catch (error) { // } catch (error) {
reg.initSynchronizerStates_[syncTargetId] = 'error'; // reg.initSynchronizerStates_[syncTargetId] = 'error';
throw error; // throw error;
} // }
} // }
} // }
reg.syncHasAuth = (syncTargetId) => { // reg.syncHasAuth = (syncTargetId) => {
return reg.syncTargets(syncTargetId).isAuthenticated(); // return reg.syncTarget(syncTargetId).isAuthenticated();
} // }
reg.scheduleSync = async (delay = null) => { reg.scheduleSync = async (delay = null) => {
if (delay === null) delay = 1000 * 3; if (delay === null) delay = 1000 * 3;
@ -107,7 +105,7 @@ reg.scheduleSync = async (delay = null) => {
const syncTargetId = Setting.value('sync.target'); const syncTargetId = Setting.value('sync.target');
if (!reg.syncHasAuth(syncTargetId)) { if (!reg.syncTarget(syncTargetId).isAuthenticated()) {
reg.logger().info('Synchroniser is missing credentials - manual sync required to authenticate.'); reg.logger().info('Synchroniser is missing credentials - manual sync required to authenticate.');
return; return;
} }
@ -143,9 +141,9 @@ reg.scheduleSync = async (delay = null) => {
} }
} }
reg.syncStarted = async () => { // reg.syncStarted = async () => {
return reg.syncTarget().syncStarted(); // return reg.syncTarget().syncStarted();
} // }
reg.setupRecurrentSync = () => { reg.setupRecurrentSync = () => {
if (reg.recurrentSyncId_) { if (reg.recurrentSyncId_) {

View File

@ -9,6 +9,7 @@ const { AppNav } = require('lib/components/app-nav.js');
const { Logger } = require('lib/logger.js'); const { Logger } = require('lib/logger.js');
const { Note } = require('lib/models/note.js'); const { Note } = require('lib/models/note.js');
const { Folder } = require('lib/models/folder.js'); const { Folder } = require('lib/models/folder.js');
const BaseSyncTarget = require('lib/BaseSyncTarget.js');
const { FoldersScreenUtils } = require('lib/folders-screen-utils.js'); const { FoldersScreenUtils } = require('lib/folders-screen-utils.js');
const { Resource } = require('lib/models/resource.js'); const { Resource } = require('lib/models/resource.js');
const { Tag } = require('lib/models/tag.js'); const { Tag } = require('lib/models/tag.js');
@ -47,7 +48,7 @@ const generalMiddleware = store => next => async (action) => {
if (action.type == 'NAV_GO') Keyboard.dismiss(); if (action.type == 'NAV_GO') Keyboard.dismiss();
if (['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) { if (['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
if (!await reg.syncStarted()) reg.scheduleSync(); if (!await reg.syncTarget().syncStarted()) reg.scheduleSync();
} }
if (action.type == 'SETTING_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTING_UPDATE_ALL') { if (action.type == 'SETTING_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTING_UPDATE_ALL') {
@ -273,6 +274,7 @@ async function initialize(dispatch, backButtonHandler) {
reg.dispatch = dispatch; reg.dispatch = dispatch;
BaseModel.dispatch = dispatch; BaseModel.dispatch = dispatch;
FoldersScreenUtils.dispatch = dispatch; FoldersScreenUtils.dispatch = dispatch;
BaseSyncTarget.dispatch = dispatch;
BaseModel.db_ = db; BaseModel.db_ = db;
BaseItem.loadClass('Note', Note); BaseItem.loadClass('Note', Note);