Skip to content

Commit f8fc116

Browse files
committed
feat: remove adherence between features and slack service, use the interface field to get informations
1 parent 00b36e0 commit f8fc116

4 files changed

Lines changed: 14 additions & 19 deletions

File tree

src/feature/assistant-feature.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const _ = require('lodash'),
2-
SlackService = require('../interface/slack-service');
1+
const _ = require('lodash');
32

43

54
class AssistantFeature {
@@ -67,9 +66,9 @@ class AssistantFeature {
6766
return this.cache;
6867
}
6968

70-
static getCacheId(channelId, userId) {
69+
static getCacheId(interfac, channelId, userId) {
7170
// channel, group ou im
72-
let datastore = SlackService.getDataStore();
71+
let datastore = interfac.getDataStore();
7372
if(datastore.getChannelById(channelId)
7473
|| datastore.getGroupById(channelId)) {
7574
// On est dans un channel/group
@@ -130,7 +129,7 @@ class AssistantFeature {
130129

131130
initAssistantFeature(interfac, context) {
132131
this.interface = interfac;
133-
this.id = this.constructor.getCacheId(context.channelId, context.userId);
132+
this.id = this.constructor.getCacheId(interfac, context.channelId, context.userId);
134133
this.context = context;
135134

136135
this.resetTtl();

src/feature/configuration/configuration.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const AssistantFeature = require('../assistant-feature'),
22
StateMachine = require('javascript-state-machine'),
3-
ConfigurationService = require('../../helpers/configuration-service.js'),
4-
SlackService = require('../../interface/slack-service');
3+
ConfigurationService = require('../../helpers/configuration-service.js');
54

65
class Configuration extends AssistantFeature {
76

@@ -64,7 +63,7 @@ class Configuration extends AssistantFeature {
6463

6564
onHelp(event, from, to) {
6665
if(this.context.interfaceType === 'im') {
67-
var fromUser = SlackService.getDataStore().getUserById(this.context.userId);
66+
var fromUser = this.interface.getDataStore().getUserById(this.context.userId);
6867
if(fromUser.is_admin || this.interface.isAdministrator(this.context.userId)) {
6968
var toSend = [
7069
'Mode configuration activé, dites "fin" pour le quitter.',

src/interface/slack-service.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ const limiter = new Bottleneck(1, 1000);
1414

1515
class SlackService extends EventEmitter {
1616

17-
static getDataStore() {
18-
if(!this.dataStore) {
19-
this.dataStore = new MemoryDataStore();
20-
}
21-
return this.dataStore;
22-
}
23-
2417
constructor(options) {
2518
super();
2619

@@ -33,6 +26,10 @@ class SlackService extends EventEmitter {
3326
this.init();
3427
this.slack.start();
3528
}
29+
30+
getDataStore() {
31+
return this.slack.dataStore;
32+
}
3633

3734
isAdministrator(userId) {
3835
return this.administrators.indexOf(userId) !== -1;
@@ -49,7 +46,7 @@ class SlackService extends EventEmitter {
4946

5047
this.slack = new RtmClient(process.env.SLACK_TOKEN, {
5148
logLevel: 'warning',
52-
dataStore: this.constructor.getDataStore()
49+
dataStore: new MemoryDataStore()
5350
});
5451
this.slack.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
5552
this.authenticatedUserId = rtmStartData.self.id;

src/virtual-assistant.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class VirtualAssistant {
4949
});
5050
}
5151

52-
_getCurrentFeatureCacheId(context) {
52+
_getCurrentFeatureCacheId(interfac, context) {
5353
let cacheKey = null;
5454
_.forEach(this.featureList, (o) => {
5555
if(!cacheKey) {
56-
let currentCacheId = o.getCacheId(context.channelId, context.userId);
56+
let currentCacheId = o.getCacheId(interfac, context.channelId, context.userId);
5757
if(currentCacheId && AssistantFeature.getCache().keys().indexOf(currentCacheId) !== -1) {
5858
cacheKey = currentCacheId;
5959
}
@@ -63,7 +63,7 @@ class VirtualAssistant {
6363
}
6464

6565
_onMessage(fromInterface, context, message) {
66-
let featureCacheId = this._getCurrentFeatureCacheId(context);
66+
let featureCacheId = this._getCurrentFeatureCacheId(fromInterface, context);
6767

6868
if(featureCacheId && AssistantFeature.getCache().get(featureCacheId)) {
6969
let feature = AssistantFeature.getCache().get(featureCacheId);

0 commit comments

Comments
 (0)