Skip to content

Commit

Permalink
Implement file_shared event
Browse files Browse the repository at this point in the history
dbeard committed Dec 4, 2018
1 parent a442154 commit fa41a52
Showing 4 changed files with 53 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/bot.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, CatchAllMessage} = require.main.require "hubot"
{SlackTextMessage, ReactionMessage, PresenceMessage} = require "./message"
{SlackTextMessage, ReactionMessage, PresenceMessage, FileSharedMessage} = require "./message"
SlackClient = require "./client"
pkg = require "../package"

@@ -287,6 +287,18 @@ class SlackBot extends Adapter

@robot.logger.debug "Received presence update message for users: #{u.id for u in users} with status: #{event.presence}"
@receive new PresenceMessage(users, event.presence)

else if event.type is "file_shared"

# Once again Hubot expects all user objects to have a room property that is used in the envelope for the message
# after it is received. If the reaction is to a message, then the `event.item.channel` contain a conversation ID.
# Otherwise reactions can be on files and file comments, which are "global" and aren't contained in a
# conversation. In that situation we fallback to an empty string.
user.room = event.channel_id

@robot.logger.debug "Received file_shared message from: #{user.id}, file_id: #{event.file_id}"
@receive new FileSharedMessage(user, event.file_id, event.event_ts)


# NOTE: we may want to wrap all other incoming events as a generic Message
# else
1 change: 1 addition & 0 deletions src/client.coffee
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ class SlackClient
@rtm.on "presence_change", @eventWrapper, this
@rtm.on "member_joined_channel", @eventWrapper, this
@rtm.on "member_left_channel", @eventWrapper, this
@rtm.on "file_shared", @eventWrapper, this
@rtm.on "user_change", @updateUserInBrain, this
@eventHandler = undefined

26 changes: 25 additions & 1 deletion src/extensions.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{Robot} = require.main.require "hubot"
{ReactionMessage, PresenceMessage} = require "./message"
{ReactionMessage, PresenceMessage, FileSharedMessage} = require "./message"

###*
# Adds a Listener for ReactionMessages with the provided matcher, options, and callback
@@ -66,5 +66,29 @@ Robot::presenceChange = (matcher, options, callback) ->
options = matcher

@listen matchPresence, options, callback

###*
# Adds a Listener for FileSharedMessages with the provided matcher, options, and callback
#
# @public
# @param {Function} [matcher] - a function to determine if the listener should run. must return something
# truthy if it should and that value with be available on `response.match`.
# @param {Object} [options] - an object of additional parameters keyed on extension name.
# @param {Function} callback - a function that is called with a Response object if the matcher function returns true
###
Robot::fileShared = (matcher, options, callback) ->
matchFileShare = (msg) -> msg instanceof FileSharedMessage

if not options and not callback
return @listen matchFileShare, matcher

else if matcher instanceof Function
matchFileShare = (msg) -> msg instanceof FileSharedMessage && matcher(msg)

else
callback = options
options = matcher

@listen matchFileShare, options, callback

# NOTE: extend Response type with a method for creating a new thread from the incoming message
14 changes: 14 additions & 0 deletions src/message.coffee
Original file line number Diff line number Diff line change
@@ -20,6 +20,19 @@ class ReactionMessage extends Message
constructor: (@type, @user, @reaction, @item_user, @item, @event_ts) ->
super @user
@type = @type.replace("reaction_", "")

class FileSharedMessage extends Message

###*
# Represents a message generated by an file_shared event
#
# @constructor
# @param {User} user - A User instance that reacted to the item.
# @param {string} file_id - A String identifying the file_id of the file that was shared.
# @param {string} event_ts - A String of the file_shared event timestamp.
###
constructor: (@user, @file_id, @event_ts) ->
super @user

class PresenceMessage extends Message

@@ -249,3 +262,4 @@ class SlackTextMessage extends TextMessage
exports.SlackTextMessage = SlackTextMessage
exports.ReactionMessage = ReactionMessage
exports.PresenceMessage = PresenceMessage
exports.FileSharedMessage = FileSharedMessage

0 comments on commit fa41a52

Please sign in to comment.