From 115a9a08c98c9775e2f0dcfa7269e249dbfb2473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Si=C3=B4n=20le=20Roux?= Date: Tue, 5 May 2020 22:48:47 +0200 Subject: [PATCH] Add timestamps for enter and leave messages Enter and leave messages don't have timestamps, unlike other events like text messages. Hubot doesn't have a place for them e.g. for enter messages in the EnterMessage class, but Slack supports them and other actions regarding enter/leave events in Slack expect you to use a timestamp, so you need to be able to get it from somewhere. This change works around the fact that they're not present in Hubot's classes by inserting a `ts` field after the message object has been created. It includes tests to confirm that Slack's event timestamp is successfully passed on to Hubot. Resolves #575 --- src/bot.coffee | 8 ++++++-- test/bot.coffee | 14 ++++++++++++-- test/stubs.coffee | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/bot.coffee b/src/bot.coffee index 65b5a89f..1cac6dd1 100644 --- a/src/bot.coffee +++ b/src/bot.coffee @@ -272,13 +272,17 @@ class SlackBot extends Adapter # this event type always has a channel user.room = channel @robot.logger.debug "Received enter message for user: #{user.id}, joining: #{channel}" - @receive new EnterMessage user + msg = new EnterMessage user + msg.ts = event.ts + @receive msg else if event.type is "member_left_channel" # this event type always has a channel user.room = channel @robot.logger.debug "Received leave message for user: #{user.id}, joining: #{channel}" - @receive new LeaveMessage user + msg = new LeaveMessage user + msg.ts = event.ts + @receive msg else if event.type is "reaction_added" or event.type is "reaction_removed" diff --git a/test/bot.coffee b/test/bot.coffee index 8e688d60..adb5e413 100644 --- a/test/bot.coffee +++ b/test/bot.coffee @@ -223,13 +223,23 @@ describe 'Handling incoming messages', -> return it 'Should handle member_joined_channel events as envisioned', -> - @slackbot.eventHandler {type: 'member_joined_channel', user: @stubs.user, channel: @stubs.channel.id} + @slackbot.eventHandler + type: 'member_joined_channel' + user: @stubs.user + channel: @stubs.channel.id + ts: @stubs.event_timestamp should.equal @stubs._received.constructor.name, "EnterMessage" + should.equal @stubs._received.ts, @stubs.event_timestamp @stubs._received.user.id.should.equal @stubs.user.id it 'Should handle member_left_channel events as envisioned', -> - @slackbot.eventHandler {type: 'member_left_channel', user: @stubs.user, channel: @stubs.channel.id} + @slackbot.eventHandler + type: 'member_left_channel' + user: @stubs.user + channel: @stubs.channel.id + ts: @stubs.event_timestamp should.equal @stubs._received.constructor.name, "LeaveMessage" + should.equal @stubs._received.ts, @stubs.event_timestamp @stubs._received.user.id.should.equal @stubs.user.id it 'Should handle channel_topic events as envisioned', -> diff --git a/test/stubs.coffee b/test/stubs.coffee index 5e889da9..29ba8491 100644 --- a/test/stubs.coffee +++ b/test/stubs.coffee @@ -112,6 +112,7 @@ beforeEach -> name: 'Example Team' id: 'T123' @stubs.expired_timestamp = 1528238205453 + @stubs.event_timestamp = '1360782804.083113' # Slack client @stubs.client =