| 
									
										
										
										
											2018-01-23 21:59:30 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const scriptService = require('../../services/script'); | 
					
						
							| 
									
										
										
										
											2018-08-07 12:48:11 +02:00
										 |  |  | const attributeService = require('../../services/attributes'); | 
					
						
							| 
									
										
										
										
											2021-05-17 22:09:49 +02:00
										 |  |  | const becca = require('../../becca/becca.js'); | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  | const syncService = require('../../services/sync'); | 
					
						
							| 
									
										
										
										
											2018-01-23 21:59:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-15 22:57:23 +02:00
										 |  |  | function exec(req) { | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2020-08-18 23:32:50 +02:00
										 |  |  |         const {body} = req; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-15 22:57:23 +02:00
										 |  |  |         const result = scriptService.executeScript( | 
					
						
							| 
									
										
										
										
											2020-08-18 23:32:50 +02:00
										 |  |  |             body.script, | 
					
						
							|  |  |  |             body.params, | 
					
						
							|  |  |  |             body.startNoteId, | 
					
						
							|  |  |  |             body.currentNoteId, | 
					
						
							|  |  |  |             body.originEntityName, | 
					
						
							|  |  |  |             body.originEntityId | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  |         return { | 
					
						
							|  |  |  |             success: true, | 
					
						
							|  |  |  |             executionResult: result, | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             maxEntityChangeId: syncService.getMaxEntityChangeId() | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         return { success: false, error: e.message }; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-05 23:19:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-15 22:57:23 +02:00
										 |  |  | function run(req) { | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(req.params.noteId); | 
					
						
							| 
									
										
										
										
											2018-03-05 23:19:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-15 22:57:23 +02:00
										 |  |  |     const result = scriptService.executeNote(note, { originEntity: note }); | 
					
						
							| 
									
										
										
										
											2018-03-05 23:19:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 12:03:21 -04:00
										 |  |  |     return { executionResult: result }; | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-23 21:59:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getBundlesWithLabel(label, value) { | 
					
						
							|  |  |  |     const notes = attributeService.getNotesWithLabel(label, value); | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |     const bundles = []; | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 14:21:11 -05:00
										 |  |  |     for (const note of notes) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         const bundle = scriptService.getScriptBundleForFrontend(note); | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 23:36:08 -05:00
										 |  |  |         if (bundle) { | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |             bundles.push(bundle); | 
					
						
							| 
									
										
										
										
											2018-03-08 23:36:08 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |     return bundles; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 15:09:08 -05:00
										 |  |  | function getStartupBundles(req) { | 
					
						
							| 
									
										
										
										
											2021-01-15 20:12:14 +01:00
										 |  |  |     if (!process.env.TRILIUM_SAFE_MODE) { | 
					
						
							| 
									
										
										
										
											2021-03-18 15:09:08 -05:00
										 |  |  |         if (req.query.mobile === "true") { | 
					
						
							|  |  |  |             return getBundlesWithLabel("run", "mobileStartup"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return getBundlesWithLabel("run", "frontendStartup"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-01-15 20:12:14 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getWidgetBundles() { | 
					
						
							| 
									
										
										
										
											2021-01-15 20:12:14 +01:00
										 |  |  |     if (!process.env.TRILIUM_SAFE_MODE) { | 
					
						
							|  |  |  |         return getBundlesWithLabel("widget"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return []; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getRelationBundles(req) { | 
					
						
							| 
									
										
										
										
											2018-07-29 18:39:10 +02:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-07-29 18:39:10 +02:00
										 |  |  |     const relationName = req.params.relationName; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const attributes = note.getAttributes(); | 
					
						
							| 
									
										
										
										
											2018-08-07 12:48:11 +02:00
										 |  |  |     const filtered = attributes.filter(attr => attr.type === 'relation' && attr.name === relationName); | 
					
						
							|  |  |  |     const targetNoteIds = filtered.map(relation => relation.value); | 
					
						
							| 
									
										
										
										
											2018-07-29 18:39:10 +02:00
										 |  |  |     const uniqueNoteIds = Array.from(new Set(targetNoteIds)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const bundles = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const noteId of uniqueNoteIds) { | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |         const note = becca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2019-01-16 22:52:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!note.isJavaScript() || note.getScriptEnv() !== 'frontend') { | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         const bundle = scriptService.getScriptBundleForFrontend(note); | 
					
						
							| 
									
										
										
										
											2019-01-13 11:56:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (bundle) { | 
					
						
							|  |  |  |             bundles.push(bundle); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-29 18:39:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return bundles; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getBundle(req) { | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(req.params.noteId); | 
					
						
							| 
									
										
										
										
											2019-01-13 11:56:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     return scriptService.getScriptBundleForFrontend(note); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     exec, | 
					
						
							|  |  |  |     run, | 
					
						
							|  |  |  |     getStartupBundles, | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |     getWidgetBundles, | 
					
						
							| 
									
										
										
										
											2018-07-29 18:39:10 +02:00
										 |  |  |     getRelationBundles, | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |     getBundle | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |