trilium/src/routes/api/tree.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
2017-10-16 07:47:05 +08:00
const sql = require('../../services/sql');
2017-11-03 08:48:02 +08:00
const options = require('../../services/options');
2017-10-16 07:47:05 +08:00
const utils = require('../../services/utils');
const config = require('../../services/config');
2017-11-11 11:55:19 +08:00
const protected_session = require('../../services/protected_session');
2017-11-20 05:35:35 +08:00
const sync_table = require('../../services/sync_table');
2018-03-31 00:57:22 +08:00
async function getTree(req) {
2018-03-25 09:39:15 +08:00
const branches = await sql.getRows(`
SELECT
2018-03-25 09:39:15 +08:00
branchId,
noteId,
parentNoteId,
notePosition,
prefix,
isExpanded
FROM
2018-03-25 09:39:15 +08:00
branches
WHERE
isDeleted = 0
ORDER BY
2018-01-29 08:30:14 +08:00
notePosition`);
2018-03-31 00:57:22 +08:00
const notes = [{
noteId: 'root',
title: 'root',
isProtected: false,
type: 'none',
mime: 'none'
2018-03-31 00:57:22 +08:00
}].concat(await sql.getRows(`
SELECT
notes.noteId,
notes.title,
notes.isProtected,
notes.type,
notes.mime,
hideInAutocomplete.labelId AS 'hideInAutocomplete'
FROM
notes
LEFT JOIN labels AS hideInAutocomplete ON hideInAutocomplete.noteId = notes.noteId
AND hideInAutocomplete.name = 'hide_in_autocomplete'
AND hideInAutocomplete.isDeleted = 0
WHERE
notes.isDeleted = 0`));
protected_session.decryptNotes(notes);
notes.forEach(note => {
note.hideInAutocomplete = !!note.hideInAutocomplete;
note.isProtected = !!note.isProtected;
});
2018-03-31 00:57:22 +08:00
return {
instanceName: config.General ? config.General.instanceName : null,
2018-03-25 09:39:15 +08:00
branches: branches,
notes: notes,
2018-01-29 09:52:05 +08:00
start_note_path: await options.getOption('start_note_path')
2018-03-31 00:57:22 +08:00
};
}
2018-03-31 00:57:22 +08:00
async function setPrefix(req) {
2018-03-25 09:39:15 +08:00
const branchId = req.params.branchId;
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
2017-11-27 11:34:25 +08:00
await sql.doInTransaction(async () => {
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]);
2017-11-27 11:34:25 +08:00
await sync_table.addBranchSync(branchId);
2017-11-27 11:34:25 +08:00
});
2018-03-31 00:57:22 +08:00
return {};
}
2017-11-27 11:34:25 +08:00
2018-03-31 00:57:22 +08:00
module.exports = {
getTree,
setPrefix
};