mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
support for backend jobs and other script API changes
This commit is contained in:
parent
5dd93e4cdc
commit
a555b6319c
@ -43,6 +43,48 @@ class Note extends Entity {
|
|||||||
return this.repository.getEntities("SELECT * FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
|
return this.repository.getEntities("SELECT * FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getChild(name) {
|
||||||
|
return this.repository.getEntity(`
|
||||||
|
SELECT notes.*
|
||||||
|
FROM note_tree
|
||||||
|
JOIN notes USING(noteId)
|
||||||
|
WHERE notes.isDeleted = 0
|
||||||
|
AND note_tree.isDeleted = 0
|
||||||
|
AND note_tree.parentNoteId = ?
|
||||||
|
AND notes.title = ?`, [this.noteId, name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getChildren() {
|
||||||
|
return this.repository.getEntities(`
|
||||||
|
SELECT notes.*
|
||||||
|
FROM note_tree
|
||||||
|
JOIN notes USING(noteId)
|
||||||
|
WHERE notes.isDeleted = 0
|
||||||
|
AND note_tree.isDeleted = 0
|
||||||
|
AND note_tree.parentNoteId = ?`, [this.noteId]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getParents() {
|
||||||
|
return this.repository.getEntities(`
|
||||||
|
SELECT parent_notes.*
|
||||||
|
FROM
|
||||||
|
note_tree AS child_tree
|
||||||
|
JOIN notes AS parent_notes ON parent_notes.noteId = child_tree.parentNoteId
|
||||||
|
WHERE child_tree.noteId = ?
|
||||||
|
AND child_tree.isDeleted = 0
|
||||||
|
AND parent_notes.isDeleted = 0`, [this.noteId]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getNoteTree() {
|
||||||
|
return this.repository.getEntities(`
|
||||||
|
SELECT note_tree.*
|
||||||
|
FROM note_tree
|
||||||
|
JOIN notes USING(noteId)
|
||||||
|
WHERE notes.isDeleted = 0
|
||||||
|
AND note_tree.isDeleted = 0
|
||||||
|
AND note_tree.noteId = ?`, [this.noteId]);
|
||||||
|
}
|
||||||
|
|
||||||
beforeSaving() {
|
beforeSaving() {
|
||||||
this.content = JSON.stringify(this.jsonContent, null, '\t');
|
this.content = JSON.stringify(this.jsonContent, null, '\t');
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ karma: ${comment.score}, created at ${dateTimeStr}</p><p></p>`
|
|||||||
let redditAccounts = [];
|
let redditAccounts = [];
|
||||||
|
|
||||||
async function runImport() {
|
async function runImport() {
|
||||||
const rootNoteId = await date_notes.getRootNoteId();
|
const rootNoteId = await date_notes.getRootCalendarNoteId();
|
||||||
|
|
||||||
// technically mutex shouldn't be necessary but we want to avoid doing potentially expensive import
|
// technically mutex shouldn't be necessary but we want to avoid doing potentially expensive import
|
||||||
// concurrently with sync
|
// concurrently with sync
|
||||||
|
@ -174,6 +174,8 @@ const noteEditor = (function() {
|
|||||||
codeEditor.setOption("mode", info.mime);
|
codeEditor.setOption("mode", info.mime);
|
||||||
CodeMirror.autoLoadMode(codeEditor, info.mode);
|
CodeMirror.autoLoadMode(codeEditor, info.mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
codeEditor.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,10 @@ const server = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function prepareParams(params) {
|
function prepareParams(params) {
|
||||||
|
if (!params) {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
return params.map(p => {
|
return params.map(p => {
|
||||||
if (typeof p === "function") {
|
if (typeof p === "function") {
|
||||||
return "!@#Function: " + p.toString();
|
return "!@#Function: " + p.toString();
|
||||||
@ -52,6 +56,13 @@ const server = (function() {
|
|||||||
return ret.executionResult;
|
return ret.executionResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setJob(opts) {
|
||||||
|
opts.job = opts.job.toString();
|
||||||
|
opts.params = prepareParams(opts.params);
|
||||||
|
|
||||||
|
await post('script/job', opts);
|
||||||
|
}
|
||||||
|
|
||||||
let i = 1;
|
let i = 1;
|
||||||
const reqResolves = {};
|
const reqResolves = {};
|
||||||
|
|
||||||
@ -116,6 +127,7 @@ const server = (function() {
|
|||||||
put,
|
put,
|
||||||
remove,
|
remove,
|
||||||
exec,
|
exec,
|
||||||
|
setJob,
|
||||||
ajax,
|
ajax,
|
||||||
// don't remove, used from CKEditor image upload!
|
// don't remove, used from CKEditor image upload!
|
||||||
getHeaders
|
getHeaders
|
||||||
|
@ -17,6 +17,12 @@ router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
router.post('/job', auth.checkApiAuth, wrap(async (req, res, next) => {
|
||||||
|
await script.setJob(req.body);
|
||||||
|
|
||||||
|
res.send({});
|
||||||
|
}));
|
||||||
|
|
||||||
router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
|
router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
|
||||||
const noteIds = await attributes.getNoteIdsWithAttribute("run_on_startup");
|
const noteIds = await attributes.getNoteIdsWithAttribute("run_on_startup");
|
||||||
const repository = new Repository(req);
|
const repository = new Repository(req);
|
||||||
|
@ -29,7 +29,7 @@ async function getNoteStartingWith(parentNoteId, startsWith) {
|
|||||||
AND note_tree.isDeleted = 0`, [parentNoteId]);
|
AND note_tree.isDeleted = 0`, [parentNoteId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getRootNoteId() {
|
async function getRootCalendarNoteId() {
|
||||||
let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
|
let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
|
||||||
WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`);
|
WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`);
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {
|
|||||||
|
|
||||||
async function getDateNoteId(dateTimeStr, rootNoteId = null) {
|
async function getDateNoteId(dateTimeStr, rootNoteId = null) {
|
||||||
if (!rootNoteId) {
|
if (!rootNoteId) {
|
||||||
rootNoteId = await getRootNoteId();
|
rootNoteId = await getRootCalendarNoteId();
|
||||||
}
|
}
|
||||||
|
|
||||||
const dateStr = dateTimeStr.substr(0, 10);
|
const dateStr = dateTimeStr.substr(0, 10);
|
||||||
@ -119,7 +119,7 @@ async function getDateNoteId(dateTimeStr, rootNoteId = null) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getRootNoteId,
|
getRootCalendarNoteId,
|
||||||
getYearNoteId,
|
getYearNoteId,
|
||||||
getMonthNoteId,
|
getMonthNoteId,
|
||||||
getDateNoteId
|
getDateNoteId
|
||||||
|
@ -18,7 +18,40 @@ async function executeScript(dataKey, script, params) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timeouts = {};
|
||||||
|
const intervals = {};
|
||||||
|
|
||||||
|
function clearExistingJob(name) {
|
||||||
|
if (timeouts[name]) {
|
||||||
|
clearTimeout(timeouts[name]);
|
||||||
|
|
||||||
|
delete timeouts[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (intervals[name]) {
|
||||||
|
clearInterval(intervals[name]);
|
||||||
|
|
||||||
|
delete intervals[name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setJob(opts) {
|
||||||
|
clearExistingJob(opts.name);
|
||||||
|
|
||||||
|
if (opts.runEveryMs && opts.runEveryMs > 0) {
|
||||||
|
intervals[opts.name] = setInterval(() => executeScript(null, opts.job, opts.params), opts.runEveryMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.initialRunAfterMs && opts.initialRunAfterMs > 0) {
|
||||||
|
timeouts[opts.name] = setTimeout(() => executeScript(null, opts.job, opts.params), opts.initialRunAfterMs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getParams(params) {
|
function getParams(params) {
|
||||||
|
if (!params) {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
return params.map(p => {
|
return params.map(p => {
|
||||||
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
|
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
|
||||||
return p.substr(13);
|
return p.substr(13);
|
||||||
@ -30,5 +63,6 @@ function getParams(params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
executeScript
|
executeScript,
|
||||||
|
setJob
|
||||||
};
|
};
|
@ -23,10 +23,10 @@ function ScriptContext(noteId, dataKey) {
|
|||||||
return notes.length > 0 ? notes[0] : null;
|
return notes.length > 0 ? notes[0] : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.createNote = async function (parentNoteId, name, jsonContent, extraOptions = {}) {
|
this.createNote = async function (parentNoteId, title, content = "", extraOptions = {}) {
|
||||||
const note = {
|
const note = {
|
||||||
title: name,
|
title: title,
|
||||||
content: extraOptions.json ? JSON.stringify(jsonContent, null, '\t') : jsonContent,
|
content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
|
||||||
target: 'into',
|
target: 'into',
|
||||||
isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false,
|
isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false,
|
||||||
type: extraOptions.type,
|
type: extraOptions.type,
|
||||||
@ -58,10 +58,9 @@ function ScriptContext(noteId, dataKey) {
|
|||||||
|
|
||||||
this.updateEntity = this.repository.updateEntity;
|
this.updateEntity = this.repository.updateEntity;
|
||||||
|
|
||||||
this.log = function(message) {
|
this.log = message => log.info(`Script: ${message}`);
|
||||||
log.info(`Script: ${message}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;
|
||||||
this.getDateNoteId = date_notes.getDateNoteId;
|
this.getDateNoteId = date_notes.getDateNoteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user