From 3232671a6a24361d34bef0d9fa650dfca5e39bda Mon Sep 17 00:00:00 2001 From: ansuz Date: Sat, 14 May 2016 13:04:05 +0200 Subject: [PATCH] implement optional channel removal hooks add configuration variables to the default config --- NetfluxWebsocketSrv.js | 24 ++++++++++++++++++++++++ config.js.dist | 9 +++++++++ 2 files changed, 33 insertions(+) diff --git a/NetfluxWebsocketSrv.js b/NetfluxWebsocketSrv.js index 6fad102ed..48444f7ef 100644 --- a/NetfluxWebsocketSrv.js +++ b/NetfluxWebsocketSrv.js @@ -62,6 +62,17 @@ dropUser = function (ctx, user) { if (chan.length === 0) { console.log("Removing empty channel ["+chanName+"]"); delete ctx.channels[chanName]; + + /* Call removeChannel if it is a function and channel removal is + set to true in the config file */ + if (ctx.config.removeChannels && typeof(ctx.store.removeChannel) === 'function') { + ctx.timeouts[chanName] = setTimeout(function () { + ctx.store.removeChannel(chanName, function (err) { + if (err) { console.error("[removeChannelErr]: %s", err); } + console.log("Deleted channel [%s] history from database...", chanName); + }); + }, ctx.config.channelRemovalTimeout); + } } else { sendChannelMessage(ctx, chan, [user.id, 'LEAVE', chanName, 'Quit: [ dropUser() ]']); } @@ -91,6 +102,12 @@ const handleMessage = function (ctx, user, msg) { let chanName = obj || randName(); sendMsg(ctx, user, [seq, 'JACK', chanName]); let chan = ctx.channels[chanName] = ctx.channels[chanName] || []; + + // prevent removal of the channel if there is a pending timeout + if (ctx.config.removeChannels && ctx.timeouts[chanName]) { + clearTimeout(ctx.timeouts[chanName]); + } + chan.id = chanName; if (USE_HISTORY_KEEPER) { sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'JOIN', chanName]); @@ -154,9 +171,16 @@ const handleMessage = function (ctx, user, msg) { }; let run = module.exports.run = function (storage, socketServer, config) { + /* Channel removal timeout defaults to 60000ms (one minute) */ + config.channelRemovalTimeout = + typeof(config.channelRemovalTimeout) === 'number'? + config.channelRemovalTimeout: + 60000; + let ctx = { users: {}, channels: {}, + timeouts: {}, store: (USE_FILE_BACKUP_STORAGE) ? LogStore.create('messages.log', storage) : storage, config: config }; diff --git a/config.js.dist b/config.js.dist index 9d0ef5f32..4b5702574 100644 --- a/config.js.dist +++ b/config.js.dist @@ -17,6 +17,15 @@ module.exports = { */ logToStdout: false, + /* Cryptpad can be configured to remove channels some number of ms + after the last remaining client has disconnected. + + Default behaviour is to keep channels forever. + If you enable channel removal, the default removal time is one minute + */ + removeChannels: false, + channelRemovalTimeout: 60000, + // You now have a choice of storage engines /* amnesiadb only exists in memory.