Notes/src/services/notes.js

267 lines
7.8 KiB
JavaScript
Raw Normal View History

const sql = require('./sql');
const optionService = require('./options');
2018-04-02 20:46:46 -04:00
const dateUtils = require('./date_utils');
const syncTableService = require('./sync_table');
const attributeService = require('./attributes');
const eventService = require('./events');
const repository = require('./repository');
2018-03-31 22:23:40 -04:00
const Note = require('../entities/note');
2018-03-31 22:15:06 -04:00
const NoteImage = require('../entities/note_image');
const NoteRevision = require('../entities/note_revision');
2018-03-31 22:23:40 -04:00
const Branch = require('../entities/branch');
2018-04-01 17:38:24 -04:00
async function getNewNotePosition(parentNoteId, noteData) {
let newNotePos = 0;
2018-04-01 17:38:24 -04:00
if (noteData.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]);
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
}
2018-04-01 17:38:24 -04:00
else if (noteData.target === 'after') {
const afterNote = await sql.getRow('SELECT notePosition FROM branches WHERE branchId = ?', [noteData.target_branchId]);
2018-01-28 19:30:14 -05:00
newNotePos = afterNote.notePosition + 1;
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]);
await syncTableService.addNoteReorderingSync(parentNoteId);
}
else {
2018-04-01 17:38:24 -04:00
throw new Error('Unknown target: ' + noteData.target);
}
2018-03-31 22:23:40 -04:00
return newNotePos;
}
async function triggerNoteTitleChanged(note) {
await eventService.emit(eventService.NOTE_TITLE_CHANGED, note);
}
2018-04-01 17:38:24 -04:00
async function createNewNote(parentNoteId, noteData) {
const newNotePos = await getNewNotePosition(parentNoteId, noteData);
if (parentNoteId !== 'root') {
2018-03-31 22:23:40 -04:00
const parent = await repository.getNote(parentNoteId);
2017-11-29 21:04:30 -05:00
2018-04-01 17:38:24 -04:00
noteData.type = noteData.type || parent.type;
noteData.mime = noteData.mime || parent.mime;
}
2018-04-02 22:53:01 -04:00
const note = await new Note({
2018-04-01 17:38:24 -04:00
title: noteData.title,
content: noteData.content || '',
isProtected: noteData.isProtected,
type: noteData.type || 'text',
mime: noteData.mime || 'text/html'
2018-04-02 22:53:01 -04:00
}).save();
2018-04-02 22:53:01 -04:00
const branch = await new Branch({
2018-03-31 22:23:40 -04:00
noteId: note.noteId,
2018-01-28 19:30:14 -05:00
parentNoteId: parentNoteId,
notePosition: newNotePos,
prefix: noteData.prefix,
2018-04-01 17:38:24 -04:00
isExpanded: 0
2018-04-02 22:53:01 -04:00
}).save();
await triggerNoteTitleChanged(note);
return {
note,
branch
};
}
async function createNote(parentNoteId, title, content = "", extraOptions = {}) {
if (!parentNoteId) throw new Error("Empty parentNoteId");
if (!title) throw new Error("Empty title");
2018-04-01 11:42:12 -04:00
const noteData = {
title: title,
content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
target: 'into',
2018-04-01 17:38:24 -04:00
isProtected: !!extraOptions.isProtected,
type: extraOptions.type,
mime: extraOptions.mime
};
2018-04-01 11:42:12 -04:00
if (extraOptions.json && !noteData.type) {
noteData.type = "code";
noteData.mime = "application/json";
}
const {note, branch} = await createNewNote(parentNoteId, noteData);
for (const attr of extraOptions.attributes || []) {
await attributeService.createAttribute({
noteId: note.noteId,
type: attr.type,
name: attr.name,
value: attr.value
});
}
await triggerNoteTitleChanged(note);
return {note, branch};
}
async function protectNoteRecursively(note, protect) {
await protectNote(note, protect);
2017-11-15 00:04:26 -05:00
2018-03-31 23:08:22 -04:00
for (const child of await note.getChildNotes()) {
await protectNoteRecursively(child, protect);
2017-11-15 00:04:26 -05: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-04-01 17:38:24 -04:00
await note.save();
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) {
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-04-01 17:38:24 -04:00
await revision.save();
}
2017-11-15 00:04:26 -05:00
}
}
2018-03-31 22:15:06 -04:00
async function saveNoteImages(note) {
if (note.type !== 'text') {
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-04-02 22:53:01 -04:00
await new NoteImage({
2018-03-31 22:15:06 -04:00
noteId: note.noteId,
2018-04-01 17:38:24 -04:00
imageId: imageId
2018-04-02 22:53:01 -04:00
}).save();
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-04-01 17:38:24 -04:00
unusedNoteImage.isDeleted = true;
2018-01-06 22:38:53 -05:00
2018-04-01 17:38:24 -04:00
await unusedNoteImage.save();
2018-01-06 22:38:53 -05:00
}
}
2018-03-31 22:15:06 -04:00
async function saveNoteRevision(note) {
const now = new Date();
2018-04-02 21:47:46 -04:00
const noteRevisionSnapshotTimeInterval = parseInt(await optionService.getOption('noteRevisionSnapshotTimeInterval'));
2018-04-02 20:46:46 -04:00
const revisionCutoff = dateUtils.dateStr(new Date(now.getTime() - noteRevisionSnapshotTimeInterval * 1000));
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]);
2018-04-02 20:46:46 -04:00
const msSinceDateCreated = now.getTime() - dateUtils.parseDateTime(note.dateCreated).getTime();
2018-03-31 22:15:06 -04:00
if (note.type !== 'file'
2018-08-15 08:44:54 +02:00
&& !await note.hasLabel('disableVersioning')
2018-03-31 22:15:06 -04:00
&& !existingnoteRevisionId
&& msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
2018-04-02 22:53:01 -04:00
await new NoteRevision({
2018-03-31 22:15:06 -04:00
noteId: note.noteId,
// title and text should be decrypted now
title: note.title,
content: note.content,
2018-04-08 11:57:14 -04:00
type: note.type,
mime: note.mime,
isProtected: false, // will be fixed in the protectNoteRevisions() call
2018-03-31 22:15:06 -04:00
dateModifiedFrom: note.dateModified,
2018-04-02 20:46:46 -04:00
dateModifiedTo: dateUtils.nowDate()
2018-04-02 22:53:01 -04:00
}).save();
2018-03-31 22:15:06 -04:00
}
}
2018-03-31 22:15:06 -04:00
async function updateNote(noteId, noteUpdates) {
const note = await repository.getNote(noteId);
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-03-31 22:15:06 -04:00
await saveNoteRevision(note);
const noteTitleChanged = note.title !== noteUpdates.title;
2018-03-31 22:15:06 -04:00
note.title = noteUpdates.title;
2018-04-08 08:31:19 -04:00
note.setContent(noteUpdates.content);
2018-03-31 22:15:06 -04:00
note.isProtected = noteUpdates.isProtected;
2018-04-01 17:38:24 -04:00
await note.save();
if (noteTitleChanged) {
await triggerNoteTitleChanged(note);
}
2018-03-31 22:15:06 -04:00
await saveNoteImages(note);
2018-03-31 22:15:06 -04:00
await protectNoteRevisions(note);
}
2018-03-31 23:08:22 -04:00
async function deleteNote(branch) {
if (!branch || branch.isDeleted) {
2018-01-14 21:39:21 -05:00
return;
}
2018-03-31 23:08:22 -04:00
branch.isDeleted = true;
await branch.save();
2017-11-19 23:12:39 -05:00
2018-03-31 23:08:22 -04:00
const note = await branch.getNote();
const notDeletedBranches = await note.getBranches();
2018-03-31 23:08:22 -04:00
if (notDeletedBranches.length === 0) {
note.isDeleted = true;
note.content = '';
2018-03-31 23:08:22 -04:00
await note.save();
for (const noteRevision of await note.getRevisions()) {
noteRevision.content = '';
await noteRevision.save();
}
2018-03-31 23:08:22 -04:00
for (const childBranch of await note.getChildBranches()) {
await deleteNote(childBranch);
2017-11-19 23:12:39 -05:00
}
2018-08-15 15:27:22 +02:00
for (const attribute of await note.getOwnedAttributes()) {
attribute.isDeleted = true;
await attribute.save();
}
2017-11-19 23:12:39 -05:00
}
}
module.exports = {
createNewNote,
createNote,
updateNote,
2017-11-15 00:04:26 -05:00
deleteNote,
protectNoteRecursively
};