Notes/src/routes/api/sync.js

91 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-10-24 22:58:59 -04:00
"use strict";
const syncService = require('../../services/sync');
const syncUpdateService = require('../../services/sync_update');
const syncTableService = require('../../services/sync_table');
2017-10-31 19:34:58 -04:00
const sql = require('../../services/sql');
const optionService = require('../../services/options');
const contentHashService = require('../../services/content_hash');
2017-12-23 13:16:18 -05:00
const log = require('../../services/log');
const DOCUMENT_PATH = require('../../services/data_dir').DOCUMENT_PATH;
2017-11-21 22:11:27 -05:00
2018-03-30 14:27:41 -04:00
async function checkSync() {
return {
2018-04-08 10:09:33 -04:00
hashes: await contentHashService.getHashes(),
maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync')
2018-03-30 14:27:41 -04:00
};
}
2017-10-24 22:58:59 -04:00
2018-03-30 14:27:41 -04:00
async function syncNow() {
return await syncService.sync();
2018-03-30 14:27:41 -04:00
}
2017-10-29 11:22:41 -04:00
2018-03-30 14:27:41 -04:00
async function fillSyncRows() {
await syncTableService.fillAllSyncRows();
2017-12-23 13:16:18 -05:00
log.info("Sync rows have been filled.");
2018-03-30 14:27:41 -04:00
}
2017-12-23 13:16:18 -05:00
2018-03-30 14:27:41 -04:00
async function forceFullSync() {
2018-04-02 21:47:46 -04:00
await optionService.setOption('lastSyncedPull', 0);
await optionService.setOption('lastSyncedPush', 0);
2017-12-23 13:16:18 -05:00
log.info("Forcing full sync.");
// not awaiting for the job to finish (will probably take a long time)
syncService.sync();
2018-03-30 14:27:41 -04:00
}
2018-03-30 14:27:41 -04:00
async function forceNoteSync(req) {
const noteId = req.params.noteId;
await syncTableService.addNoteSync(noteId);
2018-03-30 14:27:41 -04: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-30 14:27:41 -04:00
}
2018-03-30 14:27:41 -04:00
for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) {
await syncTableService.addNoteRevisionSync(noteRevisionId);
2018-03-30 14:27:41 -04: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-30 14:27:41 -04:00
}
2018-04-06 18:49:37 -04:00
async function getChanged(req) {
2017-10-31 19:34:58 -04:00
const lastSyncId = parseInt(req.query.lastSyncId);
2017-10-24 22:58:59 -04:00
2018-04-07 22:25:28 -04:00
const syncs = await sql.getRows("SELECT * FROM sync WHERE id > ? LIMIT 1000", [lastSyncId]);
2018-04-07 22:25:28 -04:00
return await syncService.getSyncRecords(syncs);
}
2018-04-07 22:25:28 -04:00
async function update(req) {
const sourceId = req.body.sourceId;
const entities = req.body.entities;
2018-04-07 22:25:28 -04:00
for (const {sync, entity} of entities) {
2018-04-12 18:31:29 -04:00
await syncUpdateService.updateEntity(sync, entity, sourceId);
}
2018-03-30 14:27:41 -04:00
}
2017-10-26 21:16:21 -04:00
async function getDocument(req, resp) {
log.info("Serving document.");
resp.sendFile(DOCUMENT_PATH);
}
2018-03-30 14:27:41 -04:00
module.exports = {
checkSync,
syncNow,
fillSyncRows,
forceFullSync,
forceNoteSync,
getChanged,
update,
getDocument
2018-03-30 14:27:41 -04:00
};