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

feat(im): disabling auto-binding current/default installation and client #690

Merged
merged 1 commit into from
May 12, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions AVOS/LeanCloudObjcTests/IMClientTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,44 @@ class IMClientTestCase: RTMBaseTestCase {
NotificationCenter.default.removeObserver(observer)
}
}

func testDisableAutoBindingInstallation() {
let installation = LCInstallation.default()
let option = LCIMClientOption()
option.isAutoBindingInstallationDisabled = true
let client = try! LCIMClient(clientId: uuid, option: option)
XCTAssertNil(client.installation)
XCTAssertNil(client.currentDeviceToken)
expecting { (exp) in
client.open { (success, error) in
XCTAssertTrue(success)
XCTAssertNil(error)
exp.fulfill()
}
}
let deviceToken = uuid
var observer: NSObjectProtocol?
expecting(timeout: 5, expectation: {
let exp = self.expectation(description: "will not upload deviceToken")
exp.expectedFulfillmentCount = 1
exp.isInverted = true
return exp
}) { (exp) in
observer = NotificationCenter.default.addObserver(
forName: NSNotification.Name(rawValue: "Test.LCIMClient.reportDeviceToken"),
object: nil,
queue: .main)
{ (notification) in
XCTAssertNil(notification.userInfo?["error"])
exp.fulfill()
}
installation.setDeviceTokenHexString(deviceToken, teamId: "LeanCloud")
}
XCTAssertNil(client.installation)
XCTAssertNil(client.currentDeviceToken)
XCTAssertNotNil(observer)
if let observer = observer {
NotificationCenter.default.removeObserver(observer)
}
}
}


4 changes: 2 additions & 2 deletions AVOS/LeanCloudObjcTests/LCIMClientTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class LCIMClientTestCase: RTMBaseTestCase {
XCTAssertTrue(installation1.save())

let delegator1 = LCIMClientDelegator.init()
let client1: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, installation: installation1)
let client1: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, option: nil, installation: installation1)
XCTAssertNotNil(client1)
client1.delegate = delegator1
expecting { (exp) in
Expand All @@ -269,7 +269,7 @@ class LCIMClientTestCase: RTMBaseTestCase {
XCTAssertTrue(installation2.save())

let delegator2 = LCIMClientDelegator.init()
let client2: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, installation: installation2)
let client2: LCIMClient! = try? LCIMClient.init(clientId: clientID, tag: tag, option: nil, installation: installation2)
XCTAssertNotNil(client2)
client2.delegate = delegator2

Expand Down
44 changes: 44 additions & 0 deletions AVOS/Sources/Realtime/IM/Client/LCIMClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ NS_ASSUME_NONNULL_BEGIN

@end

/// The option of the client.
@interface LCIMClientOption : NSObject

/// Set with `true` means disabling auto-binding current/default installation and client.
@property (nonatomic) BOOL isAutoBindingInstallationDisabled;

@end

/// IM Client.
@interface LCIMClient : NSObject

Expand Down Expand Up @@ -75,6 +83,14 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable instancetype)initWithClientId:(NSString *)clientId
error:(NSError * __autoreleasing *)error;

/// Initializing with an ID and an option.
/// @param clientId The length of the ID should in range `[1, 64]`.
/// @param option See `LCIMClientOption`.
/// @param error Throws exception when error occurred.
- (nullable instancetype)initWithClientId:(NSString *)clientId
option:(LCIMClientOption * _Nullable)option
error:(NSError * __autoreleasing *)error;

/// Initializing with an ID and a tag.
/// @param clientId The length of the ID should in range `[1, 64]`.
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
Expand All @@ -83,12 +99,30 @@ NS_ASSUME_NONNULL_BEGIN
tag:(NSString * _Nullable)tag
error:(NSError * __autoreleasing *)error;

