| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 19:12:52 -04:00
										 |  |  | const noteCacheService = require('../../services/note_cache'); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2018-11-04 22:10:52 +01:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2018-12-12 21:28:38 +01:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							|  |  |  | const optionService = require('../../services/options'); | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function getAutocomplete(req) { | 
					
						
							| 
									
										
										
										
											2019-09-09 20:29:07 +02:00
										 |  |  |     const query = req.query.query.trim(); | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |     const activeNoteId = req.query.activeNoteId || 'none'; | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |     let results; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-04 22:10:52 +01:00
										 |  |  |     const timestampStarted = Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-09 20:29:07 +02:00
										 |  |  |     if (query.length === 0) { | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |         results = await getRecentNotes(activeNoteId); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2020-05-15 11:04:55 +02:00
										 |  |  |         results = await noteCacheService.findNotesWithFulltext(query); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-04 22:10:52 +01:00
										 |  |  |     const msTaken = Date.now() - timestampStarted; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (msTaken >= 100) { | 
					
						
							|  |  |  |         log.info(`Slow autocomplete took ${msTaken}ms`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 09:35:29 +01:00
										 |  |  |     return results; | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  | async function getRecentNotes(activeNoteId) { | 
					
						
							| 
									
										
										
										
											2018-12-12 21:28:38 +01:00
										 |  |  |     let extraCondition = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const hoistedNoteId = await optionService.getOption('hoistedNoteId'); | 
					
						
							|  |  |  |     if (hoistedNoteId !== 'root') { | 
					
						
							|  |  |  |         extraCondition = `AND recent_notes.notePath LIKE '%${utils.sanitizeSql(hoistedNoteId)}%'`; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |     const recentNotes = await repository.getEntities(`
 | 
					
						
							|  |  |  |       SELECT  | 
					
						
							|  |  |  |         recent_notes.*  | 
					
						
							|  |  |  |       FROM  | 
					
						
							|  |  |  |         recent_notes | 
					
						
							| 
									
										
										
										
											2019-05-21 21:47:28 +02:00
										 |  |  |         JOIN notes USING(noteId) | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |       WHERE | 
					
						
							|  |  |  |         recent_notes.isDeleted = 0 | 
					
						
							| 
									
										
										
										
											2019-05-21 21:47:28 +02:00
										 |  |  |         AND notes.isDeleted = 0 | 
					
						
							|  |  |  |         AND notes.noteId != ? | 
					
						
							| 
									
										
										
										
											2018-12-12 21:28:38 +01:00
										 |  |  |         ${extraCondition} | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |       ORDER BY  | 
					
						
							| 
									
										
										
										
											2019-03-12 20:58:31 +01:00
										 |  |  |         utcDateCreated DESC | 
					
						
							| 
									
										
										
										
											2019-03-14 20:21:27 +01:00
										 |  |  |       LIMIT 200`, [activeNoteId]);
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return recentNotes.map(rn => { | 
					
						
							| 
									
										
										
										
											2018-11-07 17:16:33 +01:00
										 |  |  |         const title = noteCacheService.getNoteTitleForPath(rn.notePath.split('/')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |         return { | 
					
						
							|  |  |  |             path: rn.notePath, | 
					
						
							| 
									
										
										
										
											2019-11-07 22:43:01 +01:00
										 |  |  |             pathTitle: title, | 
					
						
							|  |  |  |             highlightedTitle: title, | 
					
						
							|  |  |  |             noteTitle: noteCacheService.getNoteTitleFromPath(rn.notePath) | 
					
						
							| 
									
										
										
										
											2018-07-26 16:05:09 +02:00
										 |  |  |         }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 00:26:42 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     getAutocomplete | 
					
						
							| 
									
										
										
										
											2020-05-15 11:04:55 +02:00
										 |  |  | }; |