235 lines
7.5 KiB
JavaScript
Raw Normal View History

const WebSocket = require('ws');
const utils = require('./utils');
const log = require('./log');
const sql = require('./sql');
2020-01-31 22:32:24 +01:00
const cls = require('./cls');
const config = require('./config');
2019-10-22 21:59:51 +02:00
const syncMutexService = require('./sync_mutex');
const protectedSessionService = require('./protected_session');
const becca = require("../becca/becca");
const AbstractBeccaEntity = require("../becca/entities/abstract_becca_entity");
let webSocketServer;
2021-03-21 22:43:41 +01:00
let lastSyncedPush = null;
function init(httpServer, sessionParser) {
webSocketServer = new WebSocket.Server({
verifyClient: (info, done) => {
sessionParser(info.req, {}, () => {
const allowed = utils.isElectron()
|| info.req.session.loggedIn
|| (config.General && config.General.noAuthentication);
if (!allowed) {
log.error("WebSocket connection not allowed because session is neither electron nor logged in.");
}
done(allowed)
});
},
server: httpServer
});
2017-12-01 22:28:22 -05:00
webSocketServer.on('connection', (ws, req) => {
ws.id = utils.randomString(10);
console.log(`websocket client connected`);
2017-12-01 22:28:22 -05:00
2020-06-20 12:31:38 +02:00
ws.on('message', async messageJson => {
2017-12-01 22:28:22 -05:00
const message = JSON.parse(messageJson);
if (message.type === 'log-error') {
log.info(`JS Error: ${message.error}\r
Stack: ${message.stack}`);
2017-12-01 22:28:22 -05:00
}
2021-09-17 22:34:23 +02:00
else if (message.type === 'log-info') {
log.info(`JS Info: ${message.info}`);
2021-09-17 22:34:23 +02:00
}
else if (message.type === 'ping') {
2020-06-20 12:31:38 +02:00
await syncMutexService.doExclusively(() => sendPing(ws));
}
2017-12-01 22:28:22 -05:00
else {
log.error('Unrecognized message: ');
log.error(message);
}
});
});
2022-12-07 23:58:22 +01:00
webSocketServer.on('error', error => {
// https://github.com/zadam/trilium/issues/3374#issuecomment-1341053765
console.log(error);
});
}
2019-10-28 18:42:22 +01:00
function sendMessage(client, message) {
const jsonStr = JSON.stringify(message);
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
}
2019-10-28 18:42:22 +01:00
function sendMessageToAllClients(message) {
const jsonStr = JSON.stringify(message);
2019-01-02 22:36:06 +01:00
if (webSocketServer) {
2022-09-17 23:06:17 +02:00
if (message.type !== 'sync-failed' && message.type !== 'api-log-messages') {
log.info(`Sending message to all clients: ${jsonStr}`);
2021-03-21 22:43:41 +01:00
}
2018-01-01 19:41:22 -05:00
2019-01-02 22:36:06 +01:00
webSocketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
});
}
}
function fillInAdditionalProperties(entityChange) {
if (entityChange.isErased) {
return;
}
// fill in some extra data needed by the frontend
// first try to use becca which works for non-deleted entities
// only when that fails try to load from database
if (entityChange.entityName === 'attributes') {
entityChange.entity = becca.getAttribute(entityChange.entityId);
if (!entityChange.entity) {
entityChange.entity = sql.getRow(`SELECT * FROM attributes WHERE attributeId = ?`, [entityChange.entityId]);
}
} else if (entityChange.entityName === 'branches') {
entityChange.entity = becca.getBranch(entityChange.entityId);
if (!entityChange.entity) {
entityChange.entity = sql.getRow(`SELECT * FROM branches WHERE branchId = ?`, [entityChange.entityId]);
}
} else if (entityChange.entityName === 'notes') {
entityChange.entity = becca.getNote(entityChange.entityId);
if (!entityChange.entity) {
entityChange.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [entityChange.entityId]);
if (entityChange.entity.isProtected) {
entityChange.entity.title = protectedSessionService.decryptString(entityChange.entity.title);
}
}
} else if (entityChange.entityName === 'note_revisions') {
entityChange.noteId = sql.getValue(`SELECT noteId
FROM note_revisions
WHERE noteRevisionId = ?`, [entityChange.entityId]);
} else if (entityChange.entityName === 'note_reordering') {
entityChange.positions = {};
const parentNote = becca.getNote(entityChange.entityId);
if (parentNote) {
for (const childBranch of parentNote.getChildBranches()) {
entityChange.positions[childBranch.branchId] = childBranch.notePosition;
}
}
2023-03-15 22:44:08 +01:00
} else if (entityChange.entityName === 'options') {
entityChange.entity = becca.getOption(entityChange.entityId);
if (!entityChange.entity) {
entityChange.entity = sql.getRow(`SELECT * FROM options WHERE name = ?`, [entityChange.entityId]);
}
2023-03-15 22:44:08 +01:00
} else if (entityChange.entityName === 'blob') {
entityChange.noteIds = sql.getColumn("SELECT noteId FROM notes WHERE blobId = ? AND isDeleted = 0", [entityChange.entityId]);
}
if (entityChange.entity instanceof AbstractBeccaEntity) {
entityChange.entity = entityChange.entity.getPojo();
}
}
2021-10-02 23:26:18 +02:00
// entities with higher number can reference the entities with lower number
const ORDERING = {
2022-01-10 17:09:20 +01:00
"etapi_tokens": 0,
2021-10-02 23:26:18 +02:00
"attributes": 1,
"branches": 1,
"note_contents": 1,
"note_reordering": 1,
"note_revision_contents": 2,
"note_revisions": 1,
"notes": 0,
"options": 0
};
function sendPing(client, entityChangeIds = []) {
if (entityChangeIds.length === 0) {
2021-09-26 15:37:18 +02:00
sendMessage(client, { type: 'ping' });
return;
}
const entityChanges = sql.getManyRows(`SELECT * FROM entity_changes WHERE id IN (???)`, entityChangeIds);
2021-10-02 23:26:18 +02:00
// sort entity changes since froca expects "referential order", i.e. referenced entities should already exist
// in froca.
// Froca needs this since it is incomplete copy, it can't create "skeletons" like becca.
entityChanges.sort((a, b) => ORDERING[a.entityName] - ORDERING[b.entityName]);
2021-03-21 22:43:41 +01:00
for (const entityChange of entityChanges) {
try {
2021-03-21 22:43:41 +01:00
fillInAdditionalProperties(entityChange);
}
catch (e) {
log.error(`Could not fill additional properties for entity change ${JSON.stringify(entityChange)} because of error: ${e.message}: ${e.stack}`);
2019-10-20 12:29:34 +02:00
}
}
2019-10-28 18:42:22 +01:00
sendMessage(client, {
2021-03-21 22:43:41 +01:00
type: 'frontend-update',
data: {
lastSyncedPush,
entityChanges
}
});
}
2021-03-21 22:43:41 +01:00
function sendTransactionEntityChangesToAllClients() {
if (webSocketServer) {
const entityChangeIds = cls.getAndClearEntityChangeIds();
webSocketServer.clients.forEach(client => sendPing(client, entityChangeIds));
}
}
2019-10-28 18:42:22 +01:00
function syncPullInProgress() {
2021-05-15 22:00:53 +02:00
sendMessageToAllClients({ type: 'sync-pull-in-progress', lastSyncedPush });
2019-10-25 22:20:14 +02:00
}
2021-03-21 00:01:28 +01:00
function syncPushInProgress() {
2021-05-15 22:00:53 +02:00
sendMessageToAllClients({ type: 'sync-push-in-progress', lastSyncedPush });
2021-03-21 00:01:28 +01:00
}
function syncFinished() {
2021-05-15 22:00:53 +02:00
sendMessageToAllClients({ type: 'sync-finished', lastSyncedPush });
2021-03-21 00:01:28 +01:00
}
function syncFailed() {
2021-05-15 22:00:53 +02:00
sendMessageToAllClients({ type: 'sync-failed', lastSyncedPush });
2019-10-25 22:20:14 +02:00
}
function reloadFrontend() {
sendMessageToAllClients({ type: 'reload-frontend' });
}
2021-03-21 22:43:41 +01:00
function setLastSyncedPush(entityChangeId) {
lastSyncedPush = entityChangeId;
}
module.exports = {
init,
sendMessageToAllClients,
2021-03-21 00:01:28 +01:00
syncPushInProgress,
2019-10-25 22:20:14 +02:00
syncPullInProgress,
2021-03-21 00:01:28 +01:00
syncFinished,
syncFailed,
2021-03-21 22:43:41 +01:00
sendTransactionEntityChangesToAllClients,
setLastSyncedPush,
reloadFrontend
};