trilium/routes/api/recent_notes.js

51 lines
1.4 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');
const wrap = require('express-promise-wrap').wrap;
2017-11-05 11:46:50 +08:00
router.get('', auth.checkApiAuth, wrap(async (req, res, next) => {
2017-11-05 11:46:50 +08:00
res.send(await getRecentNotes());
}));
2017-11-05 11:46:50 +08:00
router.put('/:noteTreeId/:notePath', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteTreeId = req.params.noteTreeId;
2017-11-20 01:06:48 +08:00
const notePath = req.params.notePath;
2018-01-29 08:30:14 +08:00
const sourceId = req.headers.sourceId;
await sql.doInTransaction(async () => {
await sql.replace('recent_notes', {
2018-01-29 08:30:14 +08:00
noteTreeId: noteTreeId,
notePath: notePath,
dateAccessed: utils.nowDate(),
isDeleted: 0
});
2017-11-05 11:46:50 +08:00
await sync_table.addRecentNoteSync(noteTreeId, sourceId);
2018-01-29 08:30:14 +08:00
await options.setOption('start_notePath', notePath, sourceId);
});
2017-11-05 12:16:02 +08:00
2017-11-05 11:46:50 +08:00
res.send(await getRecentNotes());
}));
2017-11-05 11:46:50 +08:00
async function getRecentNotes() {
return await sql.getAll(`
SELECT
recent_notes.*
FROM
recent_notes
2018-01-29 08:30:14 +08:00
JOIN notes_tree USING(noteTreeId)
WHERE
2018-01-29 08:30:14 +08:00
recent_notes.isDeleted = 0
AND notes_tree.isDeleted = 0
ORDER BY
2018-01-29 08:30:14 +08:00
dateAccessed DESC`);
2017-11-05 11:46:50 +08:00
}
module.exports = router;