mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	persisted recent notes
This commit is contained in:
		
							parent
							
								
									30df3cba1c
								
							
						
					
					
						commit
						3fb30a0b5c
					
				
							
								
								
									
										4
									
								
								migrations/0026__recent_notes_table.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								migrations/0026__recent_notes_table.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | |||||||
|  | CREATE TABLE `recent_notes` ( | ||||||
|  | 	`note_id`	TEXT NOT NULL PRIMARY KEY, | ||||||
|  | 	`date_accessed`	INTEGER NOT NULL | ||||||
|  | ); | ||||||
| @ -8,20 +8,37 @@ const recentNotes = (function() { | |||||||
|     const noteDetailEl = $('#note-detail'); |     const noteDetailEl = $('#note-detail'); | ||||||
|     let list = []; |     let list = []; | ||||||
| 
 | 
 | ||||||
|  |     $.ajax({ | ||||||
|  |         url: baseApiUrl + 'recent-notes', | ||||||
|  |         type: 'GET', | ||||||
|  |         error: () => error("Error getting recent notes.") | ||||||
|  |     }).then(result => { | ||||||
|  |         list = result.map(r => r.note_id); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|     function addRecentNote(noteTreeId, noteContentId) { |     function addRecentNote(noteTreeId, noteContentId) { | ||||||
|         setTimeout(() => { |         setTimeout(() => { | ||||||
|             // we include the note into recent list only if the user stayed on the note at least 5 seconds
 |             // we include the note into recent list only if the user stayed on the note at least 5 seconds
 | ||||||
|             if (noteTreeId === noteEditor.getCurrentNoteId() || noteContentId === noteEditor.getCurrentNoteId()) { |             if (noteTreeId === noteEditor.getCurrentNoteId() || noteContentId === noteEditor.getCurrentNoteId()) { | ||||||
|                 // if it's already there, remove the note
 |                 $.ajax({ | ||||||
|                 list = list.filter(note => note !== noteTreeId); |                     url: baseApiUrl + 'recent-notes/' + noteTreeId, | ||||||
| 
 |                     type: 'PUT', | ||||||
|                 list.unshift(noteTreeId); |                     error: () => error("Error setting recent notes.") | ||||||
|  |                 }).then(result => { | ||||||
|  |                     list = result.map(r => r.note_id); | ||||||
|  |                 }); | ||||||
|             } |             } | ||||||
|         }, 1500); |         }, 1500); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function removeRecentNote(noteIdToRemove) { |     function removeRecentNote(noteIdToRemove) { | ||||||
|         list = list.filter(note => note !== noteIdToRemove); |         $.ajax({ | ||||||
|  |             url: baseApiUrl + 'recent-notes/' + noteIdToRemove, | ||||||
|  |             type: 'DELETE', | ||||||
|  |             error: () => error("Error removing note from recent notes.") | ||||||
|  |         }).then(result => { | ||||||
|  |             list = result.map(r => r.note_id); | ||||||
|  |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function showDialog() { |     function showDialog() { | ||||||
|  | |||||||
							
								
								
									
										42
									
								
								routes/api/recent_notes.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								routes/api/recent_notes.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,42 @@ | |||||||
|  | "use strict"; | ||||||
|  | 
 | ||||||
|  | const express = require('express'); | ||||||
|  | const router = express.Router(); | ||||||
|  | const sql = require('../../services/sql'); | ||||||
|  | const auth = require('../../services/auth'); | ||||||
|  | const utils = require('../../services/utils'); | ||||||
|  | 
 | ||||||
|  | router.get('', auth.checkApiAuth, async (req, res, next) => { | ||||||
|  |     res.send(await getRecentNotes()); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.put('/:noteId', auth.checkApiAuth, async (req, res, next) => { | ||||||
|  |     await sql.replace('recent_notes', { | ||||||
|  |         note_id: req.params.noteId, | ||||||
|  |         date_accessed: utils.nowTimestamp() | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     res.send(await getRecentNotes()); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.delete('/:noteId', auth.checkApiAuth, async (req, res, next) => { | ||||||
|  |     await sql.remove('recent_notes', req.params.noteId); | ||||||
|  | 
 | ||||||
|  |     res.send(await getRecentNotes()); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | async function getRecentNotes() { | ||||||
|  |     await deleteOld(); | ||||||
|  | 
 | ||||||
|  |     return await sql.getResults("SELECT * FROM recent_notes ORDER BY date_accessed DESC"); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function deleteOld() { | ||||||
|  |     const cutoffDateAccessed = await sql.getSingleValue("SELECT date_accessed FROM recent_notes ORDER BY date_accessed DESC LIMIT 100, 1"); | ||||||
|  | 
 | ||||||
|  |     if (cutoffDateAccessed) { | ||||||
|  |         await sql.execute("DELETE FROM recent_notes WHERE date_accessed < ?", [cutoffDateAccessed]); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = router; | ||||||
| @ -16,6 +16,7 @@ const migrationApiRoute = require('./api/migration'); | |||||||
| const syncApiRoute = require('./api/sync'); | const syncApiRoute = require('./api/sync'); | ||||||
| const loginApiRoute = require('./api/login'); | const loginApiRoute = require('./api/login'); | ||||||
| const eventLogRoute = require('./api/event_log'); | const eventLogRoute = require('./api/event_log'); | ||||||
|  | const recentNotesRoute = require('./api/recent_notes'); | ||||||
| 
 | 
 | ||||||
| function register(app) { | function register(app) { | ||||||
|     app.use('/', indexRoute); |     app.use('/', indexRoute); | ||||||
| @ -35,6 +36,7 @@ function register(app) { | |||||||
|     app.use('/api/sync', syncApiRoute); |     app.use('/api/sync', syncApiRoute); | ||||||
|     app.use('/api/login', loginApiRoute); |     app.use('/api/login', loginApiRoute); | ||||||
|     app.use('/api/event-log', eventLogRoute); |     app.use('/api/event-log', eventLogRoute); | ||||||
|  |     app.use('/api/recent-notes', recentNotesRoute); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|  | |||||||
| @ -4,7 +4,7 @@ const options = require('./options'); | |||||||
| const fs = require('fs-extra'); | const fs = require('fs-extra'); | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| 
 | 
 | ||||||
| const APP_DB_VERSION = 25; | const APP_DB_VERSION = 26; | ||||||
| const MIGRATIONS_DIR = "./migrations"; | const MIGRATIONS_DIR = "./migrations"; | ||||||
| 
 | 
 | ||||||
| async function migrate() { | async function migrate() { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner