trilium/src/routes/api/tree_changes.js

123 lines
4.9 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.
*/
router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteTreeId = req.params.noteTreeId;
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
const noteToMove = await tree.getNoteTree(noteTreeId);
2018-01-29 08:30:14 +08:00
if (!await tree.validateParentChild(res, parentNoteId, noteToMove.noteId, noteTreeId)) {
2018-01-14 07:02:41 +08:00
return;
}
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree 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 () => {
await sql.execute("UPDATE note_tree SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE noteTreeId = ?",
2018-01-14 07:02:41 +08:00
[parentNoteId, newNotePos, now, noteTreeId]);
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
});
res.send({ success: true });
}));
router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteTreeId = req.params.noteTreeId;
const beforeNoteTreeId = req.params.beforeNoteTreeId;
2018-01-29 10:57:46 +08:00
const sourceId = req.headers.source_id;
2018-01-14 07:02:41 +08:00
const noteToMove = await tree.getNoteTree(noteTreeId);
const beforeNote = await tree.getNoteTree(beforeNoteTreeId);
2018-01-29 08:30:14 +08:00
if (!await tree.validateParentChild(res, beforeNote.parentNoteId, noteToMove.noteId, noteTreeId)) {
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
await sql.execute("UPDATE note_tree 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
await sql.execute("UPDATE note_tree SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE noteTreeId = ?",
[beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), noteTreeId]);
2018-01-14 07:02:41 +08:00
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
});
res.send({ success: true });
}));
router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteTreeId = req.params.noteTreeId;
const afterNoteTreeId = req.params.afterNoteTreeId;
2018-01-29 10:57:46 +08:00
const sourceId = req.headers.source_id;
2018-01-14 07:02:41 +08:00
const noteToMove = await tree.getNoteTree(noteTreeId);
const afterNote = await tree.getNoteTree(afterNoteTreeId);
2018-01-29 08:30:14 +08:00
if (!await tree.validateParentChild(res, afterNote.parentNoteId, noteToMove.noteId, noteTreeId)) {
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
await sql.execute("UPDATE note_tree 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
await sql.execute("UPDATE note_tree SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE noteTreeId = ?",
2018-01-29 08:30:14 +08:00
[afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), noteTreeId]);
2018-01-14 07:02:41 +08:00
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
});
res.send({ success: true });
}));
router.put('/:noteTreeId/expanded/:expanded', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteTreeId = req.params.noteTreeId;
const expanded = req.params.expanded;
await sql.doInTransaction(async () => {
await sql.execute("UPDATE note_tree SET isExpanded = ? WHERE noteTreeId = ?", [expanded, noteTreeId]);
2018-01-14 07:02:41 +08:00
// we don't sync expanded attribute
});
res.send({});
}));
router.delete('/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
await sql.doInTransaction(async () => {
2018-01-29 10:57:46 +08:00
await notes.deleteNote(req.params.noteTreeId, req.headers.source_id);
2018-01-14 07:02:41 +08:00
});
res.send({});
}));
module.exports = router;