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

Support for presence subscriptions #350

Merged
merged 4 commits into from May 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/_reference/RTMClient.md
Expand Up @@ -33,6 +33,7 @@ permalink: /reference/RTMClient
* [.sendMessage(text, channelId, [optCb])](#RTMClient+sendMessage)
* [.updateMessage(message, [optCb])](#RTMClient+updateMessage)
* [.sendTyping(channelId)](#RTMClient+sendTyping)
* [.subscribePresence(userIds)](#RTMClient+subscribePresence)
* [.send(message, [optCb])](#RTMClient+send)

<a name="new_RTMClient_new"></a>
Expand Down Expand Up @@ -275,6 +276,18 @@ Sends a typing indicator to indicate that the user with `activeUserId` is typing
| --- | --- | --- |
| channelId | <code>string</code> | The id of the channel|group|DM to send this message to. |

<a name="RTMClient+subscribePresence"></a>

### rtmClient.subscribePresence(userIds)
Subscribes this socket to presence changes for only the given `userIds`.
This requires `presence_sub` to have been passed as an argument to `start`.

**Kind**: instance method of <code>[RTMClient](#RTMClient)</code>

| Param | Type | Description |
| --- | --- | --- |
| userIds | <code>Array</code> | The user IDs to subscribe to |

<a name="RTMClient+send"></a>

### rtmClient.send(message, [optCb])
Expand Down
16 changes: 15 additions & 1 deletion lib/clients/rtm/client.js
Expand Up @@ -250,13 +250,14 @@ RTMClient.prototype.start = function start(opts) {
this._startOpts = opts;

if (this._useRtmConnect) {
this._rtm.connect(bind(this._onStart, this));
this._rtm.connect(opts, bind(this._onStart, this));
} else {
this._rtm.start(opts, bind(this._onStart, this));
}
}
};


/**
* @deprecated since version 2.0.0, use start() instead.
*/
Expand Down Expand Up @@ -716,6 +717,19 @@ RTMClient.prototype.sendTyping = function sendTyping(channelId) {
};


/**
* Subscribes this socket to presence changes for only the given `userIds`.
* This requires `presence_sub` to have been passed as an argument to `start`.
* @param {Array} userIds The user IDs to subscribe to
*/
RTMClient.prototype.subscribePresence = function subscribePresence(userIds) {
this.send({
type: 'presence_sub',
ids: userIds
}, noop);
};


/**
* Sends a message over the websocket to the server.
* @param {*} message The message to send back to the server.
Expand Down
23 changes: 12 additions & 11 deletions lib/clients/web/facets/rtm.js
Expand Up @@ -17,14 +17,13 @@ function RtmFacet(makeAPICall) {
* Starts a Real Time Messaging session.
* @see {@link https://api.slack.com/methods/rtm.start|rtm.start}
*
* @param {Object=} opts
* @param {Boolean} opts.simple_latest - Return timestamp only for latest message object of each
* channel (improves performance).
* @param {Boolean} opts.no_unreads - Skip unread counts for each channel (improves performance).
* @param {Boolean} opts.no_latest - Exclude latest timestamps for channels, groups, mpims, and
* ims. Automatically sets no_unreads to 1
* @param {Boolean} opts.mpim_aware - Returns MPIMs to the client in the API response.
* @param {function=} optCb Optional callback, if not using promises.
* @param {Object} opts
* @param {Boolean} opts.simple_latest Return timestamp only for latest message object of each
* channel (improves performance).
* @param {Boolean} opts.no_unreads Skip unread counts for each channel (improves performance).
* @param {Boolean} opts.mpim_aware Returns MPIMs to the client in the API response.
* @param {Boolean} opts.presence_sub Support presence subscriptions on this socket connection.
* @param {Function} optCb Optional callback, if not using promises.
*/
RtmFacet.prototype.start = function start(opts, optCb) {
return this.makeAPICall('rtm.start', null, opts, optCb);
Expand All @@ -36,10 +35,12 @@ RtmFacet.prototype.start = function start(opts, optCb) {
* This will give us a WebSocket URL without the payload of `rtm.start`.
* @see {@link https://api.slack.com/methods/rtm.connect|rtm.connect}
*
* @param {function=} optCb Optional callback, if not using promises.
* @param {Object} opts
* @param {Boolean} opts.presence_sub Support presence subscriptions on this socket connection.
* @param {Function} optCb Optional callback, if not using promises.
*/
RtmFacet.prototype.connect = function connect(optCb) {
return this.makeAPICall('rtm.connect', null, null, optCb);
RtmFacet.prototype.connect = function connect(opts, optCb) {
Copy link
Contributor

Choose a reason for hiding this comment

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

yikes, is this a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but we just added connect in #344 (we haven't done a release with it yet) 😛

Copy link
Contributor

Choose a reason for hiding this comment

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

YEEE! unreleasssed :)

return this.makeAPICall('rtm.connect', null, opts, optCb);
};


Expand Down
11 changes: 11 additions & 0 deletions test/clients/rtm/send-message.js
Expand Up @@ -28,6 +28,17 @@ describe('RTM API Client', function () {
done();
});
});

it('should send a presence_sub message when subscribePresence is called', function () {
var rtm = createRtmClient();
var userIds = ['VinDiesel'];
sinon.spy(rtm, 'send');
rtm.subscribePresence(userIds);
expect(rtm.send.calledWith({
type: 'presence_sub',
ids: userIds
})).to.equal(true);
});
});

describe('Message Response Handling', function () {
Expand Down