| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const optionService = require('../../services/options'); | 
					
						
							| 
									
										
										
										
											2018-06-02 13:02:20 -04:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2019-01-27 21:18:11 +01:00
										 |  |  | const attributes = require('../../services/attributes'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 17:41:28 -04:00
										 |  |  | // options allowed to be updated directly in options dialog
 | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  | const ALLOWED_OPTIONS = [ | 
					
						
							|  |  |  |     'protectedSessionTimeout', | 
					
						
							|  |  |  |     'noteRevisionSnapshotTimeInterval', | 
					
						
							|  |  |  |     'zoomFactor', | 
					
						
							|  |  |  |     'theme', | 
					
						
							|  |  |  |     'syncServerHost', | 
					
						
							|  |  |  |     'syncServerTimeout', | 
					
						
							|  |  |  |     'syncProxy', | 
					
						
							|  |  |  |     'leftPaneMinWidth', | 
					
						
							|  |  |  |     'leftPaneWidthPercent', | 
					
						
							|  |  |  |     'hoistedNoteId', | 
					
						
							|  |  |  |     'mainFontSize', | 
					
						
							|  |  |  |     'treeFontSize', | 
					
						
							|  |  |  |     'detailFontSize', | 
					
						
							| 
									
										
										
										
											2019-05-12 21:45:30 +02:00
										 |  |  |     'openTabs', | 
					
						
							|  |  |  |     'hideTabRowForOneTab' | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  | ]; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  | async function getOptions() { | 
					
						
							| 
									
										
										
										
											2018-09-06 11:54:04 +02:00
										 |  |  |     return await optionService.getOptionsMap(ALLOWED_OPTIONS); | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 17:41:28 -04:00
										 |  |  | async function updateOption(req) { | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |     const {name, value} = req.params; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-22 22:21:16 +02:00
										 |  |  |     if (!update(name, value)) { | 
					
						
							|  |  |  |         return [400, "not allowed option to change"]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateOptions(req) { | 
					
						
							|  |  |  |     for (const optionName in req.body) { | 
					
						
							|  |  |  |         if (!update(optionName, req.body[optionName])) { | 
					
						
							|  |  |  |             // this should be improved
 | 
					
						
							|  |  |  |             // it should return 400 instead of current 500, but at least it now rollbacks transaction
 | 
					
						
							|  |  |  |             throw new Error(`${optionName} is not allowed to change`); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function update(name, value) { | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |     if (!ALLOWED_OPTIONS.includes(name)) { | 
					
						
							| 
									
										
										
										
											2018-07-22 22:21:16 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-02 13:02:20 -04:00
										 |  |  |     log.info(`Updating option ${name} to ${value}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     await optionService.setOption(name, value); | 
					
						
							| 
									
										
										
										
											2018-07-22 22:21:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 21:18:11 +01:00
										 |  |  | async function getUserThemes() { | 
					
						
							| 
									
										
										
										
											2019-02-03 00:12:57 +01:00
										 |  |  |     const notes = await attributes.getNotesWithLabel('appTheme'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const ret = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const note of notes) { | 
					
						
							| 
									
										
										
										
											2019-02-03 15:37:01 +01:00
										 |  |  |         let value = await note.getLabelValue('appTheme'); | 
					
						
							| 
									
										
										
										
											2019-02-03 00:12:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-03 15:35:37 +01:00
										 |  |  |         if (!value) { | 
					
						
							| 
									
										
										
										
											2019-02-03 00:12:57 +01:00
										 |  |  |             value = note.title.toLowerCase().replace(/[^a-z0-9]/gi, '-'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ret.push({ | 
					
						
							|  |  |  |             val: value, | 
					
						
							|  |  |  |             title: note.title, | 
					
						
							|  |  |  |             noteId: note.noteId | 
					
						
							| 
									
										
										
										
											2019-01-27 21:18:11 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-02-03 00:12:57 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-01-27 21:18:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |     getOptions, | 
					
						
							| 
									
										
										
										
											2018-07-22 22:21:16 +02:00
										 |  |  |     updateOption, | 
					
						
							| 
									
										
										
										
											2019-01-27 21:18:11 +01:00
										 |  |  |     updateOptions, | 
					
						
							|  |  |  |     getUserThemes | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | }; |