Skip to content

Commit

Permalink
Merge branch 'master' into slackapiGH-558
Browse files Browse the repository at this point in the history
  • Loading branch information
iancward committed May 17, 2019
2 parents 10698d8 + 362885b commit 277509b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/_posts/2019-04-29-v4.7.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: changelog
---

* Adds ability to configure the lifetime of conversation data in the cache using the
`HUBOT_SLACK_CONVERSATION_CACHE_TTL_MS` environment variable (#560) thanks @aoberoi
5 changes: 5 additions & 0 deletions docs/_posts/2019-04-30-v4.7.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
layout: changelog
---

* Fixes an issue parsing the `HUBOT_SLACK_CONVERSATION_CACHE_TTL_MS` as a number (#562) thanks @aoberoi and @mistydemeo
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubot-slack",
"version": "4.6.0",
"version": "4.7.1",
"description": "A Slack adapter for hubot",
"main": "./slack",
"scripts": {
Expand Down
21 changes: 19 additions & 2 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ class SlackClient
###
@PAGE_SIZE = 100

###*
# Number of milliseconds which the information returned by `conversations.info` is considered to be valid. The default
# value is 5 minutes, and it can be customized by setting the `HUBOT_SLACK_CONVERSATION_CACHE_TTL_MS` environment
# variable. Setting this number higher will reduce the number of requests made to the Web API, which may be helpful if
# your Hubot is experiencing rate limiting errors. However, setting this number too high will result in stale data
# being referenced, and your scripts may experience errors related to channel info (like the name) being incorrect
# after a user changes it in Slack.
# @private
###
@CONVERSATION_CACHE_TTL_MS =
if process.env.HUBOT_SLACK_CONVERSATION_CACHE_TTL_MS
then parseInt(process.env.HUBOT_SLACK_CONVERSATION_CACHE_TTL_MS, 10)
else (5 * 60 * 1000)

###*
# @constructor
# @param {Object} options - Configuration options for this SlackClient instance
Expand Down Expand Up @@ -241,8 +255,8 @@ class SlackClient
# @public
###
fetchConversation: (conversationId) ->
# Current date minus 5 minutes (time of expiration for conversation info)
expiration = Date.now() - (5 * 60 * 1000)
# Current date minus time of expiration for conversation info
expiration = Date.now() - SlackClient.CONVERSATION_CACHE_TTL_MS

# Check whether conversation is held in client's channelData map and whether information is expired
return Promise.resolve(@channelData[conversationId].channel) if @channelData[conversationId]?.channel? and
Expand Down Expand Up @@ -387,4 +401,7 @@ class SlackClient
# @param {Array<SlackUserInfo>} results.members
###

if SlackClient.CONVERSATION_CACHE_TTL_MS is NaN
throw new Error('HUBOT_SLACK_CONVERSATION_CACHE_TTL_MS must be a number. It could not be parsed.')

module.exports = SlackClient

0 comments on commit 277509b

Please sign in to comment.