1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _debugLevels = require('debug-levels');
|
8 |
|
9 | var _debugLevels2 = _interopRequireDefault(_debugLevels);
|
10 |
|
11 | var _botLang = require('bot-lang');
|
12 |
|
13 | var _botLang2 = _interopRequireDefault(_botLang);
|
14 |
|
15 | var _requireDir = require('require-dir');
|
16 |
|
17 | var _requireDir2 = _interopRequireDefault(_requireDir);
|
18 |
|
19 | var _async = require('async');
|
20 |
|
21 | var _async2 = _interopRequireDefault(_async);
|
22 |
|
23 | var _lodash = require('lodash');
|
24 |
|
25 | var _lodash2 = _interopRequireDefault(_lodash);
|
26 |
|
27 | var _util = require('./util');
|
28 |
|
29 | var _util2 = _interopRequireDefault(_util);
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | const debug = (0, _debugLevels2.default)('SS:Message');
|
34 |
|
35 | const loadPlugins = function loadPlugins(path) {
|
36 | const plugins = {};
|
37 | try {
|
38 | const pluginFiles = (0, _requireDir2.default)(path);
|
39 | Object.keys(pluginFiles).forEach(file => {
|
40 |
|
41 | if (pluginFiles[file].default) {
|
42 | pluginFiles[file] = pluginFiles[file].default;
|
43 | }
|
44 |
|
45 | Object.keys(pluginFiles[file]).forEach(func => {
|
46 | debug.verbose('Loading plugin:', path, func);
|
47 | plugins[func] = pluginFiles[file][func];
|
48 | });
|
49 | });
|
50 | } catch (e) {
|
51 | console.error(`Could not load plugins from ${ path }: ${ e }`);
|
52 | }
|
53 |
|
54 | return plugins;
|
55 | };
|
56 |
|
57 |
|
58 | class Message {
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | constructor(message, options, callback) {
|
69 | debug.verbose(`Creating message from string: ${ message }`);
|
70 |
|
71 | this.id = _util2.default.genId();
|
72 | this.plugins = options.plugins || {};
|
73 |
|
74 |
|
75 | if (options.replyId) {
|
76 | this.replyId = options.replyId;
|
77 | }
|
78 |
|
79 | if (options.clearConversation) {
|
80 | this.clearConversation = options.clearConversation;
|
81 | }
|
82 |
|
83 | this.factSystem = options.factSystem;
|
84 | this.createdAt = new Date();
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | this.original = message;
|
96 | this.raw = _botLang2.default.replace.all(message).trim();
|
97 | this.clean = _util2.default.cleanMessage(this.raw).trim();
|
98 |
|
99 | debug.verbose('Message before cleaning: ', message);
|
100 | debug.verbose('Message RAW: ', this.raw);
|
101 | debug.verbose('Message CLEAN: ', this.clean);
|
102 |
|
103 | const scope = _lodash2.default.merge({}, options.scope);
|
104 | scope.message = this;
|
105 |
|
106 | const self = this;
|
107 | const eachPluginItor = (functionName, next) => {
|
108 | const functionArgs = [];
|
109 | functionArgs.push(err => next(err, null));
|
110 | functionName.apply(scope, functionArgs);
|
111 | };
|
112 |
|
113 | _async2.default.each(this.plugins, eachPluginItor, () => {
|
114 | callback(null, self);
|
115 | });
|
116 | }
|
117 |
|
118 | static createMessage(message, options, callback) {
|
119 | if (!message) {
|
120 | debug.verbose('Message received was empty, callback immediately');
|
121 | return callback(null, {});
|
122 | }
|
123 |
|
124 |
|
125 | options.plugins = loadPlugins(`${ __dirname }/plugins`);
|
126 |
|
127 |
|
128 | if (options.pluginsPath) {
|
129 | options.plugins = _lodash2.default.merge(loadPlugins(options.pluginsPath, options.plugins));
|
130 | }
|
131 |
|
132 | return new Message(message, options, callback);
|
133 | }
|
134 | }
|
135 |
|
136 | exports.default = Message; |
\ | No newline at end of file |