Skip to content

Commit

Permalink
Fail publish presence according to RTP16b/RTP16c (Removed RTP10a TODO…
Browse files Browse the repository at this point in the history
… as it is not needed, since realtime already sends previously set data value).
  • Loading branch information
maratal committed Apr 13, 2024
1 parent bf348e2 commit 81ef883
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
39 changes: 23 additions & 16 deletions Source/ARTRealtimePresence.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "ARTBaseMessage+Private.h"
#import "ARTProtocolMessage+Private.h"
#import "ARTEventEmitter+Private.h"
#import "ARTClientOptions.h"

#pragma mark - ARTRealtimePresenceQuery

Expand Down Expand Up @@ -380,7 +381,7 @@ - (void)enterOrUpdateAfterChecks:(ARTPresenceAction)action messageId:(NSString *
case ARTRealtimeChannelDetached:
case ARTRealtimeChannelFailed: {
if (cb) {
ARTErrorInfo *channelError = [ARTErrorInfo createWithCode:ARTErrorChannelOperationFailedInvalidState message:[NSString stringWithFormat:@"unable to enter presence channel (incompatible channel state: %@)", ARTRealtimeChannelStateToStr(_channel.state_nosync)]];
ARTErrorInfo *channelError = [ARTErrorInfo createWithCode:ARTErrorUnableToEnterPresenceChannelInvalidState message:[NSString stringWithFormat:@"unable to enter presence channel (incompatible channel state: %@)", ARTRealtimeChannelStateToStr(_channel.state_nosync)]];
cb(channelError);
}
return;
Expand Down Expand Up @@ -449,10 +450,9 @@ - (void)leaveClient:(NSString *)clientId data:(id)data callback:(ARTCallback)cb
});
}

