trilium/services/notes.js

219 lines
8.3 KiB
JavaScript
Raw Normal View History

const sql = require('./sql');
const options = require('./options');
const utils = require('./utils');
const notes = require('./notes');
const data_encryption = require('./data_encryption');
const sync_table = require('./sync_table');
2017-11-25 09:51:44 +08:00
async function createNewNote(parentNoteId, note) {
const noteId = utils.newNoteId();
2017-11-19 21:47:22 +08:00
const noteTreeId = utils.newNoteTreeId();
let newNotePos = 0;
await sql.doInTransaction(async () => {
if (note.target === 'into') {
const maxNotePos = await sql.getSingleValue('SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0', [parentNoteId]);
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
}
else if (note.target === 'after') {
const afterNote = await sql.getSingleResult('SELECT note_pos FROM notes_tree WHERE note_tree_id = ?', [note.target_note_tree_id]);
newNotePos = afterNote.note_pos + 1;
await sql.execute('UPDATE notes_tree SET note_pos = note_pos + 1, date_modified = ? WHERE note_pid = ? AND note_pos > ? AND is_deleted = 0',
[utils.nowDate(), parentNoteId, afterNote.note_pos]);
2017-11-30 10:04:30 +08:00
await sync_table.addNoteReorderingSync(parentNoteId);
}
else {
2017-12-07 09:58:59 +08:00
throw new Error('Unknown target: ' + note.target);
}
const now = utils.nowDate();
await sql.insert("notes", {
note_id: noteId,
note_title: note.note_title,
note_text: '',
date_created: now,
date_modified: now,
is_protected: note.is_protected
});
2017-11-30 10:04:30 +08:00
await sync_table.addNoteSync(noteId);
await sql.insert("notes_tree", {
note_tree_id: noteTreeId,
note_id: noteId,
note_pid: parentNoteId,
note_pos: newNotePos,
is_expanded: 0,
date_modified: now,
is_deleted: 0
});
2017-11-30 10:04:30 +08:00
await sync_table.addNoteTreeSync(noteTreeId);
});
return {
noteId,
noteTreeId
};
}
async function encryptNote(note, ctx) {
note.detail.note_title = data_encryption.encrypt(ctx.getDataKey(), data_encryption.noteTitleIv(note.detail.note_id), note.detail.note_title);
note.detail.note_text = data_encryption.encrypt(ctx.getDataKey(), data_encryption.noteTextIv(note.detail.note_id), note.detail.note_text);
}
async function protectNoteRecursively(noteId, dataKey, protect) {
2017-11-15 13:04:26 +08:00
const note = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
await protectNote(note, dataKey, protect);
2017-11-15 13:04:26 +08:00
const children = await sql.getFlattenedResults("note_id", "SELECT note_id FROM notes_tree WHERE note_pid = ?", [noteId]);
for (const childNoteId of children) {
await protectNoteRecursively(childNoteId, dataKey, protect);
}
}
2017-12-07 12:39:43 +08:00
function decryptNote(note, dataKey) {
note.note_title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title);
note.note_text = data_encryption.decryptString(dataKey, data_encryption.noteTextIv(note.note_id), note.note_text);
note.is_protected = false;
}
async function protectNote(note, dataKey, protect) {
2017-11-15 13:04:26 +08:00
let changed = false;
if (protect && !note.is_protected) {
note.note_title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(note.note_id), note.note_title);
note.note_text = data_encryption.encrypt(dataKey, data_encryption.noteTextIv(note.note_id), note.note_text);
2017-11-15 13:04:26 +08:00
note.is_protected = true;
changed = true;
}
else if (!protect && note.is_protected) {
2017-12-07 12:39:43 +08:00
decryptNote(note, dataKey);
2017-11-15 13:04:26 +08:00
changed = true;
}
if (changed) {
console.log("Updating...");
await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ? WHERE note_id = ?",
2017-11-15 13:04:26 +08:00
[note.note_title, note.note_text, note.is_protected, note.note_id]);
await sync_table.addNoteSync(note.note_id);
2017-11-15 13:04:26 +08:00
}
await protectNoteHistory(note.note_id, dataKey, protect);
2017-11-15 13:04:26 +08:00
}
async function protectNoteHistory(noteId, dataKey, protect) {
2017-11-15 13:04:26 +08:00
const historyToChange = await sql.getResults("SELECT * FROM notes_history WHERE note_id = ? AND is_protected != ?", [noteId, protect]);
for (const history of historyToChange) {
if (protect) {
history.note_title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(history.note_history_id), history.note_title);
history.note_text = data_encryption.encrypt(dataKey, data_encryption.noteTextIv(history.note_history_id), history.note_text);
2017-11-15 13:04:26 +08:00
history.is_protected = true;
}
else {
history.note_title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(history.note_history_id), history.note_title);
history.note_text = data_encryption.decryptString(dataKey, data_encryption.noteTextIv(history.note_history_id), history.note_text);
2017-11-15 13:04:26 +08:00
history.is_protected = false;
}
await sql.execute("UPDATE notes_history SET note_title = ?, note_text = ?, is_protected = ? WHERE note_history_id = ?",
2017-11-15 13:04:26 +08:00
[history.note_title, history.note_text, history.is_protected, history.note_history_id]);
await sync_table.addNoteHistorySync(history.note_history_id);
2017-11-15 13:04:26 +08:00
}
}
async function updateNote(noteId, newNote, ctx) {
if (newNote.detail.is_protected) {
await encryptNote(newNote, ctx);
}
const now = new Date();
const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval'));
const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000));
const existingNoteHistoryId = await sql.getSingleValue(
"SELECT note_history_id FROM notes_history WHERE note_id = ? AND date_modified_to >= ?", [noteId, historyCutoff]);
await sql.doInTransaction(async () => {
const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.date_created).getTime();
if (!existingNoteHistoryId && msSinceDateCreated >= historySnapshotTimeInterval * 1000) {
const oldNote = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
2017-12-07 12:39:43 +08:00
if (oldNote.is_protected) {
decryptNote(oldNote, ctx.getDataKey());
}
2017-11-19 21:47:22 +08:00
const newNoteHistoryId = utils.newNoteHistoryId();
await sql.insert('notes_history', {
2017-11-21 12:51:28 +08:00
note_history_id: newNoteHistoryId,
note_id: noteId,
2017-12-07 12:39:43 +08:00
// title and text should be decrypted now
note_title: oldNote.note_title,
note_text: oldNote.note_text,
2017-12-07 12:39:43 +08:00
is_protected: 0, // will be fixed in the protectNoteHistory() call
date_modified_from: oldNote.date_modified,
2017-11-21 12:51:28 +08:00
date_modified_to: now
});
2017-11-16 13:22:00 +08:00
await sync_table.addNoteHistorySync(newNoteHistoryId);
}
await protectNoteHistory(noteId, ctx.getDataKeyOrNull(), newNote.detail.is_protected);
await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [
newNote.detail.note_title,
newNote.detail.note_text,
newNote.detail.is_protected,
now,
noteId]);
await sync_table.addNoteSync(noteId);
});
}
async function deleteNote(noteTreeId) {
const now = utils.nowDate();
2017-11-30 10:04:30 +08:00
await sql.execute("UPDATE notes_tree SET is_deleted = 1, date_modified = ? WHERE note_tree_id = ?", [now, noteTreeId]);
await sync_table.addNoteTreeSync(noteTreeId);
2017-11-20 12:12:39 +08:00
const noteId = await sql.getSingleValue("SELECT note_id FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
2017-11-24 12:54:54 +08:00
const notDeletedNoteTreesCount = await sql.getSingleValue("SELECT COUNT(*) FROM notes_tree WHERE note_id = ? AND is_deleted = 0", [noteId]);
2017-11-20 12:12:39 +08:00
if (!notDeletedNoteTreesCount) {
await sql.execute("UPDATE notes SET is_deleted = 1, date_modified = ? WHERE note_id = ?", [now, noteId]);
await sync_table.addNoteSync(noteId);
2017-11-24 12:54:54 +08:00
const children = await sql.getResults("SELECT note_tree_id FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [noteId]);
2017-11-20 12:12:39 +08:00
for (const child of children) {
await deleteNote(child.note_tree_id);
2017-11-20 12:12:39 +08:00
}
}
}
module.exports = {
createNewNote,
updateNote,
2017-11-15 13:04:26 +08:00
deleteNote,
protectNoteRecursively
};