209 lines
6.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 AbstractEntity = require("../becca/entities/abstract_entity.js");
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\nStack: ' + 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);
}
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);
}
});
});
}
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) {
2021-03-21 22:43:41 +01:00
if (message.type !== 'sync-failed') {
log.info("Sending message to all clients: " + jsonStr);
}
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;
}
}
}
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]);
}
}
if (entityChange.entity instanceof AbstractEntity) {
entityChange.entity = entityChange.entity.getPojo();
}
}
function sendPing(client, entityChangeIds = []) {
if (entityChangeIds.length === 0) {
return;
}
const entityChanges = sql.getManyRows(`SELECT * FROM entity_changes WHERE id IN (???)`, entityChangeIds);
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) {
2021-03-21 22:43:41 +01:00
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
};