1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const ArgumentsValidator = require('./ArgumentsValidator')
|
22 | const ChatServiceError = require('./ChatServiceError')
|
23 | const MemoryState = require('./MemoryState')
|
24 | const Promise = require('bluebird')
|
25 | const RecoveryAPI = require('./RecoveryAPI')
|
26 | const RedisState = require('./RedisState')
|
27 | const Room = require('./Room')
|
28 | const ServiceAPI = require('./ServiceAPI')
|
29 | const SocketIOClusterBus = require('./SocketIOClusterBus')
|
30 | const SocketIOTransport = require('./SocketIOTransport')
|
31 | const User = require('./User')
|
32 | const _ = require('lodash')
|
33 | const uid = require('uid-safe')
|
34 | const { EventEmitter } = require('events')
|
35 | const { checkNameSymbols, convertError, execHook, logError } =
|
36 | require('./utils')
|
37 | const { mixin } = require('es6-mixin')
|
38 |
|
39 | const rpcRequestsNames = [
|
40 | 'directAddToList',
|
41 | 'directGetAccessList',
|
42 | 'directGetWhitelistMode',
|
43 | 'directMessage',
|
44 | 'directRemoveFromList',
|
45 | 'directSetWhitelistMode',
|
46 | 'listOwnSockets',
|
47 | 'roomAddToList',
|
48 | 'roomCreate',
|
49 | 'roomDelete',
|
50 | 'roomGetAccessList',
|
51 | 'roomGetOwner',
|
52 | 'roomGetWhitelistMode',
|
53 | 'roomHistoryGet',
|
54 | 'roomHistoryInfo',
|
55 | 'roomJoin',
|
56 | 'roomLeave',
|
57 | 'roomMessage',
|
58 | 'roomNotificationsInfo',
|
59 | 'roomRecentHistory',
|
60 | 'roomRemoveFromList',
|
61 | 'roomSetWhitelistMode',
|
62 | 'roomUserSeen',
|
63 | 'systemMessage'
|
64 | ]
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | class ChatService extends EventEmitter {
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | constructor (options = {}, hooks = {}) {
|
127 | super()
|
128 | this.options = options
|
129 | this.hooks = hooks
|
130 | this.initVariables()
|
131 | this.setOptions()
|
132 | this.setIntegraionOptions()
|
133 | this.setComponents()
|
134 | this.attachBusListeners()
|
135 | mixin(this, ServiceAPI, this.state,
|
136 | () => new User(this), this.clusterBus)
|
137 | mixin(this, RecoveryAPI, this.state, this.transport,
|
138 | this.execUserCommand.bind(this), this.instanceUID)
|
139 | this.startServer()
|
140 | }
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | |
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | |
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | |
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | initVariables () {
|
261 | this.instanceUID = uid.sync(18)
|
262 | this.runningCommands = 0
|
263 | this.rpcRequestsNames = rpcRequestsNames
|
264 | this.closed = false
|
265 |
|
266 | this.ChatServiceError = ChatServiceError
|
267 | this.SocketIOClusterBus = SocketIOClusterBus
|
268 | this.User = User
|
269 | this.Room = Room
|
270 | }
|
271 |
|
272 | setOptions () {
|
273 | this.closeTimeout = this.options.closeTimeout || 15000
|
274 | this.busAckTimeout = this.options.busAckTimeout || 5000
|
275 | this.heartbeatRate = this.options.heartbeatRate || 10000
|
276 | this.heartbeatTimeout = this.options.heartbeatTimeout || 30000
|
277 | this.directListSizeLimit = this.options.directListSizeLimit || 1000
|
278 | this.roomListSizeLimit = this.options.roomListSizeLimit || 10000
|
279 | this.enableAccessListsUpdates =
|
280 | this.options.enableAccessListsUpdates || false
|
281 | this.enableDirectMessages = this.options.enableDirectMessages || false
|
282 | this.enableRoomsManagement = this.options.enableRoomsManagement || false
|
283 | this.enableUserlistUpdates = this.options.enableUserlistUpdates || false
|
284 | this.historyMaxGetMessages = this.options.historyMaxGetMessages
|
285 | if (!_.isNumber(this.historyMaxGetMessages) ||
|
286 | this.historyMaxGetMessages < 0) {
|
287 | this.historyMaxGetMessages = 100
|
288 | }
|
289 | this.historyMaxSize = this.options.historyMaxSize
|
290 | if (!_.isNumber(this.historyMaxSize) ||
|
291 | this.historyMaxSize < 0) {
|
292 | this.historyMaxSize = 10000
|
293 | }
|
294 | this.port = this.options.port || 8000
|
295 | this.directMessagesChecker = this.hooks.directMessagesChecker
|
296 | this.roomMessagesChecker = this.hooks.roomMessagesChecker
|
297 | this.useRawErrorObjects = this.options.useRawErrorObjects || false
|
298 | }
|
299 |
|
300 | setIntegraionOptions () {
|
301 | this.adapterConstructor = this.options.adapter || 'memory'
|
302 | this.adapterOptions = _.castArray(this.options.adapterOptions)
|
303 |
|
304 | this.stateConstructor = this.options.state || 'memory'
|
305 | this.stateOptions = this.options.stateOptions || {}
|
306 |
|
307 | this.transportConstructor = this.options.transport || 'socket.io'
|
308 | this.transportOptions = this.options.transportOptions || {}
|
309 | }
|
310 |
|
311 | setComponents () {
|
312 | let State = (() => {
|
313 | switch (true) {
|
314 | case this.stateConstructor === 'memory':
|
315 | return MemoryState
|
316 | case this.stateConstructor === 'redis':
|
317 | return RedisState
|
318 | case _.isFunction(this.stateConstructor):
|
319 | return this.stateConstructor
|
320 | default:
|
321 | throw new Error(`Invalid state: ${this.stateConstructor}`)
|
322 | }
|
323 | })()
|
324 | let Transport = (() => {
|
325 | switch (true) {
|
326 | case this.transportConstructor === 'socket.io':
|
327 | return SocketIOTransport
|
328 | case _.isFunction(this.transportConstructor):
|
329 | return this.transportConstructor
|
330 | default:
|
331 | throw new Error(`Invalid transport: ${this.transportConstructor}`)
|
332 | }
|
333 | })()
|
334 | this.validator = new ArgumentsValidator(this)
|
335 | this.checkArguments = this.validator.checkArguments.bind(this.validator)
|
336 | this.state = new State(this, this.stateOptions)
|
337 | this.transport = new Transport(
|
338 | this, this.transportOptions,
|
339 | this.adapterConstructor, this.adapterOptions)
|
340 | this.clusterBus = this.transport.clusterBus
|
341 | }
|
342 |
|
343 | attachBusListeners () {
|
344 | this.clusterBus.on('roomLeaveSocket', (id, roomName) => {
|
345 | return this.transport.leaveChannel(id, roomName)
|
346 | .then(() => this.clusterBus.emit('socketRoomLeft', id, roomName))
|
347 | .catchReturn()
|
348 | })
|
349 | this.clusterBus.on('disconnectUserSockets', userName => {
|
350 | return this.state.getUser(userName)
|
351 | .then(user => user.disconnectInstanceSockets())
|
352 | .catchReturn()
|
353 | })
|
354 | }
|
355 |
|
356 |
|
357 | convertError (error) {
|
358 | return convertError(error, this.useRawErrorObjects)
|
359 | }
|
360 |
|
361 |
|
362 | onConnect (id) {
|
363 | if (this.hooks.onConnect) {
|
364 | return Promise.try(() => {
|
365 | return execHook(this.hooks.onConnect, this, id)
|
366 | }).then(loginData => {
|
367 | loginData = _.castArray(loginData)
|
368 | return Promise.resolve(loginData)
|
369 | }).catch(error => logError(error))
|
370 | } else {
|
371 | return Promise.resolve([])
|
372 | }
|
373 | }
|
374 |
|
375 |
|
376 | registerClient (userName, id) {
|
377 | return checkNameSymbols(userName)
|
378 | .then(() => this.state.getOrAddUser(userName))
|
379 | .then(user => user.registerSocket(id))
|
380 | .catch(error => logError(error))
|
381 | }
|
382 |
|
383 | waitCommands () {
|
384 | if (this.runningCommands > 0) {
|
385 | return Promise.fromCallback(cb => {
|
386 | return this.once('commandsFinished', cb)
|
387 | })
|
388 | } else {
|
389 | return Promise.resolve()
|
390 | }
|
391 | }
|
392 |
|
393 | closeTransport () {
|
394 | return this.transport.close()
|
395 | .then(() => this.waitCommands())
|
396 | .timeout(this.closeTimeout)
|
397 | }
|
398 |
|
399 | startServer () {
|
400 | return Promise.try(() => {
|
401 | if (this.hooks.onStart) {
|
402 | return this.clusterBus.listen()
|
403 | .then(() => execHook(this.hooks.onStart, this))
|
404 | .then(() => this.transport.setEvents())
|
405 | } else {
|
406 |
|
407 | this.transport.setEvents()
|
408 | return this.clusterBus.listen()
|
409 | }
|
410 | }).then(() => {
|
411 | this.state.updateHeartbeat()
|
412 | let hbupdater = this.state.updateHeartbeat.bind(this.state)
|
413 | this.hbtimer = setInterval(hbupdater, this.heartbeatRate)
|
414 | return this.emit('ready')
|
415 | }).catch(error => {
|
416 | this.closed = true
|
417 | return this.closeTransport()
|
418 | .then(() => this.state.close())
|
419 | .finally(() => this.emit('closed', error))
|
420 | })
|
421 | }
|
422 |
|
423 | |
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 | close (cb) {
|
431 | if (this.closed) { return Promise.resolve() }
|
432 | this.closed = true
|
433 | clearInterval(this.hbtimer)
|
434 | let closeError = null
|
435 | return this.closeTransport().then(
|
436 | () => execHook(this.hooks.onClose, this, null),
|
437 | error => {
|
438 | if (this.hooks.onClose) {
|
439 | return execHook(this.hooks.onClose, this, error)
|
440 | } else {
|
441 | return Promise.reject(error)
|
442 | }
|
443 | }).catch(error => {
|
444 | closeError = error
|
445 | return Promise.reject(error)
|
446 | }).finally(() => {
|
447 | return this.state.close()
|
448 | .finally(() => this.emit('closed', closeError))
|
449 | }).asCallback(cb)
|
450 | }
|
451 | }
|
452 |
|
453 |
|
454 | ChatService.ChatServiceError = ChatServiceError
|
455 |
|
456 |
|
457 | ChatService.SocketIOClusterBus = SocketIOClusterBus
|
458 |
|
459 |
|
460 | ChatService.User = User
|
461 | ChatService.Room = Room
|
462 |
|
463 | module.exports = ChatService
|