2017-10-22 09:10:33 +08:00
"use strict" ;
2017-10-15 11:31:44 +08:00
const express = require ( 'express' ) ;
const router = express . Router ( ) ;
2017-10-16 07:47:05 +08:00
const sql = require ( '../../services/sql' ) ;
2017-11-03 08:48:02 +08:00
const options = require ( '../../services/options' ) ;
2017-10-16 07:47:05 +08:00
const auth = require ( '../../services/auth' ) ;
2017-11-02 10:36:26 +08:00
const sync = require ( '../../services/sync' ) ;
const audit _category = require ( '../../services/audit_category' ) ;
2017-10-15 11:31:44 +08:00
2017-11-02 10:36:26 +08:00
router . post ( '' , auth . checkApiAuth , async ( req , res , next ) => {
const treeLoadTime = req . body . treeLoadTime ;
const currentNoteId = req . body . currentNoteId ;
2017-11-05 09:02:56 +08:00
const currentNoteLoadTime = req . body . currentNoteLoadTime ;
2017-10-15 11:31:44 +08:00
const browserId = req . get ( 'x-browser-id' ) ;
2017-11-02 10:36:26 +08:00
const noteTreeChangesCount = await sql . getSingleValue ( "SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
2017-11-06 09:37:25 +08:00
"AND date_modified >= ? AND category IN (?, ?, ?, ?)" , [ browserId , treeLoadTime ,
audit _category . UPDATE _TITLE , audit _category . CHANGE _PARENT , audit _category . CHANGE _POSITION , audit _category . DELETE _NOTE ] ) ;
2017-10-27 11:21:31 +08:00
2017-11-02 10:36:26 +08:00
const currentNoteChangesCount = await sql . getSingleValue ( "SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
2017-11-05 23:41:54 +08:00
"AND date_modified >= ? AND note_id = ? AND category IN (?, ?)" , [ browserId , currentNoteLoadTime , currentNoteId ,
audit _category . UPDATE _TITLE , audit _category . UPDATE _CONTENT ] ) ;
2017-11-02 10:36:26 +08:00
2017-11-02 11:39:17 +08:00
if ( currentNoteChangesCount > 0 ) {
console . log ( "Current note changed!" ) ;
console . log ( "SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != '" + browserId + "') " +
2017-11-05 23:41:54 +08:00
"AND date_modified >= " + currentNoteLoadTime + " AND note_id = '" + currentNoteId + "' AND category IN ('" + audit _category . UPDATE _TITLE + "', '" + audit _category . UPDATE _CONTENT + "')" ) ;
2017-11-02 11:39:17 +08:00
}
2017-11-02 10:36:26 +08:00
let changesToPushCount = 0 ;
if ( sync . isSyncSetup ) {
2017-11-03 08:48:02 +08:00
const lastSyncedPush = await options . getOption ( 'last_synced_push' ) ;
2017-11-02 10:36:26 +08:00
changesToPushCount = await sql . getSingleValue ( "SELECT COUNT(*) FROM sync WHERE id > ?" , [ lastSyncedPush ] ) ;
}
2017-11-02 08:31:44 +08:00
2017-10-15 11:31:44 +08:00
res . send ( {
2017-11-02 10:36:26 +08:00
'changedTree' : noteTreeChangesCount > 0 ,
'changedCurrentNote' : currentNoteChangesCount > 0 ,
2017-11-02 08:31:44 +08:00
'changesToPushCount' : changesToPushCount
2017-10-15 11:31:44 +08:00
} ) ;
} ) ;
module . exports = router ;