Skip to content

Commit

Permalink
Add ability to listen for topic changes
Browse files Browse the repository at this point in the history
Adapters must specifically implement receiving TopicMessage when they handle a
changing topic.

Closes hubotio#349
  • Loading branch information
tombell committed Mar 11, 2013
1 parent 49d235e commit 10645fd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
15 changes: 8 additions & 7 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
User = require './src/user'
Brain = require './src/brain'
Robot = require './src/robot'
Adapter = require './src/adapter'
Response = require './src/response'
{Listener,TextListener} = require './src/listener'
{TextMessage,EnterMessage,LeaveMessage,CatchAllMessage} = require './src/message'
User = require './src/user'
Brain = require './src/brain'
Robot = require './src/robot'
Adapter = require './src/adapter'
Response = require './src/response'
{Listener,TextListener} = require './src/listener'
{TextMessage,EnterMessage,LeaveMessage,TopicMessage,CatchAllMessage} = require './src/message'

module.exports = {
User
Expand All @@ -17,6 +17,7 @@ module.exports = {
TextMessage
EnterMessage
LeaveMessage
TopicMessage
CatchAllMessage
}

Expand Down
10 changes: 7 additions & 3 deletions src/adapters/campfire.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
HTTPS = require 'https'
{EventEmitter} = require 'events'

Robot = require '../robot'
Adapter = require '../adapter'
{TextMessage,EnterMessage,LeaveMessage} = require '../message'
Robot = require '../robot'
Adapter = require '../adapter'
{TextMessage,EnterMessage,LeaveMessage,TopicMessage} = require '../message'

class Campfire extends Adapter
send: (envelope, strings...) ->
Expand Down Expand Up @@ -55,6 +55,10 @@ class Campfire extends Adapter
unless bot.info.id == author.id
self.receive new LeaveMessage(author, null, id)

bot.on "TopicChangeMessage", withAuthor (id, created, room, user, body, author) ->
unless bot.info.id == author.id
self.receive new TopicMessage(author, body, id)

bot.Me (err, data) ->
bot.info = data.user
bot.name = bot.info.name
Expand Down
8 changes: 8 additions & 0 deletions src/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class EnterMessage extends Message
# id - A String of the message ID.
class LeaveMessage extends Message

# Represents an incoming topic change notification.
#
# user - A User instance for the user who changed the topic.
# text - A String of the new topic
# id - A String of the message ID.
class TopicMessage extends Message

class CatchAllMessage extends Message
# Represents a message that no matchers matched.
#
Expand All @@ -53,5 +60,6 @@ module.exports = {
TextMessage
EnterMessage
LeaveMessage
TopicMessage
CatchAllMessage
}
14 changes: 13 additions & 1 deletion src/robot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,25 @@ class Robot
leave: (callback) ->
@listeners.push new Listener(@, ((msg) -> msg instanceof LeaveMessage), callback)

# Public: Adds a Listener that triggers when anyone changes the topic.
#
# callback - A Function that is called with a Response object.
#
# Returns nothing.
topic: (callback) ->
@listeners.push new Listener(@, ((msg) -> msg instanceof TopicMessage), callback)

# Public: Adds a Listener that triggers when no other text matchers match.
#
# callback - A Function that is called with a Response object.
#
# Returns nothing.
catchAll: (callback) ->
@listeners.push new Listener(@, ((msg) -> msg instanceof CatchAllMessage), ((msg) -> msg.message = msg.message.message; callback msg))
@listeners.push new Listener(
@,
((msg) -> msg instanceof CatchAllMessage),
((msg) -> msg.message = msg.message.message; callback msg)
)

# Public: Passes the given message to any interested Listeners.
#
Expand Down

0 comments on commit 10645fd

Please sign in to comment.