Notes/src/routes/api/script.ts

134 lines
3.7 KiB
TypeScript
Raw Normal View History

"use strict";
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";
import type { Request } from "express";
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;
}
// 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) {
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);
2025-01-09 18:07:02 +02:00
const result = body.transactional ? sql.transactional(() => execute(body)) : await execute(body);
return {
success: true,
executionResult: result,
maxEntityChangeId: syncService.getMaxEntityChangeId()
};
} catch (e: unknown) {
const [errMessage] = safeExtractMessageAndStackFromError(e);
return {
success: false,
error: errMessage
};
}
}
2024-04-06 22:38:17 +03:00
function run(req: Request) {
const note = becca.getNoteOrThrow(req.params.noteId);
const result = scriptService.executeNote(note, { originEntity: note });
2018-04-01 12:03:21 -04:00
return { executionResult: result };
}
2024-04-06 22:38:17 +03:00
function getBundlesWithLabel(label: string, value?: string) {
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;
}
2024-04-06 22:38:17 +03:00
function getStartupBundles(req: Request) {
if (!process.env.TRILIUM_SAFE_MODE) {
if (req.query.mobile === "true") {
return getBundlesWithLabel("run", "mobileStartup");
2025-01-09 18:07:02 +02:00
} else {
return getBundlesWithLabel("run", "frontendStartup");
}
2025-01-09 18:07:02 +02:00
} 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");
2025-01-09 18:07:02 +02:00
} else {
return [];
}
2020-03-16 21:16:09 +01:00
}
2024-04-06 22:38:17 +03:00
function getRelationBundles(req: Request) {
const noteId = req.params.noteId;
2024-04-06 22:38:17 +03:00
const note = becca.getNoteOrThrow(noteId);
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);
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);
2025-01-09 18:07:02 +02:00
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;
}
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;
return scriptService.getScriptBundleForFrontend(note, script, params);
}
export default {
exec,
run,
getStartupBundles,
2020-03-16 21:16:09 +01:00
getWidgetBundles,
getRelationBundles,
getBundle
2020-06-20 12:31:38 +02:00
};