Skip to content

Commit

Permalink
Merge PR #256 'issue252'
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWoolf committed Apr 5, 2016
2 parents c2a4912 + 3cfd5e2 commit d5e6fa1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
17 changes: 13 additions & 4 deletions common/lib/client/realtimepresence.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ var RealtimePresence = (function() {
return realtimePresence.channel.realtime.auth.clientId;
}

function isAnonymous(realtimePresence) {
var realtime = realtimePresence.channel.realtime;
/* If not currently connected, we can't assume that we're an anonymous
* client, as realtime may inform us of our clientId in the CONNECTED
* message. So assume we're not anonymous and leave it to realtime to
* return an error if we are */
return !realtime.auth.clientId && realtime.connection.state === 'connected';
}

function waitAttached(channel, callback, action) {
switch(channel.state) {
case 'attached':
Expand Down Expand Up @@ -36,14 +45,14 @@ var RealtimePresence = (function() {
Utils.inherits(RealtimePresence, Presence);

RealtimePresence.prototype.enter = function(data, callback) {
if(!getClientId(this))
if(isAnonymous(this))
throw new ErrorInfo('clientId must be specified to enter a presence channel', 40012, 400);
this._enterOrUpdateClient(undefined, data, callback, 'enter');
};

RealtimePresence.prototype.update = function(data, callback) {
if(!getClientId(this))
throw new Error('clientId must be specified to update presence data', 40012, 400);
if(isAnonymous(this))
throw new ErrorInfo('clientId must be specified to update presence data', 40012, 400);
this._enterOrUpdateClient(undefined, data, callback, 'update');
};

Expand Down Expand Up @@ -105,7 +114,7 @@ var RealtimePresence = (function() {
};

RealtimePresence.prototype.leave = function(data, callback) {
if(!getClientId(this))
if(isAnonymous(this))
throw new ErrorInfo('clientId must have been specified to enter or leave a presence channel', 40012, 400);
this.leaveClient(undefined, data, callback);
};
Expand Down
31 changes: 31 additions & 0 deletions spec/realtime/presence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1294,5 +1294,36 @@ define(['ably', 'shared_helper', 'async'], function(Ably, helper, async) {
runTestWithEventListener(test, channelName, listenerFor('enter', testClientId), enterInheritedClientId);
};

/*
* Request a token using clientId, then initialize a connection without one,
* and check that can enter presence with the clientId inherited from tokenDetails
* before we're connected, so before we know our clientId
*/
exports.presence_enter_before_know_clientid = function(test) {
test.expect(4);
var channelName = "enter_before_know_clientid"

var enterInheritedClientId = function(cb) {
rest.auth.requestToken({clientId: testClientId}, function(err, tokenDetails) {
if(err) {
test.ok(false, displayError(err));
test.done();
return;
}
var realtime = helper.AblyRealtime({ token: tokenDetails.token, autoConnect: false });
var channel = realtime.channels.get(channelName);
test.equal(realtime.auth.clientId, null, 'no clientId when entering');
channel.presence.enter("test data", function(err) {
test.equal(realtime.auth.clientId, testClientId, 'clientId has been set by the time we entered');
cb(err, realtime);
});
realtime.connect()
monitorConnection(test, realtime);
});
}

runTestWithEventListener(test, channelName, listenerFor('enter', testClientId), enterInheritedClientId);
};

return module.exports = helper.withTimeout(exports);
});

0 comments on commit d5e6fa1

Please sign in to comment.