trilium/src/routes/api/script.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

"use strict";
const labelService = require('../../services/labels');
const scriptService = require('../../services/script');
const relationService = require('../../services/relations');
2018-03-31 21:07:58 +08:00
const repository = require('../../services/repository');
async function exec(req) {
const result = await scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId,
req.body.currentNoteId, req.body.targetNoteId);
2018-04-02 00:03:21 +08:00
return { executionResult: result };
}
async function run(req) {
const note = await repository.getNote(req.params.noteId);
const result = await scriptService.executeNote(req, note);
2018-04-02 00:03:21 +08:00
return { executionResult: result };
}
2018-04-05 11:51:47 +08:00
async function getStartupBundles() {
const notes = await labelService.getNotesWithLabel("run", "frontendStartup");
const bundles = [];
2018-03-05 03:21:11 +08:00
for (const note of notes) {
const bundle = await scriptService.getScriptBundle(note);
2018-03-03 22:11:41 +08:00
if (bundle) {
bundles.push(bundle);
}
}
return bundles;
}
async function getRelationBundles(req) {
const noteId = req.params.noteId;
const relationName = req.params.relationName;
const relations = await relationService.getEffectiveRelations(noteId);
const filtered = relations.filter(relation => relation.name === relationName);
const targetNoteIds = filtered.map(relation => relation.targetNoteId);
const uniqueNoteIds = Array.from(new Set(targetNoteIds));
const bundles = [];
for (const noteId of uniqueNoteIds) {
bundles.push(await scriptService.getScriptBundleForNoteId(noteId));
}
return bundles;
}
async function getBundle(req) {
return await scriptService.getScriptBundleForNoteId(req.params.noteId);
}
module.exports = {
exec,
run,
getStartupBundles,
getRelationBundles,
getBundle
};