/// Initializing with an ID, a tag and an option.
/// @param clientId The length of the ID should in range `[1, 64]`.
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
/// @param option See `LCIMClientOption`.
/// @param error Throws exception when error occurred.
- (nullable instancetype)initWithClientId:(NSString *)clientId
tag:(NSString * _Nullable)tag
option:(LCIMClientOption * _Nullable)option
error:(NSError * __autoreleasing *)error;

/// Initializing with an `LCUser`.
/// @param user The user should have logged in.
/// @param error Throws exception when error occurred.
- (nullable instancetype)initWithUser:(LCUser *)user
error:(NSError * __autoreleasing *)error;

/// Initializing with an `LCUser` and an option.
/// @param user The user should have logged in.
/// @param option See `LCIMClientOption`.
/// @param error Throws exception when error occurred.
- (nullable instancetype)initWithUser:(LCUser *)user
option:(LCIMClientOption * _Nullable)option
error:(NSError * __autoreleasing *)error;

/// Initializing with an `LCUser` and a tag.
/// @param user The user should have logged in.
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
Expand All @@ -97,6 +131,16 @@ NS_ASSUME_NONNULL_BEGIN
tag:(NSString * _Nullable)tag
error:(NSError * __autoreleasing *)error;

/// Initializing with an `LCUser`, a tag and an option.
/// @param user The user should have logged in.
/// @param tag Using a tag to specify the context, `@"default"` is reserved.
/// @param option See `LCIMClientOption`.
/// @param error Throws exception when error occurred.
- (nullable instancetype)initWithUser:(LCUser *)user
tag:(NSString * _Nullable)tag
option:(LCIMClientOption * _Nullable)option
error:(NSError * __autoreleasing *)error;

// MARK: Open & Close

