Notes/src/routes/api/script.js

122 lines
2.8 KiB
JavaScript
Raw Normal View History

"use strict";
const scriptService = require('../../services/script');
const attributeService = require('../../services/attributes');
2021-06-29 22:15:57 +02:00
const becca = require('../../becca/becca');
const syncService = require('../../services/sync');
function exec(req) {
try {
2020-08-18 23:32:50 +02:00
const {body} = req;
const result = scriptService.executeScript(
2020-08-18 23:32:50 +02:00
body.script,
body.params,
body.startNoteId,
body.currentNoteId,
body.originEntityName,
body.originEntityId
);
return {
success: true,
executionResult: result,
maxEntityChangeId: syncService.getMaxEntityChangeId()
};
}
catch (e) {
return { success: false, error: e.message };
}
}
function run(req) {
2021-05-02 11:23:58 +02:00
const note = becca.getNote(req.params.noteId);
const result = scriptService.executeNote(note, { originEntity: note });
2018-04-01 12:03:21 -04:00
return { executionResult: result };
}
2020-06-20 12:31:38 +02:00
function getBundlesWithLabel(label, value) {
const notes = attributeService.getNotesWithLabel(label, value);
const bundles = [];
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
if (bundle) {
bundles.push(bundle);
}
}
return bundles;
}
function getStartupBundles(req) {
if (!process.env.TRILIUM_SAFE_MODE) {
if (req.query.mobile === "true") {
return getBundlesWithLabel("run", "mobileStartup");
}
else {
return getBundlesWithLabel("run", "frontendStartup");
}
}
else {
return [];
}
2020-03-16 21:16:09 +01:00
}
2020-06-20 12:31:38 +02:00
function getWidgetBundles() {
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) {
const noteId = req.params.noteId;
2021-05-02 11:23:58 +02:00
const note = becca.getNote(noteId);
const relationName = req.params.relationName;
2020-06-20 12:31:38 +02:00
const attributes = note.getAttributes();
const filtered = attributes.filter(attr => attr.type === 'relation' && attr.name === relationName);
const targetNoteIds = filtered.map(relation => relation.value);
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);
if (!note.isJavaScript() || note.getScriptEnv() !== 'frontend') {
continue;
}
2020-06-20 12:31:38 +02:00
const bundle = scriptService.getScriptBundleForFrontend(note);
if (bundle) {
bundles.push(bundle);
}
}
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);
2020-06-20 12:31:38 +02:00
return scriptService.getScriptBundleForFrontend(note);
}
module.exports = {
exec,
run,
getStartupBundles,
2020-03-16 21:16:09 +01:00
getWidgetBundles,
getRelationBundles,
getBundle
2020-06-20 12:31:38 +02:00
};