| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 19:47:05 -04:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  | const protectedSessionService = require('../../services/protected_session'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | async function getRecentChanges() { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const recentChanges = await sql.getRows( | 
					
						
							| 
									
										
										
										
											2019-09-02 20:26:18 +02:00
										 |  |  |         `
 | 
					
						
							|  |  |  |         SELECT * FROM ( | 
					
						
							|  |  |  |             SELECT  | 
					
						
							|  |  |  |                 notes.noteId, | 
					
						
							|  |  |  |                 notes.isDeleted AS current_isDeleted, | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  |                 notes.deleteId AS current_deleteId, | 
					
						
							|  |  |  |                 notes.isErased AS current_isErased, | 
					
						
							| 
									
										
										
										
											2019-09-02 20:26:18 +02:00
										 |  |  |                 notes.title AS current_title, | 
					
						
							|  |  |  |                 notes.isProtected AS current_isProtected, | 
					
						
							|  |  |  |                 note_revisions.title, | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |                 note_revisions.utcDateCreated AS date | 
					
						
							| 
									
										
										
										
											2019-09-02 20:26:18 +02:00
										 |  |  |             FROM  | 
					
						
							|  |  |  |                 note_revisions | 
					
						
							|  |  |  |                 JOIN notes USING(noteId) | 
					
						
							| 
									
										
										
										
											2019-11-19 19:02:16 +01:00
										 |  |  |             ORDER BY | 
					
						
							|  |  |  |                 note_revisions.utcDateCreated DESC | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  |             LIMIT 200 | 
					
						
							| 
									
										
										
										
											2019-09-02 20:26:18 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |         UNION ALL SELECT * FROM ( | 
					
						
							|  |  |  |             SELECT | 
					
						
							|  |  |  |                 notes.noteId, | 
					
						
							|  |  |  |                 notes.isDeleted AS current_isDeleted, | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  |                 notes.deleteId AS current_deleteId, | 
					
						
							|  |  |  |                 notes.isErased AS current_isErased, | 
					
						
							| 
									
										
										
										
											2019-09-02 20:26:18 +02:00
										 |  |  |                 notes.title AS current_title, | 
					
						
							|  |  |  |                 notes.isProtected AS current_isProtected, | 
					
						
							|  |  |  |                 notes.title, | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  |                 notes.utcDateModified AS date | 
					
						
							| 
									
										
										
										
											2019-09-02 20:26:18 +02:00
										 |  |  |             FROM | 
					
						
							|  |  |  |                 notes | 
					
						
							|  |  |  |             ORDER BY | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  |                 utcDateModified DESC | 
					
						
							|  |  |  |             LIMIT 200 | 
					
						
							| 
									
										
										
										
											2019-09-02 20:26:18 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |         ORDER BY date DESC  | 
					
						
							|  |  |  |         LIMIT 200`);
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 20:44:20 +01:00
										 |  |  |     for (const change of recentChanges) { | 
					
						
							|  |  |  |         if (change.current_isProtected) { | 
					
						
							|  |  |  |             if (protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							| 
									
										
										
										
											2019-11-02 07:50:23 +01:00
										 |  |  |                 change.title = protectedSessionService.decryptString(change.title); | 
					
						
							|  |  |  |                 change.current_title = protectedSessionService.decryptString(change.current_title); | 
					
						
							| 
									
										
										
										
											2019-03-04 20:44:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  |                 change.title = change.current_title = "[Protected]"; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (change.current_isDeleted) { | 
					
						
							|  |  |  |             if (change.current_isErased) { | 
					
						
							|  |  |  |                 change.canBeUndeleted = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 const deleteId = change.current_deleteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const undeletedParentCount = await sql.getValue(`
 | 
					
						
							|  |  |  |                     SELECT COUNT(parentNote.noteId) | 
					
						
							|  |  |  |                     FROM branches | 
					
						
							|  |  |  |                     JOIN notes AS parentNote ON parentNote.noteId = branches.parentNoteId | 
					
						
							|  |  |  |                     WHERE branches.noteId = ? | 
					
						
							|  |  |  |                       AND branches.isDeleted = 1 | 
					
						
							|  |  |  |                       AND branches.deleteId = ? | 
					
						
							|  |  |  |                       AND parentNote.isDeleted = 0`, [change.noteId, deleteId]);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // note (and the subtree) can be undeleted if there's at least one undeleted parent (whose branch would be undeleted by this op)
 | 
					
						
							|  |  |  |                 change.canBeUndeleted = undeletedParentCount > 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-06 17:47:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  |     return recentChanges; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     getRecentChanges | 
					
						
							|  |  |  | }; |