2017-11-05 10:41:54 -05:00
|
|
|
const sql = require('./sql');
|
|
|
|
const options = require('./options');
|
|
|
|
const utils = require('./utils');
|
2017-11-16 21:50:00 -05:00
|
|
|
const sync_table = require('./sync_table');
|
2018-03-24 22:02:26 -04:00
|
|
|
const labels = require('./labels');
|
2018-01-24 22:13:41 -05:00
|
|
|
const protected_session = require('./protected_session');
|
2018-03-31 10:51:37 -04:00
|
|
|
const repository = require('./repository');
|
2018-03-31 22:15:06 -04:00
|
|
|
const NoteImage = require('../entities/note_image');
|
|
|
|
const NoteRevision = require('../entities/note_revision');
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
async function createNewNote(parentNoteId, noteOpts) {
|
2017-11-05 10:41:54 -05:00
|
|
|
const noteId = utils.newNoteId();
|
2018-03-24 21:39:15 -04:00
|
|
|
const branchId = utils.newBranchId();
|
2017-11-05 10:41:54 -05:00
|
|
|
|
|
|
|
let newNotePos = 0;
|
|
|
|
|
2018-01-27 17:18:19 -05:00
|
|
|
if (noteOpts.target === 'into') {
|
2018-03-24 21:39:15 -04:00
|
|
|
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-01-27 17:18:19 -05:00
|
|
|
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
|
|
|
}
|
|
|
|
else if (noteOpts.target === 'after') {
|
2018-03-24 21:39:15 -04:00
|
|
|
const afterNote = await sql.getRow('SELECT notePosition FROM branches WHERE branchId = ?', [noteOpts.target_branchId]);
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2018-01-28 19:30:14 -05:00
|
|
|
newNotePos = afterNote.notePosition + 1;
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2018-01-28 19:30:14 -05:00
|
|
|
// not updating dateModified to avoig having to sync whole rows
|
2018-03-24 21:39:15 -04:00
|
|
|
await sql.execute('UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0',
|
2018-01-28 19:30:14 -05:00
|
|
|
[parentNoteId, afterNote.notePosition]);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addNoteReorderingSync(parentNoteId);
|
2018-01-27 17:18:19 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error('Unknown target: ' + noteOpts.target);
|
|
|
|
}
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-01-27 17:18:19 -05:00
|
|
|
if (parentNoteId !== 'root') {
|
2018-01-29 17:41:59 -05:00
|
|
|
const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
|
2017-11-29 21:04:30 -05:00
|
|
|
|
2018-01-27 17:18:19 -05:00
|
|
|
if (!noteOpts.type) {
|
|
|
|
noteOpts.type = parent.type;
|
2017-11-26 23:10:23 -05:00
|
|
|
}
|
2018-01-27 17:18:19 -05:00
|
|
|
|
|
|
|
if (!noteOpts.mime) {
|
|
|
|
noteOpts.mime = parent.mime;
|
2017-11-26 23:10:23 -05:00
|
|
|
}
|
2018-01-27 17:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const now = utils.nowDate();
|
|
|
|
|
|
|
|
const note = {
|
2018-01-28 19:30:14 -05:00
|
|
|
noteId: noteId,
|
|
|
|
title: noteOpts.title,
|
|
|
|
content: noteOpts.content ? noteOpts.content : '',
|
|
|
|
isProtected: noteOpts.isProtected,
|
2018-01-27 17:18:19 -05:00
|
|
|
type: noteOpts.type ? noteOpts.type : 'text',
|
|
|
|
mime: noteOpts.mime ? noteOpts.mime : 'text/html',
|
2018-01-28 19:30:14 -05:00
|
|
|
dateCreated: now,
|
|
|
|
dateModified: now
|
2018-01-27 17:18:19 -05:00
|
|
|
};
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-01-28 19:30:14 -05:00
|
|
|
if (note.isProtected) {
|
2018-03-31 08:53:52 -04:00
|
|
|
protected_session.encryptNote(note);
|
2018-01-27 17:18:19 -05:00
|
|
|
}
|
2018-01-26 21:31:52 -05:00
|
|
|
|
2018-01-27 17:18:19 -05:00
|
|
|
await sql.insert("notes", note);
|
2018-01-26 21:31:52 -05:00
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addNoteSync(noteId);
|
2018-01-26 21:31:52 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
await sql.insert("branches", {
|
|
|
|
branchId: branchId,
|
2018-01-28 19:30:14 -05:00
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
notePosition: newNotePos,
|
|
|
|
isExpanded: 0,
|
|
|
|
dateModified: now,
|
|
|
|
isDeleted: 0
|
2017-11-05 10:41:54 -05:00
|
|
|
});
|
2017-11-18 17:05:50 -05:00
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addBranchSync(branchId);
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2017-11-18 17:05:50 -05:00
|
|
|
return {
|
|
|
|
noteId,
|
2018-03-24 21:39:15 -04:00
|
|
|
branchId,
|
2018-01-28 10:37:43 -05:00
|
|
|
note
|
2017-11-18 17:05:50 -05:00
|
|
|
};
|
2017-11-05 10:41:54 -05:00
|
|
|
}
|
|
|
|
|
2018-02-26 00:07:43 -05:00
|
|
|
async function createNote(parentNoteId, title, content = "", extraOptions = {}) {
|
2018-02-26 20:47:34 -05:00
|
|
|
if (!parentNoteId) throw new Error("Empty parentNoteId");
|
|
|
|
if (!title) throw new Error("Empty title");
|
|
|
|
|
2018-02-26 00:07:43 -05:00
|
|
|
const note = {
|
|
|
|
title: title,
|
|
|
|
content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
|
|
|
|
target: 'into',
|
|
|
|
isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false,
|
|
|
|
type: extraOptions.type,
|
|
|
|
mime: extraOptions.mime
|
|
|
|
};
|
|
|
|
|
2018-03-23 23:08:29 -04:00
|
|
|
if (extraOptions.json && !note.type) {
|
2018-02-26 00:07:43 -05:00
|
|
|
note.type = "code";
|
|
|
|
note.mime = "application/json";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!note.type) {
|
|
|
|
note.type = "text";
|
|
|
|
note.mime = "text/html";
|
|
|
|
}
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
const {noteId} = await createNewNote(parentNoteId, note);
|
2018-02-26 00:07:43 -05:00
|
|
|
|
2018-03-24 22:02:26 -04:00
|
|
|
if (extraOptions.labels) {
|
|
|
|
for (const attrName in extraOptions.labels) {
|
|
|
|
await labels.createLabel(noteId, attrName, extraOptions.labels[attrName]);
|
2018-02-26 00:07:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return noteId;
|
|
|
|
}
|
|
|
|
|
2018-03-31 10:51:37 -04:00
|
|
|
async function protectNoteRecursively(note, protect) {
|
2018-03-31 08:53:52 -04:00
|
|
|
await protectNote(note, protect);
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2018-03-31 10:51:37 -04:00
|
|
|
for (const child of await note.getChildren()) {
|
|
|
|
await protectNoteRecursively(child, protect);
|
2017-11-15 00:04:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
async function protectNote(note, protect) {
|
2018-03-31 22:15:06 -04:00
|
|
|
if (protect !== note.isProtected) {
|
|
|
|
note.isProtected = protect;
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2018-03-31 10:51:37 -04:00
|
|
|
await repository.updateEntity(note);
|
2017-11-15 00:04:26 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
await protectNoteRevisions(note);
|
2017-11-15 00:04:26 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
async function protectNoteRevisions(note) {
|
2018-03-31 10:51:37 -04:00
|
|
|
for (const revision of await note.getRevisions()) {
|
2018-03-31 22:15:06 -04:00
|
|
|
if (note.isProtected !== revision.isProtected) {
|
|
|
|
revision.isProtected = note.isProtected;
|
2017-11-15 00:04:26 -05:00
|
|
|
|
2018-03-31 10:51:37 -04:00
|
|
|
await repository.updateEntity(revision);
|
|
|
|
}
|
2017-11-15 00:04:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
async function saveNoteImages(note) {
|
|
|
|
if (note.type !== 'text') {
|
2018-02-23 22:58:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
const existingNoteImages = await note.getNoteImages();
|
2018-01-06 22:38:53 -05:00
|
|
|
const foundImageIds = [];
|
|
|
|
const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g;
|
|
|
|
let match;
|
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
while (match = re.exec(note.content)) {
|
2018-01-06 22:38:53 -05:00
|
|
|
const imageId = match[1];
|
2018-01-28 19:30:14 -05:00
|
|
|
const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId);
|
2018-01-06 22:38:53 -05:00
|
|
|
|
|
|
|
if (!existingNoteImage) {
|
2018-03-31 22:15:06 -04:00
|
|
|
await repository.updateEntity(new NoteImage({
|
|
|
|
noteImageId: utils.newNoteImageId(),
|
|
|
|
noteId: note.noteId,
|
2018-01-28 19:30:14 -05:00
|
|
|
imageId: imageId,
|
2018-03-31 22:15:06 -04:00
|
|
|
isDeleted: 0
|
|
|
|
}));
|
2018-01-06 22:38:53 -05: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-28 19:30:14 -05:00
|
|
|
const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.imageId));
|
2018-01-06 22:38:53 -05:00
|
|
|
|
|
|
|
for (const unusedNoteImage of unusedNoteImages) {
|
2018-03-31 22:15:06 -04:00
|
|
|
unusedNoteImage.isDeleted = 1;
|
2018-01-06 22:38:53 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
await repository.updateEntity(unusedNoteImage);
|
2018-01-06 22:38:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
async function saveNoteRevision(note) {
|
|
|
|
const labelsMap = await note.getLabelMap();
|
2018-01-23 22:11:03 -05:00
|
|
|
|
2017-12-10 12:56:59 -05:00
|
|
|
const now = new Date();
|
2018-03-25 20:52:38 -04:00
|
|
|
const noteRevisionSnapshotTimeInterval = parseInt(await options.getOption('note_revision_snapshot_time_interval'));
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-25 20:52:38 -04:00
|
|
|
const revisionCutoff = utils.dateStr(new Date(now.getTime() - noteRevisionSnapshotTimeInterval * 1000));
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
const existingnoteRevisionId = await sql.getValue(
|
2018-03-31 22:15:06 -04:00
|
|
|
"SELECT noteRevisionId FROM note_revisions WHERE noteId = ? AND dateModifiedTo >= ?", [note.noteId, revisionCutoff]);
|
|
|
|
|
|
|
|
const msSinceDateCreated = now.getTime() - utils.parseDateTime(note.dateCreated).getTime();
|
|
|
|
|
|
|
|
if (note.type !== 'file'
|
|
|
|
&& labelsMap.disable_versioning !== 'true'
|
|
|
|
&& !existingnoteRevisionId
|
|
|
|
&& msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
|
|
|
|
|
|
|
|
await repository.updateEntity(new NoteRevision({
|
|
|
|
noteRevisionId: utils.newNoteRevisionId(),
|
|
|
|
noteId: note.noteId,
|
|
|
|
// title and text should be decrypted now
|
|
|
|
title: note.title,
|
|
|
|
content: note.content,
|
|
|
|
isProtected: 0, // will be fixed in the protectNoteRevisions() call
|
|
|
|
dateModifiedFrom: note.dateModified,
|
|
|
|
dateModifiedTo: utils.nowDate()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
async function updateNote(noteId, noteUpdates) {
|
|
|
|
const note = await repository.getNote(noteId);
|
2017-12-10 12:56:59 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
if (note.type === 'file') {
|
|
|
|
// for update file, newNote doesn't contain file payloads
|
|
|
|
noteUpdates.content = note.content;
|
|
|
|
}
|
2018-01-23 22:11:03 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
await saveNoteRevision(note);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
note.title = noteUpdates.title;
|
|
|
|
note.content = noteUpdates.content;
|
|
|
|
note.isProtected = noteUpdates.isProtected;
|
2018-01-06 22:38:53 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
await repository.updateEntity(note);
|
2017-11-14 22:21:56 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
await saveNoteImages(note);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-31 22:15:06 -04:00
|
|
|
await protectNoteRevisions(note);
|
2017-11-05 10:41:54 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
async function deleteNote(branchId) {
|
2018-03-24 21:39:15 -04:00
|
|
|
const branch = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [branchId]);
|
2018-01-14 21:39:21 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
if (!branch || branch.isDeleted === 1) {
|
2018-01-14 21:39:21 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-10 12:56:59 -05:00
|
|
|
const now = utils.nowDate();
|
2017-11-29 21:04:30 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
await sql.execute("UPDATE branches SET isDeleted = 1, dateModified = ? WHERE branchId = ?", [now, branchId]);
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addBranchSync(branchId);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const noteId = await sql.getValue("SELECT noteId FROM branches WHERE branchId = ?", [branchId]);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const notDeletedBranchsCount = await sql.getValue("SELECT COUNT(*) FROM branches WHERE noteId = ? AND isDeleted = 0", [noteId]);
|
2017-11-19 23:12:39 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
if (!notDeletedBranchsCount) {
|
2018-01-28 19:30:14 -05:00
|
|
|
await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]);
|
2018-03-30 19:41:54 -04:00
|
|
|
await sync_table.addNoteSync(noteId);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2018-03-24 21:39:15 -04:00
|
|
|
const children = await sql.getRows("SELECT branchId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
|
2017-11-05 10:41:54 -05:00
|
|
|
|
2017-11-19 23:12:39 -05:00
|
|
|
for (const child of children) {
|
2018-03-30 19:41:54 -04:00
|
|
|
await deleteNote(child.branchId);
|
2017-11-19 23:12:39 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-05 10:41:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createNewNote,
|
2018-02-26 00:07:43 -05:00
|
|
|
createNote,
|
2017-11-05 10:41:54 -05:00
|
|
|
updateNote,
|
2017-11-15 00:04:26 -05:00
|
|
|
deleteNote,
|
|
|
|
protectNoteRecursively
|
2017-11-05 17:58:55 -05:00
|
|
|
};
|