trilium/routes/api/notes_move.js

100 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
const express = require('express');
const router = express.Router();
2017-10-16 07:47:05 +08:00
const sql = require('../../services/sql');
const utils = require('../../services/utils');
const audit_category = require('../../services/audit_category');
const auth = require('../../services/auth');
const sync_table = require('../../services/sync_table');
2017-10-16 04:32:49 +08:00
router.put('/:noteId/moveTo/:parentId', auth.checkApiAuth, async (req, res, next) => {
let noteId = req.params.noteId;
let parentId = req.params.parentId;
2017-10-25 10:58:59 +08:00
const row = await sql.getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ? and is_deleted = 0', [parentId]);
2017-11-17 11:18:25 +08:00
const maxNotePos = row.max_note_pos;
let newNotePos = 0;
if (maxNotePos === null) // no children yet
newNotePos = 0;
else
newNotePos = maxNotePos + 1;
2017-10-23 10:56:42 +08:00
const now = utils.nowTimestamp();
await sql.doInTransaction(async () => {
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?",
[parentId, newNotePos, now, noteId]);
await sync_table.addNoteTreeSync(noteId);
await sql.addAudit(audit_category.CHANGE_PARENT, utils.browserId(req), noteId, null, parentId);
});
res.send({});
});
router.put('/:noteId/moveBefore/:beforeNoteId', async (req, res, next) => {
let noteId = req.params.noteId;
let beforeNoteId = req.params.beforeNoteId;
const beforeNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [beforeNoteId]);
2017-10-23 10:56:42 +08:00
if (beforeNote) {
await sql.doInTransaction(async () => {
2017-11-03 10:55:22 +08:00
// we don't change date_modified so other changes are prioritized in case of conflict
await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos >= ? and is_deleted = 0",
2017-11-17 11:18:25 +08:00
[beforeNote.note_pid, beforeNote.note_pos]);
const now = utils.nowTimestamp();
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?",
2017-11-17 11:18:25 +08:00
[beforeNote.note_pid, beforeNote.note_pos, now, noteId]);
await sync_table.addNoteTreeSync(noteId);
2017-11-17 11:18:25 +08:00
await sync_table.addNoteReorderingSync(beforeNote.note_pid);
await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), beforeNote.note_pid);
});
}
res.send({});
});
router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => {
let noteId = req.params.noteId;
let afterNoteId = req.params.afterNoteId;
const afterNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [afterNoteId]);
2017-10-23 10:56:42 +08:00
if (afterNote) {
await sql.doInTransaction(async () => {
2017-11-03 10:55:22 +08:00
// we don't change date_modified so other changes are prioritized in case of conflict
await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ? and is_deleted = 0",
2017-11-17 11:18:25 +08:00
[afterNote.note_pid, afterNote.note_pos]);
const now = utils.nowTimestamp();
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?",
2017-11-17 11:18:25 +08:00
[afterNote.note_pid, afterNote.note_pos + 1, now, noteId]);
await sync_table.addNoteTreeSync(noteId);
2017-11-17 11:18:25 +08:00
await sync_table.addNoteReorderingSync(afterNote.note_pid);
await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), afterNote.note_pid);
});
}
res.send({});
});
router.put('/:noteId/expanded/:expanded', async (req, res, next) => {
2017-10-23 10:56:42 +08:00
const noteId = req.params.noteId;
const expanded = req.params.expanded;
await sql.doInTransaction(async () => {
await sql.execute("update notes_tree set is_expanded = ? where note_id = ?", [expanded, noteId]);
});
res.send({});
});
module.exports = router;