2017-10-24 22:58:59 -04:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
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');
|
2018-04-01 21:27:46 -04:00
|
|
|
const optionService = require('../../services/options');
|
|
|
|
const contentHashService = require('../../services/content_hash');
|
2017-12-23 13:16:18 -05:00
|
|
|
const log = require('../../services/log');
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function checkSync() {
|
|
|
|
return {
|
2018-04-01 21:27:46 -04:00
|
|
|
'hashes': await contentHashService.getHashes(),
|
2018-01-29 17:41:59 -05:00
|
|
|
'max_sync_id': 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() {
|
2018-04-01 21:27:46 -04:00
|
|
|
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() {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncTableService.fillAllSyncRows();
|
2017-12-19 22:04:51 -05:00
|
|
|
|
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-13 23:03:48 -05:00
|
|
|
|
2017-12-23 13:16:18 -05:00
|
|
|
log.info("Forcing full sync.");
|
|
|
|
|
2017-12-13 23:03:48 -05:00
|
|
|
// not awaiting for the job to finish (will probably take a long time)
|
2018-04-01 21:27:46 -04:00
|
|
|
syncService.sync();
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-12-13 23:03:48 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function forceNoteSync(req) {
|
2017-12-30 21:44:26 -05:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncTableService.addNoteSync(noteId);
|
2017-12-30 21:44:26 -05:00
|
|
|
|
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])) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncTableService.addBranchSync(branchId);
|
|
|
|
await syncTableService.addRecentNoteSync(branchId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-12-30 21:44:26 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncTableService.addNoteRevisionSync(noteRevisionId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-12-30 21:44:26 -05:00
|
|
|
|
|
|
|
log.info("Forcing note sync for " + noteId);
|
|
|
|
|
|
|
|
// not awaiting for the job to finish (will probably take a long time)
|
2018-04-01 21:27:46 -04:00
|
|
|
syncService.sync();
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-12-30 21:44:26 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getChanged() {
|
2017-10-31 19:34:58 -04:00
|
|
|
const lastSyncId = parseInt(req.query.lastSyncId);
|
2017-10-24 22:58:59 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSyncId]);
|
|
|
|
}
|
2017-10-26 21:16:21 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getNote(req) {
|
2017-10-31 19:34:58 -04:00
|
|
|
const noteId = req.params.noteId;
|
2018-02-18 22:55:36 -05:00
|
|
|
const entity = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
syncService.serializeNoteContentBuffer(entity);
|
2017-10-26 21:16:21 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return {
|
2018-02-18 22:55:36 -05:00
|
|
|
entity: entity
|
2018-03-30 14:27:41 -04:00
|
|
|
};
|
|
|
|
}
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getBranch(req) {
|
2018-03-24 21:39:15 -04:00
|
|
|
const branchId = req.params.branchId;
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]);
|
|
|
|
}
|
2017-10-31 19:34:58 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getNoteRevision(req) {
|
2018-01-28 19:38:05 -05:00
|
|
|
const noteRevisionId = req.params.noteRevisionId;
|
2017-10-31 19:34:58 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return await sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]);
|
|
|
|
}
|
2017-10-26 21:16:21 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getOption(req) {
|
2018-01-28 19:30:14 -05:00
|
|
|
const name = req.params.name;
|
2018-01-29 17:41:59 -05:00
|
|
|
const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
|
2017-11-02 20:48:02 -04:00
|
|
|
|
2018-01-28 19:30:14 -05:00
|
|
|
if (!opt.isSynced) {
|
2018-03-30 14:27:41 -04:00
|
|
|
return [400, "This option can't be synced."];
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
else {
|
2018-03-30 14:27:41 -04:00
|
|
|
return opt;
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-11-02 20:48:02 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getNoteReordering(req) {
|
2018-01-28 19:30:14 -05:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2017-11-02 22:55:22 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return {
|
2018-01-28 19:30:14 -05:00
|
|
|
parentNoteId: parentNoteId,
|
2018-03-24 21:39:15 -04:00
|
|
|
ordering: await sql.getMap("SELECT branchId, notePosition FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId])
|
2018-03-30 14:27:41 -04:00
|
|
|
};
|
|
|
|
}
|
2017-11-02 22:55:22 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getRecentNote(req) {
|
2018-03-24 21:39:15 -04:00
|
|
|
const branchId = req.params.branchId;
|
2017-11-05 00:16:02 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return await sql.getRow("SELECT * FROM recent_notes WHERE branchId = ?", [branchId]);
|
|
|
|
}
|
2017-11-05 00:16:02 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getImage(req) {
|
2018-01-06 15:56:00 -05:00
|
|
|
const imageId = req.params.imageId;
|
2018-01-29 17:41:59 -05:00
|
|
|
const entity = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [imageId]);
|
2018-01-06 15:56:00 -05:00
|
|
|
|
|
|
|
if (entity && entity.data !== null) {
|
|
|
|
entity.data = entity.data.toString('base64');
|
|
|
|
}
|
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return entity;
|
|
|
|
}
|
2018-01-06 15:56:00 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getNoteImage(req) {
|
2018-01-06 21:49:02 -05:00
|
|
|
const noteImageId = req.params.noteImageId;
|
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [noteImageId]);
|
|
|
|
}
|
2018-01-06 21:49:02 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getLabel(req) {
|
2018-03-24 22:02:26 -04:00
|
|
|
const labelId = req.params.labelId;
|
2018-01-09 22:09:45 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return await sql.getRow("SELECT * FROM labels WHERE labelId = ?", [labelId]);
|
|
|
|
}
|
2018-01-09 22:09:45 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function getApiToken(req) {
|
2018-02-11 00:18:59 -05:00
|
|
|
const apiTokenId = req.params.apiTokenId;
|
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
return await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [apiTokenId]);
|
|
|
|
}
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateNote(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateNote(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-10-29 22:22:30 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateBranch(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateBranch(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-10-29 22:22:30 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateNoteRevision(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateNoteRevision(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateNoteReordering(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateNoteReordering(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-11-02 22:55:22 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateOption(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateOptions(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-11-02 20:48:02 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateRecentNote(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateRecentNotes(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2017-11-05 00:16:02 -04:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateImage(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateImage(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2018-01-06 15:56:00 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateNoteImage(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateNoteImage(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2018-01-06 21:49:02 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateLabel(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateLabel(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04:00
|
|
|
}
|
2018-01-09 22:09:45 -05:00
|
|
|
|
2018-03-30 14:27:41 -04:00
|
|
|
async function updateApiToken(req) {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncUpdateService.updateApiToken(req.body.entity, req.body.sourceId);
|
2018-03-30 14:27:41 -04: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
|
|
|
|
};
|