forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…idation #77 smarti widget invalidation
- Loading branch information
Showing
5 changed files
with
140 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters