mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
sync cleanup
This commit is contained in:
parent
0e8fcbc0f5
commit
72905a9854
@ -19,9 +19,7 @@ router.get('/changed', auth.checkApiAuth, async (req, res, next) => {
|
|||||||
const lastSyncId = parseInt(req.query.lastSyncId);
|
const lastSyncId = parseInt(req.query.lastSyncId);
|
||||||
const sourceId = req.query.sourceId;
|
const sourceId = req.query.sourceId;
|
||||||
|
|
||||||
const result = await sync.getChanged(lastSyncId, sourceId);
|
res.send(await sql.getResults("SELECT * FROM sync WHERE id > ? AND source_id != ?", [lastSyncId, sourceId]));
|
||||||
|
|
||||||
res.send(result);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/notes/:noteId', auth.checkApiAuth, async (req, res, next) => {
|
router.get('/notes/:noteId', auth.checkApiAuth, async (req, res, next) => {
|
||||||
|
@ -48,8 +48,6 @@ async function pullSync(cookieJar, syncLog) {
|
|||||||
logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e, syncLog);
|
logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e, syncLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: ideally this should be in transaction
|
|
||||||
|
|
||||||
if (sync.entity_name === 'notes') {
|
if (sync.entity_name === 'notes') {
|
||||||
await updateNote(resp.entity, resp.links, sync.source_id, syncLog);
|
await updateNote(resp.entity, resp.links, sync.source_id, syncLog);
|
||||||
}
|
}
|
||||||
@ -69,7 +67,7 @@ async function pullSync(cookieJar, syncLog) {
|
|||||||
logSync("Finished pull");
|
logSync("Finished pull");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function syncEntity(entity, entityName, cookieJar, syncLog) {
|
async function pushEntity(entity, entityName, cookieJar, syncLog) {
|
||||||
try {
|
try {
|
||||||
const payload = {
|
const payload = {
|
||||||
sourceId: SOURCE_ID,
|
sourceId: SOURCE_ID,
|
||||||
@ -108,28 +106,26 @@ async function pushSync(cookieJar, syncLog) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
let entity;
|
||||||
let entity;
|
|
||||||
|
|
||||||
if (sync.entity_name === 'notes') {
|
if (sync.entity_name === 'notes') {
|
||||||
entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]);
|
entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]);
|
||||||
}
|
}
|
||||||
else if (sync.entity_name === 'notes_tree') {
|
else if (sync.entity_name === 'notes_tree') {
|
||||||
entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_id = ?', [sync.entity_id]);
|
entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_id = ?', [sync.entity_id]);
|
||||||
}
|
}
|
||||||
else if (sync.entity_name === 'notes_history') {
|
else if (sync.entity_name === 'notes_history') {
|
||||||
entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]);
|
entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog);
|
logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
await syncEntity(entity, sync.entity_name, cookieJar, syncLog);
|
await pushEntity(entity, sync.entity_name, cookieJar, syncLog);
|
||||||
|
|
||||||
lastSyncedPush = sync.id;
|
lastSyncedPush = sync.id;
|
||||||
|
|
||||||
await sql.setOption('last_synced_push', lastSyncedPush);
|
await sql.setOption('last_synced_push', lastSyncedPush);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,12 +216,6 @@ function logSyncError(message, e, syncLog) {
|
|||||||
throw new Error(completeMessage);
|
throw new Error(completeMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getChanged(lastSyncId, sourceId) {
|
|
||||||
console.log([lastSyncId, sourceId]);
|
|
||||||
|
|
||||||
return await sql.getResults("SELECT * FROM sync WHERE id > ? AND source_id != ?", [lastSyncId, sourceId]);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function updateNote(entity, links, sourceId, syncLog) {
|
async function updateNote(entity, links, sourceId, syncLog) {
|
||||||
const origNote = await sql.getSingleResult("select * from notes where note_id = ?", [entity.note_id]);
|
const origNote = await sql.getSingleResult("select * from notes where note_id = ?", [entity.note_id]);
|
||||||
|
|
||||||
@ -301,7 +291,6 @@ else {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sync,
|
sync,
|
||||||
getChanged,
|
|
||||||
updateNote,
|
updateNote,
|
||||||
updateNoteTree,
|
updateNoteTree,
|
||||||
updateNoteHistory
|
updateNoteHistory
|
||||||
|
Loading…
x
Reference in New Issue
Block a user