mirror of https://github.com/TriliumNext/Notes
persisted recent notes
parent
30df3cba1c
commit
3fb30a0b5c
@ -0,0 +1,4 @@
|
||||
CREATE TABLE `recent_notes` (
|
||||
`note_id` TEXT NOT NULL PRIMARY KEY,
|
||||
`date_accessed` INTEGER NOT NULL
|
||||
);
|
||||
@ -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;
|
||||
Loading…
Reference in New Issue