mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	added sync for recent notes
This commit is contained in:
		
							
								
								
									
										1
									
								
								migrations/0027__is_deleted_in_recent_notes.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/0027__is_deleted_in_recent_notes.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | ALTER TABLE recent_notes ADD COLUMN is_deleted INT; | ||||||
| @@ -13,14 +13,19 @@ router.get('', auth.checkApiAuth, async (req, res, next) => { | |||||||
| router.put('/:noteId', auth.checkApiAuth, async (req, res, next) => { | router.put('/:noteId', auth.checkApiAuth, async (req, res, next) => { | ||||||
|     await sql.replace('recent_notes', { |     await sql.replace('recent_notes', { | ||||||
|         note_id: req.params.noteId, |         note_id: req.params.noteId, | ||||||
|         date_accessed: utils.nowTimestamp() |         date_accessed: utils.nowTimestamp(), | ||||||
|  |         is_deleted: 0 | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|  |     await sql.addRecentNoteSync(req.params.noteId); | ||||||
|  |  | ||||||
|     res.send(await getRecentNotes()); |     res.send(await getRecentNotes()); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| router.delete('/:noteId', auth.checkApiAuth, async (req, res, next) => { | router.delete('/:noteId', auth.checkApiAuth, async (req, res, next) => { | ||||||
|     await sql.remove('recent_notes', req.params.noteId); |     await sql.execute('UPDATE recent_notes SET is_deleted = 1 WHERE note_id = ?', [req.params.noteId]); | ||||||
|  |  | ||||||
|  |     await sql.addRecentNoteSync(req.params.noteId); | ||||||
|  |  | ||||||
|     res.send(await getRecentNotes()); |     res.send(await getRecentNotes()); | ||||||
| }); | }); | ||||||
| @@ -28,11 +33,11 @@ router.delete('/:noteId', auth.checkApiAuth, async (req, res, next) => { | |||||||
| async function getRecentNotes() { | async function getRecentNotes() { | ||||||
|     await deleteOld(); |     await deleteOld(); | ||||||
|  |  | ||||||
|     return await sql.getResults("SELECT * FROM recent_notes ORDER BY date_accessed DESC"); |     return await sql.getResults("SELECT * FROM recent_notes WHERE is_deleted = 0 ORDER BY date_accessed DESC"); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function deleteOld() { | async function deleteOld() { | ||||||
|     const cutoffDateAccessed = await sql.getSingleValue("SELECT date_accessed FROM recent_notes ORDER BY date_accessed DESC LIMIT 100, 1"); |     const cutoffDateAccessed = await sql.getSingleValue("SELECT date_accessed FROM recent_notes WHERE is_deleted = 0 ORDER BY date_accessed DESC LIMIT 100, 1"); | ||||||
|  |  | ||||||
|     if (cutoffDateAccessed) { |     if (cutoffDateAccessed) { | ||||||
|         await sql.execute("DELETE FROM recent_notes WHERE date_accessed < ?", [cutoffDateAccessed]); |         await sql.execute("DELETE FROM recent_notes WHERE date_accessed < ?", [cutoffDateAccessed]); | ||||||
|   | |||||||
| @@ -59,6 +59,12 @@ router.get('/notes_reordering/:noteParentId', auth.checkApiAuth, async (req, res | |||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | router.get('/recent_notes/:noteId', auth.checkApiAuth, async (req, res, next) => { | ||||||
|  |     const noteId = req.params.noteId; | ||||||
|  |  | ||||||
|  |     res.send(await sql.getSingleResult("SELECT * FROM recent_notes WHERE note_id = ?", [noteId])); | ||||||
|  | }); | ||||||
|  |  | ||||||
| router.put('/notes', auth.checkApiAuth, async (req, res, next) => { | router.put('/notes', auth.checkApiAuth, async (req, res, next) => { | ||||||
|     await sync.updateNote(req.body.entity, req.body.links, req.body.sourceId); |     await sync.updateNote(req.body.entity, req.body.links, req.body.sourceId); | ||||||
|  |  | ||||||
| @@ -89,4 +95,10 @@ router.put('/options', auth.checkApiAuth, async (req, res, next) => { | |||||||
|     res.send({}); |     res.send({}); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | router.put('/recent_notes', auth.checkApiAuth, async (req, res, next) => { | ||||||
|  |     await sync.updateRecentNotes(req.body.entity, req.body.sourceId); | ||||||
|  |  | ||||||
|  |     res.send({}); | ||||||
|  | }); | ||||||
|  |  | ||||||
| module.exports = router; | module.exports = router; | ||||||
| @@ -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 = 26; | const APP_DB_VERSION = 27; | ||||||
| const MIGRATIONS_DIR = "./migrations"; | const MIGRATIONS_DIR = "./migrations"; | ||||||
|  |  | ||||||
| async function migrate() { | async function migrate() { | ||||||
|   | |||||||
| @@ -148,6 +148,10 @@ async function addOptionsSync(optName, sourceId) { | |||||||
|     await addEntitySync("options", optName, sourceId); |     await addEntitySync("options", optName, sourceId); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function addRecentNoteSync(noteId, sourceId) { | ||||||
|  |     await addEntitySync("recent_notes", noteId, sourceId); | ||||||
|  | } | ||||||
|  |  | ||||||
| async function addEntitySync(entityName, entityId, sourceId) { | async function addEntitySync(entityName, entityId, sourceId) { | ||||||
|     await replace("sync", { |     await replace("sync", { | ||||||
|         entity_name: entityName, |         entity_name: entityName, | ||||||
| @@ -210,5 +214,6 @@ module.exports = { | |||||||
|     addNoteTreeSync, |     addNoteTreeSync, | ||||||
|     addNoteReorderingSync, |     addNoteReorderingSync, | ||||||
|     addNoteHistorySync, |     addNoteHistorySync, | ||||||
|     addOptionsSync |     addOptionsSync, | ||||||
|  |     addRecentNoteSync | ||||||
| }; | }; | ||||||
| @@ -67,6 +67,9 @@ async function pullSync(syncContext) { | |||||||
|         else if (sync.entity_name === 'options') { |         else if (sync.entity_name === 'options') { | ||||||
|             await updateOptions(resp, syncContext.sourceId); |             await updateOptions(resp, syncContext.sourceId); | ||||||
|         } |         } | ||||||
|  |         else if (sync.entity_name === 'recent_notes') { | ||||||
|  |             await updateRecentNotes(resp, syncContext.sourceId); | ||||||
|  |         } | ||||||
|         else { |         else { | ||||||
|             logSyncError("Unrecognized entity type " + sync.entity_name, e); |             logSyncError("Unrecognized entity type " + sync.entity_name, e); | ||||||
|         } |         } | ||||||
| @@ -123,6 +126,9 @@ async function readAndPushEntity(sync, syncContext) { | |||||||
|     else if (sync.entity_name === 'options') { |     else if (sync.entity_name === 'options') { | ||||||
|         entity = await sql.getSingleResult('SELECT * FROM options WHERE opt_name = ?', [sync.entity_id]); |         entity = await sql.getSingleResult('SELECT * FROM options WHERE opt_name = ?', [sync.entity_id]); | ||||||
|     } |     } | ||||||
|  |     else if (sync.entity_name === 'recent_notes') { | ||||||
|  |         entity = await sql.getSingleResult('SELECT * FROM recent_notes WHERE note_id = ?', [sync.entity_id]); | ||||||
|  |     } | ||||||
|     else { |     else { | ||||||
|         logSyncError("Unrecognized entity type " + sync.entity_name, null); |         logSyncError("Unrecognized entity type " + sync.entity_name, null); | ||||||
|     } |     } | ||||||
| @@ -358,6 +364,18 @@ async function updateOptions(entity, sourceId) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | async function updateRecentNotes(entity, sourceId) { | ||||||
|  |     const orig = await sql.getSingleResultOrNull("select * from recent_notes where note_id = ?", [entity.note_id]); | ||||||
|  |  | ||||||
|  |     if (orig === null || orig.date_accessed < entity.date_accessed) { | ||||||
|  |         await sql.doInTransaction(async () => { | ||||||
|  |             await sql.replace('recent_notes', entity); | ||||||
|  |  | ||||||
|  |             await sql.addRecentNoteSync(entity.note_id, sourceId); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| if (SYNC_SERVER) { | if (SYNC_SERVER) { | ||||||
|     log.info("Setting up sync"); |     log.info("Setting up sync"); | ||||||
|  |  | ||||||
| @@ -377,5 +395,6 @@ module.exports = { | |||||||
|     updateNoteHistory, |     updateNoteHistory, | ||||||
|     updateNoteReordering, |     updateNoteReordering, | ||||||
|     updateOptions, |     updateOptions, | ||||||
|  |     updateRecentNotes, | ||||||
|     isSyncSetup |     isSyncSetup | ||||||
| }; | }; | ||||||
		Reference in New Issue
	
	Block a user