- (void)leaveAfterChecks:(NSString *_Nullable)clientId data:(id)data callback:(ARTCallback)cb {
- (void)leaveAfterChecks:(NSString *_Nullable)clientId data:(id _Nullable)data callback:(ARTCallback)cb {
ARTPresenceMessage *msg = [[ARTPresenceMessage alloc] init];
msg.action = ARTPresenceLeave;
// TODO: RTP10a (if the language permits the data argument to be omitted, then the previously set data value will be sent as a convenience)
msg.data = data;
msg.clientId = clientId;
msg.connectionId = _realtime.connection.id_nosync;
Expand Down Expand Up @@ -619,28 +619,35 @@ - (void)publishPresence:(ARTPresenceMessage *)msg callback:(ARTCallback)callback

ARTRealtimeChannelState channelState = _channel.state_nosync;
switch (channelState) {
case ARTRealtimeChannelInitialized:
case ARTRealtimeChannelDetached: // TODO: RTP16b (should fail if DETACHED)
[_channel _attach:nil];
case ARTRealtimeChannelAttaching: { // TODO: RTP16a (should check `ARTClientOptions.queueMessages`)
[self addPendingPresence:pm callback:^(ARTStatus *status) {
if (callback) {
callback(status.errorInfo);
}
}];
break;
}
case ARTRealtimeChannelAttached: {
[_realtime send:pm sentCallback:nil ackCallback:^(ARTStatus *status) {
[_realtime send:pm sentCallback:nil ackCallback:^(ARTStatus *status) { // RTP16a
if (callback) callback(status.errorInfo);
}];
break;
}
case ARTRealtimeChannelInitialized:
if (_realtime.options.queueMessages) { // RTP16b
[_channel _attach:nil];
}
// fallthrough
case ARTRealtimeChannelAttaching: {
if (_realtime.options.queueMessages) { // RTP16b
[self addPendingPresence:pm callback:^(ARTStatus *status) {
if (callback) {
callback(status.errorInfo);
}
}];
break;
}
// else fallthrough
}
// RTP16c
case ARTRealtimeChannelSuspended:
case ARTRealtimeChannelDetaching:
case ARTRealtimeChannelDetached:
case ARTRealtimeChannelFailed: {
if (callback) {
ARTErrorInfo *invalidChannelError = [ARTErrorInfo createWithCode:ARTErrorChannelOperationFailedInvalidState message:[NSString stringWithFormat:@"channel operation failed (invalid channel state: %@)", ARTRealtimeChannelStateToStr(channelState)]];
ARTErrorInfo *invalidChannelError = [ARTErrorInfo createWithCode:ARTErrorUnableToEnterPresenceChannelInvalidState message:[NSString stringWithFormat:@"channel operation failed (invalid channel state: %@)", ARTRealtimeChannelStateToStr(channelState)]];
callback(invalidChannelError);
}
break;
Expand Down
16 changes: 8 additions & 8 deletions Test/Tests/RealtimeClientPresenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ class RealtimeClientPresenceTests: XCTestCase {

waitUntil(timeout: testTimeout) { done in
channel.presence.enter(nil) { error in
XCTAssertEqual(error?.code, ARTErrorCode.channelOperationFailedInvalidState.intValue)
XCTAssertEqual(error?.code, ARTErrorCode.unableToEnterPresenceChannelInvalidState.intValue)
done()
}
}
Expand All @@ -1208,7 +1208,7 @@ class RealtimeClientPresenceTests: XCTestCase {

waitUntil(timeout: testTimeout) { done in
channel.presence.enter(nil) { error in
XCTAssertEqual(error?.code, ARTErrorCode.channelOperationFailedInvalidState.intValue)
XCTAssertEqual(error?.code, ARTErrorCode.unableToEnterPresenceChannelInvalidState.intValue)
done()
}
}
Expand Down Expand Up @@ -1418,7 +1418,7 @@ class RealtimeClientPresenceTests: XCTestCase {
}

// RTP10a
func skipped__test__044__Presence__leave__should_leave_the_current_client_with_no_data() throws {
func test__044__Presence__leave__should_leave_the_current_client_with_no_data() throws {
let test = Test()
let options = try AblyTests.commonAppSetup(for: test)
options.clientId = "john"
Expand Down Expand Up @@ -2065,7 +2065,7 @@ class RealtimeClientPresenceTests: XCTestCase {

waitUntil(timeout: testTimeout) { done in
channel.presence.update(nil) { error in
XCTAssertEqual(error?.code, ARTErrorCode.channelOperationFailedInvalidState.intValue)
XCTAssertEqual(error?.code, ARTErrorCode.unableToEnterPresenceChannelInvalidState.intValue)
done()
}
}
Expand All @@ -2086,7 +2086,7 @@ class RealtimeClientPresenceTests: XCTestCase {

waitUntil(timeout: testTimeout) { done in
channel.presence.update(nil) { error in
XCTAssertEqual(error?.code, ARTErrorCode.channelOperationFailedInvalidState.intValue)
XCTAssertEqual(error?.code, ARTErrorCode.unableToEnterPresenceChannelInvalidState.intValue)
done()
}
}
Expand Down Expand Up @@ -2298,7 +2298,7 @@ class RealtimeClientPresenceTests: XCTestCase {

waitUntil(timeout: testTimeout) { done in
channel.presence.enter("online") { error in
XCTAssertEqual(error?.code, ARTErrorCode.channelOperationFailedInvalidState.intValue)
XCTAssertEqual(error?.code, ARTErrorCode.unableToEnterPresenceChannelInvalidState.intValue)
done()
}
}
Expand Down Expand Up @@ -2330,7 +2330,7 @@ class RealtimeClientPresenceTests: XCTestCase {

waitUntil(timeout: testTimeout) { done in
channel.presence.enter("online") { error in
XCTAssertEqual(error?.code, ARTErrorCode.channelOperationFailedInvalidState.intValue)
XCTAssertEqual(error?.code, ARTErrorCode.unableToEnterPresenceChannelInvalidState.intValue)
done()
}
}
Expand Down Expand Up @@ -3103,7 +3103,7 @@ class RealtimeClientPresenceTests: XCTestCase {
waitUntil(timeout: testTimeout) { done in
// Call: enterClient, updateClient and leaveClient
performMethod(channel.presence) { error in
XCTAssertEqual(error?.code, ARTErrorCode.channelOperationFailedInvalidState.intValue)
XCTAssertEqual(error?.code, ARTErrorCode.unableToEnterPresenceChannelInvalidState.intValue)
XCTAssertEqual(channel.state, ARTRealtimeChannelState.failed)
guard let reason = channel.errorReason else {
fail("Reason is empty"); done(); return
Expand Down

0 comments on commit 81ef883

Please sign in to comment.