@hubot-friends/hubot-slack 3.5.0 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/src/Bot.mjs +20 -20
  3. package/test/Bot.mjs +60 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hubot-friends/hubot-slack",
3
- "version": "3.5.0",
3
+ "version": "3.7.0",
4
4
  "description": "A new Slack adapter for Hubot",
5
5
  "homepage": "https://github.com/hubot-friends/hubot-slack#readme",
6
6
  "main": "./index.mjs",
@@ -25,7 +25,7 @@
25
25
  "url": "https://github.com/hubot-friends/hubot-slack/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@slack/socket-mode": "^1.3.2",
28
+ "@slack/socket-mode": "^2.0.4",
29
29
  "@slack/web-api": "^6.11.2"
30
30
  },
31
31
  "engines": {
package/src/Bot.mjs CHANGED
@@ -31,7 +31,7 @@ class SlackClient {
31
31
 
32
32
  // Event handling
33
33
  // NOTE: add channel join and leave events
34
- this.socket.on('authenticated', this.eventWrapper, this);
34
+ this.socket.on("authenticated", this.eventWrapper, this);
35
35
  this.socket.on("message", this.eventWrapper, this);
36
36
  this.socket.on("reaction_added", this.eventWrapper, this);
37
37
  this.socket.on("reaction_removed", this.eventWrapper, this);
@@ -203,8 +203,7 @@ class SlackBot extends Adapter {
203
203
  }
204
204
 
205
205
  this.client.socket.on("open", this.open.bind(this));
206
- this.client.socket.on("close", this.close.bind(this));
207
- this.client.socket.on("disconnect", this.disconnect.bind(this));
206
+ this.client.socket.on("close", this.onSocketClose.bind(this));
208
207
  this.client.socket.on("error", this.error.bind(this));
209
208
  this.client.socket.on("authenticated", this.authenticated.bind(this));
210
209
  this.client.onEvent(this.eventHandler.bind(this));
@@ -358,16 +357,23 @@ class SlackBot extends Adapter {
358
357
  * Slack client has closed the connection
359
358
  * @private
360
359
  */
361
- close() {
362
- // NOTE: not confident that @options.autoReconnect works
363
- if (this.options.autoReconnect) {
360
+ onSocketClose() {
361
+ if (this.socket.autoReconnectEnabled && !this.socket.shuttingDown) {
364
362
  this.robot.logger.info("Disconnected from Slack Socket");
365
363
  return this.robot.logger.info("Waiting for reconnect...");
366
364
  } else {
367
- return this.disconnect();
365
+ return this.robot.logger.info("Disconnected from Slack Socket");
368
366
  }
369
367
  }
370
368
 
369
+ /**
370
+ * Close the connection
371
+ * @private
372
+ */
373
+ close() {
374
+ this.disconnect();
375
+ }
376
+
371
377
  /**
372
378
  * Slack client has closed the connection and will not reconnect
373
379
  * @private
@@ -432,10 +438,11 @@ class SlackBot extends Adapter {
432
438
  */
433
439
  async eventHandler(message) {
434
440
  this.robot.logger.debug(`eventHandler ${JSON.stringify(message, null, 2)}`);
441
+ if (message?.ack) {
442
+ await message?.ack();
443
+ }
444
+
435
445
  if(!message?.body?.event?.user) {
436
- if (message?.ack) {
437
- return await message?.ack();
438
- }
439
446
  return;
440
447
  }
441
448
 
@@ -455,9 +462,7 @@ class SlackBot extends Adapter {
455
462
 
456
463
  // Ignore anything we sent
457
464
  if (from?.id === this.self.user_id) {
458
- if (message?.ack) {
459
- return await message?.ack();
460
- }
465
+ return ;
461
466
  }
462
467
 
463
468
  this.robot.logger.debug(`event ${JSON.stringify(message, null, 2)} user = ${user}`);
@@ -468,7 +473,7 @@ class SlackBot extends Adapter {
468
473
  // // Skip events generated by other workspace users in a shared channel
469
474
  // if ((event_team_id != null) && (event_team_id !== this.self.installed_team_id)) {
470
475
  // this.robot.logger.debug(`Skipped an event generated by an other workspace user (team: ${event_team_id}) in shared channel (channel: ${channel})`);
471
- // return await message?.ack();
476
+ // return;
472
477
  // }
473
478
  // }
474
479
 
@@ -487,8 +492,7 @@ class SlackBot extends Adapter {
487
492
  // Ignore messages we've already seen
488
493
  if (this.seenMessages.has(message.body.event.client_msg_id)) {
489
494
  this.robot.logger.debug(`Ignoring message ${message.body.event.client_msg_id}`);
490
- if (!message?.ack) return;
491
- return await message.ack();
495
+ return;
492
496
  }
493
497
 
494
498
  this.seenMessages.add(message.body.event.client_msg_id);
@@ -542,10 +546,6 @@ class SlackBot extends Adapter {
542
546
  this.robot.logger.debug(`Removing message ${message.body.event.client_msg_id}`);
543
547
  this.seenMessages.delete(message.body.event.client_msg_id);
544
548
  }
545
-
546
- if (message?.ack) {
547
- return await message?.ack();
548
- }
549
549
  }
550
550
 
551
551
  /**
package/test/Bot.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { describe, it, beforeEach } from 'node:test'
1
+ import { describe, it, beforeEach, mock } from 'node:test'
2
2
  import assert from 'node:assert/strict'
3
3
  import { SlackBot } from '../src/Bot.mjs'
4
4
  import hubotSlackMock from '../index.mjs'
@@ -76,6 +76,65 @@ describe('Authenticate', () => {
76
76
  })
77
77
  })
78
78
 
79
+ describe('Socket', () => {
80
+ let stubs, slackbot
81
+ beforeEach(async () => {
82
+ ({stubs, slackbot} = (await import('./Stubs.mjs')).default())
83
+ slackbot.socket.disconnect = mock.fn(() => {
84
+ slackbot.socket.shuttingDown = true
85
+ slackbot.socket.emit('close')
86
+ })
87
+ })
88
+
89
+ it('Should socket.disconnect() once', async () => {
90
+ slackbot.socket.autoReconnectEnabled = false
91
+
92
+ await slackbot.run()
93
+ await slackbot.socket.disconnect()
94
+ assert.equal(slackbot.socket.disconnect.mock.callCount(), 1)
95
+ })
96
+
97
+ it('Should log socket close event when we call socket.disconnect()', async () => {
98
+ const { logger } = slackbot.robot
99
+
100
+ slackbot.socket.autoReconnectEnabled = true
101
+
102
+ await slackbot.run()
103
+ await slackbot.socket.disconnect()
104
+ assert.deepEqual(logger.logs.info.slice(-1), ['Disconnected from Slack Socket'])
105
+ })
106
+
107
+ it('Should log waiting for reconnect on socket close event', async () => {
108
+ const { logger } = slackbot.robot
109
+ // Skip socket.disconnect() because it overrides automatic reconnect
110
+ slackbot.socket.websocket = {
111
+ disconnect: mock.fn(() => {
112
+ slackbot.socket.emit('close')
113
+ })
114
+ }
115
+ slackbot.socket.autoReconnectEnabled = true
116
+
117
+ await slackbot.run()
118
+ await slackbot.socket.websocket.disconnect()
119
+ assert.deepEqual(logger.logs.info.slice(-2), ['Disconnected from Slack Socket', 'Waiting for reconnect...'])
120
+ })
121
+
122
+ it('Should log socket close event', async () => {
123
+ const { logger } = slackbot.robot
124
+ // Skip socket.disconnect() because it overrides automatic reconnect
125
+ slackbot.socket.websocket = {
126
+ disconnect: mock.fn(() => {
127
+ slackbot.socket.emit('close')
128
+ })
129
+ }
130
+ slackbot.socket.autoReconnectEnabled = false
131
+
132
+ await slackbot.run()
133
+ await slackbot.socket.websocket.disconnect()
134
+ assert.deepEqual(logger.logs.info.slice(-1), ['Disconnected from Slack Socket'])
135
+ })
136
+ })
137
+
79
138
  describe('Logger', () => {
80
139
  let stubs, slackbot
81
140
  beforeEach(async () => {