mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
converted cloning and label routes
This commit is contained in:
parent
efffc29649
commit
8550ed72f2
@ -1,4 +1,5 @@
|
|||||||
import treeService from './tree.js';
|
import treeService from './tree.js';
|
||||||
|
import server from './server.js';
|
||||||
|
|
||||||
async function cloneNoteTo(childNoteId, parentNoteId, prefix) {
|
async function cloneNoteTo(childNoteId, parentNoteId, prefix) {
|
||||||
const resp = await server.put('notes/' + childNoteId + '/clone-to/' + parentNoteId, {
|
const resp = await server.put('notes/' + childNoteId + '/clone-to/' + parentNoteId, {
|
||||||
|
@ -1,28 +1,25 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const express = require('express');
|
|
||||||
const router = express.Router();
|
|
||||||
const sql = require('../../services/sql');
|
const sql = require('../../services/sql');
|
||||||
const auth = require('../../services/auth');
|
|
||||||
const utils = require('../../services/utils');
|
const utils = require('../../services/utils');
|
||||||
const sync_table = require('../../services/sync_table');
|
const sync_table = require('../../services/sync_table');
|
||||||
const wrap = require('express-promise-wrap').wrap;
|
|
||||||
const tree = require('../../services/tree');
|
const tree = require('../../services/tree');
|
||||||
|
|
||||||
router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
async function cloneNoteToParent(req) {
|
||||||
const parentNoteId = req.params.parentNoteId;
|
const parentNoteId = req.params.parentNoteId;
|
||||||
const childNoteId = req.params.childNoteId;
|
const childNoteId = req.params.childNoteId;
|
||||||
const prefix = req.body.prefix;
|
const prefix = req.body.prefix;
|
||||||
const sourceId = req.headers.source_id;
|
const sourceId = req.headers.source_id;
|
||||||
|
|
||||||
if (!await tree.validateParentChild(res, parentNoteId, childNoteId)) {
|
const validationResult = await tree.validateParentChild(parentNoteId, childNoteId);
|
||||||
return;
|
|
||||||
|
if (!validationResult.success) {
|
||||||
|
return validationResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
||||||
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
|
||||||
const branch = {
|
const branch = {
|
||||||
branchId: utils.newBranchId(),
|
branchId: utils.newBranchId(),
|
||||||
noteId: childNoteId,
|
noteId: childNoteId,
|
||||||
@ -39,23 +36,23 @@ router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async
|
|||||||
await sync_table.addBranchSync(branch.branchId, sourceId);
|
await sync_table.addBranchSync(branch.branchId, sourceId);
|
||||||
|
|
||||||
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
||||||
});
|
|
||||||
|
|
||||||
res.send({ success: true });
|
return { success: true };
|
||||||
}));
|
}
|
||||||
|
|
||||||
router.put('/:noteId/clone-after/:afterBranchId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
async function cloneNoteAfter(req) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const afterBranchId = req.params.afterBranchId;
|
const afterBranchId = req.params.afterBranchId;
|
||||||
const sourceId = req.headers.source_id;
|
const sourceId = req.headers.source_id;
|
||||||
|
|
||||||
const afterNote = await tree.getBranch(afterBranchId);
|
const afterNote = await tree.getBranch(afterBranchId);
|
||||||
|
|
||||||
if (!await tree.validateParentChild(res, afterNote.parentNoteId, noteId)) {
|
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteId);
|
||||||
return;
|
|
||||||
|
if (!validationResult.result) {
|
||||||
|
return validationResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
|
||||||
// we don't change dateModified so other changes are prioritized in case of conflict
|
// we don't change dateModified so other changes are prioritized in case of conflict
|
||||||
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
||||||
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
||||||
@ -76,9 +73,11 @@ router.put('/:noteId/clone-after/:afterBranchId', auth.checkApiAuth, wrap(async
|
|||||||
await sql.replace("branches", branch);
|
await sql.replace("branches", branch);
|
||||||
|
|
||||||
await sync_table.addBranchSync(branch.branchId, sourceId);
|
await sync_table.addBranchSync(branch.branchId, sourceId);
|
||||||
});
|
|
||||||
|
|
||||||
res.send({ success: true });
|
return { success: true };
|
||||||
}));
|
}
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = {
|
||||||
|
cloneNoteToParent,
|
||||||
|
cloneNoteAfter
|
||||||
|
};
|
@ -1,26 +1,21 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const express = require('express');
|
|
||||||
const router = express.Router();
|
|
||||||
const sql = require('../../services/sql');
|
const sql = require('../../services/sql');
|
||||||
const auth = require('../../services/auth');
|
|
||||||
const sync_table = require('../../services/sync_table');
|
const sync_table = require('../../services/sync_table');
|
||||||
const utils = require('../../services/utils');
|
const utils = require('../../services/utils');
|
||||||
const wrap = require('express-promise-wrap').wrap;
|
|
||||||
const labels = require('../../services/labels');
|
const labels = require('../../services/labels');
|
||||||
|
|
||||||
router.get('/notes/:noteId/labels', auth.checkApiAuth, wrap(async (req, res, next) => {
|
async function getNoteLabels(req) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
|
|
||||||
res.send(await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]));
|
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||||
}));
|
}
|
||||||
|
|
||||||
router.put('/notes/:noteId/labels', auth.checkApiAuth, wrap(async (req, res, next) => {
|
async function updateNoteLabels(req, res, next) {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const labels = req.body;
|
const labels = req.body;
|
||||||
const now = utils.nowDate();
|
const now = utils.nowDate();
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
|
||||||
for (const attr of labels) {
|
for (const attr of labels) {
|
||||||
if (attr.labelId) {
|
if (attr.labelId) {
|
||||||
await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
|
await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
|
||||||
@ -48,12 +43,11 @@ router.put('/notes/:noteId/labels', auth.checkApiAuth, wrap(async (req, res, nex
|
|||||||
|
|
||||||
await sync_table.addLabelSync(attr.labelId);
|
await sync_table.addLabelSync(attr.labelId);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
res.send(await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]));
|
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||||
}));
|
}
|
||||||
|
|
||||||
router.get('/labels/names', auth.checkApiAuth, wrap(async (req, res, next) => {
|
async function getAllLabelNames(req) {
|
||||||
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
|
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
|
||||||
|
|
||||||
for (const attr of labels.BUILTIN_LABELS) {
|
for (const attr of labels.BUILTIN_LABELS) {
|
||||||
@ -64,15 +58,18 @@ router.get('/labels/names', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|||||||
|
|
||||||
names.sort();
|
names.sort();
|
||||||
|
|
||||||
res.send(names);
|
return names;
|
||||||
}));
|
}
|
||||||
|
|
||||||
router.get('/labels/values/:labelName', auth.checkApiAuth, wrap(async (req, res, next) => {
|
async function getValuesForLabel(req) {
|
||||||
const labelName = req.params.labelName;
|
const labelName = req.params.labelName;
|
||||||
|
|
||||||
const values = await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]);
|
return await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]);
|
||||||
|
}
|
||||||
|
|
||||||
res.send(values);
|
module.exports = {
|
||||||
}));
|
getNoteLabels,
|
||||||
|
updateNoteLabels,
|
||||||
module.exports = router;
|
getAllLabelNames,
|
||||||
|
getValuesForLabel
|
||||||
|
};
|
@ -1,14 +1,10 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const express = require('express');
|
|
||||||
const router = express.Router();
|
|
||||||
const sql = require('../../services/sql');
|
const sql = require('../../services/sql');
|
||||||
const auth = require('../../services/auth');
|
|
||||||
const utils = require('../../services/utils');
|
const utils = require('../../services/utils');
|
||||||
const sync_table = require('../../services/sync_table');
|
const sync_table = require('../../services/sync_table');
|
||||||
const tree = require('../../services/tree');
|
const tree = require('../../services/tree');
|
||||||
const notes = require('../../services/notes');
|
const notes = require('../../services/notes');
|
||||||
const wrap = require('express-promise-wrap').wrap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique
|
* Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique
|
||||||
|
@ -32,6 +32,7 @@ const senderRoute = require('./api/sender');
|
|||||||
const filesRoute = require('./api/file_upload');
|
const filesRoute = require('./api/file_upload');
|
||||||
const searchRoute = require('./api/search');
|
const searchRoute = require('./api/search');
|
||||||
|
|
||||||
|
const log = require('../services/log');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const auth = require('../services/auth');
|
const auth = require('../services/auth');
|
||||||
@ -41,20 +42,29 @@ const sql = require('../services/sql');
|
|||||||
function apiRoute(method, path, handler) {
|
function apiRoute(method, path, handler) {
|
||||||
router[method](path, auth.checkApiAuth, async (req, res, next) => {
|
router[method](path, auth.checkApiAuth, async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const resp = await cls.init(async () => {
|
const result = await cls.init(async () => {
|
||||||
|
cls.namespace.set('sourceId', req.headers.source_id);
|
||||||
|
|
||||||
return await sql.doInTransaction(async () => {
|
return await sql.doInTransaction(async () => {
|
||||||
return await handler(req, res, next);
|
return await handler(req, res, next);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Array.isArray(resp)) {
|
// if it's an array and first element is integer then we consider this to be [statusCode, response] format
|
||||||
res.status(resp[0]).send(resp[1]);
|
if (Array.isArray(result) && result.length > 0 && Number.isInteger(result[0])) {
|
||||||
|
const [statusCode, response] = result;
|
||||||
|
|
||||||
|
res.status(statusCode).send(response);
|
||||||
|
|
||||||
|
if (statusCode !== 200) {
|
||||||
|
log.info(`${method} ${path} returned ${statusCode} with response ${JSON.stringify(response)}`);
|
||||||
}
|
}
|
||||||
else if (resp === undefined) {
|
}
|
||||||
|
else if (result === undefined) {
|
||||||
res.status(200);
|
res.status(200);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
res.status(200).send(resp);
|
res.status(200).send(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
@ -88,8 +98,14 @@ function register(app) {
|
|||||||
apiRoute(PUT, '/api/notes/:noteId/protect-sub-tree/:isProtected', notesApiRoute.protectBranch);
|
apiRoute(PUT, '/api/notes/:noteId/protect-sub-tree/:isProtected', notesApiRoute.protectBranch);
|
||||||
apiRoute(PUT, /\/api\/notes\/(.*)\/type\/(.*)\/mime\/(.*)/, notesApiRoute.setNoteTypeMime);
|
apiRoute(PUT, /\/api\/notes\/(.*)\/type\/(.*)\/mime\/(.*)/, notesApiRoute.setNoteTypeMime);
|
||||||
|
|
||||||
app.use('/api/notes', cloningApiRoute);
|
apiRoute(PUT, '/api/notes/:childNoteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent);
|
||||||
app.use('/api', labelsRoute);
|
apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter);
|
||||||
|
|
||||||
|
apiRoute(GET, '/api/notes/:noteId/labels', labelsRoute.getNoteLabels);
|
||||||
|
apiRoute(PUT, '/api/notes/:noteId/labels', labelsRoute.updateNoteLabels);
|
||||||
|
apiRoute(GET, '/api/labels/names', labelsRoute.getAllLabelNames);
|
||||||
|
apiRoute(GET, '/api/labels/values/:labelName', labelsRoute.getValuesForLabel);
|
||||||
|
|
||||||
app.use('/api/notes-revisions', noteRevisionsApiRoute);
|
app.use('/api/notes-revisions', noteRevisionsApiRoute);
|
||||||
app.use('/api/recent-changes', recentChangesApiRoute);
|
app.use('/api/recent-changes', recentChangesApiRoute);
|
||||||
app.use('/api/settings', settingsApiRoute);
|
app.use('/api/settings', settingsApiRoute);
|
||||||
|
@ -9,8 +9,13 @@ function wrap(callback) {
|
|||||||
return async () => await init(callback);
|
return async () => await init(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSourceId() {
|
||||||
|
return namespace.get('sourceId');
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init,
|
init,
|
||||||
wrap,
|
wrap,
|
||||||
namespace
|
namespace,
|
||||||
|
getSourceId
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user