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 audit _category = require ( './audit_category' ) ;
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
async function updateNote ( entity , links , sourceId ) {
const origNote = await sql . getSingleResult ( "select * from notes where note_id = ?" , [ entity . note _id ] ) ;
if ( ! origNote || origNote . date _modified <= entity . date _modified ) {
await sql . doInTransaction ( async ( ) => {
await sql . replace ( "notes" , entity ) ;
await sql . remove ( "links" , entity . note _id ) ;
for ( const link of links ) {
delete link [ 'lnk_id' ] ;
await sql . insert ( 'link' , link ) ;
}
2017-11-17 10:50:00 +08:00
await sync _table . addNoteSync ( entity . note _id , sourceId ) ;
2017-11-10 09:52:47 +08:00
await notes . addNoteAudits ( origNote , entity , sourceId ) ;
await eventLog . addNoteEvent ( entity . note _id , "Synced note <note>" ) ;
} ) ;
log . info ( "Update/sync note " + entity . note _id ) ;
}
else {
await eventLog . addNoteEvent ( entity . note _id , "Sync conflict in note <note>, " + utils . formatTwoTimestamps ( origNote . date _modified , entity . date _modified ) ) ;
}
}
async function updateNoteTree ( entity , sourceId ) {
const orig = await sql . getSingleResultOrNull ( "select * from notes_tree where note_id = ?" , [ entity . note _id ] ) ;
if ( orig === null || orig . date _modified < entity . date _modified ) {
await sql . doInTransaction ( async ( ) => {
2017-11-10 10:11:33 +08:00
delete entity . is _expanded ;
2017-11-10 09:52:47 +08:00
await sql . replace ( 'notes_tree' , entity ) ;
2017-11-17 10:50:00 +08:00
await sync _table . addNoteTreeSync ( entity . note _id , sourceId ) ;
2017-11-10 09:52:47 +08:00
await sql . addAudit ( audit _category . UPDATE _TITLE , sourceId , entity . note _id ) ;
} ) ;
log . info ( "Update/sync note tree " + entity . note _id ) ;
}
else {
await eventLog . addNoteEvent ( entity . note _id , "Sync conflict in note tree <note>, " + utils . formatTwoTimestamps ( orig . date _modified , entity . date _modified ) ) ;
}
}
async function updateNoteHistory ( entity , sourceId ) {
const orig = await sql . getSingleResultOrNull ( "select * from notes_history where note_history_id = ?" , [ entity . note _history _id ] ) ;
if ( orig === null || orig . date _modified _to < entity . date _modified _to ) {
await sql . doInTransaction ( async ( ) => {
await sql . replace ( 'notes_history' , entity ) ;
2017-11-17 10:50:00 +08:00
await sync _table . addNoteHistorySync ( entity . note _history _id , sourceId ) ;
2017-11-10 09:52:47 +08:00
} ) ;
log . info ( "Update/sync note history " + entity . note _history _id ) ;
}
else {
await eventLog . addNoteEvent ( entity . note _id , "Sync conflict in note history for <note>, " + utils . formatTwoTimestamps ( orig . date _modified _to , entity . date _modified _to ) ) ;
}
}
async function updateNoteReordering ( entity , sourceId ) {
await sql . doInTransaction ( async ( ) => {
Object . keys ( entity . ordering ) . forEach ( async key => {
await sql . execute ( "UPDATE notes_tree SET note_pos = ? WHERE note_id = ?" , [ entity . ordering [ key ] , key ] ) ;
} ) ;
2017-11-17 10:50:00 +08:00
await sync _table . addNoteReorderingSync ( entity . note _pid , sourceId ) ;
2017-11-10 09:52:47 +08:00
await sql . addAudit ( audit _category . CHANGE _POSITION , sourceId , entity . note _pid ) ;
} ) ;
}
async function updateOptions ( entity , sourceId ) {
if ( ! options . SYNCED _OPTIONS . includes ( entity . opt _name ) ) {
return ;
}
const orig = await sql . getSingleResultOrNull ( "select * from options where opt_name = ?" , [ entity . opt _name ] ) ;
if ( orig === null || orig . date _modified < entity . date _modified ) {
await sql . doInTransaction ( async ( ) => {
await sql . replace ( 'options' , entity ) ;
2017-11-17 10:50:00 +08:00
await sync _table . addOptionsSync ( entity . opt _name , sourceId ) ;
2017-11-10 09:52:47 +08:00
} ) ;
await eventLog . addEvent ( "Synced option " + entity . opt _name ) ;
}
else {
await eventLog . addEvent ( "Sync conflict in options for " + entity . opt _name + ", " + utils . formatTwoTimestamps ( orig . date _modified , entity . date _modified ) ) ;
}
}
async function updateRecentNotes ( entity , sourceId ) {
const orig = await sql . getSingleResultOrNull ( "select * from recent_notes where note_id = ?" , [ entity . note _id ] ) ;
if ( orig === null || orig . date _accessed < entity . date _accessed ) {
await sql . doInTransaction ( async ( ) => {
await sql . replace ( 'recent_notes' , entity ) ;
2017-11-17 10:50:00 +08:00
await sync _table . addRecentNoteSync ( entity . note _id , sourceId ) ;
2017-11-10 09:52:47 +08:00
} ) ;
}
}
module . exports = {
updateNote ,
updateNoteTree ,
updateNoteHistory ,
updateNoteReordering ,
updateOptions ,
updateRecentNotes
} ;