trilium/src/services/notes.js

337 lines
11 KiB
JavaScript
Raw Normal View History

const sql = require('./sql');
const options = require('./options');
const utils = require('./utils');
const sync_table = require('./sync_table');
const attributes = require('./attributes');
const protected_session = require('./protected_session');
async function getNoteById(noteId, dataKey) {
2018-01-29 08:30:14 +08:00
const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
protected_session.decryptNote(dataKey, note);
return note;
}
async function getJsonNoteById(noteId, dataKey) {
const note = await getNoteById(noteId, dataKey);
2018-01-29 08:30:14 +08:00
note.data = JSON.parse(note.content);
return note;
}
async function updateJsonNote(noteId, data) {
const ret = await createNewNote(noteId, {
2018-01-29 08:30:14 +08:00
title: name,
content: JSON.stringify(data),
target: 'into',
2018-01-29 08:30:14 +08:00
isProtected: false,
type: 'code',
mime: 'application/json'
});
return ret.noteId;
}
async function createNewJsonNote(parentNoteId, name, payload) {
const ret = await createNewNote(parentNoteId, {
2018-01-29 08:30:14 +08:00
title: name,
content: JSON.stringify(payload),
target: 'into',
2018-01-29 08:30:14 +08:00
isProtected: false,
type: 'code',
mime: 'application/json'
});
return ret.noteId;
}
async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
const noteId = utils.newNoteId();
2017-11-19 21:47:22 +08:00
const noteTreeId = utils.newNoteTreeId();
let newNotePos = 0;
if (noteOpts.target === 'into') {
const maxNotePos = await sql.getFirstValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
}
else if (noteOpts.target === 'after') {
const afterNote = await sql.getFirst('SELECT notePosition FROM note_tree WHERE noteTreeId = ?', [noteOpts.target_noteTreeId]);
2018-01-29 08:30:14 +08:00
newNotePos = afterNote.notePosition + 1;
2018-01-29 08:30:14 +08:00
// not updating dateModified to avoig having to sync whole rows
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
[parentNoteId, afterNote.notePosition]);
await sync_table.addNoteReorderingSync(parentNoteId, sourceId);
}
else {
throw new Error('Unknown target: ' + noteOpts.target);
}
if (parentNoteId !== 'root') {
2018-01-29 08:30:14 +08:00
const parent = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
2017-11-30 10:04:30 +08:00
if (!noteOpts.type) {
noteOpts.type = parent.type;
}
if (!noteOpts.mime) {
noteOpts.mime = parent.mime;
}
}
const now = utils.nowDate();
const note = {
2018-01-29 08:30:14 +08:00
noteId: noteId,
title: noteOpts.title,
content: noteOpts.content ? noteOpts.content : '',
isProtected: noteOpts.isProtected,
type: noteOpts.type ? noteOpts.type : 'text',
mime: noteOpts.mime ? noteOpts.mime : 'text/html',
2018-01-29 08:30:14 +08:00
dateCreated: now,
dateModified: now
};
2018-01-29 08:30:14 +08:00
if (note.isProtected) {
protected_session.encryptNote(dataKey, note);
}
await sql.insert("notes", note);
await sync_table.addNoteSync(noteId, sourceId);
await sql.insert("note_tree", {
2018-01-29 08:30:14 +08:00
noteTreeId: noteTreeId,
noteId: noteId,
parentNoteId: parentNoteId,
notePosition: newNotePos,
isExpanded: 0,
dateModified: now,
isDeleted: 0
});
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
return {
noteId,
noteTreeId,
note
};
}
async function protectNoteRecursively(noteId, dataKey, protect, sourceId) {
2018-01-29 08:30:14 +08:00
const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
2017-11-15 13:04:26 +08:00
await protectNote(note, dataKey, protect, sourceId);
2017-11-15 13:04:26 +08:00
const children = await sql.getFirstColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
2017-11-15 13:04:26 +08:00
for (const childNoteId of children) {
await protectNoteRecursively(childNoteId, dataKey, protect, sourceId);
2017-11-15 13:04:26 +08:00
}
}
async function protectNote(note, dataKey, protect, sourceId) {
2017-11-15 13:04:26 +08:00
let changed = false;
2018-01-29 08:30:14 +08:00
if (protect && !note.isProtected) {
protected_session.encryptNote(dataKey, note);
2018-01-29 08:30:14 +08:00
note.isProtected = true;
2017-11-15 13:04:26 +08:00
changed = true;
}
2018-01-29 08:30:14 +08:00
else if (!protect && note.isProtected) {
protected_session.decryptNote(dataKey, note);
2018-01-29 08:30:14 +08:00
note.isProtected = false;
2017-11-15 13:04:26 +08:00
changed = true;
}
if (changed) {
2018-01-29 08:30:14 +08:00
await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ? WHERE noteId = ?",
[note.title, note.content, note.isProtected, note.noteId]);
2017-11-15 13:04:26 +08:00
2018-01-29 08:30:14 +08:00
await sync_table.addNoteSync(note.noteId, sourceId);
2017-11-15 13:04:26 +08:00
}
2018-01-29 08:30:14 +08:00
await protectNoteHistory(note.noteId, dataKey, protect, sourceId);
2017-11-15 13:04:26 +08:00
}
async function protectNoteHistory(noteId, dataKey, protect, sourceId) {
const historyToChange = await sql.getAll("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]);
2017-11-15 13:04:26 +08:00
for (const history of historyToChange) {
if (protect) {
protected_session.encryptNoteHistoryRow(dataKey, history);
2018-01-29 08:30:14 +08:00
history.isProtected = true;
2017-11-15 13:04:26 +08:00
}
else {
protected_session.decryptNoteHistoryRow(dataKey, history);
2018-01-29 08:30:14 +08:00
history.isProtected = false;
2017-11-15 13:04:26 +08:00
}
await sql.execute("UPDATE note_revisions SET title = ?, content = ?, isProtected = ? WHERE noteRevisionId = ?",
[history.title, history.content, history.isProtected, history.noteRevisionId]);
2017-11-15 13:04:26 +08:00
await sync_table.addNoteHistorySync(history.noteRevisionId, sourceId);
2017-11-15 13:04:26 +08:00
}
}
2018-01-07 11:38:53 +08:00
async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) {
2018-01-29 08:30:14 +08:00
const oldNote = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]);
2018-01-07 11:38:53 +08:00
2018-01-29 08:30:14 +08:00
if (oldNote.isProtected) {
protected_session.decryptNote(dataKey, oldNote);
2018-01-29 08:30:14 +08:00
note.isProtected = false;
2018-01-07 11:38:53 +08:00
}
const newnoteRevisionId = utils.newnoteRevisionId();
2018-01-07 11:38:53 +08:00
await sql.insert('note_revisions', {
noteRevisionId: newnoteRevisionId,
2018-01-29 08:30:14 +08:00
noteId: noteId,
2018-01-07 11:38:53 +08:00
// title and text should be decrypted now
2018-01-29 08:30:14 +08:00
title: oldNote.title,
content: oldNote.content,
isProtected: 0, // will be fixed in the protectNoteHistory() call
dateModifiedFrom: oldNote.dateModified,
dateModifiedTo: nowStr
2018-01-07 11:38:53 +08:00
});
await sync_table.addNoteHistorySync(newnoteRevisionId, sourceId);
2018-01-07 11:38:53 +08:00
}
async function saveNoteImages(noteId, noteText, sourceId) {
const existingNoteImages = await sql.getAll("SELECT * FROM note_images WHERE noteId = ?", [noteId]);
2018-01-07 11:38:53 +08:00
const foundImageIds = [];
const now = utils.nowDate();
const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g;
let match;
while (match = re.exec(noteText)) {
const imageId = match[1];
2018-01-29 08:30:14 +08:00
const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId);
2018-01-07 11:38:53 +08:00
if (!existingNoteImage) {
const noteImageId = utils.newNoteImageId();
await sql.insert("note_images", {
2018-01-29 08:30:14 +08:00
noteImageId: noteImageId,
noteId: noteId,
imageId: imageId,
isDeleted: 0,
dateModified: now,
dateCreated: now
2018-01-07 11:38:53 +08:00
});
await sync_table.addNoteImageSync(noteImageId, sourceId);
}
2018-01-29 08:30:14 +08:00
else if (existingNoteImage.isDeleted) {
await sql.execute("UPDATE note_images SET isDeleted = 0, dateModified = ? WHERE noteImageId = ?",
2018-01-29 08:30:14 +08:00
[now, existingNoteImage.noteImageId]);
2018-01-07 11:38:53 +08:00
2018-01-29 08:30:14 +08:00
await sync_table.addNoteImageSync(existingNoteImage.noteImageId, sourceId);
2018-01-07 11:38:53 +08:00
}
// else we don't need to do anything
foundImageIds.push(imageId);
}
// marking note images as deleted if they are not present on the page anymore
2018-01-29 08:30:14 +08:00
const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.imageId));
2018-01-07 11:38:53 +08:00
for (const unusedNoteImage of unusedNoteImages) {
await sql.execute("UPDATE note_images SET isDeleted = 1, dateModified = ? WHERE noteImageId = ?",
2018-01-29 08:30:14 +08:00
[now, unusedNoteImage.noteImageId]);
2018-01-07 11:38:53 +08:00
2018-01-29 08:30:14 +08:00
await sync_table.addNoteImageSync(unusedNoteImage.noteImageId, sourceId);
2018-01-07 11:38:53 +08:00
}
}
async function updateNote(noteId, newNote, dataKey, sourceId) {
2018-01-29 08:30:14 +08:00
if (newNote.detail.isProtected) {
await protected_session.encryptNote(dataKey, newNote.detail);
}
const attributesMap = await attributes.getNoteAttributeMap(noteId);
const now = new Date();
2017-12-14 12:27:02 +08:00
const nowStr = utils.nowDate();
const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval'));
const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000));
const existingnoteRevisionId = await sql.getFirstValue(
"SELECT noteRevisionId FROM note_revisions WHERE noteId = ? AND dateModifiedTo >= ?", [noteId, historyCutoff]);
await sql.doInTransaction(async () => {
2018-01-29 08:30:14 +08:00
const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.dateCreated).getTime();
if (attributesMap.disable_versioning !== 'true'
&& !existingnoteRevisionId
&& msSinceDateCreated >= historySnapshotTimeInterval * 1000) {
2018-01-07 11:38:53 +08:00
await saveNoteHistory(noteId, dataKey, sourceId, nowStr);
}
2018-01-29 08:30:14 +08:00
await saveNoteImages(noteId, newNote.detail.content, sourceId);
2018-01-07 11:38:53 +08:00
2018-01-29 08:30:14 +08:00
await protectNoteHistory(noteId, dataKey, newNote.detail.isProtected);
2018-01-29 08:30:14 +08:00
await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ?, dateModified = ? WHERE noteId = ?", [
newNote.detail.title,
newNote.detail.content,
newNote.detail.isProtected,
2017-12-14 12:27:02 +08:00
nowStr,
noteId]);
await sync_table.addNoteSync(noteId, sourceId);
});
}
async function deleteNote(noteTreeId, sourceId) {
const noteTree = await sql.getFirstOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
2018-01-15 10:39:21 +08:00
2018-01-29 08:30:14 +08:00
if (!noteTree || noteTree.isDeleted === 1) {
2018-01-15 10:39:21 +08:00
return;
}
const now = utils.nowDate();
2017-11-30 10:04:30 +08:00
await sql.execute("UPDATE note_tree SET isDeleted = 1, dateModified = ? WHERE noteTreeId = ?", [now, noteTreeId]);
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
const noteId = await sql.getFirstValue("SELECT noteId FROM note_tree WHERE noteTreeId = ?", [noteTreeId]);
const notDeletedNoteTreesCount = await sql.getFirstValue("SELECT COUNT(*) FROM note_tree WHERE noteId = ? AND isDeleted = 0", [noteId]);
2017-11-20 12:12:39 +08:00
if (!notDeletedNoteTreesCount) {
2018-01-29 08:30:14 +08:00
await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]);
await sync_table.addNoteSync(noteId, sourceId);
const children = await sql.getAll("SELECT noteTreeId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
2017-11-20 12:12:39 +08:00
for (const child of children) {
2018-01-29 08:30:14 +08:00
await deleteNote(child.noteTreeId, sourceId);
2017-11-20 12:12:39 +08:00
}
}
}
module.exports = {
getNoteById,
createNewNote,
updateNote,
2017-11-15 13:04:26 +08:00
deleteNote,
protectNoteRecursively
};