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

RTP9d #412

Merged
merged 2 commits into from
Apr 28, 2016
Merged

RTP9d #412

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Source/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ - (void)publishPresence:(ARTPresenceMessage *)msg callback:(art_nullable void (^
return;
}

if (!msg.clientId) {
msg.clientId = self.clientId;
}
if (!msg.clientId) {
if (!msg.clientId && !_realtime.auth.clientId) {
if (cb) cb([ARTErrorInfo createWithCode:ARTStateNoClientId message:@"attempted to publish presence message without clientId"]);
return;
}
Expand Down
24 changes: 20 additions & 4 deletions Source/ARTRealtimePresence.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ - (void)enter:(id)data {
}

- (void)enter:(id)data callback:(void (^)(ARTErrorInfo *))cb {
[self enterClient:_channel.clientId data:data callback:cb];
[self enterAfterChecks:nil data:data callback:cb];
}

- (void)enterClient:(NSString *)clientId data:(id)data {
Expand All @@ -96,6 +96,10 @@ - (void)enterClient:(NSString *)clientId data:(id)data callback:(void (^)(ARTErr
if (cb) cb([ARTErrorInfo createWithCode:ARTStateNoClientId message:@"attempted to publish presence message without clientId"]);
return;
}
[self enterAfterChecks:clientId data:data callback:cb];
}

- (void)enterAfterChecks:(NSString *__art_nullable)clientId data:(id)data callback:(void (^)(ARTErrorInfo *))cb {
ARTPresenceMessage *msg = [[ARTPresenceMessage alloc] init];
msg.action = ARTPresenceEnter;
msg.clientId = clientId;
Expand All @@ -110,7 +114,7 @@ - (void)update:(id)data {
}

- (void)update:(id)data callback:(void (^)(ARTErrorInfo * _Nullable))cb {
[self updateClient:_channel.clientId data:data callback:cb];
[self updateAfterChecks:nil data:data callback:cb];
}

- (void)updateClient:(NSString *)clientId data:(id)data {
Expand All @@ -122,6 +126,10 @@ - (void)updateClient:(NSString *)clientId data:(id)data callback:(void (^)(ARTEr
if (cb) cb([ARTErrorInfo createWithCode:ARTStateNoClientId message:@"attempted to publish presence message without clientId"]);
return;
}
[self updateAfterChecks:clientId data:data callback:cb];
}

- (void)updateAfterChecks:(NSString *__art_nullable)clientId data:(id)data callback:(void (^)(ARTErrorInfo *))cb {
ARTPresenceMessage *msg = [[ARTPresenceMessage alloc] init];
msg.action = ARTPresenceUpdate;
msg.clientId = clientId;
Expand All @@ -136,7 +144,11 @@ - (void)leave:(id)data {
}

- (void)leave:(id)data callback:(void (^)(ARTErrorInfo * _Nullable))cb {
[self leaveClient:_channel.clientId data:data callback:cb];
if (!_channel.clientId) {
if (cb) cb([ARTErrorInfo createWithCode:ARTStateNoClientId message:@"attempted to publish presence message without clientId"]);
return;
}
[self leaveAfterChecks:nil data:data callback:cb];
}

- (void)leaveClient:(NSString *)clientId data:(id)data {
Expand All @@ -148,7 +160,11 @@ - (void)leaveClient:(NSString *)clientId data:(id)data callback:(void (^)(ARTErr
if (cb) cb([ARTErrorInfo createWithCode:ARTStateNoClientId message:@"attempted to publish presence message without clientId"]);
return;
}
if ([clientId isEqualToString:_channel.clientId]) {
[self leaveAfterChecks:clientId data:data callback:cb];
}

- (void)leaveAfterChecks:(NSString *__art_nullable)clientId data:(id)data callback:(void (^)(ARTErrorInfo *))cb {
if (!clientId || [clientId isEqualToString:_channel.clientId]) {
if(_channel.lastPresenceAction != ARTPresenceEnter && _channel.lastPresenceAction != ARTPresenceUpdate) {
[NSException raise:@"Cannot leave a channel before you've entered it" format:@""];
}
Expand Down
31 changes: 31 additions & 0 deletions Spec/RealtimeClientPresence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,37 @@ class RealtimeClientPresence: QuickSpec {
}
}

// RTP9d
it("update without an explicit PresenceMessage#clientId should implicitly use the clientId of the current connection") {
let options = AblyTests.commonAppSetup()
options.clientId = "john"
let client = AblyTests.newRealtime(options)
defer { client.close() }
let channel = client.channels.get("test")

waitUntil(timeout: testTimeout) { done in
channel.presence.enter("online") { error in
channel.presence.update("offline") { error in
expect(error).to(beNil())
done()
}
}
}

let transport = client.transport as! TestProxyTransport
let sent = transport.protocolMessagesSent.filter({ $0.action == .Presence })[1].presence![0]
expect(sent.action).to(equal(ARTPresenceAction.Update))
expect(sent.clientId).to(beNil())

let received = transport.protocolMessagesReceived
.filter({ $0.action == .Presence })
.map({ $0.presence! })
.reduce([], combine: +)
.filter({ $0.action == .Update })[0]
expect(received.action).to(equal(ARTPresenceAction.Update))
expect(received.clientId).to(equal("john"))
}

}

// RTP10
Expand Down