| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  | const beccaService = require('../../becca/becca_service'); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | const protectedSessionService = require('../../services/protected_session'); | 
					
						
							| 
									
										
										
										
											2020-05-07 23:34:13 +02:00
										 |  |  | const noteRevisionService = require('../../services/note_revisions'); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2021-03-22 23:07:43 +01:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  | const becca = require("../../becca/becca"); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getNoteRevisions(req) { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:59:16 +02:00
										 |  |  |     return becca.getNoteRevisionsFromQuery(`
 | 
					
						
							| 
									
										
										
										
											2020-08-16 22:57:48 +02:00
										 |  |  |         SELECT note_revisions.*, | 
					
						
							|  |  |  |                LENGTH(note_revision_contents.content) AS contentLength | 
					
						
							|  |  |  |         FROM note_revisions | 
					
						
							|  |  |  |         JOIN note_revision_contents ON note_revisions.noteRevisionId = note_revision_contents.noteRevisionId  | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  |         WHERE noteId = ? | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |         ORDER BY utcDateCreated DESC`, [req.params.noteId]);
 | 
					
						
							| 
									
										
										
										
											2019-09-08 09:17:16 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getNoteRevision(req) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:18:14 +02:00
										 |  |  |     const noteRevision = becca.getNoteRevision(req.params.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2019-09-08 09:17:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  |     if (noteRevision.type === 'file') { | 
					
						
							|  |  |  |         if (noteRevision.isStringNote()) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:18:14 +02:00
										 |  |  |             noteRevision.content = noteRevision.getContent().substr(0, 10000); | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         noteRevision.content = noteRevision.getContent(); | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  |         if (noteRevision.content && noteRevision.type === 'image') { | 
					
						
							|  |  |  |             noteRevision.content = noteRevision.content.toString('base64'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-08 23:09:57 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  |     return noteRevision; | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {NoteRevision} noteRevision | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function getRevisionFilename(noteRevision) { | 
					
						
							| 
									
										
										
										
											2020-05-12 10:28:31 +02:00
										 |  |  |     let filename = utils.formatDownloadTitle(noteRevision.title, noteRevision.type, noteRevision.mime); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const extension = path.extname(filename); | 
					
						
							|  |  |  |     const date = noteRevision.dateCreated | 
					
						
							|  |  |  |         .substr(0, 19) | 
					
						
							|  |  |  |         .replace(' ', '_') | 
					
						
							|  |  |  |         .replace(/[^0-9_]/g, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (extension) { | 
					
						
							|  |  |  |         filename = filename.substr(0, filename.length - extension.length) | 
					
						
							|  |  |  |             + '-' + date + extension; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         filename += '-' + date; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return filename; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function downloadNoteRevision(req, res) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:18:14 +02:00
										 |  |  |     const noteRevision = becca.getNoteRevision(req.params.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (noteRevision.noteId !== req.params.noteId) { | 
					
						
							|  |  |  |         return res.status(400).send(`Note revision ${req.params.noteRevisionId} does not belong to note ${req.params.noteId}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (noteRevision.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |         return res.status(401).send("Protected session not available"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const filename = getRevisionFilename(noteRevision); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); | 
					
						
							|  |  |  |     res.setHeader('Content-Type', noteRevision.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     res.send(noteRevision.getContent()); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function eraseAllNoteRevisions(req) { | 
					
						
							| 
									
										
										
										
											2020-12-16 15:01:20 +01:00
										 |  |  |     const noteRevisionIdsToErase = sql.getColumn('SELECT noteRevisionId FROM note_revisions WHERE noteId = ?', | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |         [req.params.noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  |     noteRevisionService.eraseNoteRevisions(noteRevisionIdsToErase); | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function eraseNoteRevision(req) { | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  |     noteRevisionService.eraseNoteRevisions([req.params.noteRevisionId]); | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function restoreNoteRevision(req) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:18:14 +02:00
										 |  |  |     const noteRevision = becca.getNoteRevision(req.params.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2020-05-07 23:34:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 15:01:20 +01:00
										 |  |  |     if (noteRevision) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         const note = noteRevision.getNote(); | 
					
						
							| 
									
										
										
										
											2020-05-07 23:34:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         noteRevisionService.createNoteRevision(note); | 
					
						
							| 
									
										
										
										
											2020-05-07 23:34:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         note.title = noteRevision.title; | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         note.setContent(noteRevision.getContent()); | 
					
						
							|  |  |  |         note.save(); | 
					
						
							| 
									
										
										
										
											2020-05-07 23:34:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getEditedNotesOnDate(req) { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:59:16 +02:00
										 |  |  |     const noteIds = sql.getColumn(`
 | 
					
						
							| 
									
										
										
										
											2019-12-01 14:30:59 +01:00
										 |  |  |         SELECT notes.* | 
					
						
							|  |  |  |         FROM notes | 
					
						
							|  |  |  |         WHERE noteId IN ( | 
					
						
							|  |  |  |                 SELECT noteId FROM notes  | 
					
						
							| 
									
										
										
										
											2020-06-20 23:24:34 +02:00
										 |  |  |                 WHERE notes.dateCreated LIKE :date | 
					
						
							|  |  |  |                    OR notes.dateModified LIKE :date | 
					
						
							| 
									
										
										
										
											2019-12-01 14:30:59 +01:00
										 |  |  |             UNION ALL | 
					
						
							|  |  |  |                 SELECT noteId FROM note_revisions | 
					
						
							| 
									
										
										
										
											2020-06-20 23:24:34 +02:00
										 |  |  |                 WHERE note_revisions.dateLastEdited LIKE :date | 
					
						
							| 
									
										
										
										
											2019-12-01 14:30:59 +01:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  |         ORDER BY isDeleted | 
					
						
							| 
									
										
										
										
											2020-06-20 23:24:34 +02:00
										 |  |  |         LIMIT 50`, {date: req.params.date + '%'});
 | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:59:16 +02:00
										 |  |  |     const notes = becca.getNotes(noteIds); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  |     for (const note of notes) { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         const notePath = note.isDeleted ? null : beccaService.getNotePath(note.noteId); | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         note.notePath = notePath ? notePath.notePath : null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return notes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  |     getNoteRevisions, | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |     getNoteRevision, | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  |     downloadNoteRevision, | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     getEditedNotesOnDate, | 
					
						
							|  |  |  |     eraseAllNoteRevisions, | 
					
						
							| 
									
										
										
										
											2020-05-07 23:34:13 +02:00
										 |  |  |     eraseNoteRevision, | 
					
						
							|  |  |  |     restoreNoteRevision | 
					
						
							| 
									
										
										
										
											2020-05-12 10:28:31 +02:00
										 |  |  | }; |