2018-01-23 21:59:30 -05:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import scriptService from "../../services/script.js";
|
|
|
|
import attributeService from "../../services/attributes.js";
|
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import syncService from "../../services/sync.js";
|
|
|
|
import sql from "../../services/sql.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { Request } from "express";
|
2025-03-08 17:07:25 +01:00
|
|
|
import { safeExtractMessageAndStackFromError } from "../../services/utils.js";
|
2024-04-06 22:38:17 +03:00
|
|
|
|
|
|
|
interface ScriptBody {
|
|
|
|
script: string;
|
|
|
|
params: any[];
|
|
|
|
startNoteId: string;
|
|
|
|
currentNoteId: string;
|
|
|
|
originEntityName: string;
|
|
|
|
originEntityId: string;
|
|
|
|
transactional: boolean;
|
|
|
|
}
|
2018-01-23 21:59:30 -05:00
|
|
|
|
2023-10-29 00:51:23 +02:00
|
|
|
// The async/await here is very confusing, because the body.script may, but may not be async. If it is async, then we
|
|
|
|
// need to await it and make the complete response including metadata available in a Promise, so that the route detects
|
|
|
|
// this and does result.then().
|
2024-04-06 22:38:17 +03:00
|
|
|
async function exec(req: Request) {
|
2018-08-17 11:31:42 +02:00
|
|
|
try {
|
2025-01-09 18:07:02 +02:00
|
|
|
const body = req.body as ScriptBody;
|
2020-08-18 23:32:50 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const execute = (body: ScriptBody) => scriptService.executeScript(body.script, body.params, body.startNoteId, body.currentNoteId, body.originEntityName, body.originEntityId);
|
2018-08-17 11:31:42 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const result = body.transactional ? sql.transactional(() => execute(body)) : await execute(body);
|
2023-10-29 00:51:23 +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
|
|
|
};
|
2025-03-08 16:01:53 +01:00
|
|
|
} catch (e: unknown) {
|
2025-03-08 17:07:25 +01:00
|
|
|
const [errMessage] = safeExtractMessageAndStackFromError(e);
|
2025-03-08 16:01:53 +01:00
|
|
|
return {
|
|
|
|
success: false,
|
2025-03-08 17:07:25 +01:00
|
|
|
error: errMessage
|
2025-03-08 16:01:53 +01:00
|
|
|
};
|
2018-08-17 11:31:42 +02:00
|
|
|
}
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-03-05 23:19:46 -05:00
|
|
|
|
2024-04-06 22:38:17 +03:00
|
|
|
function run(req: Request) {
|
|
|
|
const note = becca.getNoteOrThrow(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
|
|
|
|
2024-04-06 22:38:17 +03:00
|
|
|
function getBundlesWithLabel(label: string, value?: string) {
|
2023-03-06 23:00:29 +01:00
|
|
|
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
|
|
|
|
2024-04-06 22:38:17 +03:00
|
|
|
function getStartupBundles(req: Request) {
|
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");
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2021-03-18 15:09:08 -05:00
|
|
|
return getBundlesWithLabel("run", "frontendStartup");
|
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2021-01-15 20:12:14 +01:00
|
|
|
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");
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2021-01-15 20:12:14 +01:00
|
|
|
return [];
|
|
|
|
}
|
2020-03-16 21:16:09 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 22:38:17 +03:00
|
|
|
function getRelationBundles(req: Request) {
|
2018-07-29 18:39:10 +02:00
|
|
|
const noteId = req.params.noteId;
|
2024-04-06 22:38:17 +03:00
|
|
|
const note = becca.getNoteOrThrow(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();
|
2025-01-09 18:07:02 +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) {
|
2024-04-06 22:38:17 +03:00
|
|
|
const note = becca.getNoteOrThrow(noteId);
|
2019-01-16 22:52:32 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!note.isJavaScript() || note.getScriptEnv() !== "frontend") {
|
2019-01-16 22:52:32 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-06 22:38:17 +03:00
|
|
|
function getBundle(req: Request) {
|
|
|
|
const note = becca.getNoteOrThrow(req.params.noteId);
|
2024-04-04 22:47:58 +03:00
|
|
|
const { script, params } = req.body;
|
2019-01-13 11:56:50 +01:00
|
|
|
|
2023-08-30 23:18:16 +02:00
|
|
|
return scriptService.getScriptBundleForFrontend(note, script, params);
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-01-23 22:53:27 -05:00
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2018-03-30 17:29:13 -04:00
|
|
|
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
|
|
|
};
|