From 7c2b35832467320e04fd5c89e6e67e803f59abe9 Mon Sep 17 00:00:00 2001 From: jonfreedman Date: Tue, 2 Aug 2016 09:01:10 +0100 Subject: [PATCH] fix(adapter): replace single quote with double quote in @mention, better handle datafeed errors generates a http 500 error while does not datafeed/x/read alternately returns http 400 after 204 (raised with Symphony) --- src/adapter.coffee | 26 +++++++++++++++++--------- test/adapter-test.coffee | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/adapter.coffee b/src/adapter.coffee index 2d0c008..d52d1c2 100644 --- a/src/adapter.coffee +++ b/src/adapter.coffee @@ -37,7 +37,7 @@ class SymphonyAdapter extends Adapter reply: (envelope, strings...) -> @robot.logger.debug "Reply" for string in strings - @symphony.sendMessage(envelope.room, " #{string}", 'MESSAGEML') + @symphony.sendMessage(envelope.room, " #{string}", 'MESSAGEML') run: => @robot.logger.info "Initialising..." @@ -53,22 +53,30 @@ class SymphonyAdapter extends Adapter @robot.emit 'error', new Error("Unable to resolve identity: #{err}") hourlyRefresh = memoize @symphony.getUser, {maxAge: 3600000, length: 1} @userLookup = (userId) => hourlyRefresh userId - @symphony.createDatafeed() + @_createDatafeed() .then (response) => - @robot.logger.info "Created datafeed: #{response.id}" - this.on 'poll', @_pollDatafeed @emit 'connected' @robot.logger.debug "'connected' event emitted" - @emit 'poll', response.id - @robot.logger.debug "First 'poll' event emitted" - .fail (err) => - @robot.emit 'error', new Error("Unable to create datafeed: #{err}") return close: => @robot.logger.debug 'Removing datafeed poller' this.removeListener 'poll', @_pollDatafeed + _createDatafeed: => + @symphony.createDatafeed() + .then (response) => + if response.id? + @robot.logger.info "Created datafeed: #{response.id}" + this.removeAllListeners 'poll' + this.on 'poll', @_pollDatafeed + @emit 'poll', response.id + @robot.logger.debug "First 'poll' event emitted" + else + @robot.emit 'error', new Error("Unable to create datafeed: #{response}") + .fail (err) => + @robot.emit 'error', new Error("Unable to create datafeed: #{err}") + _pollDatafeed: (id) => # defer execution to ensure we don't go into an infinite polling loop process.nextTick => @@ -76,7 +84,7 @@ class SymphonyAdapter extends Adapter @symphony.readDatafeed(id) .then (response) => if response? - @robot.logger.debug "Received #{response.length} datafeed messages" + @robot.logger.debug "Received #{response.length ? 0} datafeed messages" @_receiveMessage msg for msg in response when msg.v2messageType = 'V2Message' @emit 'poll', id .fail (err) => diff --git a/test/adapter-test.coffee b/test/adapter-test.coffee index 379b008..3e711e0 100644 --- a/test/adapter-test.coffee +++ b/test/adapter-test.coffee @@ -82,6 +82,6 @@ describe 'Adapter test suite', () -> adapter.reply(envelope, 'foo bar baz') adapter.close() nock.on 'received', () -> - assert.isAtLeast((m for m in nock.messages when m.message is " foo bar baz").length, 1) + assert.isAtLeast((m for m in nock.messages when m.message is " foo bar baz").length, 1) done() adapter.run()