-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontroller.js
67 lines (51 loc) · 1.21 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const sendRequest = require( 'request' );
const matchPublic = /^\s*public\s*(.*)$/;
class Controller {
constructor( storageRepository, persistentModels ) {
this.commands = [];
this.storageRepository = storageRepository;
for ( let model of Object.keys( persistentModels ) ) {
this[ model ] = persistentModels[ model ];
}
}
addCommand( command ) {
this.commands.push( command );
}
handleRequest( request ) {
let isPublic = false;
const values = matchPublic.exec( request.text );
if ( values ) {
request.text = values[ 1 ];
isPublic = true;
}
request.resolvedText = this.aliases.resolve( request.text || '' );
for ( let command of this.commands ) {
const response = command.handleRequest( request, asyncResponse );
if ( response ) {
if ( isPublic ) {
response.response_type = 'in_channel';
}
return response;
}
}
return {
'text': 'Incorrect command.\n' +
'Use `' + request.command + ' help` for help.'
};
}
}
function asyncResponse( uri, text, type ) {
if ( !type ) {
type = 'in_channel'
}
sendRequest( {
uri: uri,
method: 'POST',
json: {
'response_type': type,
'text': text
}
} );
}
module.exports = Controller;