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-12-23 22:35:00 +08:00
|
|
|
const sync_table = require('../../services/sync_table');
|
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-11-22 11:11:27 +08:00
|
|
|
const content_hash = require('../../services/content_hash');
|
2017-12-24 02:16:18 +08:00
|
|
|
const log = require('../../services/log');
|
2018-01-07 22:35:44 +08:00
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2017-11-22 11:11:27 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/check', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-22 11:11:27 +08:00
|
|
|
res.send({
|
2017-12-16 10:14:10 +08:00
|
|
|
'hashes': await content_hash.getHashes(),
|
2018-01-30 06:41:59 +08:00
|
|
|
'max_sync_id': await sql.getValue('SELECT MAX(id) FROM sync')
|
2017-11-22 11:11:27 +08:00
|
|
|
});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-25 10:58:59 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.post('/now', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-05 09:21:09 +08:00
|
|
|
res.send(await sync.sync());
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-29 23:22:41 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.post('/fill-sync-rows', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-20 11:04:51 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-12-24 02:55:13 +08:00
|
|
|
await sync_table.fillAllSyncRows();
|
2017-12-20 11:04:51 +08:00
|
|
|
});
|
|
|
|
|
2017-12-24 02:16:18 +08:00
|
|
|
log.info("Sync rows have been filled.");
|
|
|
|
|
2017-12-20 11:04:51 +08:00
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-12-20 11:04:51 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.post('/force-full-sync', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-14 12:03:48 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await options.setOption('last_synced_pull', 0);
|
|
|
|
await options.setOption('last_synced_push', 0);
|
|
|
|
});
|
|
|
|
|
2017-12-24 02:16:18 +08:00
|
|
|
log.info("Forcing full sync.");
|
|
|
|
|
2017-12-14 12:03:48 +08:00
|
|
|
// not awaiting for the job to finish (will probably take a long time)
|
|
|
|
sync.sync();
|
|
|
|
|
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-12-14 12:03:48 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.post('/force-note-sync/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-31 10:44:26 +08:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sync_table.addNoteSync(noteId);
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
for (const noteTreeId of await sql.getColumn("SELECT noteTreeId FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [noteId])) {
|
2017-12-31 10:44:26 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId);
|
|
|
|
await sync_table.addRecentNoteSync(noteTreeId);
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) {
|
2018-01-29 08:38:05 +08:00
|
|
|
await sync_table.addNoteHistorySync(noteRevisionId);
|
2017-12-31 10:44:26 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
log.info("Forcing note sync for " + noteId);
|
|
|
|
|
|
|
|
// not awaiting for the job to finish (will probably take a long time)
|
|
|
|
sync.sync();
|
|
|
|
|
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-12-31 10:44:26 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/changed', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-01 07:34:58 +08:00
|
|
|
const lastSyncId = parseInt(req.query.lastSyncId);
|
2017-10-25 10:58:59 +08:00
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
res.send(await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSyncId]));
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-27 09:16:21 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/notes/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-01 07:34:58 +08:00
|
|
|
const noteId = req.params.noteId;
|
2018-02-19 11:55:36 +08:00
|
|
|
const entity = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
|
|
|
|
|
|
|
sync.serializeNoteContentBuffer(entity);
|
2017-10-27 09:16:21 +08:00
|
|
|
|
2017-11-01 07:34:58 +08:00
|
|
|
res.send({
|
2018-02-19 11:55:36 +08:00
|
|
|
entity: entity
|
2017-11-01 07:34:58 +08:00
|
|
|
});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
router.get('/note_tree/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-24 11:25:39 +08:00
|
|
|
const noteTreeId = req.params.noteTreeId;
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
res.send(await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]));
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-01 07:34:58 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
router.get('/note_revisions/:noteRevisionId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const noteRevisionId = req.params.noteRevisionId;
|
2017-11-01 07:34:58 +08:00
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
res.send(await sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]));
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-27 09:16:21 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
router.get('/options/:name', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const name = req.params.name;
|
2018-01-30 06:41:59 +08:00
|
|
|
const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
|
2017-11-03 08:48:02 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (!opt.isSynced) {
|
2017-11-03 08:48:02 +08:00
|
|
|
res.send("This option can't be synced.");
|
|
|
|
}
|
|
|
|
else {
|
2018-01-12 11:45:25 +08:00
|
|
|
res.send(opt);
|
2017-11-03 08:48:02 +08:00
|
|
|
}
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-03 08:48:02 +08:00
|
|
|
|
2018-01-29 10:57:46 +08:00
|
|
|
router.get('/note_reordering/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2018-01-29 08:30:14 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2017-11-03 10:55:22 +08:00
|
|
|
|
|
|
|
res.send({
|
2018-01-29 08:30:14 +08:00
|
|
|
parentNoteId: parentNoteId,
|
2018-01-29 08:38:05 +08:00
|
|
|
ordering: await sql.getMap("SELECT noteTreeId, notePosition FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId])
|
2017-11-03 10:55:22 +08:00
|
|
|
});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-03 10:55:22 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/recent_notes/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-03 23:06:53 +08:00
|
|
|
const noteTreeId = req.params.noteTreeId;
|
2017-11-05 12:16:02 +08:00
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
res.send(await sql.getRow("SELECT * FROM recent_notes WHERE noteTreeId = ?", [noteTreeId]));
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-05 12:16:02 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2018-01-07 04:56:00 +08:00
|
|
|
const imageId = req.params.imageId;
|
2018-01-30 06:41:59 +08:00
|
|
|
const entity = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [imageId]);
|
2018-01-07 04:56:00 +08:00
|
|
|
|
|
|
|
if (entity && entity.data !== null) {
|
|
|
|
entity.data = entity.data.toString('base64');
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(entity);
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2018-01-07 04:56:00 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
router.get('/note_images/:noteImageId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2018-01-07 10:49:02 +08:00
|
|
|
const noteImageId = req.params.noteImageId;
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
res.send(await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [noteImageId]));
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2018-01-07 10:49:02 +08:00
|
|
|
|
2018-01-10 11:09:45 +08:00
|
|
|
router.get('/attributes/:attributeId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const attributeId = req.params.attributeId;
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
res.send(await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [attributeId]));
|
2018-01-10 11:09:45 +08:00
|
|
|
}));
|
|
|
|
|
2018-02-11 13:18:59 +08:00
|
|
|
router.get('/api_tokens/:apiTokenId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const apiTokenId = req.params.apiTokenId;
|
|
|
|
|
|
|
|
res.send(await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [apiTokenId]));
|
|
|
|
}));
|
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/notes', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-30 09:47:01 +08:00
|
|
|
await syncUpdate.updateNote(req.body.entity, req.body.sourceId);
|
2017-10-30 10:22:30 +08:00
|
|
|
|
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-30 10:22:30 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
router.put('/note_tree', auth.checkApiAuth, wrap(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({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-30 10:22:30 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
router.put('/note_revisions', auth.checkApiAuth, wrap(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({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-25 10:58:59 +08:00
|
|
|
|
2018-01-29 10:57:46 +08:00
|
|
|
router.put('/note_reordering', auth.checkApiAuth, wrap(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({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-03 10:55:22 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/options', auth.checkApiAuth, wrap(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({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-03 08:48:02 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/recent_notes', auth.checkApiAuth, wrap(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({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-05 12:16:02 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/images', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2018-01-07 04:56:00 +08:00
|
|
|
await syncUpdate.updateImage(req.body.entity, req.body.sourceId);
|
|
|
|
|
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2018-01-07 04:56:00 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
router.put('/note_images', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2018-01-07 10:49:02 +08:00
|
|
|
await syncUpdate.updateNoteImage(req.body.entity, req.body.sourceId);
|
|
|
|
|
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2018-01-07 10:49:02 +08:00
|
|
|
|
2018-01-10 11:09:45 +08:00
|
|
|
router.put('/attributes', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2018-01-12 11:45:25 +08:00
|
|
|
await syncUpdate.updateAttribute(req.body.entity, req.body.sourceId);
|
2018-01-10 11:09:45 +08:00
|
|
|
|
|
|
|
res.send({});
|
|
|
|
}));
|
|
|
|
|
2018-02-11 13:18:59 +08:00
|
|
|
router.put('/api_tokens', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
await syncUpdate.updateApiToken(req.body.entity, req.body.sourceId);
|
|
|
|
|
|
|
|
res.send({});
|
|
|
|
}));
|
|
|
|
|
2017-10-25 10:58:59 +08:00
|
|
|
module.exports = router;
|