trilium/src/routes/api/tree_changes.js

123 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-01-14 07:02:41 +08:00
"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const auth = require('../../services/auth');
const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table');
const tree = require('../../services/tree');
const notes = require('../../services/notes');
2018-01-14 07:02:41 +08:00
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
* for not deleted note trees. There may be multiple deleted note-parent note relationships.
*/
2018-03-25 09:39:15 +08:00
router.put('/:branchId/move-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const branchId = req.params.branchId;
2018-01-14 07:02:41 +08:00
const parentNoteId = req.params.parentNoteId;
2018-01-29 10:57:46 +08:00
const sourceId = req.headers.source_id;
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
const noteToMove = await tree.getBranch(branchId);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
if (!await tree.validateParentChild(res, parentNoteId, noteToMove.noteId, branchId)) {
2018-01-14 07:02:41 +08:00
return;
}
2018-03-25 09:39:15 +08:00
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
2018-01-14 07:02:41 +08:00
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const now = utils.nowDate();
await sql.doInTransaction(async () => {
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[parentNoteId, newNotePos, now, branchId]);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
await sync_table.addBranchSync(branchId, sourceId);
2018-01-14 07:02:41 +08:00
});
res.send({ success: true });
}));
2018-03-25 09:39:15 +08:00
router.put('/:branchId/move-before/:beforeBranchId', auth.checkApiAuth, wrap(async (req, res, next) => {
const branchId = req.params.branchId;
const beforeBranchId = req.params.beforeBranchId;
2018-01-29 10:57:46 +08:00
const sourceId = req.headers.source_id;
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
const noteToMove = await tree.getBranch(branchId);
const beforeNote = await tree.getBranch(beforeBranchId);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
if (!await tree.validateParentChild(res, beforeNote.parentNoteId, noteToMove.noteId, branchId)) {
2018-01-14 07:02:41 +08:00
return;
}
await sql.doInTransaction(async () => {
2018-01-29 08:30:14 +08:00
// we don't change dateModified so other changes are prioritized in case of conflict
2018-01-14 07:02:41 +08:00
// also we would have to sync all those modified note trees otherwise hash checks would fail
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
2018-01-29 08:30:14 +08:00
[beforeNote.parentNoteId, beforeNote.notePosition]);
2018-01-14 07:02:41 +08:00
2018-01-29 08:30:14 +08:00
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId, sourceId);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
await sync_table.addBranchSync(branchId, sourceId);
2018-01-14 07:02:41 +08:00
});
res.send({ success: true });
}));
2018-03-25 09:39:15 +08:00
router.put('/:branchId/move-after/:afterBranchId', auth.checkApiAuth, wrap(async (req, res, next) => {
const branchId = req.params.branchId;
const afterBranchId = req.params.afterBranchId;
2018-01-29 10:57:46 +08:00
const sourceId = req.headers.source_id;
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
const noteToMove = await tree.getBranch(branchId);
const afterNote = await tree.getBranch(afterBranchId);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
if (!await tree.validateParentChild(res, afterNote.parentNoteId, noteToMove.noteId, branchId)) {
2018-01-14 07:02:41 +08:00
return;
}
await sql.doInTransaction(async () => {
2018-01-29 08:30:14 +08:00
// we don't change dateModified so other changes are prioritized in case of conflict
2018-01-14 07:02:41 +08:00
// also we would have to sync all those modified note trees otherwise hash checks would fail
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
2018-01-29 08:30:14 +08:00
[afterNote.parentNoteId, afterNote.notePosition]);
2018-01-14 07:02:41 +08:00
2018-01-29 08:30:14 +08:00
await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
await sync_table.addBranchSync(branchId, sourceId);
2018-01-14 07:02:41 +08:00
});
res.send({ success: true });
}));
2018-03-25 09:39:15 +08:00
router.put('/:branchId/expanded/:expanded', auth.checkApiAuth, wrap(async (req, res, next) => {
const branchId = req.params.branchId;
2018-01-14 07:02:41 +08:00
const expanded = req.params.expanded;
await sql.doInTransaction(async () => {
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
2018-01-14 07:02:41 +08:00
// we don't sync expanded label
2018-01-14 07:02:41 +08:00
});
res.send({});
}));
2018-03-25 09:39:15 +08:00
router.delete('/:branchId', auth.checkApiAuth, wrap(async (req, res, next) => {
2018-01-14 07:02:41 +08:00
await sql.doInTransaction(async () => {
2018-03-25 09:39:15 +08:00
await notes.deleteNote(req.params.branchId, req.headers.source_id);
2018-01-14 07:02:41 +08:00
});
res.send({});
}));
module.exports = router;