2017-11-10 09:52:47 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const log = require('./log');
|
|
|
|
const eventLog = require('./event_log');
|
|
|
|
const notes = require('./notes');
|
2017-11-17 10:50:00 +08:00
|
|
|
const sync_table = require('./sync_table');
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2017-11-30 09:47:01 +08:00
|
|
|
async function updateNote(entity, sourceId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const origNote = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (!origNote || origNote.dateModified <= entity.dateModified) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sql.replace("notes", entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addNoteSync(entity.noteId, sourceId);
|
|
|
|
await eventLog.addNoteEvent(entity.noteId, "Synced note <note>");
|
2017-11-10 09:52:47 +08:00
|
|
|
});
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
log.info("Update/sync note " + entity.noteId);
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNoteTree(entity, sourceId) {
|
2018-01-29 08:38:05 +08:00
|
|
|
const orig = await sql.getFirstOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [entity.noteTreeId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-29 08:30:14 +08:00
|
|
|
if (orig === null || orig.dateModified < entity.dateModified) {
|
|
|
|
delete entity.isExpanded;
|
2017-11-10 10:11:33 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.replace('note_tree', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addNoteTreeSync(entity.noteTreeId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
log.info("Update/sync note tree " + entity.noteTreeId);
|
2017-11-29 06:04:47 +08:00
|
|
|
}
|
|
|
|
});
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNoteHistory(entity, sourceId) {
|
2018-01-29 08:38:05 +08:00
|
|
|
const orig = await sql.getFirstOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-12-31 08:38:30 +08:00
|
|
|
// we update note history even if date modified to is the same because the only thing which might have changed
|
2018-01-29 08:30:14 +08:00
|
|
|
// is the protected status (and correnspondingly title and content) which doesn't affect the dateModifiedTo
|
|
|
|
if (orig === null || orig.dateModifiedTo <= entity.dateModifiedTo) {
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.replace('note_revisions', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sync_table.addNoteHistorySync(entity.noteRevisionId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
log.info("Update/sync note history " + entity.noteRevisionId);
|
2017-11-29 06:04:47 +08:00
|
|
|
}
|
|
|
|
});
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNoteReordering(entity, sourceId) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-10 09:52:47 +08:00
|
|
|
Object.keys(entity.ordering).forEach(async key => {
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.execute("UPDATE note_tree SET notePosition = ? WHERE noteTreeId = ?", [entity.ordering[key], key]);
|
2017-11-10 09:52:47 +08:00
|
|
|
});
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addNoteReorderingSync(entity.parentNoteId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateOptions(entity, sourceId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const orig = await sql.getFirstOrNull("SELECT * FROM options WHERE name = ?", [entity.name]);
|
2018-01-12 11:45:25 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (!orig.isSynced) {
|
2017-11-10 09:52:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-29 08:30:14 +08:00
|
|
|
if (orig === null || orig.dateModified < entity.dateModified) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.replace('options', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addOptionsSync(entity.name, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await eventLog.addEvent("Synced option " + entity.name);
|
2017-11-29 06:04:47 +08:00
|
|
|
}
|
|
|
|
});
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateRecentNotes(entity, sourceId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const orig = await sql.getFirstOrNull("SELECT * FROM recent_notes WHERE noteTreeId = ?", [entity.noteTreeId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (orig === null || orig.dateAccessed < entity.dateAccessed) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sql.replace('recent_notes', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addRecentNoteSync(entity.noteTreeId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 04:56:00 +08:00
|
|
|
async function updateImage(entity, sourceId) {
|
|
|
|
if (entity.data !== null) {
|
|
|
|
entity.data = Buffer.from(entity.data, 'base64');
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
const origImage = await sql.getFirst("SELECT * FROM images WHERE imageId = ?", [entity.imageId]);
|
2018-01-07 04:56:00 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (!origImage || origImage.dateModified <= entity.dateModified) {
|
2018-01-07 04:56:00 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sql.replace("images", entity);
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addImageSync(entity.imageId, sourceId);
|
2018-01-07 04:56:00 +08:00
|
|
|
});
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
log.info("Update/sync image " + entity.imageId);
|
2018-01-07 04:56:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 10:49:02 +08:00
|
|
|
async function updateNoteImage(entity, sourceId) {
|
2018-01-29 08:38:05 +08:00
|
|
|
const origNoteImage = await sql.getFirst("SELECT * FROM note_images WHERE noteImageId = ?", [entity.noteImageId]);
|
2018-01-07 10:49:02 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (!origNoteImage || origNoteImage.dateModified <= entity.dateModified) {
|
2018-01-07 10:49:02 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.replace("note_images", entity);
|
2018-01-07 10:49:02 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addNoteImageSync(entity.noteImageId, sourceId);
|
2018-01-07 10:49:02 +08:00
|
|
|
});
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
log.info("Update/sync note image " + entity.noteImageId);
|
2018-01-07 10:49:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:09:45 +08:00
|
|
|
async function updateAttribute(entity, sourceId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const origAttribute = await sql.getFirst("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);
|
2018-01-10 11:09:45 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (!origAttribute || origAttribute.dateModified <= entity.dateModified) {
|
2018-01-10 11:09:45 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-14 12:33:09 +08:00
|
|
|
await sql.replace("attributes", entity);
|
2018-01-10 11:09:45 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.addAttributeSync(entity.attributeId, sourceId);
|
2018-01-10 11:09:45 +08:00
|
|
|
});
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
log.info("Update/sync attribute " + entity.attributeId);
|
2018-01-10 11:09:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 09:52:47 +08:00
|
|
|
module.exports = {
|
|
|
|
updateNote,
|
|
|
|
updateNoteTree,
|
|
|
|
updateNoteHistory,
|
|
|
|
updateNoteReordering,
|
|
|
|
updateOptions,
|
2018-01-07 04:56:00 +08:00
|
|
|
updateRecentNotes,
|
2018-01-07 10:49:02 +08:00
|
|
|
updateImage,
|
2018-01-10 11:09:45 +08:00
|
|
|
updateNoteImage,
|
|
|
|
updateAttribute
|
2017-11-10 09:52:47 +08:00
|
|
|
};
|