2017-11-05 23:41:54 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const options = require('./options');
|
|
|
|
const utils = require('./utils');
|
|
|
|
const notes = require('./notes');
|
2017-11-15 10:54:12 +08:00
|
|
|
const data_encryption = require('./data_encryption');
|
2017-11-17 10:50:00 +08:00
|
|
|
const sync_table = require('./sync_table');
|
2018-01-24 11:11:03 +08:00
|
|
|
const attributes = require('./attributes');
|
2018-01-25 11:13:41 +08:00
|
|
|
const protected_session = require('./protected_session');
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
async function createNewNote(parentNoteId, note, sourceId) {
|
2017-11-05 23:41:54 +08:00
|
|
|
const noteId = utils.newNoteId();
|
2017-11-19 21:47:22 +08:00
|
|
|
const noteTreeId = utils.newNoteTreeId();
|
2017-11-05 23:41:54 +08:00
|
|
|
|
|
|
|
let newNotePos = 0;
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-27 12:10:23 +08:00
|
|
|
if (note.target === 'into') {
|
2017-12-24 00:02:38 +08:00
|
|
|
const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-11-27 12:10:23 +08:00
|
|
|
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
|
|
|
}
|
|
|
|
else if (note.target === 'after') {
|
2017-12-24 00:02:38 +08:00
|
|
|
const afterNote = await sql.getFirst('SELECT note_position FROM notes_tree WHERE note_tree_id = ?', [note.target_note_tree_id]);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-20 10:40:48 +08:00
|
|
|
newNotePos = afterNote.note_position + 1;
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-19 09:58:13 +08:00
|
|
|
// not updating date_modified to avoig having to sync whole rows
|
2017-12-20 10:40:48 +08:00
|
|
|
await sql.execute('UPDATE notes_tree SET note_position = note_position + 1 WHERE parent_note_id = ? AND note_position > ? AND is_deleted = 0',
|
|
|
|
[parentNoteId, afterNote.note_position]);
|
2017-11-30 10:04:30 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteReorderingSync(parentNoteId, sourceId);
|
2017-11-27 12:10:23 +08:00
|
|
|
}
|
|
|
|
else {
|
2017-12-07 09:58:59 +08:00
|
|
|
throw new Error('Unknown target: ' + note.target);
|
2017-11-27 12:10:23 +08:00
|
|
|
}
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
const now = utils.nowDate();
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.insert("notes", {
|
2017-12-11 01:56:59 +08:00
|
|
|
note_id: noteId,
|
|
|
|
note_title: note.note_title,
|
2018-01-14 04:25:09 +08:00
|
|
|
note_text: note.note_text ? note.note_text : '',
|
2018-01-21 10:56:03 +08:00
|
|
|
is_protected: note.is_protected,
|
|
|
|
type: 'text',
|
2018-01-22 12:36:09 +08:00
|
|
|
mime: 'text/html',
|
2017-12-11 01:56:59 +08:00
|
|
|
date_created: now,
|
2018-01-21 10:56:03 +08:00
|
|
|
date_modified: now
|
2017-11-05 23:41:54 +08:00
|
|
|
});
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteSync(noteId, sourceId);
|
2017-11-30 10:04:30 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.insert("notes_tree", {
|
2017-12-11 01:56:59 +08:00
|
|
|
note_tree_id: noteTreeId,
|
|
|
|
note_id: noteId,
|
2017-12-20 10:40:48 +08:00
|
|
|
parent_note_id: parentNoteId,
|
|
|
|
note_position: newNotePos,
|
2017-12-11 01:56:59 +08:00
|
|
|
is_expanded: 0,
|
|
|
|
date_modified: now,
|
|
|
|
is_deleted: 0
|
2017-11-05 23:41:54 +08:00
|
|
|
});
|
2017-11-30 10:04:30 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
|
2017-11-05 23:41:54 +08:00
|
|
|
});
|
2017-11-19 06:05:50 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
noteId,
|
|
|
|
noteTreeId
|
|
|
|
};
|
2017-11-05 23:41:54 +08:00
|
|
|
}
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
async function protectNoteRecursively(noteId, dataKey, protect, sourceId) {
|
2017-12-24 00:02:38 +08:00
|
|
|
const note = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]);
|
2017-11-15 13:04:26 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await protectNote(note, dataKey, protect, sourceId);
|
2017-11-15 13:04:26 +08:00
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
const children = await sql.getFirstColumn("SELECT note_id FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [noteId]);
|
2017-11-15 13:04:26 +08:00
|
|
|
|
|
|
|
for (const childNoteId of children) {
|
2017-12-17 09:48:34 +08:00
|
|
|
await protectNoteRecursively(childNoteId, dataKey, protect, sourceId);
|
2017-11-15 13:04:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
async function protectNote(note, dataKey, protect, sourceId) {
|
2017-11-15 13:04:26 +08:00
|
|
|
let changed = false;
|
|
|
|
|
|
|
|
if (protect && !note.is_protected) {
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.encryptNote(dataKey, note);
|
|
|
|
|
2017-11-15 13:04:26 +08:00
|
|
|
note.is_protected = true;
|
|
|
|
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else if (!protect && note.is_protected) {
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.decryptNote(dataKey, note);
|
|
|
|
|
|
|
|
note.is_protected = false;
|
2017-11-15 13:04:26 +08:00
|
|
|
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
2017-11-29 06:24:08 +08:00
|
|
|
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]);
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteSync(note.note_id, sourceId);
|
2017-11-15 13:04:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await protectNoteHistory(note.note_id, dataKey, protect, sourceId);
|
2017-11-15 13:04:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
async function protectNoteHistory(noteId, dataKey, protect, sourceId) {
|
2017-12-24 00:02:38 +08:00
|
|
|
const historyToChange = await sql.getAll("SELECT * FROM notes_history WHERE note_id = ? AND is_protected != ?", [noteId, protect]);
|
2017-11-15 13:04:26 +08:00
|
|
|
|
|
|
|
for (const history of historyToChange) {
|
|
|
|
if (protect) {
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.encryptNoteHistoryRow(dataKey, history);
|
|
|
|
|
2017-11-15 13:04:26 +08:00
|
|
|
history.is_protected = true;
|
|
|
|
}
|
|
|
|
else {
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.decryptNoteHistoryRow(dataKey, history);
|
|
|
|
|
2017-11-15 13:04:26 +08:00
|
|
|
history.is_protected = false;
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
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]);
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteHistorySync(history.note_history_id, sourceId);
|
2017-11-15 13:04:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 11:38:53 +08:00
|
|
|
async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) {
|
|
|
|
const oldNote = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]);
|
|
|
|
|
|
|
|
if (oldNote.is_protected) {
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.decryptNote(dataKey, oldNote);
|
|
|
|
|
|
|
|
note.is_protected = false;
|
2018-01-07 11:38:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const newNoteHistoryId = utils.newNoteHistoryId();
|
|
|
|
|
|
|
|
await sql.insert('notes_history', {
|
|
|
|
note_history_id: newNoteHistoryId,
|
|
|
|
note_id: noteId,
|
|
|
|
// title and text should be decrypted now
|
|
|
|
note_title: oldNote.note_title,
|
|
|
|
note_text: oldNote.note_text,
|
|
|
|
is_protected: 0, // will be fixed in the protectNoteHistory() call
|
|
|
|
date_modified_from: oldNote.date_modified,
|
|
|
|
date_modified_to: nowStr
|
|
|
|
});
|
|
|
|
|
|
|
|
await sync_table.addNoteHistorySync(newNoteHistoryId, sourceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveNoteImages(noteId, noteText, sourceId) {
|
|
|
|
const existingNoteImages = await sql.getAll("SELECT * FROM notes_image WHERE note_id = ?", [noteId]);
|
|
|
|
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];
|
|
|
|
const existingNoteImage = existingNoteImages.find(ni => ni.image_id === imageId);
|
|
|
|
|
|
|
|
if (!existingNoteImage) {
|
|
|
|
const noteImageId = utils.newNoteImageId();
|
|
|
|
|
|
|
|
await sql.insert("notes_image", {
|
|
|
|
note_image_id: noteImageId,
|
|
|
|
note_id: noteId,
|
|
|
|
image_id: imageId,
|
|
|
|
is_deleted: 0,
|
|
|
|
date_modified: now,
|
|
|
|
date_created: now
|
|
|
|
});
|
|
|
|
|
|
|
|
await sync_table.addNoteImageSync(noteImageId, sourceId);
|
|
|
|
}
|
|
|
|
else if (existingNoteImage.is_deleted) {
|
|
|
|
await sql.execute("UPDATE notes_image SET is_deleted = 0, date_modified = ? WHERE note_image_id = ?",
|
|
|
|
[now, existingNoteImage.note_image_id]);
|
|
|
|
|
|
|
|
await sync_table.addNoteImageSync(existingNoteImage.note_image_id, sourceId);
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.image_id));
|
|
|
|
|
|
|
|
for (const unusedNoteImage of unusedNoteImages) {
|
|
|
|
await sql.execute("UPDATE notes_image SET is_deleted = 1, date_modified = ? WHERE note_image_id = ?",
|
|
|
|
[now, unusedNoteImage.note_image_id]);
|
|
|
|
|
|
|
|
await sync_table.addNoteImageSync(unusedNoteImage.note_image_id, sourceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:23:35 +08:00
|
|
|
async function updateNote(noteId, newNote, dataKey, sourceId) {
|
2018-01-24 12:01:30 +08:00
|
|
|
if (newNote.detail.note_text === '<p> </p>') {
|
2018-01-25 11:13:41 +08:00
|
|
|
newNote.detail.note_text = ''
|
2018-01-24 12:01:30 +08:00
|
|
|
}
|
|
|
|
|
2017-11-15 10:54:12 +08:00
|
|
|
if (newNote.detail.is_protected) {
|
2018-01-25 11:13:41 +08:00
|
|
|
await protected_session.encryptNote(dataKey, newNote.detail);
|
2017-11-05 23:41:54 +08:00
|
|
|
}
|
|
|
|
|
2018-01-24 11:11:03 +08:00
|
|
|
const attributesMap = await attributes.getNoteAttributeMap(noteId);
|
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
const now = new Date();
|
2017-12-14 12:27:02 +08:00
|
|
|
const nowStr = utils.nowDate();
|
2017-11-05 23:41:54 +08:00
|
|
|
|
|
|
|
const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval'));
|
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000));
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const existingNoteHistoryId = await sql.getFirstValue(
|
2017-12-11 01:56:59 +08:00
|
|
|
"SELECT note_history_id FROM notes_history WHERE note_id = ? AND date_modified_to >= ?", [noteId, historyCutoff]);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-12-11 01:56:59 +08:00
|
|
|
const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.date_created).getTime();
|
|
|
|
|
2018-01-24 11:11:03 +08:00
|
|
|
if (attributesMap.disable_versioning !== 'true'
|
|
|
|
&& !existingNoteHistoryId
|
|
|
|
&& msSinceDateCreated >= historySnapshotTimeInterval * 1000) {
|
|
|
|
|
2018-01-07 11:38:53 +08:00
|
|
|
await saveNoteHistory(noteId, dataKey, sourceId, nowStr);
|
2017-11-05 23:41:54 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 11:38:53 +08:00
|
|
|
await saveNoteImages(noteId, newNote.detail.note_text, sourceId);
|
|
|
|
|
2017-12-17 10:23:35 +08:00
|
|
|
await protectNoteHistory(noteId, dataKey, newNote.detail.is_protected);
|
2017-11-15 11:21:56 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [
|
2017-11-05 23:41:54 +08:00
|
|
|
newNote.detail.note_title,
|
|
|
|
newNote.detail.note_text,
|
2017-11-15 10:54:12 +08:00
|
|
|
newNote.detail.is_protected,
|
2017-12-14 12:27:02 +08:00
|
|
|
nowStr,
|
2017-11-05 23:41:54 +08:00
|
|
|
noteId]);
|
|
|
|
|
2017-12-17 10:23:35 +08:00
|
|
|
await sync_table.addNoteSync(noteId, sourceId);
|
2017-11-05 23:41:54 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
async function deleteNote(noteTreeId, sourceId) {
|
2018-01-15 10:39:21 +08:00
|
|
|
const noteTree = await sql.getFirstOrNull("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
|
|
|
|
|
|
|
|
if (!noteTree || noteTree.is_deleted === 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
const now = utils.nowDate();
|
2017-11-30 10:04:30 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET is_deleted = 1, date_modified = ? WHERE note_tree_id = ?", [now, noteTreeId]);
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const noteId = await sql.getFirstValue("SELECT note_id FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const notDeletedNoteTreesCount = await sql.getFirstValue("SELECT COUNT(*) FROM notes_tree WHERE note_id = ? AND is_deleted = 0", [noteId]);
|
2017-11-20 12:12:39 +08:00
|
|
|
|
|
|
|
if (!notDeletedNoteTreesCount) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes SET is_deleted = 1, date_modified = ? WHERE note_id = ?", [now, noteId]);
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteSync(noteId, sourceId);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const children = await sql.getAll("SELECT note_tree_id FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [noteId]);
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2017-11-20 12:12:39 +08:00
|
|
|
for (const child of children) {
|
2017-12-17 09:48:34 +08:00
|
|
|
await deleteNote(child.note_tree_id, sourceId);
|
2017-11-20 12:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-05 23:41:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createNewNote,
|
|
|
|
updateNote,
|
2017-11-15 13:04:26 +08:00
|
|
|
deleteNote,
|
|
|
|
protectNoteRecursively
|
2017-11-06 06:58:55 +08:00
|
|
|
};
|