2017-11-10 09:52:47 +08:00
const sql = require ( './sql' ) ;
const log = require ( './log' ) ;
const options = require ( './options' ) ;
const utils = require ( './utils' ) ;
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 ) {
2017-11-21 12:51:28 +08:00
const origNote = await sql . getSingleResult ( "SELECT * FROM notes WHERE note_id = ?" , [ entity . note _id ] ) ;
2017-11-10 09:52:47 +08:00
if ( ! origNote || origNote . date _modified <= entity . date _modified ) {
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
2017-11-29 06:24:08 +08:00
await sync _table . addNoteSync ( entity . note _id , sourceId ) ;
await eventLog . addNoteEvent ( entity . note _id , "Synced note <note>" ) ;
2017-11-10 09:52:47 +08:00
} ) ;
log . info ( "Update/sync note " + entity . note _id ) ;
}
else {
2017-11-29 06:24:08 +08:00
await eventLog . addNoteEvent ( entity . note _id , "Sync conflict in note <note>, " + utils . formatTwoTimestamps ( origNote . date _modified , entity . date _modified ) ) ;
2017-11-10 09:52:47 +08:00
}
}
async function updateNoteTree ( entity , sourceId ) {
2017-11-21 12:51:28 +08:00
const orig = await sql . getSingleResultOrNull ( "SELECT * FROM notes_tree WHERE note_tree_id = ?" , [ entity . note _tree _id ] ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:24:08 +08:00
await sql . doInTransaction ( async ( ) => {
2017-11-29 06:04:47 +08:00
if ( orig === null || orig . date _modified < entity . date _modified ) {
2017-11-10 10:11:33 +08:00
delete entity . is _expanded ;
2017-11-29 06:24:08 +08:00
await sql . replace ( 'notes_tree' , entity ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:24:08 +08:00
await sync _table . addNoteTreeSync ( entity . note _tree _id , sourceId ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:04:47 +08:00
log . info ( "Update/sync note tree " + entity . note _tree _id ) ;
}
else {
2017-11-29 06:24:08 +08:00
await eventLog . addNoteEvent ( entity . note _tree _id , "Sync conflict in note tree <note>, " + utils . formatTwoTimestamps ( orig . date _modified , entity . date _modified ) ) ;
2017-11-29 06:04:47 +08:00
}
} ) ;
2017-11-10 09:52:47 +08:00
}
async function updateNoteHistory ( entity , sourceId ) {
2017-11-21 12:51:28 +08:00
const orig = await sql . getSingleResultOrNull ( "SELECT * FROM notes_history WHERE note_history_id = ?" , [ entity . note _history _id ] ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:24:08 +08:00
await sql . doInTransaction ( async ( ) => {
2017-11-29 06:04:47 +08:00
if ( orig === null || orig . date _modified _to < entity . date _modified _to ) {
2017-11-29 06:24:08 +08:00
await sql . replace ( 'notes_history' , entity ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:24:08 +08:00
await sync _table . addNoteHistorySync ( entity . note _history _id , sourceId ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:04:47 +08:00
log . info ( "Update/sync note history " + entity . note _history _id ) ;
}
else {
2017-11-29 06:24:08 +08:00
await eventLog . addNoteEvent ( entity . note _id , "Sync conflict in note history for <note>, " + utils . formatTwoTimestamps ( orig . date _modified _to , entity . date _modified _to ) ) ;
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 => {
2017-11-29 06:24:08 +08:00
await sql . execute ( "UPDATE notes_tree SET note_pos = ? WHERE note_tree_id = ?" , [ entity . ordering [ key ] , key ] ) ;
2017-11-10 09:52:47 +08:00
} ) ;
2017-11-29 06:24:08 +08:00
await sync _table . addNoteReorderingSync ( entity . note _pid , sourceId ) ;
2017-11-10 09:52:47 +08:00
} ) ;
}
async function updateOptions ( entity , sourceId ) {
if ( ! options . SYNCED _OPTIONS . includes ( entity . opt _name ) ) {
return ;
}
2017-11-21 12:51:28 +08:00
const orig = await sql . getSingleResultOrNull ( "SELECT * FROM options WHERE opt_name = ?" , [ entity . opt _name ] ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:24:08 +08:00
await sql . doInTransaction ( async ( ) => {
2017-11-29 06:04:47 +08:00
if ( orig === null || orig . date _modified < entity . date _modified ) {
2017-11-29 06:24:08 +08:00
await sql . replace ( 'options' , entity ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:24:08 +08:00
await sync _table . addOptionsSync ( entity . opt _name , sourceId ) ;
2017-11-10 09:52:47 +08:00
2017-11-29 06:24:08 +08:00
await eventLog . addEvent ( "Synced option " + entity . opt _name ) ;
2017-11-29 06:04:47 +08:00
}
else {
2017-11-29 06:24:08 +08:00
await eventLog . addEvent ( "Sync conflict in options for " + entity . opt _name + ", " + utils . formatTwoTimestamps ( orig . date _modified , entity . date _modified ) ) ;
2017-11-29 06:04:47 +08:00
}
} ) ;
2017-11-10 09:52:47 +08:00
}
async function updateRecentNotes ( entity , sourceId ) {
2017-11-21 12:51:28 +08:00
const orig = await sql . getSingleResultOrNull ( "SELECT * FROM recent_notes WHERE note_path = ?" , [ entity . note _path ] ) ;
2017-11-10 09:52:47 +08:00
if ( orig === null || orig . date _accessed < entity . date _accessed ) {
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
2017-11-29 06:24:08 +08:00
await sync _table . addRecentNoteSync ( entity . note _path , sourceId ) ;
2017-11-10 09:52:47 +08:00
} ) ;
}
}
module . exports = {
updateNote ,
updateNoteTree ,
updateNoteHistory ,
updateNoteReordering ,
updateOptions ,
updateRecentNotes
} ;