| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 10:12:02 +01:00
										 |  |  | const attributeService = require("../../services/attributes"); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | const noteService = require('../../services/notes'); | 
					
						
							|  |  |  | const dateNoteService = require('../../services/date_notes'); | 
					
						
							|  |  |  | const dateUtils = require('../../services/date_utils'); | 
					
						
							|  |  |  | const imageService = require('../../services/image'); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | const appInfo = require('../../services/app_info'); | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  | const ws = require('../../services/ws.js'); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2019-07-07 13:12:40 +02:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2021-05-17 22:09:49 +02:00
										 |  |  | const Attribute = require('../../becca/entities/attribute.js'); | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  | const htmlSanitizer = require('../../services/html_sanitizer'); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function findClippingNote(todayNote, pageUrl) { | 
					
						
							|  |  |  |     const notes = todayNote.getDescendantNotesWithLabel('pageUrl', pageUrl); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const note of notes) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         if (note.getOwnedLabelValue('clipType') === 'clippings') { | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |             return note; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getClipperInboxNote() { | 
					
						
							|  |  |  |     let clipperInbox = attributeService.getNoteWithLabel('clipperInbox'); | 
					
						
							| 
									
										
										
										
											2020-02-09 10:12:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!clipperInbox) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         clipperInbox = dateNoteService.getDateNote(dateUtils.localNowDate()); | 
					
						
							| 
									
										
										
										
											2020-02-09 10:12:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return clipperInbox; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function addClipping(req) { | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     const {title, content, pageUrl, images} = req.body; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const clipperInbox = getClipperInboxNote(); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     let clippingNote = findClippingNote(clipperInbox, pageUrl); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!clippingNote) { | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  |         clippingNote = noteService.createNewNote({ | 
					
						
							| 
									
										
										
										
											2020-02-09 10:12:02 +01:00
										 |  |  |             parentNoteId: clipperInbox.noteId, | 
					
						
							| 
									
										
										
										
											2019-11-16 11:09:52 +01:00
										 |  |  |             title: title, | 
					
						
							| 
									
										
										
										
											2019-12-08 09:41:31 +01:00
										 |  |  |             content: '', | 
					
						
							|  |  |  |             type: 'text' | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  |         }).note; | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         clippingNote.setLabel('clipType', 'clippings'); | 
					
						
							|  |  |  |         clippingNote.setLabel('pageUrl', pageUrl); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 20:32:20 +02:00
										 |  |  |     const rewrittenContent = processContent(images, clippingNote, content); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const existingContent = clippingNote.getContent(); | 
					
						
							| 
									
										
										
										
											2020-04-05 15:35:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     clippingNote.setContent(existingContent + (existingContent.trim() ? "<br/>" : "") + rewrittenContent); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         noteId: clippingNote.noteId | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function createNote(req) { | 
					
						
							| 
									
										
										
										
											2020-07-11 23:05:28 +02:00
										 |  |  |     let {title, content, pageUrl, images, clipType} = req.body; | 
					
						
							| 
									
										
										
										
											2020-04-08 11:07:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 23:05:28 +02:00
										 |  |  |     if (!title || !title.trim()) { | 
					
						
							|  |  |  |         title = "Clipped note from " + pageUrl; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const clipperInbox = getClipperInboxNote(); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const {note} = noteService.createNewNote({ | 
					
						
							| 
									
										
										
										
											2020-02-09 10:12:02 +01:00
										 |  |  |         parentNoteId: clipperInbox.noteId, | 
					
						
							| 
									
										
										
										
											2019-11-16 11:09:52 +01:00
										 |  |  |         title, | 
					
						
							| 
									
										
										
										
											2019-12-08 09:41:31 +01:00
										 |  |  |         content, | 
					
						
							|  |  |  |         type: 'text' | 
					
						
							| 
									
										
										
										
											2019-11-16 11:09:52 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.setLabel('clipType', clipType); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (pageUrl) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         note.setLabel('pageUrl', pageUrl); | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 20:32:20 +02:00
										 |  |  |     const rewrittenContent = processContent(images, note, content); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.setContent(rewrittenContent); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         noteId: note.noteId | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 20:32:20 +02:00
										 |  |  | function processContent(images, note, content) { | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  |     let rewrittenContent = htmlSanitizer.sanitize(content) | 
					
						
							|  |  |  |         // H1 is not supported so convert it to H2
 | 
					
						
							| 
									
										
										
										
											2020-09-16 20:32:20 +02:00
										 |  |  |         .replace(/<h1/ig, "<h2") | 
					
						
							|  |  |  |         .replace(/<\/h1/ig, "</h2"); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     if (images) { | 
					
						
							|  |  |  |         for (const {src, dataUrl, imageId} of images) { | 
					
						
							|  |  |  |             const filename = path.basename(src); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 20:39:42 +01:00
										 |  |  |             if (!dataUrl || !dataUrl.startsWith("data:image")) { | 
					
						
							| 
									
										
										
										
											2021-04-12 23:29:02 +02:00
										 |  |  |                 const excerpt = dataUrl | 
					
						
							|  |  |  |                     ? dataUrl.substr(0, Math.min(100, dataUrl.length)) | 
					
						
							|  |  |  |                     : "null"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 log.info("Image could not be recognized as data URL: " + excerpt); | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             const buffer = Buffer.from(dataUrl.split(",")[1], 'base64'); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             const {note: imageNote, url} = imageService.saveImage(note.noteId, buffer, filename, true); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             new Attribute({ | 
					
						
							| 
									
										
										
										
											2020-05-13 23:06:13 +02:00
										 |  |  |                 noteId: imageNote.noteId, | 
					
						
							|  |  |  |                 type: 'label', | 
					
						
							| 
									
										
										
										
											2020-09-07 00:05:01 +02:00
										 |  |  |                 name: 'archived' | 
					
						
							|  |  |  |             }).save(); // so that these image notes don't show up in search / autocomplete
 | 
					
						
							| 
									
										
										
										
											2020-05-13 23:06:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             new Attribute({ | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |                 noteId: note.noteId, | 
					
						
							| 
									
										
										
										
											2019-08-19 20:12:00 +02:00
										 |  |  |                 type: 'relation', | 
					
						
							| 
									
										
										
										
											2020-05-13 23:06:13 +02:00
										 |  |  |                 name: 'imageLink', | 
					
						
							|  |  |  |                 value: imageNote.noteId | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             }).save(); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             console.log(`Replacing ${imageId} with ${url}`); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 22:55:11 +02:00
										 |  |  |             rewrittenContent = utils.replaceAll(rewrittenContent, imageId, url); | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     return rewrittenContent; | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function openNote(req) { | 
					
						
							| 
									
										
										
										
											2019-07-07 13:12:40 +02:00
										 |  |  |     if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  |         ws.sendMessageToAllClients({ | 
					
						
							| 
									
										
										
										
											2021-04-24 11:39:44 +02:00
										 |  |  |             type: 'openNote', | 
					
						
							| 
									
										
										
										
											2019-07-07 13:12:40 +02:00
										 |  |  |             noteId: req.params.noteId | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result: 'ok' | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result: 'open-in-browser' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function handshake() { | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         appName: "trilium", | 
					
						
							| 
									
										
										
										
											2019-07-07 22:27:06 +02:00
										 |  |  |         protocolVersion: appInfo.clipperProtocolVersion | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     createNote, | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     addClipping, | 
					
						
							| 
									
										
										
										
											2019-06-23 13:25:00 +02:00
										 |  |  |     openNote, | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     handshake | 
					
						
							| 
									
										
										
										
											2020-05-13 23:06:13 +02:00
										 |  |  | }; |