trilium/routes/api/sync.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-10-25 10:58:59 +08:00
"use strict";
const express = require('express');
const router = express.Router();
const auth = require('../../services/auth');
2017-10-27 09:16:21 +08:00
const sync = require('../../services/sync');
2017-11-10 09:52:47 +08:00
const syncUpdate = require('../../services/sync_update');
2017-11-01 07:34:58 +08:00
const sql = require('../../services/sql');
2017-11-03 08:48:02 +08:00
const options = require('../../services/options');
2017-10-25 10:58:59 +08:00
2017-10-29 23:22:41 +08:00
router.post('/now', auth.checkApiAuth, async (req, res, next) => {
res.send(await sync.sync());
2017-10-29 23:22:41 +08:00
});
2017-11-01 07:34:58 +08:00
router.get('/changed', auth.checkApiAuth, async (req, res, next) => {
const lastSyncId = parseInt(req.query.lastSyncId);
2017-10-25 10:58:59 +08:00
res.send(await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSyncId]));
2017-10-27 09:16:21 +08:00
});
2017-11-01 07:34:58 +08:00
router.get('/notes/:noteId', auth.checkApiAuth, async (req, res, next) => {
const noteId = req.params.noteId;
2017-10-27 09:16:21 +08:00
2017-11-01 07:34:58 +08:00
res.send({
entity: await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]),
links: await sql.getResults("SELECT * FROM links WHERE note_id = ?", [noteId])
});
2017-10-26 10:39:21 +08:00
});
2017-11-01 07:34:58 +08:00
router.get('/notes_tree/:noteId', auth.checkApiAuth, async (req, res, next) => {
2017-10-26 10:39:21 +08:00
const noteId = req.params.noteId;
2017-11-01 07:34:58 +08:00
res.send(await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_id = ?", [noteId]));
});
router.get('/notes_history/:noteHistoryId', auth.checkApiAuth, async (req, res, next) => {
const noteHistoryId = req.params.noteHistoryId;
res.send(await sql.getSingleResult("SELECT * FROM notes_history WHERE note_history_id = ?", [noteHistoryId]));
2017-10-27 09:16:21 +08:00
});
2017-11-03 08:48:02 +08:00
router.get('/options/:optName', auth.checkApiAuth, async (req, res, next) => {
const optName = req.params.optName;
if (!options.SYNCED_OPTIONS.includes(optName)) {
res.send("This option can't be synced.");
}
else {
res.send(await sql.getSingleResult("SELECT * FROM options WHERE opt_name = ?", [optName]));
}
});
2017-11-03 10:55:22 +08:00
router.get('/notes_reordering/:noteParentId', auth.checkApiAuth, async (req, res, next) => {
const noteParentId = req.params.noteParentId;
res.send({
note_pid: noteParentId,
ordering: await sql.getMap("SELECT note_id, note_pos FROM notes_tree WHERE note_pid = ?", [noteParentId])
});
});
2017-11-05 12:16:02 +08:00
router.get('/recent_notes/:noteId', auth.checkApiAuth, async (req, res, next) => {
const noteId = req.params.noteId;
res.send(await sql.getSingleResult("SELECT * FROM recent_notes WHERE note_id = ?", [noteId]));
});
2017-10-30 10:22:30 +08:00
router.put('/notes', auth.checkApiAuth, async (req, res, next) => {
2017-11-10 09:52:47 +08:00
await syncUpdate.updateNote(req.body.entity, req.body.links, req.body.sourceId);
2017-10-30 10:22:30 +08:00
res.send({});
});
router.put('/notes_tree', auth.checkApiAuth, async (req, res, next) => {
2017-11-10 09:52:47 +08:00
await syncUpdate.updateNoteTree(req.body.entity, req.body.sourceId);
2017-10-30 10:22:30 +08:00
res.send({});
});
router.put('/notes_history', auth.checkApiAuth, async (req, res, next) => {
2017-11-10 09:52:47 +08:00
await syncUpdate.updateNoteHistory(req.body.entity, req.body.sourceId);
2017-10-26 10:39:21 +08:00
2017-10-27 09:16:21 +08:00
res.send({});
2017-10-25 10:58:59 +08:00
});
2017-11-03 10:55:22 +08:00
router.put('/notes_reordering', auth.checkApiAuth, async (req, res, next) => {
2017-11-10 09:52:47 +08:00
await syncUpdate.updateNoteReordering(req.body.entity, req.body.sourceId);
2017-11-03 10:55:22 +08:00
res.send({});
});
2017-11-03 08:48:02 +08:00
router.put('/options', auth.checkApiAuth, async (req, res, next) => {
2017-11-10 09:52:47 +08:00
await syncUpdate.updateOptions(req.body.entity, req.body.sourceId);
2017-11-03 08:48:02 +08:00
res.send({});
});
2017-11-05 12:16:02 +08:00
router.put('/recent_notes', auth.checkApiAuth, async (req, res, next) => {
2017-11-10 09:52:47 +08:00
await syncUpdate.updateRecentNotes(req.body.entity, req.body.sourceId);
2017-11-05 12:16:02 +08:00
res.send({});
});
2017-10-25 10:58:59 +08:00
module.exports = router;