From 57cd00cdbf25358320ffece0dbdfa8f1a97c60d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Tue, 23 Oct 2018 15:22:36 +0200 Subject: [PATCH] Don't load the Smarti Widget if Smarti is disabled --- .../assistify-ai/server/lib/SmartiAdapter.js | 4 +++ .../server/methods/SmartiWidgetBackend.js | 27 ++++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/assistify-ai/server/lib/SmartiAdapter.js b/packages/assistify-ai/server/lib/SmartiAdapter.js index 77066bda79d1..61bb26b65462 100644 --- a/packages/assistify-ai/server/lib/SmartiAdapter.js +++ b/packages/assistify-ai/server/lib/SmartiAdapter.js @@ -19,6 +19,10 @@ function terminateCurrentSync() { */ export class SmartiAdapter { + static isEnabled() { + return !!RocketChat.settings.get('Assistify_AI_Enabled'); + } + /** * Returns the webhook URL that reveives the analysis callback from Smarti. */ diff --git a/packages/assistify-ai/server/methods/SmartiWidgetBackend.js b/packages/assistify-ai/server/methods/SmartiWidgetBackend.js index c8d7331bb885..143aba90d2d5 100644 --- a/packages/assistify-ai/server/methods/SmartiWidgetBackend.js +++ b/packages/assistify-ai/server/methods/SmartiWidgetBackend.js @@ -114,22 +114,25 @@ let script; let timeoutHandle; function loadSmarti() { - - let script = RocketChat.RateLimiter.limitFunction( - SmartiProxy.propagateToSmarti, 5, 1000, { - userId(userId) { - return !RocketChat.authz.hasPermission(userId, 'send-many-messages'); + if (SmartiAdapter.isEnabled()) { + let script = RocketChat.RateLimiter.limitFunction( + SmartiProxy.propagateToSmarti, 5, 1000, { + userId(userId) { + return !RocketChat.authz.hasPermission(userId, 'send-many-messages'); + } } + )(verbs.get, 'plugin/v1/rocket.chat.js'); + if (!script.error && script) { + // add pseudo comment in order to make the script appear in the frontend as a file. This makes it de-buggable + script = `${ script } //# sourceURL=SmartiWidget.js`; + } else { + SystemLogger.error('Could not load Smarti script from', '${SMARTI-SERVER}/plugin/v1/rocket.chat.js'); + throw new Meteor.Error('no-smarti-ui-script', 'no-smarti-ui-script'); } - )(verbs.get, 'plugin/v1/rocket.chat.js'); - if (script) { - // add pseudo comment in order to make the script appear in the frontend as a file. This makes it de-buggable - script = `${ script } //# sourceURL=SmartiWidget.js`; + return script; } else { - SystemLogger.error('Could not load Smarti script from', '${SMARTI-SERVER}/plugin/v1/rocket.chat.js'); - throw new Meteor.Error('no-smarti-ui-script', 'no-smarti-ui-script'); + return ''; // there is no script to be added, so return an empty source (and not null) - hte consumer expects a string } - return script; } function delayedReload() {