Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List users in a room and invite users #90

Merged
merged 5 commits into from
Sep 29, 2015

Conversation

anupdhml
Copy link
Contributor

This adds functionality to invite users to rooms, as well as the ability to list the users in a room.

Borrows the invite code from #72 with tests added.

For the list feature, a custom event receivedUsersForRoom is emitted after the server response is successfully parsed, and scripts can listen to it to get the list of users. Let me know if other approach makes sense (saving the data in hubot brain?).

The xmpp specification leaves it up to the client to discard the invites if the user is already in the room, but clients do not seem to follow it (pidgin and gajim at least). I added the functionality to list the users in a room, so that scripts can use it to check if a user is already in the room, and if they are not, send the invite. Something like:

room = {jid: [email protected]}
usersToInvite = ['anup', 'mark'] 

robot.adapter.getUsersInRoom room

robot.adapter.once 'receivedUsersForRoom', (roomJID, usersInRoom) ->
  for userName in usersToInvite
    if userName not in usersInRoom
      # On clients that support it, users will get a popup with invitation request
      robot.adapter.sendInvite room, "#{userName}@example.com", 'Inviting to test!'

Not entirely sure what happens when there are concurrent requests for getUsersInRoom(), but right now, we are calling it only once at a time.

The list functionality should also be useful in other cases. There's an open issue regarding this in the hubot repo: hubotio/hubot#742

@markstory markstory added this to the 0.1.18 milestone Sep 27, 2015
@markstory
Copy link
Contributor

Not entirely sure what happens when there are concurrent requests for getUsersInRoom(), but right now, we are calling it only once at a time.

It looks like the downstream events/messages would happen multiple times which could result in a user receiving multiple invites.

@anupdhml
Copy link
Contributor Author

Changed getUsersInRoom() to generate (pseudo) random ids and pass it along the chain of server call and events, so that we can uniquely identify each request in case of concurrent requests.

Also modified the function to accept a callback, to be used after the user data is retrieved. The previous code snippet I posted now becomes:

room = {jid: [email protected]}
usersToInvite = ['anup', 'mark'] 

robot.adapter.getUsersInRoom room, (usersInRoom) ->
  for userName in usersToInvite
    if userName not in usersInRoom
      # On clients that support it, users will get a popup with invitation request
      robot.adapter.sendInvite room, "#{userName}@example.com", 'Inviting to test!'

This way, users writing scripts need not deal with adapter events, and also, we ensure that there are no memory leaks: completedRequest (earlier receivedUsersForRoom) events have one-time listeners internally.

usersInRoom = (item.attrs.name for item in userItems)
@robot.logger.debug "[users in room] #{roomJID} has #{usersInRoom}"

@emit "completedRequest#{stanza.attrs.id}", usersInRoom
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sneaky 😄

markstory added a commit that referenced this pull request Sep 29, 2015
List users in a room and invite users
@markstory markstory merged commit 7fbc3a8 into xmppjs:master Sep 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants