| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const noteService = require('../../services/notes'); | 
					
						
							|  |  |  | const treeService = require('../../services/tree'); | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							|  |  |  | const TaskContext = require('../../services/task_context'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getNote(req) { | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const note = repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  |     if (!note) { | 
					
						
							|  |  |  |         return [404, "Note " + noteId + " has not been found."]; | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 22:45:44 +01:00
										 |  |  |     if (note.isStringNote()) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         note.content = note.getContent(); | 
					
						
							| 
									
										
										
										
											2019-02-06 20:19:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-04 22:06:25 +02:00
										 |  |  |         if (note.type === 'file' && note.content.length > 10000) { | 
					
						
							|  |  |  |             note.content = note.content.substr(0, 10000) | 
					
						
							|  |  |  |                 + `\r\n\r\n... and ${note.content.length - 10000} more characters.`; | 
					
						
							| 
									
										
										
										
											2019-02-07 22:16:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 22:57:48 +02:00
										 |  |  |     const contentMetadata = note.getContentMetadata(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 22:20:47 +02:00
										 |  |  |     note.contentLength = contentMetadata.contentLength; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 22:57:48 +02:00
										 |  |  |     note.combinedUtcDateModified = note.utcDateModified > contentMetadata.utcDateModified ? note.utcDateModified : contentMetadata.utcDateModified; | 
					
						
							|  |  |  |     note.combinedDateModified = note.utcDateModified > contentMetadata.utcDateModified ? note.dateModified : contentMetadata.dateModified; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  |     return note; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function createNote(req) { | 
					
						
							| 
									
										
										
										
											2019-11-16 11:09:52 +01:00
										 |  |  |     const params = Object.assign({}, req.body); // clone
 | 
					
						
							|  |  |  |     params.parentNoteId = req.params.parentNoteId; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-16 12:28:47 +01:00
										 |  |  |     const { target, targetBranchId } = req.query; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const { note, branch } = noteService.createNewNoteWithTarget(target, targetBranchId, params); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |         note, | 
					
						
							|  |  |  |         branch | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateNote(req) { | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |     const note = req.body; | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     return noteService.updateNote(noteId, note); | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function deleteNote(req) { | 
					
						
							| 
									
										
										
										
											2018-11-14 23:30:28 +01:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2019-10-19 00:11:07 +02:00
										 |  |  |     const taskId = req.query.taskId; | 
					
						
							|  |  |  |     const last = req.query.last === 'true'; | 
					
						
							| 
									
										
										
										
											2018-11-14 23:30:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  |     // note how deleteId is separate from taskId - single taskId produces separate deleteId for each "top level" deleted note
 | 
					
						
							|  |  |  |     const deleteId = utils.randomString(10); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const note = repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-11-14 23:30:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 00:11:07 +02:00
										 |  |  |     const taskContext = TaskContext.getInstance(taskId, 'delete-notes'); | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     for (const branch of note.getBranches()) { | 
					
						
							|  |  |  |         noteService.deleteBranch(branch, deleteId, taskContext); | 
					
						
							| 
									
										
										
										
											2018-11-14 23:30:28 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-19 00:11:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (last) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         taskContext.taskSucceeded(); | 
					
						
							| 
									
										
										
										
											2019-10-19 00:11:07 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-14 23:30:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function undeleteNote(req) { | 
					
						
							|  |  |  |     const note = repository.getNote(req.params.noteId); | 
					
						
							| 
									
										
										
										
											2020-01-03 13:14:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-03 21:32:41 +01:00
										 |  |  |     const taskContext = TaskContext.getInstance(utils.randomString(10), 'undelete-notes'); | 
					
						
							| 
									
										
										
										
											2020-01-03 13:14:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     noteService.undeleteNote(note, note.deleteId, taskContext); | 
					
						
							| 
									
										
										
										
											2020-01-03 13:14:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     taskContext.taskSucceeded(); | 
					
						
							| 
									
										
										
										
											2020-01-03 13:14:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function sortNotes(req) { | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     treeService.sortNotesAlphabetically(noteId); | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function protectNote(req) { | 
					
						
							| 
									
										
										
										
											2018-01-13 20:53:00 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const note = repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |     const protect = !!parseInt(req.params.isProtected); | 
					
						
							| 
									
										
										
										
											2020-02-26 16:37:17 +01:00
										 |  |  |     const includingSubTree = !!parseInt(req.query.subtree); | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 09:58:18 +02:00
										 |  |  |     const taskContext = new TaskContext(utils.randomString(10), 'protect-notes', {protect}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     noteService.protectNoteRecursively(note, protect, includingSubTree, taskContext); | 
					
						
							| 
									
										
										
										
											2019-10-19 09:58:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     taskContext.taskSucceeded(); | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function setNoteTypeMime(req) { | 
					
						
							| 
									
										
										
										
											2018-04-04 23:04:31 -04:00
										 |  |  |     // can't use [] destructuring because req.params is not iterable
 | 
					
						
							|  |  |  |     const noteId = req.params[0]; | 
					
						
							|  |  |  |     const type = req.params[1]; | 
					
						
							|  |  |  |     const mime = req.params[2]; | 
					
						
							| 
									
										
										
										
											2018-01-20 21:56:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const note = repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-04-01 17:38:24 -04:00
										 |  |  |     note.type = type; | 
					
						
							|  |  |  |     note.mime = mime; | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.save(); | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-20 21:56:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getRelationMap(req) { | 
					
						
							| 
									
										
										
										
											2018-10-25 12:06:36 +02:00
										 |  |  |     const noteIds = req.body.noteIds; | 
					
						
							|  |  |  |     const resp = { | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  |         // noteId => title
 | 
					
						
							| 
									
										
										
										
											2018-10-25 12:06:36 +02:00
										 |  |  |         noteTitles: {}, | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  |         relations: [], | 
					
						
							| 
									
										
										
										
											2018-11-19 12:07:33 +01:00
										 |  |  |         // relation name => inverse relation name
 | 
					
						
							| 
									
										
										
										
											2019-11-23 20:54:49 +01:00
										 |  |  |         inverseRelations: { | 
					
						
							|  |  |  |             'internalLink': 'internalLink' | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-10-25 12:06:36 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (noteIds.length === 0) { | 
					
						
							|  |  |  |         return resp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const questionMarks = noteIds.map(noteId => '?').join(','); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const notes = repository.getEntities(`SELECT * FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds); | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const note of notes) { | 
					
						
							|  |  |  |         resp.noteTitles[note.noteId] = note.title; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 22:21:44 +02:00
										 |  |  |         resp.relations = resp.relations.concat(note.getRelations() | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  |             .filter(relation => noteIds.includes(relation.value)) | 
					
						
							| 
									
										
										
										
											2020-02-28 22:07:08 +01:00
										 |  |  |             .map(relation => ({ | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  |                 attributeId: relation.attributeId, | 
					
						
							|  |  |  |                 sourceNoteId: relation.noteId, | 
					
						
							|  |  |  |                 targetNoteId: relation.value, | 
					
						
							|  |  |  |                 name: relation.name | 
					
						
							| 
									
										
										
										
											2020-02-28 22:07:08 +01:00
										 |  |  |             }))); | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         for (const relationDefinition of note.getRelationDefinitions()) { | 
					
						
							| 
									
										
										
										
											2020-09-12 22:21:44 +02:00
										 |  |  |             const def = relationDefinition.getDefinition(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (def.inverseRelation) { | 
					
						
							|  |  |  |                 resp.inverseRelations[relationDefinition.getDefinedName()] = def.inverseRelation; | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-25 12:06:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return resp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function changeTitle(req) { | 
					
						
							| 
									
										
										
										
											2018-10-30 22:18:20 +01:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  |     const title = req.body.title; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const note = repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-10-30 22:18:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							|  |  |  |         return [404, `Note ${noteId} has not been found`]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!note.isContentAvailable) { | 
					
						
							|  |  |  |         return [400, `Note ${noteId} is not available for change`]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 22:03:01 +02:00
										 |  |  |     const noteTitleChanged = note.title !== title; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-30 22:18:20 +01:00
										 |  |  |     note.title = title; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.save(); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 22:03:01 +02:00
										 |  |  |     if (noteTitleChanged) { | 
					
						
							| 
									
										
										
										
											2020-06-24 22:29:53 +02:00
										 |  |  |         noteService.triggerNoteTitleChanged(note); | 
					
						
							| 
									
										
										
										
											2020-06-23 22:03:01 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |     return note; | 
					
						
							| 
									
										
										
										
											2018-10-30 22:18:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 14:06:32 +01:00
										 |  |  | function duplicateSubtree(req) { | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  |     const {noteId, parentNoteId} = req.params; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 14:06:32 +01:00
										 |  |  |     return noteService.duplicateSubtree(noteId, parentNoteId); | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     getNote, | 
					
						
							|  |  |  |     updateNote, | 
					
						
							| 
									
										
										
										
											2018-11-14 23:30:28 +01:00
										 |  |  |     deleteNote, | 
					
						
							| 
									
										
										
										
											2020-01-03 13:14:43 +01:00
										 |  |  |     undeleteNote, | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  |     createNote, | 
					
						
							|  |  |  |     sortNotes, | 
					
						
							| 
									
										
										
										
											2020-02-26 16:37:17 +01:00
										 |  |  |     protectNote, | 
					
						
							| 
									
										
										
										
											2018-10-21 10:26:14 +02:00
										 |  |  |     setNoteTypeMime, | 
					
						
							| 
									
										
										
										
											2018-10-30 22:18:20 +01:00
										 |  |  |     getRelationMap, | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  |     changeTitle, | 
					
						
							| 
									
										
										
										
											2020-11-19 14:06:32 +01:00
										 |  |  |     duplicateSubtree | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |