Skip to content

Build your personal knowledge base with Trilium Notes

persisted recent notes

azivner3fb30a0b5c44
diff --git a/migrations/0026__recent_notes_table.sql b/migrations/0026__recent_notes_table.sql
new file mode 100644
index 0000000000..e7a621e0a5
--- /dev/null
+++ b/migrations/0026__recent_notes_table.sql
@@ -0,0 +1,4 @@
+CREATE TABLE `recent_notes` (
+ `note_id` TEXT NOT NULL PRIMARY KEY,
+ `date_accessed` INTEGER NOT NULL
+);
\ No newline at end of file
diff --git a/public/javascripts/dialogs/recent_notes.js b/public/javascripts/dialogs/recent_notes.js
index 73268fe66a..52c3f4416f 100644
--- a/public/javascripts/dialogs/recent_notes.js
+++ b/public/javascripts/dialogs/recent_notes.js
@@ -8,20 +8,37 @@ const recentNotes = (function() {
const noteDetailEl = $('#note-detail');
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) {
setTimeout(() => {
// 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 it's already there, remove the note
- list = list.filter(note => note !== noteTreeId);
-
- list.unshift(noteTreeId);
+ $.ajax({
+ url: baseApiUrl + 'recent-notes/' + noteTreeId,
+ type: 'PUT',
+ error: () => error("Error setting recent notes.")
+ }).then(result => {
+ list = result.map(r => r.note_id);
+ });
}
}, 1500);
}
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() {
diff --git a/routes/api/recent_notes.js b/routes/api/recent_notes.js
new file mode 100644
index 0000000000..22056db84a
--- /dev/null
+++ b/routes/api/recent_notes.js
@@ -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;
\ No newline at end of file
diff --git a/routes/routes.js b/routes/routes.js
index fb9994f55d..c917e87055 100644
--- a/routes/routes.js
+++ b/routes/routes.js
@@ -16,6 +16,7 @@ const migrationApiRoute = require('./api/migration');
const syncApiRoute = require('./api/sync');
const loginApiRoute = require('./api/login');
const eventLogRoute = require('./api/event_log');
+const recentNotesRoute = require('./api/recent_notes');
function register(app) {
app.use('/', indexRoute);
@@ -35,6 +36,7 @@ function register(app) {
app.use('/api/sync', syncApiRoute);
app.use('/api/login', loginApiRoute);
app.use('/api/event-log', eventLogRoute);
+ app.use('/api/recent-notes', recentNotesRoute);
}
module.exports = {
diff --git a/services/migration.js b/services/migration.js
index e4045ca470..a6076ba448 100644
--- a/services/migration.js
+++ b/services/migration.js
@@ -4,7 +4,7 @@ const options = require('./options');
const fs = require('fs-extra');
const log = require('./log');
-const APP_DB_VERSION = 25;
+const APP_DB_VERSION = 26;
const MIGRATIONS_DIR = "./migrations";
async function migrate() {