Skip to content

Commit

Permalink
Merge pull request #78 from mrsimpson/feature/#77-smarti-widget-inval…
Browse files Browse the repository at this point in the history
…idation

#77 smarti widget invalidation
  • Loading branch information
ruKurz authored Aug 17, 2017
2 parents 8103dc3 + cee9402 commit 9ae2925
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 105 deletions.
167 changes: 80 additions & 87 deletions packages/assistify/config.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,93 @@
Meteor.startup(()=>{
RocketChat.settings.addGroup('Assistify');

RocketChat.settings.add('Assistify_Show_Standard_Features', false, {
group: 'Assistify',
i18nLabel: 'Assistify_Show_Standard_Features',
type: 'boolean',
public: true
});

Meteor.startup(() => {
// Copy Settings from dbs-ai in order to simplify maintenance.
// Consequently hide the original group
RocketChat.settings.removeById('dbsAI');

RocketChat.settings.add('DBS_AI_Enabled', false, {
type: 'boolean',
group: 'Assistify',
section: 'Knowledge_Base',
public: true,
i18nLabel: 'Enabled'
});
RocketChat.settings.addGroup('Assistify', function () {

RocketChat.settings.add('DBS_AI_Source', '', {
type: 'select',
group: 'Assistify',
section: 'Knowledge_Base',
values: [
{ key: '0', i18nLabel: 'DBS_AI_Source_APIAI'},
{ key: '1', i18nLabel: 'DBS_AI_Source_Redlink'},
{ key: '2', i18nLabel: 'DBS_AI_Source_Smarti'}
],
public: true,
i18nLabel: 'DBS_AI_Source'
});
this.add('Assistify_Show_Standard_Features', false, {
i18nLabel: 'Assistify_Show_Standard_Features',
type: 'boolean',
public: true
});

RocketChat.settings.add('DBS_AI_Redlink_URL', '', {
type: 'string',
group: 'Assistify',
section: 'Knowledge_Base',
public: true,
i18nLabel: 'DBS_AI_Redlink_URL'
});
this.section('Knowledge_Base', function () {

/* Currently, Redlink does not offer hashed API_keys, but uses simple password-auth
* This is of course far from perfect and is hopeully going to change sometime later */
RocketChat.settings.add('DBS_AI_Redlink_Auth_Token', '', {
type: 'string',
group: 'Assistify',
section: 'Knowledge_Base',
public: true,
i18nLabel: 'DBS_AI_Redlink_Auth_Token'
});
this.add('DBS_AI_Enabled', false, {
type: 'boolean',
public: true,
i18nLabel: 'Enabled'
});

RocketChat.settings.add('DBS_AI_Redlink_Hook_Token', '', {
type: 'string',
group: 'Assistify',
section: 'Knowledge_Base',
public: true,
i18nLabel: 'DBS_AI_Redlink_Hook_Token'
});
this.add('DBS_AI_Source', '', {
type: 'select',
values: [
{key: '0', i18nLabel: 'DBS_AI_Source_APIAI'},
{key: '1', i18nLabel: 'DBS_AI_Source_Redlink'},
{key: '2', i18nLabel: 'DBS_AI_Source_Smarti'}
],
public: true,
i18nLabel: 'DBS_AI_Source'
});

let domain = RocketChat.settings.get('Site_Url');
if (domain) {
domain = domain
.replace('https://', '')
.replace('http://', '');
while (domain.charAt(domain.length - 1) === '/') {
domain = domain.substr(0, domain.length - 1);
}
}
RocketChat.settings.add('DBS_AI_Redlink_Domain', domain, {
type: 'string',
group: 'Assistify',
section: 'Knowledge_Base',
public: true,
i18nLabel: 'DBS_AI_Redlink_Domain'
});
this.add('DBS_AI_Redlink_URL', '', {
type: 'string',
public: true,
i18nLabel: 'DBS_AI_Redlink_URL'
});

/* Currently, Redlink does not offer hashed API_keys, but uses simple password-auth
* This is of course far from perfect and is hopeully going to change sometime later */
this.add('DBS_AI_Redlink_Auth_Token', '', {
type: 'string',
public: true,
i18nLabel: 'DBS_AI_Redlink_Auth_Token'
});

this.add('DBS_AI_Redlink_Hook_Token', '', {
type: 'string',
public: true,
i18nLabel: 'DBS_AI_Redlink_Hook_Token'
});

let domain = RocketChat.settings.get('Site_Url');
if (domain) {
domain = domain
.replace('https://', '')
.replace('http://', '');
while (domain.charAt(domain.length - 1) === '/') {
domain = domain.substr(0, domain.length - 1);
}
}
this.add('DBS_AI_Redlink_Domain', domain, {
type: 'string',
public: true,
i18nLabel: 'DBS_AI_Redlink_Domain'
});

this.add('Assistify_AI_Widget_Posting_Type', '', {
type: 'select',
values: [
{key: 'suggestText', i18nLabel: 'Assistify_AI_Widget_Posting_Type_SuggestText'},
{key: 'postText', i18nLabel: 'Assistify_AI_Widget_Posting_Type_PostText'},
{key: 'postRichText', i18nLabel: 'Assistify_AI_Widget_Posting_Type_PostRichText'}
],
public: true,
i18nLabel: 'Assistify_AI_Widget_Posting_Type'
});

this.add('Assistify_AI_DBSearch_Suffix', '', {
type: 'code',
multiline: true,
public: true,
i18nLabel: 'Assistify_AI_DBSearch_Suffix'
});

RocketChat.settings.add('Assistify_AI_Widget_Posting_Type', '', {
type: 'select',
group: 'Assistify',
section: 'Knowledge_Base',
values: [
{ key: 'suggestText', i18nLabel: 'Assistify_AI_Widget_Posting_Type_SuggestText'},
{ key: 'postText', i18nLabel: 'Assistify_AI_Widget_Posting_Type_PostText'},
{ key: 'postRichText', i18nLabel: 'Assistify_AI_Widget_Posting_Type_PostRichText'}
],
public: true,
i18nLabel: 'Assistify_AI_Widget_Posting_Type'
});

RocketChat.settings.add('Assistify_AI_DBSearch_Suffix', '', {
type: 'code',
multiline: true,
group: 'Assistify',
section: 'Knowledge_Base',
public: true,
i18nLabel: 'Assistify_AI_DBSearch_Suffix'
this.add('reload_Assistify', 'reloadSmarti', {
type: 'action',
actionText: 'Reload_Settings'
});
});
});
});
4 changes: 2 additions & 2 deletions packages/dbs-ai/client/views/app/tabbar/smarti.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ RocketChat.settings.onload('DBS_AI_Redlink_URL', function() {
if (error) {
console.error('could not load Smarti:', error.message);
} else {
// generate a script tag for smarti JS
// generate a script tag for smarti JS
const doc = document;
const smartiScriptTag = doc.createElement('script');
smartiScriptTag.type = 'text/javascript';
smartiScriptTag.async = true;
smartiScriptTag.defer = true;
smartiScriptTag.innerHTML = script;
// insert the smarti script tag as first script tag
// insert the smarti script tag as first script tag
const firstScriptTag = doc.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(smartiScriptTag, firstScriptTag);
console.debug('loaded Smarti successfully');
Expand Down
66 changes: 50 additions & 16 deletions packages/dbs-ai/server/methods/getSmartiUiScript.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
/* globals SystemLogger */

let script;
let timeoutHandle;

Meteor.methods({
getSmartiUiScript() {
if (script) { //buffering
return script;
}

if (!RocketChat.settings.get('DBS_AI_Redlink_URL').trim()) {
throw new Meteor.Error('no-smarti-url-configured');
}
function loadSmarti() {
if (!RocketChat.settings.get('DBS_AI_Redlink_URL').trim()) {
throw new Meteor.Error('no-smarti-url-configured');
}

const DBS_AI_SMARTI_URL =
RocketChat.settings.get('DBS_AI_Redlink_URL').endsWith('/') ?
RocketChat.settings.get('DBS_AI_Redlink_URL') :
`${ RocketChat.settings.get('DBS_AI_Redlink_URL') }/`;
SystemLogger.debug('Trying to retrieve Smarti UI from', DBS_AI_SMARTI_URL);
const DBS_AI_SMARTI_URL =
RocketChat.settings.get('DBS_AI_Redlink_URL').endsWith('/') ?
RocketChat.settings.get('DBS_AI_Redlink_URL') :
`${ RocketChat.settings.get('DBS_AI_Redlink_URL') }/`;
SystemLogger.debug('Trying to retrieve Smarti UI from', DBS_AI_SMARTI_URL);

try {
const response = HTTP.get(`${ DBS_AI_SMARTI_URL }plugin/v1/rocket.chat.js`);
if (response.statusCode === 200) {
script = response.content;

if (!script) {
SystemLogger.error('Could not extract script from Smarti response');
throw new Meteor.Error('no-smarti-ui-script');
throw new Meteor.Error('no-smarti-ui-script', 'no-smarti-ui-script');
} else {
return 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 reach Smarti service at', DBS_AI_SMARTI_URL);
throw new Meteor.Error('no-smarti-ui-script');
throw new Meteor.Error('no-smarti-ui-script', 'no-smarti-ui-script');
}
} catch (error) {
SystemLogger.error('Could not reach Smarti service at', DBS_AI_SMARTI_URL);
throw new Meteor.Error('error-unreachable-url');
}
}

function delayedReload() {
if (timeoutHandle) {
Meteor.clearTimeout(timeoutHandle);
}
timeoutHandle = Meteor.setTimeout(loadSmarti(), 86400000);
}

/**
* This method can be used to explicitly trigger a reconfiguration of the smart-widget
*/
Meteor.methods({
reloadSmarti() {
script = undefined;
loadSmarti();
delayedReload();

return {
message: 'settings-reloaded-successfully'
};
}
});

Meteor.methods({
getSmartiUiScript() {
if (!script) { //buffering
loadSmarti();
delayedReload();
}
return script;
}
});
4 changes: 4 additions & 0 deletions packages/rocketchat-i18n/i18n/dbsAI.de.i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ similar_to: Ähnliches zu
similar_requests: Ähnliche Anfragen
Link_provided: Ich habe hier eine ähnliche Konversation gefunden
No_room_link_possible: Die originale Konversation fand nicht über diesen Chat statt. Daher kann ich sie leider nicht verlinken. Bitte wähle einzelne Nachtichten aus.
Reload_Settings: Einstellungen neu laden
reload_Assistify: Aktualisieren
error-unreachable-url: URL nicht erreichbar
settings-reloaded-successfully: Einstellung wurden erfolgreich neu geladen
4 changes: 4 additions & 0 deletions packages/rocketchat-i18n/i18n/dbsAI.en.i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ similar_to: Similar to
similar_requests: Similar requests
Link_provided: Similar question at
No_room_link_possible: Origin conversation was not provided within this chat. Cannot insert a link. Please pick individual messages.
Reload_Settings: Reload settings
reload_Assistify: Aktualisieren
error-unreachable-url: URL not reachable
settings-reloaded-successfully: Settings have been successfully reloaded

0 comments on commit 9ae2925

Please sign in to comment.