| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | const optionService = require('./options'); | 
					
						
							|  |  |  | const passwordEncryptionService = require('./password_encryption'); | 
					
						
							|  |  |  | const myScryptService = require('./my_scrypt'); | 
					
						
							|  |  |  | const appInfo = require('./app_info'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2019-11-03 11:43:04 +01:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | const dateUtils = require('./date_utils'); | 
					
						
							| 
									
										
										
										
											2019-11-18 23:01:31 +01:00
										 |  |  | const keyboardActions = require('./keyboard_actions'); | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | async function initDocumentOptions() { | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  |     await optionService.createOption('documentId', utils.randomSecureToken(16), false); | 
					
						
							|  |  |  |     await optionService.createOption('documentSecret', utils.randomSecureToken(16), false); | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | async function initSyncedOptions(username, password) { | 
					
						
							| 
									
										
										
										
											2018-08-17 18:11:03 +02:00
										 |  |  |     await optionService.createOption('username', username, true); | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 18:11:03 +02:00
										 |  |  |     await optionService.createOption('passwordVerificationSalt', utils.randomSecureToken(32), true); | 
					
						
							|  |  |  |     await optionService.createOption('passwordDerivedKeySalt', utils.randomSecureToken(32), true); | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 18:11:03 +02:00
										 |  |  |     const passwordVerificationKey = utils.toBase64(await myScryptService.getVerificationHash(password), true); | 
					
						
							|  |  |  |     await optionService.createOption('passwordVerificationHash', passwordVerificationKey, true); | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // passwordEncryptionService expects these options to already exist
 | 
					
						
							| 
									
										
										
										
											2018-08-17 18:11:03 +02:00
										 |  |  |     await optionService.createOption('encryptedDataKey', '', true); | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 18:11:03 +02:00
										 |  |  |     await passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16), true); | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:28:49 +02:00
										 |  |  | async function initNotSyncedOptions(initialized, startNotePath = 'root', opts = {}) { | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |     await optionService.createOption('openTabs', JSON.stringify([ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             notePath: startNotePath, | 
					
						
							| 
									
										
										
										
											2019-08-29 22:32:53 +02:00
										 |  |  |             active: true, | 
					
						
							|  |  |  |             sidebar: { | 
					
						
							|  |  |  |                 widgets: [] | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     ]), false); | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-13 22:43:59 +01:00
										 |  |  |     await optionService.createOption('lastDailyBackupDate', dateUtils.utcNowDateTime(), false); | 
					
						
							|  |  |  |     await optionService.createOption('lastWeeklyBackupDate', dateUtils.utcNowDateTime(), false); | 
					
						
							|  |  |  |     await optionService.createOption('lastMonthlyBackupDate', dateUtils.utcNowDateTime(), false); | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  |     await optionService.createOption('dbVersion', appInfo.dbVersion, false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  |     await optionService.createOption('initialized', initialized ? 'true' : 'false', false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await optionService.createOption('lastSyncedPull', '0', false); | 
					
						
							|  |  |  |     await optionService.createOption('lastSyncedPush', '0', false); | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:28:49 +02:00
										 |  |  |     await optionService.createOption('theme', opts.theme || 'white', false); | 
					
						
							| 
									
										
										
										
											2018-07-22 22:21:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:28:49 +02:00
										 |  |  |     await optionService.createOption('syncServerHost', opts.syncServerHost || '', false); | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  |     await optionService.createOption('syncServerTimeout', '5000', false); | 
					
						
							| 
									
										
										
										
											2019-08-11 10:28:49 +02:00
										 |  |  |     await optionService.createOption('syncProxy', opts.syncProxy || '', false); | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-24 08:12:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  | const defaultOptions = [ | 
					
						
							| 
									
										
										
										
											2019-11-16 17:03:18 +01:00
										 |  |  |     { name: 'noteRevisionSnapshotTimeInterval', value: '600', isSynced: true }, | 
					
						
							|  |  |  |     { name: 'protectedSessionTimeout', value: '600', isSynced: true }, | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  |     { name: 'hoistedNoteId', value: 'root', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'zoomFactor', value: '1.0', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'mainFontSize', value: '100', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'treeFontSize', value: '100', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'detailFontSize', value: '110', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'calendarWidget', value: '{"enabled":true,"expanded":true,"position":20}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'editedNotesWidget', value: '{"enabled":true,"expanded":true,"position":50}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'noteInfoWidget', value: '{"enabled":true,"expanded":true,"position":100}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'attributesWidget', value: '{"enabled":true,"expanded":true,"position":200}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'linkMapWidget', value: '{"enabled":true,"expanded":true,"position":300}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'noteRevisionsWidget', value: '{"enabled":true,"expanded":true,"position":400}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'whatLinksHereWidget', value: '{"enabled":false,"expanded":true,"position":500}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'similarNotesWidget', value: '{"enabled":true,"expanded":true,"position":600}', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'spellCheckEnabled', value: 'true', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'spellCheckLanguageCode', value: 'en-US', isSynced: false }, | 
					
						
							| 
									
										
										
										
											2019-11-03 11:43:04 +01:00
										 |  |  |     { name: 'imageMaxWidthHeight', value: '1200', isSynced: true }, | 
					
						
							| 
									
										
										
										
											2019-11-10 11:25:41 +01:00
										 |  |  |     { name: 'imageJpegQuality', value: '75', isSynced: true }, | 
					
						
							| 
									
										
										
										
											2019-12-08 09:12:42 +01:00
										 |  |  |     { name: 'autoFixConsistencyIssues', value: 'true', isSynced: false }, | 
					
						
							| 
									
										
										
										
											2019-12-23 20:34:29 +01:00
										 |  |  |     { name: 'codeNotesMimeTypes', value: '["text/x-csrc","text/x-c++src","text/x-csharp","text/css","text/x-go","text/x-groovy","text/x-haskell","text/html","message/http","text/x-java","application/javascript;env=frontend","application/javascript;env=backend","application/json","text/x-kotlin","text/x-markdown","text/x-perl","text/x-php","text/x-python","text/x-ruby",null,"text/x-sql","text/x-swift","text/xml","text/x-yaml"]', isSynced: true }, | 
					
						
							|  |  |  |     { name: 'leftPaneWidth', value: '25', isSynced: false }, | 
					
						
							| 
									
										
										
										
											2020-02-04 20:42:40 +01:00
										 |  |  |     { name: 'leftPaneVisible', value: 'true', isSynced: false }, | 
					
						
							| 
									
										
										
										
											2019-12-23 20:34:29 +01:00
										 |  |  |     { name: 'rightPaneWidth', value: '25', isSynced: false }, | 
					
						
							| 
									
										
										
										
											2019-12-24 12:10:32 +01:00
										 |  |  |     { name: 'rightPaneVisible', value: 'true', isSynced: false }, | 
					
						
							| 
									
										
										
										
											2020-01-03 22:32:49 +01:00
										 |  |  |     { name: 'nativeTitleBarVisible', value: 'false', isSynced: false }, | 
					
						
							|  |  |  |     { name: 'eraseNotesAfterTimeInSeconds', value: '604800', isSynced: true } // default is 7 days
 | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function initStartupOptions() { | 
					
						
							|  |  |  |     const optionsMap = await optionService.getOptionsMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 23:01:31 +01:00
										 |  |  |     const allDefaultOptions = defaultOptions.concat(getKeyboardDefaultOptions()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const {name, value, isSynced} of allDefaultOptions) { | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  |         if (!(name in optionsMap)) { | 
					
						
							|  |  |  |             await optionService.createOption(name, value, isSynced); | 
					
						
							| 
									
										
										
										
											2019-11-03 11:43:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             log.info(`Created missing option "${name}" with default value "${value}"`); | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 23:01:31 +01:00
										 |  |  | function getKeyboardDefaultOptions() { | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  |     return keyboardActions.DEFAULT_KEYBOARD_ACTIONS | 
					
						
							|  |  |  |         .filter(ka => !!ka.actionName) | 
					
						
							| 
									
										
										
										
											2020-02-28 22:07:08 +01:00
										 |  |  |         .map(ka => ({ | 
					
						
							|  |  |  |             name: "keyboardShortcuts" + ka.actionName.charAt(0).toUpperCase() + ka.actionName.slice(1), | 
					
						
							|  |  |  |             value: JSON.stringify(ka.defaultShortcuts), | 
					
						
							|  |  |  |             isSynced: false | 
					
						
							|  |  |  |         })); | 
					
						
							| 
									
										
										
										
											2019-11-18 23:01:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  |     initDocumentOptions, | 
					
						
							|  |  |  |     initSyncedOptions, | 
					
						
							| 
									
										
										
										
											2019-11-03 10:43:40 +01:00
										 |  |  |     initNotSyncedOptions, | 
					
						
							|  |  |  |     initStartupOptions | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | }; |