You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	All: Moving sync target logic to SyncTarget classes
This commit is contained in:
		| @@ -117,7 +117,7 @@ class Command extends BaseCommand { | ||||
|  | ||||
| 			const syncTarget = reg.syncTarget(this.syncTargetId_); | ||||
|  | ||||
| 			if (syncTarget.isAuthenticated()) { | ||||
| 			if (!syncTarget.isAuthenticated()) { | ||||
| 				app().gui().showConsole(); | ||||
| 				app().gui().maximizeConsole(); | ||||
|  | ||||
|   | ||||
| @@ -631,6 +631,9 @@ msgstr "" | ||||
| msgid "Unknown flag: %s" | ||||
| msgstr "" | ||||
|  | ||||
| msgid "File system" | ||||
| msgstr "" | ||||
|  | ||||
| msgid "OneDrive" | ||||
| msgstr "" | ||||
|  | ||||
| @@ -735,9 +738,6 @@ msgid "" | ||||
| "`sync.2.path` to specify the target directory." | ||||
| msgstr "" | ||||
|  | ||||
| msgid "File system" | ||||
| msgstr "" | ||||
|  | ||||
| msgid "Text editor" | ||||
| msgstr "" | ||||
|  | ||||
|   | ||||
| @@ -686,6 +686,9 @@ msgstr "Utilisation : %s" | ||||
| msgid "Unknown flag: %s" | ||||
| msgstr "Paramètre inconnu : %s" | ||||
|  | ||||
| msgid "File system" | ||||
| msgstr "Système de fichier" | ||||
|  | ||||
| msgid "OneDrive" | ||||
| msgstr "OneDrive" | ||||
|  | ||||
| @@ -796,9 +799,6 @@ msgstr "" | ||||
| "La cible avec laquelle synchroniser. Pour synchroniser avec le système de " | ||||
| "fichier, veuillez spécifier le répertoire avec `sync.2.path`." | ||||
|  | ||||
| msgid "File system" | ||||
| msgstr "Système de fichier" | ||||
|  | ||||
| msgid "Text editor" | ||||
| msgstr "Éditeur de texte" | ||||
|  | ||||
|   | ||||
| @@ -631,6 +631,9 @@ msgstr "" | ||||
| msgid "Unknown flag: %s" | ||||
| msgstr "" | ||||
|  | ||||
| msgid "File system" | ||||
| msgstr "" | ||||
|  | ||||
| msgid "OneDrive" | ||||
| msgstr "" | ||||
|  | ||||
| @@ -735,9 +738,6 @@ msgid "" | ||||
| "`sync.2.path` to specify the target directory." | ||||
| msgstr "" | ||||
|  | ||||
| msgid "File system" | ||||
| msgstr "" | ||||
|  | ||||
| msgid "Text editor" | ||||
| msgstr "" | ||||
|  | ||||
|   | ||||
| @@ -17,6 +17,8 @@ const { FileApiDriverLocal } = require('lib/file-api-driver-local.js'); | ||||
| const { FsDriverNode } = require('lib/fs-driver-node.js'); | ||||
| const { time } = require('lib/time-utils.js'); | ||||
| const SyncTargetRegistry = require('lib/SyncTargetRegistry.js'); | ||||
| const SyncTarget1 = require('lib/SyncTarget1.js'); | ||||
| const SyncTarget2 = require('lib/SyncTarget2.js'); | ||||
|  | ||||
| let databases_ = []; | ||||
| let synchronizers_ = []; | ||||
| @@ -48,6 +50,9 @@ BaseItem.loadClass('NoteTag', NoteTag); | ||||
| Setting.setConstant('appId', 'net.cozic.joplin-cli'); | ||||
| Setting.setConstant('appType', 'cli'); | ||||
|  | ||||
| SyncTargetRegistry.addClass(SyncTarget1); | ||||
| SyncTargetRegistry.addClass(SyncTarget2); | ||||
|  | ||||
| function syncTargetId() { | ||||
| 	return syncTargetId_; | ||||
| } | ||||
|   | ||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -21,6 +21,9 @@ const { _, setLocale, defaultLocale, closestSupportedLocale } = require('lib/loc | ||||
| const os = require('os'); | ||||
| const fs = require('fs-extra'); | ||||
| const EventEmitter = require('events'); | ||||
| const SyncTargetRegistry = require('lib/SyncTargetRegistry.js'); | ||||
| const SyncTarget2 = require('lib/SyncTarget2.js'); | ||||
| const SyncTarget3 = require('lib/SyncTarget3.js'); | ||||
|  | ||||
| class BaseApplication { | ||||
|  | ||||
| @@ -322,6 +325,9 @@ class BaseApplication { | ||||
| 		Setting.setConstant('resourceDir', resourceDir); | ||||
| 		Setting.setConstant('tempDir', tempDir); | ||||
|  | ||||
| 		SyncTargetRegistry.addClass(SyncTarget2); | ||||
| 		SyncTargetRegistry.addClass(SyncTarget3); | ||||
|  | ||||
| 		await fs.mkdirp(profileDir, 0o755); | ||||
| 		await fs.mkdirp(resourceDir, 0o755); | ||||
| 		await fs.mkdirp(tempDir, 0o755); | ||||
|   | ||||
| @@ -30,25 +30,25 @@ class BaseSyncTarget { | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	name() { | ||||
| 		throw new Error('Not implemented'); | ||||
| 	static id() { | ||||
| 		throw new Error('id() not implemented'); | ||||
| 	} | ||||
|  | ||||
| 	label() { | ||||
| 		throw new Error('Not implemented'); | ||||
| 	static label() { | ||||
| 		throw new Error('label() not implemented'); | ||||
| 	} | ||||
|  | ||||
| 	async initSynchronizer() { | ||||
| 		throw new Error('Not implemented'); | ||||
| 		throw new Error('initSynchronizer() not implemented'); | ||||
| 	} | ||||
|  | ||||
| 	initFileApi() { | ||||
| 		throw new Error('Not implemented'); | ||||
| 	async initFileApi() { | ||||
| 		throw new Error('initFileApi() not implemented'); | ||||
| 	} | ||||
|  | ||||
| 	fileApi() { | ||||
| 	async fileApi() { | ||||
| 		if (this.fileApi_) return this.fileApi_; | ||||
| 		this.fileApi_ = this.initFileApi(); | ||||
| 		this.fileApi_ = await this.initFileApi(); | ||||
| 		return this.fileApi_; | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -80,6 +80,14 @@ class MdToHtml { | ||||
| 			this.loadedResources_[id] = {}; | ||||
|  | ||||
| 			const resource = await Resource.load(id); | ||||
|  | ||||
| 			if (!resource) { | ||||
| 				// Can happen for example if an image is attached to a note, but the resource hasn't | ||||
| 				// been download from the sync target yet. | ||||
| 				console.warn('Cannot load resource: ' + id); | ||||
| 				return; | ||||
| 			} | ||||
|  | ||||
| 			this.loadedResources_[id] = resource; | ||||
|  | ||||
| 			if (options.onResourceLoaded) options.onResourceLoaded(); | ||||
|   | ||||
| @@ -7,15 +7,11 @@ const { Synchronizer } = require('lib/synchronizer.js'); | ||||
|  | ||||
| class SyncTarget1 extends BaseSyncTarget { | ||||
|  | ||||
| 	id() { | ||||
| 	static id() { | ||||
| 		return 1; | ||||
| 	} | ||||
|  | ||||
| 	name() { | ||||
| 		return 'memory'; | ||||
| 	} | ||||
|  | ||||
| 	label() { | ||||
| 	static label() { | ||||
| 		return 'Memory'; | ||||
| 	} | ||||
|  | ||||
| @@ -26,12 +22,12 @@ class SyncTarget1 extends BaseSyncTarget { | ||||
| 	initFileApi() { | ||||
| 		const fileApi = new FileApi('/root', new FileApiDriverMemory()); | ||||
| 		fileApi.setLogger(this.logger()); | ||||
| 		fileApi.setSyncTargetId(this.id()); | ||||
| 		fileApi.setSyncTargetId(SyncTarget1.id()); | ||||
| 		return fileApi; | ||||
| 	} | ||||
|  | ||||
| 	async initSynchronizer() { | ||||
| 		return new Synchronizer(this.db(), this.fileApi(), Setting.value('appType')); | ||||
| 		return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType')); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -7,15 +7,11 @@ const { Synchronizer } = require('lib/synchronizer.js'); | ||||
|  | ||||
| class SyncTarget2 extends BaseSyncTarget { | ||||
|  | ||||
| 	id() { | ||||
| 	static id() { | ||||
| 		return 2; | ||||
| 	} | ||||
|  | ||||
| 	name() { | ||||
| 		return 'filesystem'; | ||||
| 	} | ||||
|  | ||||
| 	label() { | ||||
| 	static label() { | ||||
| 		return _('File system'); | ||||
| 	} | ||||
|  | ||||
| @@ -23,15 +19,15 @@ class SyncTarget2 extends BaseSyncTarget { | ||||
| 		return true; | ||||
| 	} | ||||
|  | ||||
| 	initFileApi() { | ||||
| 	async initFileApi() { | ||||
| 		const fileApi = new FileApi(Setting.value('sync.2.path'), new FileApiDriverLocal()); | ||||
| 		fileApi.setLogger(this.logger()); | ||||
| 		fileApi.setSyncTargetId(this.id()); | ||||
| 		fileApi.setSyncTargetId(SyncTarget2.id()); | ||||
| 		return fileApi; | ||||
| 	} | ||||
|  | ||||
| 	async initSynchronizer() { | ||||
| 		return new Synchronizer(this.db(), this.fileApi(), Setting.value('appType')); | ||||
| 		return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType')); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -14,15 +14,11 @@ class SyncTarget3 extends BaseSyncTarget { | ||||
| 		this.api_ = null; | ||||
| 	} | ||||
|  | ||||
| 	id() { | ||||
| 	static id() { | ||||
| 		return 3; | ||||
| 	} | ||||
|  | ||||
| 	name() { | ||||
| 		return 'onedrive'; | ||||
| 	} | ||||
|  | ||||
| 	label() { | ||||
| 	static label() { | ||||
| 		return _('OneDrive'); | ||||
| 	} | ||||
|  | ||||
| @@ -40,10 +36,10 @@ class SyncTarget3 extends BaseSyncTarget { | ||||
|  | ||||
| 		this.api_.on('authRefreshed', (a) => { | ||||
| 			this.logger().info('Saving updated OneDrive auth.'); | ||||
| 			Setting.setValue('sync.' + this.id() + '.auth', a ? JSON.stringify(a) : null); | ||||
| 			Setting.setValue('sync.' + staticSelf.id() + '.auth', a ? JSON.stringify(a) : null); | ||||
| 		}); | ||||
|  | ||||
| 		let auth = Setting.value('sync.' + this.id() + '.auth'); | ||||
| 		let auth = Setting.value('sync.' + staticSelf.id() + '.auth'); | ||||
| 		if (auth) { | ||||
| 			try { | ||||
| 				auth = JSON.parse(auth); | ||||
| @@ -59,20 +55,21 @@ class SyncTarget3 extends BaseSyncTarget { | ||||
| 		return this.api_; | ||||
| 	} | ||||
|  | ||||
| 	initFileApi() { | ||||
| 	async initFileApi() { | ||||
| 		const appDir = await this.api().appDirectory(); | ||||
| 		const fileApi = new FileApi(appDir, new FileApiDriverOneDrive(this.api())); | ||||
| 		fileApi.setSyncTargetId(this.id()); | ||||
| 		fileApi.setSyncTargetId(staticSelf.id()); | ||||
| 		fileApi.setLogger(this.logger()); | ||||
| 		return fileApi; | ||||
| 	} | ||||
|  | ||||
| 	async initSynchronizer() { | ||||
| 		if (!this.isAuthenticated()) throw new Error('User is not authentified'); | ||||
| 		 | ||||
| 		const appDir = await this.api().appDirectory(); | ||||
| 		return new Synchronizer(this.db(), this.fileApi(), Setting.value('appType')); | ||||
| 		return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType')); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| const staticSelf = SyncTarget3; | ||||
|  | ||||
| module.exports = SyncTarget3; | ||||
| @@ -1,16 +1,21 @@ | ||||
| const syncTargetClasses_ = { | ||||
| 	1: require('lib/SyncTarget1.js'), | ||||
| 	2: require('lib/SyncTarget2.js'), | ||||
| 	3: require('lib/SyncTarget3.js'), | ||||
| }; | ||||
|  | ||||
| class SyncTargetRegistry { | ||||
|  | ||||
| 	static classById(syncTargetId) { | ||||
| 		if (!syncTargetClasses_[syncTargetId]) throw new Error('Invalid id: ' + syncTargetId); | ||||
| 		return syncTargetClasses_[syncTargetId]; | ||||
| 		const info = SyncTargetRegistry.reg_[syncTargetId]; | ||||
| 		if (!info) throw new Error('Invalid id: ' + syncTargetId); | ||||
| 		return info.classRef; | ||||
| 	} | ||||
|  | ||||
| 	static addClass(SyncTargetClass) { | ||||
| 		this.reg_[SyncTargetClass.id()] = { | ||||
| 			id: SyncTargetClass.id(), | ||||
| 			label: SyncTargetClass.label(), | ||||
| 			classRef: SyncTargetClass, | ||||
| 		}; | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| SyncTargetRegistry.reg_ = {}; | ||||
|  | ||||
| module.exports = SyncTargetRegistry; | ||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -37,6 +37,8 @@ const { _, setLocale, closestSupportedLocale, defaultLocale } = require('lib/loc | ||||
| const RNFetchBlob = require('react-native-fetch-blob').default; | ||||
| const { PoorManIntervals } = require('lib/poor-man-intervals.js'); | ||||
| const { reducer, defaultState } = require('lib/reducer.js'); | ||||
| const SyncTargetRegistry = require('lib/SyncTargetRegistry.js'); | ||||
| const SyncTarget3 = require('lib/SyncTarget3.js'); | ||||
|  | ||||
| const generalMiddleware = store => next => async (action) => { | ||||
| 	if (action.type !== 'SIDE_MENU_OPEN_PERCENT') reg.logger().info('Reducer action', action.type); | ||||
| @@ -255,6 +257,8 @@ async function initialize(dispatch, backButtonHandler) { | ||||
|  | ||||
| 	reg.setLogger(mainLogger); | ||||
|  | ||||
| 	SyncTargetRegistry.addClass(SyncTarget3); | ||||
|  | ||||
| 	reg.logger().info('===================================='); | ||||
| 	reg.logger().info('Starting application ' + Setting.value('appId') + ' (' + Setting.value('env') + ')'); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user