trilium/src/routes/api/recent_notes.js

52 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
2018-03-25 09:39:15 +08:00
router.put('/:branchId/:notePath', auth.checkApiAuth, wrap(async (req, res, next) => {
const branchId = req.params.branchId;
2017-11-20 01:06:48 +08:00
const notePath = req.params.notePath;
2018-01-29 10:57:46 +08:00
const sourceId = req.headers.source_id;
await sql.doInTransaction(async () => {
await sql.replace('recent_notes', {
2018-03-25 09:39:15 +08:00
branchId: branchId,
2018-01-29 08:30:14 +08:00
notePath: notePath,
dateAccessed: utils.nowDate(),
isDeleted: 0
});
2017-11-05 11:46:50 +08:00
2018-03-25 09:39:15 +08:00
await sync_table.addRecentNoteSync(branchId, sourceId);
2018-01-29 09:52:05 +08:00
await options.setOption('start_note_path', 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.getRows(`
SELECT
recent_notes.*
FROM
recent_notes
2018-03-25 09:39:15 +08:00
JOIN branches USING(branchId)
WHERE
2018-01-29 08:30:14 +08:00
recent_notes.isDeleted = 0
2018-03-25 09:39:15 +08:00
AND branches.isDeleted = 0
ORDER BY
dateAccessed DESC
LIMIT 200`);
2017-11-05 11:46:50 +08:00
}
module.exports = router;