| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  | const log = require('./log'); | 
					
						
							|  |  |  | const noteService = require('./notes'); | 
					
						
							|  |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							|  |  |  | const dateUtils = require('./date_utils'); | 
					
						
							|  |  |  | const attributeService = require('./attributes'); | 
					
						
							|  |  |  | const dateNoteService = require('./date_notes'); | 
					
						
							|  |  |  | const treeService = require('./tree'); | 
					
						
							|  |  |  | const config = require('./config'); | 
					
						
							|  |  |  | const repository = require('./repository'); | 
					
						
							|  |  |  | const axios = require('axios'); | 
					
						
							|  |  |  | const cloningService = require('./cloning'); | 
					
						
							|  |  |  | const messagingService = require('./messaging'); | 
					
						
							| 
									
										
										
										
											2019-01-18 23:57:08 +01:00
										 |  |  | const appInfo = require('./app_info'); | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |  * This is the main backend API interface for scripts. It's published in the local "api" object. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |  * @constructor | 
					
						
							|  |  |  |  * @hideconstructor | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  | function BackendScriptApi(currentNote, apiParams) { | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** @property {Note} note where script started executing */ | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  |     this.startNote = apiParams.startNote; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** @property {Note} note where script is currently executing */ | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |     this.currentNote = currentNote; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** @property {Entity} entity whose event triggered this executions */ | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  |     this.originEntity = apiParams.originEntity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const key in apiParams) { | 
					
						
							|  |  |  |         this[key] = apiParams[key]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     this.axios = axios; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.utils = { | 
					
						
							|  |  |  |         unescapeHtml: utils.unescapeHtml, | 
					
						
							|  |  |  |         isoDateTimeStr: dateUtils.dateStr, | 
					
						
							|  |  |  |         isoDateStr: date => dateUtils.dateStr(date).substr(0, 10) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Instance name identifies particular Trilium instance. It can be useful for scripts | 
					
						
							|  |  |  |      * if some action needs to happen on only one specific instance. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @returns {string|null} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getInstanceName = () => config.General ? config.General.instanceName : null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @returns {Promise<Note|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getNote = repository.getNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} branchId | 
					
						
							|  |  |  |      * @returns {Promise<Branch|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getBranch = repository.getBranch; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} attributeId | 
					
						
							|  |  |  |      * @returns {Promise<Attribute|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getAttribute = repository.getAttribute; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} imageId | 
					
						
							|  |  |  |      * @returns {Promise<Image|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getImage = repository.getImage; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Retrieves first entity from the SQL's result set. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} SQL query | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @param {Array.<?>} array of params | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |      * @returns {Promise<Entity|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getEntity = repository.getEntity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} SQL query | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @param {Array.<?>} array of params | 
					
						
							|  |  |  |      * @returns {Promise<Entity[]>} | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     this.getEntities = repository.getEntities; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Retrieves notes with given label name & value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} name - attribute name | 
					
						
							|  |  |  |      * @param {string} [value] - attribute value | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @returns {Promise<Note[]>} | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     this.getNotesWithLabel = attributeService.getNotesWithLabel; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Retrieves first note with given label name & value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} name - attribute name | 
					
						
							|  |  |  |      * @param {string} [value] - attribute value | 
					
						
							|  |  |  |      * @returns {Promise<Note|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getNoteWithLabel = attributeService.getNoteWithLabel; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * If there's no branch between note and parent note, create one. Otherwise do nothing. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {string} parentNoteId | 
					
						
							|  |  |  |      * @param {string} prefix - if branch will be create between note and parent note, set this prefix | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.ensureNoteIsPresentInParent = cloningService.ensureNoteIsPresentInParent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * If there's a branch between note and parent note, remove it. Otherwise do nothing. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {string} parentNoteId | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.ensureNoteIsAbsentFromParent = cloningService.ensureNoteIsAbsentFromParent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Based on the value, either create or remove branch between note and parent note. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {boolean} present - true if we want the branch to exist, false if we want it gone | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {string} parentNoteId | 
					
						
							|  |  |  |      * @param {string} prefix - if branch will be create between note and parent note, set this prefix | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.toggleNoteInParent = cloningService.toggleNoteInParent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @typedef {object} CreateNoteAttribute | 
					
						
							|  |  |  |      * @property {string} type - attribute type - label, relation etc. | 
					
						
							|  |  |  |      * @property {string} name - attribute name | 
					
						
							|  |  |  |      * @property {string} [value] - attribute value | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @typedef {object} CreateNoteExtraOptions | 
					
						
							|  |  |  |      * @property {boolean} [json=false] - should the note be JSON | 
					
						
							|  |  |  |      * @property {boolean} [isProtected=false] - should the note be protected | 
					
						
							|  |  |  |      * @property {string} [type='text'] - note type | 
					
						
							|  |  |  |      * @property {string} [mime='text/html'] - MIME type of the note | 
					
						
							|  |  |  |      * @property {CreateNoteAttribute[]} [attributes=[]] - attributes to be created for this note | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string} parentNoteId - create new note under this parent | 
					
						
							|  |  |  |      * @param {string} title | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @param {string} [content=""] | 
					
						
							|  |  |  |      * @param {CreateNoteExtraOptions} [extraOptions={}] | 
					
						
							|  |  |  |      * @returns {Promise<{note: Note, branch: Branch}>} object contains newly created entities note and branch | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     this.createNote = noteService.createNote; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-03 11:15:32 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Creates new note according to given params and force all connected clients to refresh their tree. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string} parentNoteId - create new note under this parent | 
					
						
							|  |  |  |      * @param {string} title | 
					
						
							|  |  |  |      * @param {string} [content=""] | 
					
						
							|  |  |  |      * @param {CreateNoteExtraOptions} [extraOptions={}] | 
					
						
							|  |  |  |      * @returns {Promise<{note: Note, branch: Branch}>} object contains newly created entities note and branch | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.createNoteAndRefresh = async function(parentNoteId, title, content, extraOptions) { | 
					
						
							|  |  |  |         await noteService.createNote(parentNoteId, title, content, extraOptions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         messagingService.refreshTree(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Log given message to trilium logs. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param message | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.log = message => log.info(`Script "${currentNote.title}" (${currentNote.noteId}): ${message}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns root note of the calendar. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @returns {Promise<Note|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getRootCalendarNote = dateNoteService.getRootCalendarNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-02-19 21:29:41 +01:00
										 |  |  |      * Returns day note for given date. If such note doesn't exist, it is created. | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2019-02-19 21:29:41 +01:00
										 |  |  |      * @param {string} date in YYYY-MM-DD format | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |      * @returns {Promise<Note|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getDateNote = dateNoteService.getDateNote; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-19 21:29:41 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns month note for given date. If such note doesn't exist, it is created. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} date in YYYY-MM format | 
					
						
							|  |  |  |      * @returns {Promise<Note|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getMonthNote = dateNoteService.getMonthNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns year note for given year. If such note doesn't exist, it is created. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} year in YYYY format | 
					
						
							|  |  |  |      * @returns {Promise<Note|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getYearNote = dateNoteService.getYearNote; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} parentNoteId - this note's child notes will be sorted | 
					
						
							|  |  |  |      * @returns Promise<void> | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.sortNotesAlphabetically = treeService.sortNotesAlphabetically; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This method finds note by its noteId and prefix and either sets it to the given parentNoteId | 
					
						
							|  |  |  |      * or removes the branch (if parentNoteId is not given). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This method looks similar to toggleNoteInParent() but differs because we're looking up branch by prefix. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {string} prefix | 
					
						
							|  |  |  |      * @param {string} [parentNoteId] | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.setNoteToParent = treeService.setNoteToParent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This functions wraps code which is supposed to be running in transaction. If transaction already | 
					
						
							|  |  |  |      * exists, then we'll use that transaction. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This method is required only when script has label manualTransactionHandling, all other scripts are | 
					
						
							|  |  |  |      * transactional by default. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      * @param {function} func | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |      * @returns {Promise<?>} result of func callback | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.transactional = sql.transactional; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 22:28:30 +01:00
										 |  |  |     this.sql = sql; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Trigger tree refresh in all connected clients. This is required when some tree change happens in | 
					
						
							|  |  |  |      * the backend. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-02-03 11:15:32 +01:00
										 |  |  |     this.refreshTree = messagingService.refreshTree; | 
					
						
							| 
									
										
										
										
											2019-01-18 23:57:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return {{syncVersion, appVersion, buildRevision, dbVersion, dataDirectory, buildDate}|*} - object representing basic info about running Trilium version | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getAppInfo = () => appInfo | 
					
						
							| 
									
										
										
										
											2018-08-23 10:10:04 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = BackendScriptApi; |