Skip to content

Commit

Permalink
OMG dynamic groups should work!
Browse files Browse the repository at this point in the history
  • Loading branch information
cycomachead committed Nov 3, 2015
1 parent ee46036 commit 49e7b81
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ That is:

**Note**: *When set in a shell environment, you may want to put `''` around your alias definition so that any `;` don't try to break the command.*

### Dynamic Configuration
Group Alias supports dynamically defining groups using the [hubot-auth][auth] package. All "roles" that are created by `hubot-auth` will be treated able to be expanded into @mention messages. To do this, simple set:

HUBOUT_GROUP_ALIAS='DYNAMIC'

and make sure `hubot-auth` is installed.

##### Notes
* The only supported modes are dynamic or pre-defined. There is currently not "hybrid" mode.
* Currently dynamic mode is _not_ case sensitive because `hubot-auth` roles act the same way.
* You should probably set `HUBOUT_GROUP_ALIAS_NAME_PROP` because otherwise, `hubot-auth` simply returns full user names.

[auth]: https://github.com/hubot-scripts/hubot-auth

## Autocomplete Abilities
By default, most chat apps don't support autocomplete for bots. :(

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hubot-group-alias",
"description": "Hubot page for user @mention aliases",
"version": "1.7.0",
"version": "2.0.0",
"author": "Michael Ball <[email protected]>",
"license": "MIT",
"keywords": [
Expand Down
52 changes: 42 additions & 10 deletions src/alias.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,58 @@ groupCache = {}
buildGroupObject = () ->
if !_.isEqual(groupCache, {})
return groupCache
# Create a 1D list of group assignments
staticGroups = config.split(';')
# Create 2D list of [alias, users]
staticGroups = _.map(staticGroups, (val, item, array) -> val.split('='))
# expand "user1,user2" to "@user1 @user2"
staticGroups = _.map(staticGroups, (val, item, array) -> [val[0],
'@' + val[1].split(',').join(' @')])
'@' + val[1].replace(/,/g, ' @')])
# Convert 2D list to native object
groupCache = _.object(staticGroups)
return groupCache

getGroups = (match) ->
getGroups = (text) ->
if useDynamicGroups
return ''
groupMap = {}
regex = /[:(]+(\w+)[:)]+|@(\w+)/gi
matches = regex.exec(text)
while matches != null
# If matched, 1 group will always be `undefined`
group = matches[1] || matches[2]
users = robot.auth.usersWithRole(group)
if !_.isEqual(users, [])
groupMap[group] = users
matches = regex.exec(text)
for own group, members of groupMap
list = _.map(members, userFromName)
list = _.map(list, mentionName)
groupMap[group] = listToMentions(list)
return groupMap
else
return buildGroupObject()

userFromName = (name) ->
allUsers = robot.brain.data.users
for own id, user of allUsers
if user.name == name
return user
return {}

# Replace aliases with @mentions in the message
expand = (message, user) ->
mentionName = (user) ->
# mention_name is for Hipchat
filterName = user[user_prop] || user.mention_name || user.name
groups = getGroups(message.match)
return user[user_prop] || user.mention_name || user.name

listToMentions = (list) ->
return '@' + list.join(' @')

# Replace aliases with @mentions in the message
expand = (message, groups, user) ->
filterName = mentionName(user)
for own alias, members of groups
# Filter inviduals from their own messages.
if filterName
members = members.replace('@' + filterName, '')
members = members.replace('@' + filterName, '').replace(/\s+/g, ' ')
reg = new RegExp('[:(]+' + alias + '[:)]+|@' + alias, 'i')
message = message.replace(reg, members)
return message
Expand All @@ -75,7 +101,13 @@ module.exports = (robot) ->
if !config
robot.logger.warning "Configuration HUBOT_GROUP_ALIAS is not defined."
return

if useDynamicGroups && !robot.auth
robot.logger.warning "Using dynamic groups requires hubot-auth to be loaded"
return

regex = buildRegExp()
robot.hear regex, (msg) ->
msg.send expand(msg.message.text, msg.message.user)
robot.hear regex, (resp) ->
groups = getGroups(resp.message.text)
if !_.isEqual(groups, {})
resp.send expand(resp.message.text, groups, resp.message.user)

0 comments on commit 49e7b81

Please sign in to comment.