trilium/routes/api/recent_notes.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-11-05 11:46:50 +08:00
"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');
const sync_table = require('../../services/sync_table');
const options = require('../../services/options');
2017-11-05 11:46:50 +08:00
router.get('', auth.checkApiAuth, async (req, res, next) => {
res.send(await getRecentNotes());
});
2017-11-20 01:06:48 +08:00
router.put('/:notePath', auth.checkApiAuth, async (req, res, next) => {
const notePath = req.params.notePath;
2017-11-05 11:46:50 +08:00
await sql.replace('recent_notes', {
2017-11-20 01:06:48 +08:00
note_path: notePath,
2017-11-05 12:16:02 +08:00
date_accessed: utils.nowTimestamp(),
is_deleted: 0
2017-11-05 11:46:50 +08:00
});
2017-11-20 01:06:48 +08:00
await sync_table.addRecentNoteSync(notePath);
2017-11-20 01:06:48 +08:00
await options.setOption('start_note_tree_id', notePath);
2017-11-05 12:16:02 +08:00
2017-11-05 11:46:50 +08:00
res.send(await getRecentNotes());
});
2017-11-20 01:06:48 +08:00
router.delete('/:notePath', auth.checkApiAuth, async (req, res, next) => {
await sql.execute('UPDATE recent_notes SET is_deleted = 1 WHERE note_path = ?', [req.params.notePath]);
2017-11-05 12:16:02 +08:00
2017-11-20 01:06:48 +08:00
await sync_table.addRecentNoteSync(req.params.notePath);
2017-11-05 11:46:50 +08:00
res.send(await getRecentNotes());
});
async function getRecentNotes() {
await deleteOld();
2017-11-05 12:16:02 +08:00
return await sql.getResults("SELECT * FROM recent_notes WHERE is_deleted = 0 ORDER BY date_accessed DESC");
2017-11-05 11:46:50 +08:00
}
async function deleteOld() {
2017-11-05 12:16:02 +08:00
const cutoffDateAccessed = await sql.getSingleValue("SELECT date_accessed FROM recent_notes WHERE is_deleted = 0 ORDER BY date_accessed DESC LIMIT 100, 1");
2017-11-05 11:46:50 +08:00
if (cutoffDateAccessed) {
await sql.execute("DELETE FROM recent_notes WHERE date_accessed < ?", [cutoffDateAccessed]);
}
}
module.exports = router;