/// Open this client before using instant messaging service,
Expand Down
81 changes: 58 additions & 23 deletions AVOS/Sources/Realtime/IM/Client/LCIMClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ - (instancetype)init {

@end

@implementation LCIMClientOption

@end

@implementation LCIMClient {
LCIMClientStatus _status;
}
Expand Down Expand Up @@ -75,48 +79,72 @@ - (instancetype)init
- (instancetype)initWithClientId:(NSString *)clientId
error:(NSError *__autoreleasing _Nullable *)error
{
return [self initWithClientId:clientId
tag:nil
error:error];
return [self initWithClientId:clientId tag:nil error:error];
}

- (instancetype)initWithClientId:(NSString *)clientId
option:(LCIMClientOption *)option
error:(NSError *__autoreleasing _Nullable *)error
{
return [self initWithClientId:clientId tag:nil option:option error:error];
}

- (instancetype)initWithClientId:(NSString *)clientId
tag:(NSString *)tag
error:(NSError *__autoreleasing _Nullable *)error
{
return [self initWithClientId:clientId tag:tag option:nil error:error];
}

- (instancetype)initWithClientId:(NSString *)clientId
tag:(NSString *)tag
option:(LCIMClientOption *)option
error:(NSError *__autoreleasing _Nullable *)error
{
return [self initWithClientId:clientId
tag:tag
installation:[LCInstallation defaultInstallation]
error:error];
LCInstallation *installation = [LCInstallation defaultInstallation];
return [self initWithClientId:clientId tag:tag option:option installation:installation error:error];
}

- (instancetype)initWithUser:(LCUser *)user
error:(NSError *__autoreleasing _Nullable *)error
{
return [self initWithUser:user
tag:nil
error:error];
return [self initWithUser:user tag:nil error:error];
}

- (instancetype)initWithUser:(LCUser *)user
option:(LCIMClientOption *)option
error:(NSError *__autoreleasing _Nullable *)error
{
return [self initWithUser:user tag:nil option:option error:error];
}

- (instancetype)initWithUser:(LCUser *)user
tag:(NSString *)tag
error:(NSError *__autoreleasing _Nullable *)error
{
return [self initWithUser:user
tag:tag
installation:[LCInstallation defaultInstallation]
error:error];
return [self initWithUser:user tag:tag option:nil error:error];
}

- (instancetype)initWithUser:(LCUser *)user
tag:(NSString *)tag
option:(LCIMClientOption *)option
error:(NSError *__autoreleasing _Nullable *)error
{
LCInstallation *installation = [LCInstallation defaultInstallation];
return [self initWithUser:user tag:tag option:option installation:installation error:error];
}

- (instancetype)initWithClientId:(NSString *)clientId
tag:(NSString *)tag
option:(LCIMClientOption *)option
installation:(LCInstallation *)installation
error:(NSError *__autoreleasing _Nullable *)error
{
self = [super init];
if (self) {
NSError *err = [self doInitializationWithClientId:clientId
tag:tag
option:option
installation:installation];
if (err) {
if (error) {
Expand All @@ -130,6 +158,7 @@ - (instancetype)initWithClientId:(NSString *)clientId

- (instancetype)initWithUser:(LCUser *)user
tag:(NSString *)tag
option:(LCIMClientOption *)option
installation:(LCInstallation *)installation
error:(NSError *__autoreleasing _Nullable *)error
{
Expand All @@ -138,6 +167,7 @@ - (instancetype)initWithUser:(LCUser *)user
_user = user;
NSError *err = [self doInitializationWithClientId:user.objectId
tag:tag
option:option
installation:installation];
if (err) {
if (error) {
Expand All @@ -151,6 +181,7 @@ - (instancetype)initWithUser:(LCUser *)user

- (NSError *)doInitializationWithClientId:(NSString *)clientId
tag:(NSString *)tag
option:(LCIMClientOption *)option
installation:(LCInstallation *)installation
{
if (!clientId ||
Expand Down Expand Up @@ -212,14 +243,17 @@ - (NSError *)doInitializationWithClientId:(NSString *)clientId
delegate:self
queue:_internalSerialQueue];
_conversationManager = [[LCIMClientInternalConversationManager alloc] initWithClient:self];
_installation = installation;
_currentDeviceToken = installation.deviceToken;
[installation addObserver:self
forKeyPath:keyPath(installation, deviceToken)
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld |
NSKeyValueObservingOptionInitial)
context:(__bridge void *)(self)];
BOOL isAutoBindingInstallationEnabled = (option ? !option.isAutoBindingInstallationDisabled : true);
if (isAutoBindingInstallationEnabled) {
_installation = installation;
_currentDeviceToken = installation.deviceToken;
[installation addObserver:self
forKeyPath:keyPath(installation, deviceToken)
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld |
NSKeyValueObservingOptionInitial)
context:(__bridge void *)(self)];
}
_conversationCache = ({
LCIMConversationCache *cache = [[LCIMConversationCache alloc] initWithClientId:_clientId];
cache.client = self;
Expand Down Expand Up @@ -1815,7 +1849,8 @@ - (void)removeAllConversationsInMemoryWith:(void (^)(void))callback
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == (__bridge void *)(self)) {
if ([keyPath isEqualToString:keyPath(self.installation, deviceToken)] &&
if (self.installation &&
[keyPath isEqualToString:keyPath(self.installation, deviceToken)] &&
object == self.installation) {
NSString *oldToken = [NSString _lc_decoding:change key:NSKeyValueChangeOldKey];
NSString *newToken = [NSString _lc_decoding:change key:NSKeyValueChangeNewKey];
Expand Down
2 changes: 2 additions & 0 deletions AVOS/Sources/Realtime/IM/Client/LCIMClient_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ void assertContextOfQueue(dispatch_queue_t queue, BOOL isRunIn);

- (instancetype)initWithClientId:(NSString *)clientId
tag:(NSString *)tag
option:(LCIMClientOption *)option
installation:(LCInstallation *)installation
error:(NSError * __autoreleasing *)error;

- (instancetype)initWithUser:(LCUser *)user
tag:(NSString *)tag
option:(LCIMClientOption *)option
installation:(LCInstallation *)installation
error:(NSError * __autoreleasing *)error;

Expand Down