trilium/src/routes/api/sync.js

250 lines
6.6 KiB
JavaScript
Raw Normal View History

2017-10-25 10:58:59 +08:00
"use strict";
const syncService = require('../../services/sync');
const syncUpdateService = require('../../services/sync_update');
const syncTableService = require('../../services/sync_table');
2017-11-01 07:34:58 +08:00
const sql = require('../../services/sql');
const optionService = require('../../services/options');
const contentHashService = require('../../services/content_hash');
2017-12-24 02:16:18 +08:00
const log = require('../../services/log');
2017-11-22 11:11:27 +08:00
2018-03-31 02:27:41 +08:00
async function checkSync() {
return {
'hashes': await contentHashService.getHashes(),
'max_sync_id': await sql.getValue('SELECT MAX(id) FROM sync')
2018-03-31 02:27:41 +08:00
};
}
2017-10-25 10:58:59 +08:00
2018-03-31 02:27:41 +08:00
async function syncNow() {
return await syncService.sync();
2018-03-31 02:27:41 +08:00
}
2017-10-29 23:22:41 +08:00
2018-03-31 02:27:41 +08:00
async function fillSyncRows() {
await syncTableService.fillAllSyncRows();
2017-12-24 02:16:18 +08:00
log.info("Sync rows have been filled.");
2018-03-31 02:27:41 +08:00
}
2017-12-24 02:16:18 +08:00
2018-03-31 02:27:41 +08:00
async function forceFullSync() {
2018-04-03 09:47:46 +08:00
await optionService.setOption('lastSyncedPull', 0);
await optionService.setOption('lastSyncedPush', 0);
2017-12-24 02:16:18 +08:00
log.info("Forcing full sync.");
// not awaiting for the job to finish (will probably take a long time)
syncService.sync();
2018-03-31 02:27:41 +08:00
}
2018-03-31 02:27:41 +08:00
async function forceNoteSync(req) {
const noteId = req.params.noteId;
await syncTableService.addNoteSync(noteId);
2018-03-31 02:27:41 +08:00
for (const branchId of await sql.getColumn("SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ?", [noteId])) {
await syncTableService.addBranchSync(branchId);
await syncTableService.addRecentNoteSync(branchId);
2018-03-31 02:27:41 +08:00
}
2018-03-31 02:27:41 +08:00
for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) {
await syncTableService.addNoteRevisionSync(noteRevisionId);
2018-03-31 02:27:41 +08:00
}
log.info("Forcing note sync for " + noteId);
// not awaiting for the job to finish (will probably take a long time)
syncService.sync();
2018-03-31 02:27:41 +08:00
}
2018-04-07 06:49:37 +08:00
async function getChanged(req) {
2017-11-01 07:34:58 +08:00
const lastSyncId = parseInt(req.query.lastSyncId);
2017-10-25 10:58:59 +08:00
const records = [];
let length = 0;
for (const sync of await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSyncId])) {
const record = {
sync: sync,
entity: await getEntityRow(sync.entityName, sync.entityId)
};
records.push(record);
length += JSON.stringify(record).length;
if (length > 1000000) {
break;
}
}
return records;
}
const primaryKeys = {
"notes": "noteId",
"branches": "branchId",
"note_revisions": "noteRevisionId",
"option": "name",
"recent_notes": "branchId",
"images": "imageId",
"note_images": "noteImageId",
"labels": "labelId",
"api_tokens": "apiTokenId"
};
async function getEntityRow(entityName, entityId) {
if (entityName === 'note_reordering') {
return await getNoteReordering(entityId);
}
else {
const primaryKey = primaryKeys[entityName];
if (!primaryKey) {
throw new Error("Unknown entity " + entityName);
}
return await sql.getRow(`SELECT * FROM ${entityName} WHERE ${primaryKey} = ?`, [entityId]);
}
2018-03-31 02:27:41 +08:00
}
2017-10-27 09:16:21 +08:00
2018-03-31 02:27:41 +08:00
async function getNote(req) {
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]);
syncService.serializeNoteContentBuffer(entity);
2017-10-27 09:16:21 +08:00
2018-03-31 02:27:41 +08:00
return {
2018-02-19 11:55:36 +08:00
entity: entity
2018-03-31 02:27:41 +08:00
};
}
2017-10-26 10:39:21 +08:00
2018-03-31 02:27:41 +08:00
async function getBranch(req) {
2018-03-25 09:39:15 +08:00
const branchId = req.params.branchId;
2017-10-26 10:39:21 +08:00
2018-03-31 02:27:41 +08:00
return await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]);
}
2017-11-01 07:34:58 +08:00
2018-03-31 02:27:41 +08:00
async function getNoteRevision(req) {
const noteRevisionId = req.params.noteRevisionId;
2017-11-01 07:34:58 +08:00
2018-03-31 02:27:41 +08:00
return await sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]);
}
2017-10-27 09:16:21 +08:00
2018-03-31 02:27:41 +08:00
async function getOption(req) {
2018-01-29 08:30:14 +08:00
const name = req.params.name;
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) {
2018-03-31 02:27:41 +08:00
return [400, "This option can't be synced."];
2017-11-03 08:48:02 +08:00
}
else {
2018-03-31 02:27:41 +08:00
return opt;
2017-11-03 08:48:02 +08:00
}
2018-03-31 02:27:41 +08:00
}
2017-11-03 08:48:02 +08:00
async function getNoteReordering(parentNoteId) {
return await sql.getMap("SELECT branchId, notePosition FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId])
2018-03-31 02:27:41 +08:00
}
2017-11-03 10:55:22 +08:00
2018-03-31 02:27:41 +08:00
async function getRecentNote(req) {
2018-03-25 09:39:15 +08:00
const branchId = req.params.branchId;
2017-11-05 12:16:02 +08:00
2018-03-31 02:27:41 +08:00
return await sql.getRow("SELECT * FROM recent_notes WHERE branchId = ?", [branchId]);
}
2017-11-05 12:16:02 +08:00
2018-03-31 02:27:41 +08:00
async function getImage(req) {
2018-01-07 04:56:00 +08:00
const imageId = req.params.imageId;
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');
}
2018-03-31 02:27:41 +08:00
return entity;
}
2018-01-07 04:56:00 +08:00
2018-03-31 02:27:41 +08:00
async function getNoteImage(req) {
2018-01-07 10:49:02 +08:00
const noteImageId = req.params.noteImageId;
2018-03-31 02:27:41 +08:00
return await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [noteImageId]);
}
2018-01-07 10:49:02 +08:00
2018-03-31 02:27:41 +08:00
async function getLabel(req) {
const labelId = req.params.labelId;
2018-03-31 02:27:41 +08:00
return await sql.getRow("SELECT * FROM labels WHERE labelId = ?", [labelId]);
}
2018-03-31 02:27:41 +08:00
async function getApiToken(req) {
2018-02-11 13:18:59 +08:00
const apiTokenId = req.params.apiTokenId;
2018-03-31 02:27:41 +08:00
return await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [apiTokenId]);
}
2018-02-11 13:18:59 +08:00
2018-03-31 02:27:41 +08:00
async function updateNote(req) {
await syncUpdateService.updateNote(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2017-10-30 10:22:30 +08:00
2018-03-31 02:27:41 +08:00
async function updateBranch(req) {
await syncUpdateService.updateBranch(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2017-10-30 10:22:30 +08:00
2018-03-31 02:27:41 +08:00
async function updateNoteRevision(req) {
await syncUpdateService.updateNoteRevision(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2017-10-26 10:39:21 +08:00
2018-03-31 02:27:41 +08:00
async function updateNoteReordering(req) {
await syncUpdateService.updateNoteReordering(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2017-11-03 10:55:22 +08:00
2018-03-31 02:27:41 +08:00
async function updateOption(req) {
await syncUpdateService.updateOptions(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2017-11-03 08:48:02 +08:00
2018-03-31 02:27:41 +08:00
async function updateRecentNote(req) {
await syncUpdateService.updateRecentNotes(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2017-11-05 12:16:02 +08:00
2018-03-31 02:27:41 +08:00
async function updateImage(req) {
await syncUpdateService.updateImage(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2018-01-07 04:56:00 +08:00
2018-03-31 02:27:41 +08:00
async function updateNoteImage(req) {
await syncUpdateService.updateNoteImage(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2018-01-07 10:49:02 +08:00
2018-03-31 02:27:41 +08:00
async function updateLabel(req) {
await syncUpdateService.updateLabel(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
2018-03-31 02:27:41 +08:00
async function updateApiToken(req) {
await syncUpdateService.updateApiToken(req.body.entity, req.body.sourceId);
2018-03-31 02:27:41 +08:00
}
module.exports = {
checkSync,
syncNow,
fillSyncRows,
forceFullSync,
forceNoteSync,
getChanged,
getNote,
getBranch,
getImage,
getNoteImage,
getNoteReordering,
getNoteRevision,
getRecentNote,
getOption,
getLabel,
getApiToken,
updateNote,
updateBranch,
updateImage,
updateNoteImage,
updateNoteReordering,
updateNoteRevision,
updateRecentNote,
updateOption,
updateLabel,
updateApiToken
};