mirror of
https://github.com/zadam/trilium.git
synced 2024-12-25 08:43:03 +08:00
push sync
This commit is contained in:
parent
9cef647811
commit
791314ba1a
2 changed files with 110 additions and 48 deletions
|
@ -2,32 +2,32 @@
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const sql = require('../../services/sql');
|
|
||||||
const auth = require('../../services/auth');
|
const auth = require('../../services/auth');
|
||||||
const utils = require('../../services/utils');
|
const sync = require('../../services/sync');
|
||||||
|
|
||||||
router.get('/changed/:since', auth.checkApiAuth, async (req, res, next) => {
|
router.get('/changed/:since', auth.checkApiAuth, async (req, res, next) => {
|
||||||
const since = parseInt(req.params.since);
|
const since = parseInt(req.params.since);
|
||||||
|
|
||||||
res.send({
|
res.send(await sync.getChangedSince(since));
|
||||||
'syncTimestamp': utils.nowTimestamp(),
|
});
|
||||||
'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]),
|
|
||||||
'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]),
|
router.put('/changed', auth.checkApiAuth, async (req, res, next) => {
|
||||||
'audit_log': await sql.getResults("select * from audit_log where date_modified >= ?", [since])
|
await sync.putChanged(req.body);
|
||||||
});
|
|
||||||
|
res.send({});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/note/:noteId/:since', auth.checkApiAuth, async (req, res, next) => {
|
router.get('/note/:noteId/:since', auth.checkApiAuth, async (req, res, next) => {
|
||||||
const noteId = req.params.noteId;
|
const noteId = req.params.noteId;
|
||||||
const since = parseInt(req.params.since);
|
const since = parseInt(req.params.since);
|
||||||
|
|
||||||
const detail = await sql.getSingleResult("select * from notes where note_id = ?", [noteId]);
|
res.send(await sync.getNoteSince(noteId, since));
|
||||||
|
});
|
||||||
|
|
||||||
res.send({
|
router.put('/note', auth.checkApiAuth, async (req, res, next) => {
|
||||||
'detail': detail,
|
await sync.putNote(req.body);
|
||||||
'images': await sql.getResults("select * from images where note_id = ? order by note_offset", [noteId]),
|
|
||||||
'history': await sql.getResults("select * from notes_history where note_id = ? and date_modified_to >= ?", [noteId, since])
|
res.send({});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
128
services/sync.js
128
services/sync.js
|
@ -4,6 +4,7 @@ const log = require('./log');
|
||||||
const rp = require('request-promise');
|
const rp = require('request-promise');
|
||||||
const sql = require('./sql');
|
const sql = require('./sql');
|
||||||
const migration = require('./migration');
|
const migration = require('./migration');
|
||||||
|
const utils = require('./utils');
|
||||||
|
|
||||||
const SYNC_SERVER = 'http://localhost:3000';
|
const SYNC_SERVER = 'http://localhost:3000';
|
||||||
|
|
||||||
|
@ -11,10 +12,10 @@ const SYNC_SERVER = 'http://localhost:3000';
|
||||||
let syncInProgress = false;
|
let syncInProgress = false;
|
||||||
|
|
||||||
async function pullSync() {
|
async function pullSync() {
|
||||||
const lastSynced = parseInt(await sql.getOption('last_synced_pull'));
|
const lastSyncedPull = parseInt(await sql.getOption('last_synced_pull'));
|
||||||
|
|
||||||
const resp = await rp({
|
const resp = await rp({
|
||||||
uri: SYNC_SERVER + '/api/sync/changed/' + lastSynced,
|
uri: SYNC_SERVER + '/api/sync/changed/' + lastSyncedPull,
|
||||||
headers: {
|
headers: {
|
||||||
auth: 'sync'
|
auth: 'sync'
|
||||||
},
|
},
|
||||||
|
@ -24,25 +25,11 @@ async function pullSync() {
|
||||||
try {
|
try {
|
||||||
await sql.beginTransaction();
|
await sql.beginTransaction();
|
||||||
|
|
||||||
for (const treeItem of resp.tree) {
|
await putChanged(resp);
|
||||||
delete treeItem['id'];
|
|
||||||
|
|
||||||
await sql.insert("notes_tree", treeItem, true);
|
|
||||||
|
|
||||||
log.info("Syncing notes_tree " + treeItem.note_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const audit of resp.audit_log) {
|
|
||||||
delete audit['id'];
|
|
||||||
|
|
||||||
await sql.insert("audit_log", audit, true);
|
|
||||||
|
|
||||||
log.info("Syncing audit_log for noteId=" + audit.note_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const noteId of resp.notes) {
|
for (const noteId of resp.notes) {
|
||||||
const note = await rp({
|
const note = await rp({
|
||||||
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSynced,
|
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull,
|
||||||
headers: {
|
headers: {
|
||||||
auth: 'sync'
|
auth: 'sync'
|
||||||
},
|
},
|
||||||
|
@ -50,21 +37,7 @@ async function pullSync() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
await sql.insert("notes", note.detail, true);
|
await putNote(note);
|
||||||
|
|
||||||
await sql.remove("images", noteId);
|
|
||||||
|
|
||||||
for (const image of note.images) {
|
|
||||||
await sql.insert("images", image);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const history of note.history) {
|
|
||||||
delete history['id'];
|
|
||||||
|
|
||||||
await sql.insert("notes_history", history);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("Syncing note" + noteId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await sql.setOption('last_synced_pull', resp.syncTimestamp);
|
await sql.setOption('last_synced_pull', resp.syncTimestamp);
|
||||||
|
@ -79,7 +52,36 @@ async function pullSync() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function pushSync() {
|
async function pushSync() {
|
||||||
|
const lastSyncedPush = parseInt(await sql.getOption('last_synced_push'));
|
||||||
|
const syncStarted = utils.nowTimestamp();
|
||||||
|
|
||||||
|
const changed = await getChangedSince(lastSyncedPush);
|
||||||
|
|
||||||
|
await rp({
|
||||||
|
method: 'PUT',
|
||||||
|
uri: SYNC_SERVER + '/api/sync/changed',
|
||||||
|
headers: {
|
||||||
|
auth: 'sync'
|
||||||
|
},
|
||||||
|
body: changed,
|
||||||
|
json: true
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const noteId of changed.notes) {
|
||||||
|
const note = await getNoteSince(noteId);
|
||||||
|
|
||||||
|
await rp({
|
||||||
|
method: 'PUT',
|
||||||
|
uri: SYNC_SERVER + '/api/sync/note',
|
||||||
|
headers: {
|
||||||
|
auth: 'sync'
|
||||||
|
},
|
||||||
|
body: note,
|
||||||
|
json: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await sql.setOption('last_synced_pull', syncStarted);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sync() {
|
async function sync() {
|
||||||
|
@ -106,7 +108,67 @@ async function sync() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getChangedSince(since) {
|
||||||
|
return {
|
||||||
|
'syncTimestamp': utils.nowTimestamp(),
|
||||||
|
'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]),
|
||||||
|
'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]),
|
||||||
|
'audit_log': await sql.getResults("select * from audit_log where date_modified >= ?", [since])
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getNoteSince(noteId, since) {
|
||||||
|
return {
|
||||||
|
'detail': await sql.getSingleResult("select * from notes where note_id = ?", [noteId]),
|
||||||
|
'images': await sql.getResults("select * from images where note_id = ? order by note_offset", [noteId]),
|
||||||
|
'history': await sql.getResults("select * from notes_history where note_id = ? and date_modified_to >= ?", [noteId, since])
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function putChanged(changed) {
|
||||||
|
for (const treeItem of changed.tree) {
|
||||||
|
delete treeItem['id'];
|
||||||
|
|
||||||
|
await sql.insert("notes_tree", treeItem, true);
|
||||||
|
|
||||||
|
log.info("Update/sync notes_tree " + treeItem.note_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const audit of changed.audit_log) {
|
||||||
|
delete audit['id'];
|
||||||
|
|
||||||
|
await sql.insert("audit_log", audit, true);
|
||||||
|
|
||||||
|
log.info("Update/sync audit_log for noteId=" + audit.note_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function putNote(note) {
|
||||||
|
await sql.insert("notes", note.detail, true);
|
||||||
|
|
||||||
|
await sql.remove("images", node.detail.note_id);
|
||||||
|
|
||||||
|
for (const image of note.images) {
|
||||||
|
await sql.insert("images", image);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const history of note.history) {
|
||||||
|
delete history['id'];
|
||||||
|
|
||||||
|
await sql.insert("notes_history", history);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Update/sync note " + note.detail.note_id);
|
||||||
|
}
|
||||||
|
|
||||||
setInterval(sync, 60000);
|
setInterval(sync, 60000);
|
||||||
|
|
||||||
// kickoff initial sync immediately
|
// kickoff initial sync immediately
|
||||||
setTimeout(sync, 1000);
|
setTimeout(sync, 1000);
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getChangedSince,
|
||||||
|
getNoteSince,
|
||||||
|
putChanged,
|
||||||
|
putNote
|
||||||
|
};
|
Loading…
Reference in a new issue