trilium/routes/api/status.js

46 lines
2 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
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');
const sync = require('../../services/sync');
const audit_category = require('../../services/audit_category');
router.post('', auth.checkApiAuth, async (req, res, next) => {
const treeLoadTime = req.body.treeLoadTime;
const currentNoteId = req.body.currentNoteId;
const currentNoteLoadTime = req.body.currentNoteLoadTime;
const browserId = req.get('x-browser-id');
const noteTreeChangesCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
"AND date_modified >= ? AND category IN (?, ?, ?)", [browserId, treeLoadTime,
audit_category.UPDATE_TITLE, audit_category.CHANGE_PARENT, audit_category.CHANGE_POSITION]);
const currentNoteChangesCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
"AND date_modified >= ? AND note_id = ? AND category IN (?)", [browserId, currentNoteLoadTime, currentNoteId,
audit_category.UPDATE_CONTENT]);
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 + "') " +
"AND date_modified >= " + currentNoteLoadTime + " AND note_id = '" + currentNoteId + "' AND category IN ('" + audit_category.UPDATE_CONTENT + "')");
2017-11-02 11:39:17 +08:00
}
let changesToPushCount = 0;
if (sync.isSyncSetup) {
2017-11-03 08:48:02 +08:00
const lastSyncedPush = await options.getOption('last_synced_push');
changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
}
res.send({
'changedTree': noteTreeChangesCount > 0,
'changedCurrentNote': currentNoteChangesCount > 0,
'changesToPushCount': changesToPushCount
});
});
module.exports = router;